[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5261] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5261] | 18 | #include "shader.h" |
---|
[1853] | 19 | |
---|
[5262] | 20 | #include "stdlibincl.h" |
---|
[5273] | 21 | #include "compiler.h" |
---|
[5262] | 22 | #include <stdio.h> |
---|
| 23 | #include "debug.h" |
---|
| 24 | |
---|
[5323] | 25 | #include "resource_manager.h" |
---|
[5262] | 26 | |
---|
[5323] | 27 | |
---|
[5262] | 28 | #ifndef PARSELINELENGHT |
---|
| 29 | #define PARSELINELENGHT 512 //!< how many chars to read at once |
---|
| 30 | #endif |
---|
| 31 | |
---|
[1856] | 32 | using namespace std; |
---|
[1853] | 33 | |
---|
[1856] | 34 | |
---|
[3245] | 35 | /** |
---|
[4838] | 36 | * standard constructor |
---|
[3245] | 37 | */ |
---|
[5262] | 38 | Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile) |
---|
[3365] | 39 | { |
---|
[5261] | 40 | this->setClassID(CL_SHADER, "Shader"); |
---|
[4320] | 41 | |
---|
[5261] | 42 | this->fragmentShaderFile = NULL; |
---|
| 43 | this->vertexShaderFile = NULL; |
---|
| 44 | this->shaderProgram = 0; |
---|
| 45 | this->vertexShader = 0; |
---|
| 46 | this->fragmentShader = 0; |
---|
[5262] | 47 | |
---|
[5263] | 48 | if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) |
---|
[5273] | 49 | { |
---|
[5320] | 50 | GLint status = 0; |
---|
| 51 | |
---|
[5273] | 52 | this->shaderProgram = glCreateProgramObjectARB(); |
---|
[5263] | 53 | |
---|
[5273] | 54 | if (vertexShaderFile != NULL) |
---|
[5285] | 55 | this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile); |
---|
[5273] | 56 | if (fragmentShaderFile != NULL) |
---|
[5285] | 57 | this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile); |
---|
[5320] | 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); |
---|
| 63 | } |
---|
[5273] | 64 | else |
---|
| 65 | { |
---|
| 66 | PRINTF(2)("Shaders are not supported on your hardware\n"); |
---|
| 67 | } |
---|
[3365] | 68 | } |
---|
[1853] | 69 | |
---|
| 70 | |
---|
[3245] | 71 | /** |
---|
[4838] | 72 | * standard deconstructor |
---|
[5318] | 73 | */ |
---|
[5261] | 74 | Shader::~Shader () |
---|
[3543] | 75 | { |
---|
[5322] | 76 | if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB)) |
---|
[5318] | 77 | Shader::deactivateShader(); |
---|
| 78 | |
---|
[3543] | 79 | // delete what has to be deleted here |
---|
[5262] | 80 | this->deleteProgram(SHADER_VERTEX); |
---|
| 81 | this->deleteProgram(SHADER_FRAGMENT); |
---|
[5263] | 82 | |
---|
[5273] | 83 | if (this->fragmentShader != 0) |
---|
[5322] | 84 | { |
---|
| 85 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5273] | 86 | glDeleteObjectARB(this->fragmentShader); |
---|
[5322] | 87 | } |
---|
[5273] | 88 | if (this->vertexShader != 0) |
---|
[5322] | 89 | { |
---|
| 90 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5273] | 91 | glDeleteObjectARB(this->vertexShader); |
---|
[5322] | 92 | } |
---|
[5273] | 93 | if (this->shaderProgram != 0) |
---|
[5321] | 94 | { |
---|
| 95 | GLint status = 0; |
---|
[5322] | 96 | //glLinkProgramARB(this->shaderProgram); |
---|
[5273] | 97 | glDeleteObjectARB(this->shaderProgram); |
---|
[5321] | 98 | // link error checking |
---|
| 99 | glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 100 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 101 | this->printError(this->shaderProgram); |
---|
| 102 | } |
---|
[3543] | 103 | } |
---|
[5261] | 104 | |
---|
[5323] | 105 | Shader* Shader::getShader(const char* vertexShaderFile, const char* fragmentShaderFile) |
---|
| 106 | { |
---|
[6645] | 107 | return (Shader*)ResourceManager::getInstance()->load(vertexShaderFile, SHADER, RP_LEVEL, fragmentShaderFile); |
---|
[5323] | 108 | } |
---|
| 109 | |
---|
| 110 | bool Shader::unload(Shader* shader) |
---|
| 111 | { |
---|
| 112 | return ResourceManager::getInstance()->unload(shader); |
---|
| 113 | } |
---|
| 114 | |
---|
[5317] | 115 | Shader* Shader::storedShader = NULL; |
---|
[5261] | 116 | |
---|
[5317] | 117 | |
---|
[5261] | 118 | bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName) |
---|
| 119 | { |
---|
[5319] | 120 | GLhandleARB shader = 0; |
---|
[5285] | 121 | |
---|
[5262] | 122 | if (type != SHADER_VERTEX && type != SHADER_FRAGMENT) |
---|
[5261] | 123 | return false; |
---|
[5262] | 124 | this->deleteProgram(type); |
---|
[5261] | 125 | |
---|
| 126 | |
---|
[7164] | 127 | std::vector<char*>* program = fileReadArray(fileName); |
---|
[5318] | 128 | |
---|
[5266] | 129 | if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader) |
---|
[5262] | 130 | { |
---|
| 131 | this->vertexShaderFile = new char[strlen(fileName)+1]; |
---|
| 132 | strcpy(this->vertexShaderFile, fileName); |
---|
| 133 | |
---|
[5269] | 134 | shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
---|
[5263] | 135 | } |
---|
[5262] | 136 | |
---|
[5263] | 137 | if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader) |
---|
| 138 | { |
---|
[5266] | 139 | this->fragmentShaderFile = new char[strlen(fileName)+1]; |
---|
| 140 | strcpy(this->fragmentShaderFile, fileName); |
---|
| 141 | |
---|
[5269] | 142 | shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); |
---|
[5263] | 143 | } |
---|
[5273] | 144 | |
---|
| 145 | if (shader != 0) |
---|
[5319] | 146 | { |
---|
[5320] | 147 | GLint status = 0; |
---|
[7164] | 148 | glShaderSourceARB(shader, program->size(), (const char**)&(*program)[0], NULL); |
---|
[5320] | 149 | glCompileShaderARB(shader); |
---|
| 150 | // checking on error. |
---|
| 151 | glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); |
---|
| 152 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 153 | this->printError(shader); |
---|
| 154 | else |
---|
| 155 | glAttachObjectARB(this->shaderProgram, shader); |
---|
[5319] | 156 | } |
---|
[7164] | 157 | for (unsigned int i=0; i< program->size(); i++) |
---|
| 158 | delete[] (*program)[i]; |
---|
[5319] | 159 | delete program; |
---|
[5261] | 160 | } |
---|
| 161 | |
---|
[5266] | 162 | char* Shader::fileRead(const char* fileName) |
---|
[5261] | 163 | { |
---|
[5266] | 164 | FILE* fileHandle; |
---|
| 165 | char* content = NULL; |
---|
| 166 | |
---|
| 167 | int count = 0; |
---|
| 168 | |
---|
[5271] | 169 | if (fileName == NULL) |
---|
| 170 | return NULL; |
---|
[5266] | 171 | |
---|
[5271] | 172 | fileHandle = fopen(fileName, "rt"); |
---|
[5266] | 173 | |
---|
[5271] | 174 | if (fileHandle == NULL) |
---|
| 175 | return NULL; |
---|
| 176 | fseek(fileHandle, 0, SEEK_END); |
---|
| 177 | count = ftell(fileHandle); |
---|
| 178 | rewind(fileHandle); |
---|
| 179 | if (count > 0) { |
---|
| 180 | content = new char[count+1]; |
---|
| 181 | count = fread(content, sizeof(char), count, fileHandle); |
---|
| 182 | content[count] = '\0'; |
---|
| 183 | } |
---|
| 184 | fclose(fileHandle); |
---|
| 185 | return content; |
---|
[5266] | 186 | } |
---|
| 187 | |
---|
[5318] | 188 | |
---|
[7164] | 189 | std::vector<char*>* Shader::fileReadArray(const char* fileName) |
---|
[5318] | 190 | { |
---|
| 191 | FILE* stream; //< The stream we use to read the file. |
---|
| 192 | |
---|
| 193 | if( (stream = fopen (fileName, "rt")) == NULL) |
---|
| 194 | { |
---|
| 195 | PRINTF(1)("Shader could not open %s\n", fileName); |
---|
| 196 | return NULL; |
---|
| 197 | } |
---|
[7164] | 198 | std::vector<char*>* file = new std::vector<char*>; |
---|
[5318] | 199 | |
---|
| 200 | char lineBuffer[PARSELINELENGHT]; |
---|
| 201 | char* addString; |
---|
| 202 | while(fgets (lineBuffer, PARSELINELENGHT, stream) != NULL) |
---|
| 203 | { |
---|
| 204 | addString = new char[strlen(lineBuffer)+1]; |
---|
| 205 | strcpy(addString, lineBuffer); |
---|
[7164] | 206 | file->push_back(addString); |
---|
[5318] | 207 | } |
---|
| 208 | fclose(stream); |
---|
| 209 | return file; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | |
---|
[5266] | 214 | void Shader::activateShader() |
---|
| 215 | { |
---|
[5273] | 216 | if (likely (this->shaderProgram != 0)) |
---|
[5317] | 217 | { |
---|
[5273] | 218 | glUseProgramObjectARB(this->shaderProgram); |
---|
[5317] | 219 | Shader::storedShader = this; |
---|
| 220 | } |
---|
[5261] | 221 | } |
---|
[5262] | 222 | |
---|
[5266] | 223 | void Shader::deactivateShader() |
---|
| 224 | { |
---|
[5364] | 225 | if (storedShader != NULL) |
---|
| 226 | glUseProgramObjectARB(0); |
---|
| 227 | Shader::storedShader = NULL; |
---|
[5266] | 228 | } |
---|
[5262] | 229 | |
---|
[5266] | 230 | |
---|
[5262] | 231 | void Shader::deleteProgram(SHADER_TYPE type) |
---|
| 232 | { |
---|
[5335] | 233 | GLint status = 0; |
---|
[5273] | 234 | if (type == SHADER_VERTEX && this->vertexShader != 0) |
---|
[5262] | 235 | { |
---|
| 236 | delete[] this->vertexShaderFile; |
---|
| 237 | this->vertexShaderFile = NULL; |
---|
[5321] | 238 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5263] | 239 | glDeleteObjectARB(this->vertexShader); |
---|
[5320] | 240 | glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 241 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 242 | Shader::printError(this->vertexShader); |
---|
[5263] | 243 | this->vertexShader = 0; |
---|
[5262] | 244 | } |
---|
[5273] | 245 | else if (type == SHADER_FRAGMENT && this->fragmentShader != 0) |
---|
[5262] | 246 | { |
---|
| 247 | delete[] this->fragmentShaderFile; |
---|
| 248 | this->fragmentShaderFile = NULL; |
---|
[5321] | 249 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5263] | 250 | glDeleteObjectARB(this->fragmentShader); |
---|
[5320] | 251 | glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 252 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 253 | Shader::printError(this->fragmentShader); |
---|
[5263] | 254 | this->fragmentShader = 0; |
---|
[5262] | 255 | } |
---|
| 256 | else |
---|
| 257 | return; |
---|
| 258 | } |
---|
| 259 | |
---|
[5264] | 260 | |
---|
[5319] | 261 | void Shader::printError(GLhandleARB program) |
---|
[5264] | 262 | { |
---|
[5273] | 263 | if (program == 0) |
---|
| 264 | return; |
---|
| 265 | |
---|
[5267] | 266 | int infologLength = 0; |
---|
| 267 | int charsWritten = 0; |
---|
| 268 | char *infoLog; |
---|
| 269 | |
---|
| 270 | glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, |
---|
| 271 | &infologLength); |
---|
| 272 | |
---|
| 273 | if (infologLength > 0) |
---|
| 274 | { |
---|
[5283] | 275 | infoLog = new char[infologLength+1]; |
---|
[5267] | 276 | glGetInfoLogARB(program, infologLength, &charsWritten, infoLog); |
---|
[5269] | 277 | printf("%s\n", infoLog); |
---|
[5283] | 278 | delete[] infoLog; |
---|
[5267] | 279 | } |
---|
[5264] | 280 | } |
---|
| 281 | |
---|
[5333] | 282 | bool Shader::checkShaderAbility() |
---|
| 283 | { |
---|
| 284 | if (GLEW_ARB_shader_objects && |
---|
| 285 | GLEW_ARB_shading_language_100 && |
---|
| 286 | GLEW_ARB_vertex_shader && |
---|
| 287 | GLEW_ARB_fragment_shader) |
---|
| 288 | return true; |
---|
| 289 | else |
---|
| 290 | return false; |
---|
| 291 | } |
---|
[5264] | 292 | |
---|
[5262] | 293 | void Shader::debug() const |
---|
| 294 | { |
---|
| 295 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
---|
| 296 | if (this->vertexShader != 0) |
---|
| 297 | { |
---|
[5266] | 298 | /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
---|
[5262] | 299 | if (this->vertexShaderSource != NULL) |
---|
| 300 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
---|
| 301 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
---|
| 302 | } |
---|
| 303 | if (this->fragmentShader != 0) |
---|
| 304 | { |
---|
| 305 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
---|
| 306 | if (this->fragmentShaderSource != NULL) |
---|
| 307 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
---|
[5266] | 308 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ |
---|
[5262] | 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|