[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" |
---|
[8037] | 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 | |
---|
[8037] | 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()) |
---|
[8037] | 53 | this->loadShaderProgramm(Shader::Vertex, vertexShaderFile); |
---|
[7221] | 54 | if (!fragmentShaderFile.empty()) |
---|
[8037] | 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 |
---|
[8037] | 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 | |
---|
[8037] | 114 | bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName) |
---|
[5261] | 115 | { |
---|
[5319] | 116 | GLhandleARB shader = 0; |
---|
[5285] | 117 | |
---|
[8037] | 118 | if (type != Shader::Vertex && type != Shader::Fragment) |
---|
[5261] | 119 | return false; |
---|
[5262] | 120 | this->deleteProgram(type); |
---|
[5261] | 121 | |
---|
| 122 | |
---|
[8037] | 123 | std::string program; |
---|
| 124 | if (!readShader(fileName, program)) |
---|
| 125 | return false; |
---|
[5318] | 126 | |
---|
[8037] | 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 | |
---|
[8037] | 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; |
---|
[8037] | 144 | const char* prog = program.c_str(); |
---|
| 145 | |
---|
| 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 | } |
---|
[8457] | 155 | |
---|
| 156 | return true; |
---|
[5261] | 157 | } |
---|
| 158 | |
---|
[8037] | 159 | |
---|
| 160 | void Shader::linkShaderProgram() |
---|
[5261] | 161 | { |
---|
[8037] | 162 | GLint status = 0; |
---|
[5266] | 163 | |
---|
[8037] | 164 | glLinkProgramARB(this->shaderProgram); |
---|
| 165 | // link error checking |
---|
| 166 | glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status); |
---|
| 167 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 168 | this->printError(this->shaderProgram); |
---|
| 169 | } |
---|
[5266] | 170 | |
---|
| 171 | |
---|
[8037] | 172 | bool Shader::readShader(const std::string& fileName, std::string& output) |
---|
| 173 | { |
---|
| 174 | char lineBuffer[PARSELINELENGTH]; |
---|
[5266] | 175 | |
---|
[8037] | 176 | std::ifstream shader; |
---|
| 177 | shader.open(fileName.c_str()); |
---|
| 178 | if (!shader.is_open()) |
---|
| 179 | return false; |
---|
[5266] | 180 | |
---|
[5318] | 181 | |
---|
[8037] | 182 | while (!shader.eof()) |
---|
[5318] | 183 | { |
---|
[8037] | 184 | shader.getline(lineBuffer, PARSELINELENGTH); |
---|
| 185 | output += lineBuffer; |
---|
| 186 | output += "\n"; |
---|
[5318] | 187 | } |
---|
| 188 | |
---|
[8037] | 189 | |
---|
| 190 | shader.close(); |
---|
| 191 | return true; |
---|
[5318] | 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | |
---|
[5266] | 196 | void Shader::activateShader() |
---|
| 197 | { |
---|
[5273] | 198 | if (likely (this->shaderProgram != 0)) |
---|
[5317] | 199 | { |
---|
[5273] | 200 | glUseProgramObjectARB(this->shaderProgram); |
---|
[5317] | 201 | Shader::storedShader = this; |
---|
| 202 | } |
---|
[5261] | 203 | } |
---|
[5262] | 204 | |
---|
[8255] | 205 | void Shader::bindShader(const char* name, const float* value, size_t size) |
---|
| 206 | { |
---|
| 207 | if (likely (this->shaderProgram != 0)) { |
---|
| 208 | glUseProgramObjectARB(this->shaderProgram); |
---|
| 209 | |
---|
| 210 | unsigned int location = glGetUniformLocationARB(this->shaderProgram, name); |
---|
| 211 | /* This is EXPENSIVE and should be avoided. */ |
---|
| 212 | |
---|
| 213 | if (size == 1) glUniform1fvARB(location, 1, value); |
---|
| 214 | else if (size == 2) glUniform2fvARB(location, 1, value); |
---|
| 215 | else if (size == 3) glUniform3fvARB(location, 1, value); |
---|
| 216 | else if (size == 4) glUniform4fvARB(location, 1, value); |
---|
| 217 | else if (size == 9) glUniformMatrix3fvARB(location, 1, false, value); |
---|
| 218 | else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value); |
---|
| 219 | |
---|
| 220 | } |
---|
| 221 | } |
---|
| 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 | |
---|
[8037] | 231 | void Shader::deleteProgram(Shader::Type type) |
---|
[5262] | 232 | { |
---|
[5335] | 233 | GLint status = 0; |
---|
[8037] | 234 | if (type == Shader::Vertex && this->vertexShader != 0) |
---|
[5262] | 235 | { |
---|
[7221] | 236 | this->vertexShaderFile = ""; |
---|
[5321] | 237 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5263] | 238 | glDeleteObjectARB(this->vertexShader); |
---|
[5320] | 239 | glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 240 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 241 | Shader::printError(this->vertexShader); |
---|
[5263] | 242 | this->vertexShader = 0; |
---|
[5262] | 243 | } |
---|
[8037] | 244 | else if (type == Shader::Fragment && this->fragmentShader != 0) |
---|
[5262] | 245 | { |
---|
[7221] | 246 | this->fragmentShaderFile = ""; |
---|
[5321] | 247 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5263] | 248 | glDeleteObjectARB(this->fragmentShader); |
---|
[5320] | 249 | glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 250 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 251 | Shader::printError(this->fragmentShader); |
---|
[5263] | 252 | this->fragmentShader = 0; |
---|
[5262] | 253 | } |
---|
| 254 | else |
---|
| 255 | return; |
---|
| 256 | } |
---|
| 257 | |
---|
[5264] | 258 | |
---|
[5319] | 259 | void Shader::printError(GLhandleARB program) |
---|
[5264] | 260 | { |
---|
[5273] | 261 | if (program == 0) |
---|
| 262 | return; |
---|
| 263 | |
---|
[5267] | 264 | int infologLength = 0; |
---|
| 265 | int charsWritten = 0; |
---|
| 266 | char *infoLog; |
---|
| 267 | |
---|
| 268 | glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, |
---|
| 269 | &infologLength); |
---|
| 270 | |
---|
| 271 | if (infologLength > 0) |
---|
| 272 | { |
---|
[5283] | 273 | infoLog = new char[infologLength+1]; |
---|
[5267] | 274 | glGetInfoLogARB(program, infologLength, &charsWritten, infoLog); |
---|
[5269] | 275 | printf("%s\n", infoLog); |
---|
[5283] | 276 | delete[] infoLog; |
---|
[5267] | 277 | } |
---|
[5264] | 278 | } |
---|
| 279 | |
---|
[5333] | 280 | bool Shader::checkShaderAbility() |
---|
| 281 | { |
---|
| 282 | if (GLEW_ARB_shader_objects && |
---|
| 283 | GLEW_ARB_shading_language_100 && |
---|
| 284 | GLEW_ARB_vertex_shader && |
---|
| 285 | GLEW_ARB_fragment_shader) |
---|
| 286 | return true; |
---|
| 287 | else |
---|
| 288 | return false; |
---|
| 289 | } |
---|
[5264] | 290 | |
---|
[5262] | 291 | void Shader::debug() const |
---|
| 292 | { |
---|
| 293 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
---|
| 294 | if (this->vertexShader != 0) |
---|
| 295 | { |
---|
[5266] | 296 | /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
---|
[5262] | 297 | if (this->vertexShaderSource != NULL) |
---|
| 298 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
---|
| 299 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
---|
| 300 | } |
---|
| 301 | if (this->fragmentShader != 0) |
---|
| 302 | { |
---|
| 303 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
---|
| 304 | if (this->fragmentShaderSource != NULL) |
---|
| 305 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
---|
[5266] | 306 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ |
---|
[5262] | 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|