00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <state/fragProgARB.h>
00026 #include <string.h>
00027 #include <iostream>
00028 #include <stdio.h>
00029
00030 using namespace std;
00031 using namespace glift;
00032 using namespace gutz;
00033
00034
00035
00036
00037
00038 FragProgARB::FragProgARB( const char *fragProg, bool isFileName, PBuffGlift* pbuff, bool noload)
00039 : m_fragProg(0), m_fragProgFile(0), m_shaderNum(0), m_pbuff(0)
00040 {
00041 initMembers( fragProg, isFileName, 0, pbuff );
00042 if(!noload)
00043 this->init();
00044 }
00045
00046
00047
00048
00049
00050 FragProgARB::FragProgARB( const FragProgARB &rhs)
00051 {
00052 initMembers(rhs);
00053 }
00054
00055
00056
00057
00058 FragProgARB& FragProgARB::operator=( const FragProgARB& rhs )
00059 {
00060 initMembers(rhs);
00061 return *this;
00062 }
00063
00064
00065
00066
00067
00068 void FragProgARB::initMembers( const char *fragProg, bool isFileName, GLuint shaderNum, PBuffGlift* pbuff )
00069 {
00070 if(!glew.ARB_fragment_program)
00071 {
00072 err() << "initMembers() Error:\n"
00073 << "\tGL_ARB_fragment_program is not available.\n"
00074 << "\tFragProgARB only available if using an ATI >= 9700 GPU.\n";
00075 exit(1);
00076 }
00077
00078 if(isFileName)
00079 {
00080 if(m_fragProgFile) delete [] m_fragProgFile;
00081 m_fragProgFile = new char[strlen(fragProg)+1];
00082 assert(m_fragProgFile);
00083 strcpy(m_fragProgFile, fragProg);
00084
00085 FILE *fp;
00086 int length;
00087
00088 if (!(fp = fopen(fragProg,"rb")))
00089 {
00090 err() << "initMembers() error: " << fragProg << " could not be read " << endl;
00091 return;
00092 }
00093
00094 fseek(fp, 0, SEEK_END);
00095 length = ftell(fp);
00096 fseek(fp, 0, SEEK_SET);
00097
00098 if(m_fragProg) delete [] m_fragProg;
00099 m_fragProg = new char[length+1];
00100 assert(m_fragProg);
00101
00102 fread( m_fragProg, 1, length, fp);
00103 m_fragProg[length] = '\0';
00104 fclose(fp);
00105 }
00106 else
00107 {
00108 if(m_fragProg) {delete [] m_fragProg;}
00109 m_fragProg = new char[strlen(fragProg)+1];
00110 assert(m_fragProg);
00111
00112 strcpy(m_fragProg, fragProg);
00113 }
00114 m_shaderNum = shaderNum;
00115 m_pbuff = pbuff;
00116 glGetProgramivARB( GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &m_maxLocalParams );
00117 }
00118
00119
00120
00121
00122
00123 void FragProgARB::initMembers( const FragProgARB &rhs)
00124 {
00125 if(rhs.m_fragProg)
00126 {
00127 if(m_fragProg) delete[] m_fragProg;
00128 m_fragProg = new char[strlen(rhs.m_fragProg)];
00129 strcpy(m_fragProg, rhs.m_fragProg);
00130 }
00131 if(rhs.m_fragProgFile)
00132 {
00133 if(m_fragProgFile) delete[] m_fragProgFile;
00134 m_fragProgFile = new char[strlen(rhs.m_fragProgFile)];
00135 strcpy(m_fragProgFile, rhs.m_fragProgFile);
00136 }
00137
00138 m_shaderNum = rhs.m_shaderNum;
00139 m_pbuff = rhs.m_pbuff;
00140 glGetProgramivARB( GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &m_maxLocalParams );
00141 }
00142
00143
00144
00145
00146
00147 FragProgARB::~FragProgARB()
00148 {
00149 destroy();
00150 if(m_fragProg) delete [] m_fragProg;
00151 m_fragProg = 0;
00152 if(m_fragProgFile) delete [] m_fragProgFile;
00153 m_fragProgFile = 0;
00154 }
00155
00156
00157
00158
00159
00160 void FragProgARB::destroy()
00161 {
00162 if( m_pbuff ) {
00163 m_pbuff->enable(true);
00164 }
00165
00166 if(glew.ARB_fragment_program)
00167 {
00168 glDeleteProgramsARB(1, &m_shaderNum );
00169 m_shaderNum = 0;
00170 }
00171
00172 if( m_pbuff ) {
00173 m_pbuff->disable();
00174 }
00175 }
00176
00177
00178
00179
00180 void FragProgARB::init()
00181 {
00182
00183 if( m_pbuff ) {
00184 m_pbuff->enable(true);
00185 }
00186
00187 if(m_fragProg)
00188 {
00189 if(!m_shaderNum)
00190 glGenProgramsARB( 1, &m_shaderNum);
00191
00192 this->bindDef();
00193
00194 #ifdef NDEBUG // Running in optimized mode, so need to eat unrelated GL errors
00195 glGetError();
00196 #endif
00197
00198 glProgramStringARB( GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(m_fragProg), m_fragProg);
00199
00200
00201 if (glGetError() != 0)
00202 {
00203 int position;
00204 glGetIntegerv( GL_PROGRAM_ERROR_POSITION_ARB, &position);
00205 err() << "init() gl error: " << glGetString(GL_PROGRAM_ERROR_STRING_ARB) << " at character " << position << endl;
00206 estr() << m_fragProg << endl;
00207 }
00208
00209 this->releaseDef();
00210 }
00211
00212 if( m_pbuff ) {
00213 m_pbuff->disable();
00214 }
00215 }
00216
00217
00218
00219
00220 void FragProgARB::reset()
00221 {
00222
00223 destroy();
00224 init();
00225
00226 if( isCompiled() ) {
00227 compile();
00228 }
00229 }
00230
00231
00232
00233
00234 void FragProgARB::reload(const char *shader)
00235 {
00236 if(!shader)
00237 {
00238 err() << "reload(), null shader" << endl;
00239 return;
00240 }
00241
00242 if(m_fragProg) {delete [] m_fragProg;}
00243 m_fragProg = 0;
00244
00245 m_fragProg = new char[strlen(shader)+1];
00246 assert(m_fragProg);
00247 m_fragProg[strlen(shader)] = '\0';
00248 strcpy(m_fragProg, shader);
00249
00250
00251 init();
00252 }
00253
00254
00255
00256
00257
00258 void FragProgARB::setLocalConstf(unsigned int constNum, const vec4f& constant)
00259 {
00260 assert( constNum < (uint)m_maxLocalParams );
00261
00262
00263
00264 bindDef();
00265 glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, constNum, constant.v());
00266
00267
00268
00269
00270
00271
00272 releaseDef();
00273
00274 }
00275
00276 void FragProgARB::setLocalConstd(unsigned int constNum, const vec4d& constant)
00277 {
00278 assert( constNum < (uint)m_maxLocalParams );
00279
00280
00281
00282 bindDef();
00283 glProgramLocalParameter4dvARB(GL_FRAGMENT_PROGRAM_ARB, constNum, constant.v());
00284 releaseDef();
00285
00286 }
00287
00288
00289
00290
00291 int FragProgARB::getInfoInt( GLenum param )
00292 {
00293 int infoInt = 0;
00294
00295
00296 bindDef();
00297 glGetProgramivARB( GL_FRAGMENT_PROGRAM_ARB, param, &infoInt );
00298 releaseDef();
00299
00300
00301 return infoInt;
00302 }
00303
00304
00305
00306
00307 void FragProgARB::bindDef()
00308 {
00309 glEnable( GL_FRAGMENT_PROGRAM_ARB );
00310 glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, m_shaderNum);
00311
00312 }
00313
00314
00315
00316
00317 void FragProgARB::releaseDef()
00318 {
00319 glDisable( GL_FRAGMENT_PROGRAM_ARB);
00320
00321 }
00322
00323
00324
00325
00326 void FragProgARB::setProgFile(const char *name)
00327 {
00328 if(m_fragProgFile) delete[] m_fragProgFile;
00329 m_fragProgFile = 0;
00330 if(name)
00331 {
00332 m_fragProgFile = new char[strlen(name)+1];
00333 strcpy(m_fragProgFile,name);
00334 m_fragProgFile[strlen(name)] = '\0';
00335 }
00336 }