Changeset 8037 in orxonox.OLD for trunk/src/lib/graphics/shader.cc
- Timestamp:
- May 31, 2006, 4:52:34 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/shader.cc
r7221 r8037 20 20 #include "stdlibincl.h" 21 21 #include "compiler.h" 22 #include <stdio.h> 22 //#include <stdio.h> 23 #include <fstream> 24 23 25 #include "debug.h" 24 26 … … 26 28 27 29 28 #ifndef PARSELINELENG HT29 #define PARSELINELENG HT512 //!< how many chars to read at once30 #ifndef PARSELINELENGTH 31 #define PARSELINELENGTH 512 //!< how many chars to read at once 30 32 #endif 31 33 … … 40 42 this->setClassID(CL_SHADER, "Shader"); 41 43 42 this->fragmentShaderFile = "";43 this->vertexShaderFile = "";44 44 this->shaderProgram = 0; 45 45 this->vertexShader = 0; … … 48 48 if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) 49 49 { 50 GLint status = 0;51 52 50 this->shaderProgram = glCreateProgramObjectARB(); 53 51 54 52 if (!vertexShaderFile.empty()) 55 this->loadShaderProgramm(S HADER_VERTEX, vertexShaderFile);53 this->loadShaderProgramm(Shader::Vertex, vertexShaderFile); 56 54 if (!fragmentShaderFile.empty()) 57 this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile); 58 glLinkProgramARB(this->shaderProgram); 59 // link error checking 60 glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status); 61 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 62 this->printError(this->shaderProgram); 55 this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile); 56 57 this->linkShaderProgram(); 58 63 59 } 64 60 else … … 78 74 79 75 // delete what has to be deleted here 80 this->deleteProgram(S HADER_VERTEX);81 this->deleteProgram(S HADER_FRAGMENT);76 this->deleteProgram(Shader::Vertex); 77 this->deleteProgram(Shader::Fragment); 82 78 83 79 if (this->fragmentShader != 0) … … 116 112 117 113 118 bool Shader::loadShaderProgramm(S HADER_TYPEtype, const std::string& fileName)114 bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName) 119 115 { 120 116 GLhandleARB shader = 0; 121 117 122 if (type != S HADER_VERTEX && type != SHADER_FRAGMENT)118 if (type != Shader::Vertex && type != Shader::Fragment) 123 119 return false; 124 120 this->deleteProgram(type); 125 121 126 122 127 std::vector<char*>* program = fileReadArray(fileName); 128 129 if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader) 123 std::string program; 124 if (!readShader(fileName, program)) 125 return false; 126 127 if (type == Shader::Vertex && GLEW_ARB_vertex_shader) 130 128 { 131 129 this->vertexShaderFile = fileName; … … 134 132 } 135 133 136 if (type == S HADER_FRAGMENT&& GLEW_ARB_fragment_shader)134 if (type == Shader::Fragment && GLEW_ARB_fragment_shader) 137 135 { 138 136 this->fragmentShaderFile = fileName; … … 144 142 { 145 143 GLint status = 0; 146 /// FIXME do it back 147 // glShaderSourceARB(shader, program->size(), (const std::string&)&(*program)[0], NULL); 144 const char* prog = program.c_str(); 145 146 glShaderSourceARB(shader, 1, &prog, NULL); 148 147 glCompileShaderARB(shader); 149 148 // checking on error. … … 154 153 glAttachObjectARB(this->shaderProgram, shader); 155 154 } 156 for (unsigned int i=0; i< program->size(); i++) 157 delete[] (*program)[i]; 158 delete program; 159 } 160 161 char* Shader::fileRead(const std::string& fileName) 162 { 163 FILE* fileHandle; 164 char* content = NULL; 165 166 int count = 0; 167 168 if (fileName.empty()) 169 return NULL; 170 171 fileHandle = fopen(fileName.c_str(), "rt"); 172 173 if (fileHandle == NULL) 174 return NULL; 175 fseek(fileHandle, 0, SEEK_END); 176 count = ftell(fileHandle); 177 rewind(fileHandle); 178 if (count > 0) { 179 content = new char[count+1]; 180 count = fread(content, sizeof(char), count, fileHandle); 181 content[count] = '\0'; 182 } 183 fclose(fileHandle); 184 return content; 185 } 186 187 188 std::vector<char*>* Shader::fileReadArray(const std::string& fileName) 189 { 190 FILE* stream; //< The stream we use to read the file. 191 192 if( (stream = fopen (fileName.c_str(), "rt")) == NULL) 193 { 194 PRINTF(1)("Shader could not open %s\n", fileName.c_str()); 195 return NULL; 196 } 197 std::vector<char*>* file = new std::vector<char*>; 198 199 char lineBuffer[PARSELINELENGHT]; 200 char* addString; 201 while(fgets (lineBuffer, PARSELINELENGHT, stream) != NULL) 202 { 203 addString = new char[strlen(lineBuffer)+1]; 204 strcpy(addString, lineBuffer); 205 file->push_back(addString); 206 } 207 fclose(stream); 208 return file; 155 } 156 157 158 void Shader::linkShaderProgram() 159 { 160 GLint status = 0; 161 162 glLinkProgramARB(this->shaderProgram); 163 // link error checking 164 glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status); 165 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 166 this->printError(this->shaderProgram); 167 } 168 169 170 bool Shader::readShader(const std::string& fileName, std::string& output) 171 { 172 char lineBuffer[PARSELINELENGTH]; 173 174 std::ifstream shader; 175 shader.open(fileName.c_str()); 176 if (!shader.is_open()) 177 return false; 178 179 180 while (!shader.eof()) 181 { 182 shader.getline(lineBuffer, PARSELINELENGTH); 183 output += lineBuffer; 184 output += "\n"; 185 } 186 187 188 shader.close(); 189 return true; 209 190 } 210 191 … … 228 209 229 210 230 void Shader::deleteProgram(S HADER_TYPEtype)211 void Shader::deleteProgram(Shader::Type type) 231 212 { 232 213 GLint status = 0; 233 if (type == S HADER_VERTEX&& this->vertexShader != 0)214 if (type == Shader::Vertex && this->vertexShader != 0) 234 215 { 235 216 this->vertexShaderFile = ""; … … 241 222 this->vertexShader = 0; 242 223 } 243 else if (type == S HADER_FRAGMENT&& this->fragmentShader != 0)224 else if (type == Shader::Fragment && this->fragmentShader != 0) 244 225 { 245 226 this->fragmentShaderFile = "";
Note: See TracChangeset
for help on using the changeset viewer.