Changeset 5266 in orxonox.OLD for trunk/src/lib
- Timestamp:
- Sep 27, 2005, 4:10:26 PM (19 years ago)
- Location:
- trunk/src/lib/graphics
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/graphics_engine.cc
r5265 r5266 29 29 30 30 using namespace std; 31 32 33 #include "shader.h"34 31 35 32 /** … … 274 271 PRINTF(4)("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); 275 272 276 Shader* shader = new Shader("test.txt", "shader.cg");277 // delete shader;278 shader->activateShader();279 // exit(-1);280 281 273 } 282 274 … … 301 293 // return -1; 302 294 } 295 glMatrixMode(GL_PROJECTION_MATRIX); 296 glLoadIdentity(); 303 297 glViewport(0,0,width,height); // Reset The Current Viewport 304 298 } -
trunk/src/lib/graphics/shader.cc
r5264 r5266 18 18 #include "shader.h" 19 19 20 21 #include "array.h"22 20 #include "stdlibincl.h" 23 21 #include <stdio.h> … … 41 39 this->fragmentShaderFile = NULL; 42 40 this->vertexShaderFile = NULL; 43 this->fragmentShaderSource = NULL;44 this->vertexShaderSource = NULL;45 this->fragmentShaderLengths = NULL;46 this->vertexShaderLenghts = NULL;47 41 this->shaderProgram = 0; 48 42 this->vertexShader = 0; … … 50 44 51 45 if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) 52 this->shaderProgram = glCreate ShaderObjectARB(GL_VERTEX_SHADER_ARB);46 this->shaderProgram = glCreateProgramObjectARB(); 53 47 54 48 if (vertexShaderFile != NULL) … … 81 75 this->deleteProgram(type); 82 76 83 FILE* stream; //< The stream we use to read the file.84 77 85 if( (stream = fopen (fileName, "r")) == NULL) 86 { 87 PRINTF(1)("Shader could not open %s\n", fileName); 88 return false; 89 } 90 Array<char*>* program = new Array<char*>; 91 Array<int>* lengths = new Array<int>; 92 if (type == SHADER_VERTEX) 78 const char* program = fileRead(fileName); 79 80 if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader) 93 81 { 94 82 this->vertexShaderFile = new char[strlen(fileName)+1]; 95 83 strcpy(this->vertexShaderFile, fileName); 96 this->vertexShaderSource = program;97 this->vertexShaderLenghts = lengths;98 }99 else100 {101 this->fragmentShaderFile = new char[strlen(fileName)+1];102 strcpy(this->fragmentShaderFile, fileName);103 this->fragmentShaderSource = program;104 this->fragmentShaderLengths = lengths;105 }106 84 107 char lineBuffer[PARSELINELENGHT];108 char* addString;109 while( !feof( stream))110 {111 // get next line112 fgets (lineBuffer, PARSELINELENGHT, stream);113 // if (strchr(lineBuffer, '\n'))114 // {115 // addString = new char[strlen(lineBuffer)];116 // strncpy(addString, lineBuffer, strlen (lineBuffer)-1);117 // addString[strlen(lineBuffer)-1] ='\0';118 // }119 // else120 {121 addString = new char[strlen(lineBuffer)+1];122 strcpy(addString, lineBuffer);123 }124 program->addEntry(addString);125 lengths->addEntry(strlen(addString));126 }127 fclose(stream);128 129 program->finalizeArray();130 lengths->finalizeArray();131 132 if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)133 {134 85 this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); 135 glShaderSourceARB(this->vertexShader, program->getCount(), (const GLcharARB**)program->getArray(), lengths->getArray());86 glShaderSourceARB(this->vertexShader, 1, (const GLcharARB**)&program, NULL); 136 87 glCompileShaderARB(this->vertexShader); 137 88 glAttachObjectARB(this->shaderProgram, this->vertexShader); … … 141 92 if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader) 142 93 { 94 this->fragmentShaderFile = new char[strlen(fileName)+1]; 95 strcpy(this->fragmentShaderFile, fileName); 96 143 97 this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); 144 glShaderSourceARB(this->fragmentShader, program->getCount(), (const GLcharARB**)program->getArray(), lengths->getArray());98 glShaderSourceARB(this->fragmentShader, 1, (const GLcharARB**)&program, NULL); 145 99 glCompileShaderARB(this->fragmentShader); 146 100 glAttachObjectARB(this->shaderProgram, this->fragmentShader); 147 101 this->printError(this->fragmentShader); 148 102 } 103 104 delete[] program; 149 105 } 150 106 151 bool Shader::activateShader() 107 108 char* Shader::fileRead(const char* fileName) 109 { 110 FILE* fileHandle; 111 char* content = NULL; 112 113 int count = 0; 114 115 if (fileName != NULL) { 116 fileHandle = fopen(fileName, "rt"); 117 118 if (fileHandle != NULL) { 119 120 fseek(fileHandle, 0, SEEK_END); 121 count = ftell(fileHandle); 122 rewind(fileHandle); 123 124 if (count > 0) { 125 content = new char[count+1]; 126 count = fread(content, sizeof(char), count, fileHandle); 127 content[count] = '\0'; 128 } 129 fclose(fileHandle); 130 } 131 } 132 return content; 133 } 134 135 void Shader::activateShader() 152 136 { 153 137 glUseProgramObjectARB(this->shaderProgram); 138 } 139 140 void Shader::deactivateShader() 141 { 142 glUseProgramObjectARB(0); 154 143 } 155 144 … … 157 146 void Shader::deleteProgram(SHADER_TYPE type) 158 147 { 159 Array<char*>* deleteArray = NULL;160 148 if (type == SHADER_VERTEX) 161 149 { 162 deleteArray = this->vertexShaderSource;163 this->vertexShaderSource = NULL;164 delete this->vertexShaderLenghts;165 this->vertexShaderLenghts = NULL;166 150 delete[] this->vertexShaderFile; 167 151 this->vertexShaderFile = NULL; … … 171 155 else if (type == SHADER_FRAGMENT) 172 156 { 173 deleteArray = this->fragmentShaderSource;174 delete this->fragmentShaderLengths;175 this->fragmentShaderLengths = NULL;176 this->fragmentShaderSource = NULL;177 157 delete[] this->fragmentShaderFile; 178 158 this->fragmentShaderFile = NULL; … … 182 162 else 183 163 return; 184 185 if (deleteArray == NULL)186 return;187 else188 {189 deleteArray->finalizeArray();190 for (unsigned int i = 0; i < deleteArray->getCount(); i++)191 {192 delete[] deleteArray->getEntry(i);193 }194 delete deleteArray;195 }196 164 } 197 165 … … 214 182 if (this->vertexShader != 0) 215 183 { 216 PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);184 /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); 217 185 if (this->vertexShaderSource != NULL) 218 186 for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) … … 224 192 if (this->fragmentShaderSource != NULL) 225 193 for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) 226 PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i)); 194 PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ 227 195 } 228 196 -
trunk/src/lib/graphics/shader.h
r5264 r5266 20 20 21 21 // FORWARD DECLARATION 22 template<class T> class Array;23 22 24 23 … … 31 30 32 31 bool loadShaderProgramm(SHADER_TYPE type, const char* fileName); 33 bool activateShader(); 32 void activateShader(); 33 static void deactivateShader(); 34 34 void deleteProgram(SHADER_TYPE type); 35 36 char* fileRead(const char* fileName); 35 37 36 38 void printError(GLenum program); … … 42 44 char* fragmentShaderFile; 43 45 char* vertexShaderFile; 44 Array<char*>* fragmentShaderSource;45 Array<char*>* vertexShaderSource;46 Array<int>* fragmentShaderLengths;47 Array<int>* vertexShaderLenghts;48 46 GLenum shaderProgram; 49 47 GLenum vertexShader;
Note: See TracChangeset
for help on using the changeset viewer.