[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" |
---|
[7822] | 22 | //#include <stdio.h> |
---|
| 23 | #include <fstream> |
---|
| 24 | |
---|
[5262] | 25 | #include "debug.h" |
---|
| 26 | |
---|
[7193] | 27 | #include "util/loading/resource_manager.h" |
---|
[5262] | 28 | |
---|
[5323] | 29 | |
---|
[7822] | 30 | #ifndef PARSELINELENGTH |
---|
| 31 | #define PARSELINELENGTH 512 //!< how many chars to read at once |
---|
[5262] | 32 | #endif |
---|
| 33 | |
---|
[1856] | 34 | using namespace std; |
---|
[1853] | 35 | |
---|
[1856] | 36 | |
---|
[3245] | 37 | /** |
---|
[4838] | 38 | * standard constructor |
---|
[3245] | 39 | */ |
---|
[7221] | 40 | Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile) |
---|
[3365] | 41 | { |
---|
[5261] | 42 | this->setClassID(CL_SHADER, "Shader"); |
---|
[4320] | 43 | |
---|
[5261] | 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 | { |
---|
| 50 | this->shaderProgram = glCreateProgramObjectARB(); |
---|
[5263] | 51 | |
---|
[7221] | 52 | if (!vertexShaderFile.empty()) |
---|
[7818] | 53 | this->loadShaderProgramm(Shader::Vertex, vertexShaderFile); |
---|
[7221] | 54 | if (!fragmentShaderFile.empty()) |
---|
[7818] | 55 | this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile); |
---|
| 56 | |
---|
| 57 | this->linkShaderProgram(); |
---|
| 58 | |
---|
[5320] | 59 | } |
---|
[5273] | 60 | else |
---|
| 61 | { |
---|
| 62 | PRINTF(2)("Shaders are not supported on your hardware\n"); |
---|
| 63 | } |
---|
[3365] | 64 | } |
---|
[1853] | 65 | |
---|
| 66 | |
---|
[3245] | 67 | /** |
---|
[4838] | 68 | * standard deconstructor |
---|
[5318] | 69 | */ |
---|
[5261] | 70 | Shader::~Shader () |
---|
[3543] | 71 | { |
---|
[5322] | 72 | if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB)) |
---|
[5318] | 73 | Shader::deactivateShader(); |
---|
| 74 | |
---|
[3543] | 75 | // delete what has to be deleted here |
---|
[7818] | 76 | this->deleteProgram(Shader::Vertex); |
---|
| 77 | this->deleteProgram(Shader::Fragment); |
---|
[5263] | 78 | |
---|
[5273] | 79 | if (this->fragmentShader != 0) |
---|
[5322] | 80 | { |
---|
| 81 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5273] | 82 | glDeleteObjectARB(this->fragmentShader); |
---|
[5322] | 83 | } |
---|
[5273] | 84 | if (this->vertexShader != 0) |
---|
[5322] | 85 | { |
---|
| 86 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5273] | 87 | glDeleteObjectARB(this->vertexShader); |
---|
[5322] | 88 | } |
---|
[5273] | 89 | if (this->shaderProgram != 0) |
---|
[5321] | 90 | { |
---|
| 91 | GLint status = 0; |
---|
[5322] | 92 | //glLinkProgramARB(this->shaderProgram); |
---|
[5273] | 93 | glDeleteObjectARB(this->shaderProgram); |
---|
[5321] | 94 | // link error checking |
---|
| 95 | glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 96 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 97 | this->printError(this->shaderProgram); |
---|
| 98 | } |
---|
[3543] | 99 | } |
---|
[5261] | 100 | |
---|
[7221] | 101 | Shader* Shader::getShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile) |
---|
[5323] | 102 | { |
---|
[6645] | 103 | return (Shader*)ResourceManager::getInstance()->load(vertexShaderFile, SHADER, RP_LEVEL, fragmentShaderFile); |
---|
[5323] | 104 | } |
---|
| 105 | |
---|
| 106 | bool Shader::unload(Shader* shader) |
---|
| 107 | { |
---|
| 108 | return ResourceManager::getInstance()->unload(shader); |
---|
| 109 | } |
---|
| 110 | |
---|
[5317] | 111 | Shader* Shader::storedShader = NULL; |
---|
[5261] | 112 | |
---|
[5317] | 113 | |
---|
[7818] | 114 | bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName) |
---|
[5261] | 115 | { |
---|
[5319] | 116 | GLhandleARB shader = 0; |
---|
[5285] | 117 | |
---|
[7818] | 118 | if (type != Shader::Vertex && type != Shader::Fragment) |
---|
[5261] | 119 | return false; |
---|
[5262] | 120 | this->deleteProgram(type); |
---|
[5261] | 121 | |
---|
| 122 | |
---|
[7822] | 123 | std::string program; |
---|
| 124 | if (!readShader(fileName, program)) |
---|
| 125 | return false; |
---|
[5318] | 126 | |
---|
[7818] | 127 | if (type == Shader::Vertex && GLEW_ARB_vertex_shader) |
---|
[5262] | 128 | { |
---|
[7221] | 129 | this->vertexShaderFile = fileName; |
---|
[5262] | 130 | |
---|
[5269] | 131 | shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
---|
[5263] | 132 | } |
---|
[5262] | 133 | |
---|
[7818] | 134 | if (type == Shader::Fragment && GLEW_ARB_fragment_shader) |
---|
[5263] | 135 | { |
---|
[7221] | 136 | this->fragmentShaderFile = fileName; |
---|
[5266] | 137 | |
---|
[5269] | 138 | shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); |
---|
[5263] | 139 | } |
---|
[5273] | 140 | |
---|
| 141 | if (shader != 0) |
---|
[5319] | 142 | { |
---|
[5320] | 143 | GLint status = 0; |
---|
[7827] | 144 | const char* prog = program.c_str(); |
---|
[7825] | 145 | |
---|
[7822] | 146 | glShaderSourceARB(shader, 1, &prog, NULL); |
---|
[5320] | 147 | glCompileShaderARB(shader); |
---|
| 148 | // checking on error. |
---|
| 149 | glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); |
---|
| 150 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 151 | this->printError(shader); |
---|
| 152 | else |
---|
| 153 | glAttachObjectARB(this->shaderProgram, shader); |
---|
[5319] | 154 | } |
---|
[5261] | 155 | } |
---|
| 156 | |
---|
[7818] | 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 | |
---|
[7822] | 170 | bool Shader::readShader(const std::string& fileName, std::string& output) |
---|
[5261] | 171 | { |
---|
[7822] | 172 | char lineBuffer[PARSELINELENGTH]; |
---|
[5266] | 173 | |
---|
[7822] | 174 | std::ifstream shader; |
---|
| 175 | shader.open(fileName.c_str()); |
---|
| 176 | if (!shader.is_open()) |
---|
| 177 | return false; |
---|
[5266] | 178 | |
---|
| 179 | |
---|
[7822] | 180 | while (!shader.eof()) |
---|
[5318] | 181 | { |
---|
[7822] | 182 | shader.getline(lineBuffer, PARSELINELENGTH); |
---|
| 183 | output += lineBuffer; |
---|
[7830] | 184 | output += "\n"; |
---|
[5318] | 185 | } |
---|
| 186 | |
---|
[7822] | 187 | |
---|
| 188 | shader.close(); |
---|
[7826] | 189 | return true; |
---|
[5318] | 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
[5266] | 194 | void Shader::activateShader() |
---|
| 195 | { |
---|
[5273] | 196 | if (likely (this->shaderProgram != 0)) |
---|
[5317] | 197 | { |
---|
[5273] | 198 | glUseProgramObjectARB(this->shaderProgram); |
---|
[5317] | 199 | Shader::storedShader = this; |
---|
| 200 | } |
---|
[5261] | 201 | } |
---|
[5262] | 202 | |
---|
[5266] | 203 | void Shader::deactivateShader() |
---|
| 204 | { |
---|
[5364] | 205 | if (storedShader != NULL) |
---|
| 206 | glUseProgramObjectARB(0); |
---|
| 207 | Shader::storedShader = NULL; |
---|
[5266] | 208 | } |
---|
[5262] | 209 | |
---|
[5266] | 210 | |
---|
[7818] | 211 | void Shader::deleteProgram(Shader::Type type) |
---|
[5262] | 212 | { |
---|
[5335] | 213 | GLint status = 0; |
---|
[7818] | 214 | if (type == Shader::Vertex && this->vertexShader != 0) |
---|
[5262] | 215 | { |
---|
[7221] | 216 | this->vertexShaderFile = ""; |
---|
[5321] | 217 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5263] | 218 | glDeleteObjectARB(this->vertexShader); |
---|
[5320] | 219 | glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 220 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 221 | Shader::printError(this->vertexShader); |
---|
[5263] | 222 | this->vertexShader = 0; |
---|
[5262] | 223 | } |
---|
[7818] | 224 | else if (type == Shader::Fragment && this->fragmentShader != 0) |
---|
[5262] | 225 | { |
---|
[7221] | 226 | this->fragmentShaderFile = ""; |
---|
[5321] | 227 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5263] | 228 | glDeleteObjectARB(this->fragmentShader); |
---|
[5320] | 229 | glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 230 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 231 | Shader::printError(this->fragmentShader); |
---|
[5263] | 232 | this->fragmentShader = 0; |
---|
[5262] | 233 | } |
---|
| 234 | else |
---|
| 235 | return; |
---|
| 236 | } |
---|
| 237 | |
---|
[5264] | 238 | |
---|
[5319] | 239 | void Shader::printError(GLhandleARB program) |
---|
[5264] | 240 | { |
---|
[5273] | 241 | if (program == 0) |
---|
| 242 | return; |
---|
| 243 | |
---|
[5267] | 244 | int infologLength = 0; |
---|
| 245 | int charsWritten = 0; |
---|
| 246 | char *infoLog; |
---|
| 247 | |
---|
| 248 | glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, |
---|
| 249 | &infologLength); |
---|
| 250 | |
---|
| 251 | if (infologLength > 0) |
---|
| 252 | { |
---|
[5283] | 253 | infoLog = new char[infologLength+1]; |
---|
[5267] | 254 | glGetInfoLogARB(program, infologLength, &charsWritten, infoLog); |
---|
[5269] | 255 | printf("%s\n", infoLog); |
---|
[5283] | 256 | delete[] infoLog; |
---|
[5267] | 257 | } |
---|
[5264] | 258 | } |
---|
| 259 | |
---|
[5333] | 260 | bool Shader::checkShaderAbility() |
---|
| 261 | { |
---|
| 262 | if (GLEW_ARB_shader_objects && |
---|
| 263 | GLEW_ARB_shading_language_100 && |
---|
| 264 | GLEW_ARB_vertex_shader && |
---|
| 265 | GLEW_ARB_fragment_shader) |
---|
| 266 | return true; |
---|
| 267 | else |
---|
| 268 | return false; |
---|
| 269 | } |
---|
[5264] | 270 | |
---|
[5262] | 271 | void Shader::debug() const |
---|
| 272 | { |
---|
| 273 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
---|
| 274 | if (this->vertexShader != 0) |
---|
| 275 | { |
---|
[5266] | 276 | /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
---|
[5262] | 277 | if (this->vertexShaderSource != NULL) |
---|
| 278 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
---|
| 279 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
---|
| 280 | } |
---|
| 281 | if (this->fragmentShader != 0) |
---|
| 282 | { |
---|
| 283 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
---|
| 284 | if (this->fragmentShaderSource != NULL) |
---|
| 285 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
---|
[5266] | 286 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ |
---|
[5262] | 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|