[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 | |
---|
[9806] | 18 | #include "shader_data.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 | |
---|
[1853] | 34 | |
---|
[9806] | 35 | ObjectListDefinition(ShaderData); |
---|
[1856] | 36 | |
---|
[3245] | 37 | /** |
---|
[4838] | 38 | * standard constructor |
---|
[3245] | 39 | */ |
---|
[7221] | 40 | Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile) |
---|
[3365] | 41 | { |
---|
[9685] | 42 | this->registerObject(this, Shader::_objectList); |
---|
[9806] | 43 | this->shaderProgram = 0; |
---|
| 44 | this->vertexShader = 0; |
---|
| 45 | this->fragmentShader = 0; |
---|
[5262] | 46 | |
---|
[9806] | 47 | if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) |
---|
| 48 | { |
---|
| 49 | this->shaderProgram = glCreateProgramObjectARB(); |
---|
[5263] | 50 | |
---|
[9806] | 51 | if (!vertexShaderFile.empty()) |
---|
| 52 | this->loadShaderProgramm(Shader::Vertex, vertexShaderFile); |
---|
| 53 | if (!fragmentShaderFile.empty()) |
---|
| 54 | this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile); |
---|
[8037] | 55 | |
---|
[9806] | 56 | this->linkShaderProgram(); |
---|
[8037] | 57 | |
---|
[9806] | 58 | } |
---|
| 59 | else |
---|
| 60 | { |
---|
| 61 | PRINTF(2)("Shaders are not supported on your hardware\n"); |
---|
| 62 | } |
---|
[3365] | 63 | } |
---|
[1853] | 64 | |
---|
| 65 | |
---|
[3245] | 66 | /** |
---|
[4838] | 67 | * standard deconstructor |
---|
[5318] | 68 | */ |
---|
[5261] | 69 | Shader::~Shader () |
---|
[3543] | 70 | { |
---|
[5322] | 71 | if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB)) |
---|
[5318] | 72 | Shader::deactivateShader(); |
---|
| 73 | |
---|
[3543] | 74 | // delete what has to be deleted here |
---|
[8037] | 75 | this->deleteProgram(Shader::Vertex); |
---|
| 76 | this->deleteProgram(Shader::Fragment); |
---|
[5263] | 77 | |
---|
[5273] | 78 | if (this->fragmentShader != 0) |
---|
[5322] | 79 | { |
---|
| 80 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5273] | 81 | glDeleteObjectARB(this->fragmentShader); |
---|
[5322] | 82 | } |
---|
[5273] | 83 | if (this->vertexShader != 0) |
---|
[5322] | 84 | { |
---|
| 85 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5273] | 86 | glDeleteObjectARB(this->vertexShader); |
---|
[5322] | 87 | } |
---|
[5273] | 88 | if (this->shaderProgram != 0) |
---|
[5321] | 89 | { |
---|
| 90 | GLint status = 0; |
---|
[5322] | 91 | //glLinkProgramARB(this->shaderProgram); |
---|
[5273] | 92 | glDeleteObjectARB(this->shaderProgram); |
---|
[9806] | 93 | // link error checking |
---|
[5321] | 94 | glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 95 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 96 | this->printError(this->shaderProgram); |
---|
| 97 | } |
---|
[3543] | 98 | } |
---|
[5261] | 99 | |
---|
[5317] | 100 | Shader* Shader::storedShader = NULL; |
---|
[5261] | 101 | |
---|
[5317] | 102 | |
---|
[8037] | 103 | bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName) |
---|
[5261] | 104 | { |
---|
[5319] | 105 | GLhandleARB shader = 0; |
---|
[5285] | 106 | |
---|
[8037] | 107 | if (type != Shader::Vertex && type != Shader::Fragment) |
---|
[5261] | 108 | return false; |
---|
[5262] | 109 | this->deleteProgram(type); |
---|
[5261] | 110 | |
---|
| 111 | |
---|
[8037] | 112 | std::string program; |
---|
| 113 | if (!readShader(fileName, program)) |
---|
| 114 | return false; |
---|
[5318] | 115 | |
---|
[8037] | 116 | if (type == Shader::Vertex && GLEW_ARB_vertex_shader) |
---|
[5262] | 117 | { |
---|
[7221] | 118 | this->vertexShaderFile = fileName; |
---|
[5262] | 119 | |
---|
[5269] | 120 | shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
---|
[5263] | 121 | } |
---|
[5262] | 122 | |
---|
[8037] | 123 | if (type == Shader::Fragment && GLEW_ARB_fragment_shader) |
---|
[5263] | 124 | { |
---|
[7221] | 125 | this->fragmentShaderFile = fileName; |
---|
[5266] | 126 | |
---|
[5269] | 127 | shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); |
---|
[5263] | 128 | } |
---|
[5273] | 129 | |
---|
| 130 | if (shader != 0) |
---|
[5319] | 131 | { |
---|
[5320] | 132 | GLint status = 0; |
---|
[8037] | 133 | const char* prog = program.c_str(); |
---|
| 134 | |
---|
| 135 | glShaderSourceARB(shader, 1, &prog, NULL); |
---|
[5320] | 136 | glCompileShaderARB(shader); |
---|
| 137 | // checking on error. |
---|
| 138 | glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); |
---|
| 139 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 140 | this->printError(shader); |
---|
| 141 | else |
---|
| 142 | glAttachObjectARB(this->shaderProgram, shader); |
---|
[5319] | 143 | } |
---|
[8316] | 144 | return true; |
---|
[5261] | 145 | } |
---|
| 146 | |
---|
[8037] | 147 | |
---|
| 148 | void Shader::linkShaderProgram() |
---|
[5261] | 149 | { |
---|
[8037] | 150 | GLint status = 0; |
---|
[5266] | 151 | |
---|
[8037] | 152 | glLinkProgramARB(this->shaderProgram); |
---|
[9806] | 153 | // link error checking |
---|
[8037] | 154 | glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status); |
---|
| 155 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 156 | this->printError(this->shaderProgram); |
---|
| 157 | } |
---|
[5266] | 158 | |
---|
| 159 | |
---|
[8037] | 160 | bool Shader::readShader(const std::string& fileName, std::string& output) |
---|
| 161 | { |
---|
| 162 | char lineBuffer[PARSELINELENGTH]; |
---|
[5266] | 163 | |
---|
[8037] | 164 | std::ifstream shader; |
---|
| 165 | shader.open(fileName.c_str()); |
---|
| 166 | if (!shader.is_open()) |
---|
| 167 | return false; |
---|
[5266] | 168 | |
---|
[5318] | 169 | |
---|
[8037] | 170 | while (!shader.eof()) |
---|
[5318] | 171 | { |
---|
[8037] | 172 | shader.getline(lineBuffer, PARSELINELENGTH); |
---|
| 173 | output += lineBuffer; |
---|
| 174 | output += "\n"; |
---|
[5318] | 175 | } |
---|
| 176 | |
---|
[8037] | 177 | |
---|
| 178 | shader.close(); |
---|
| 179 | return true; |
---|
[5318] | 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
[5266] | 184 | void Shader::activateShader() |
---|
| 185 | { |
---|
[5273] | 186 | if (likely (this->shaderProgram != 0)) |
---|
[5317] | 187 | { |
---|
[5273] | 188 | glUseProgramObjectARB(this->shaderProgram); |
---|
[5317] | 189 | Shader::storedShader = this; |
---|
| 190 | } |
---|
[5261] | 191 | } |
---|
[5262] | 192 | |
---|
[8255] | 193 | void Shader::bindShader(const char* name, const float* value, size_t size) |
---|
| 194 | { |
---|
[9806] | 195 | if (likely (this->shaderProgram != 0)) |
---|
| 196 | { |
---|
| 197 | glUseProgramObjectARB(this->shaderProgram); |
---|
[8255] | 198 | |
---|
[9806] | 199 | unsigned int location = glGetUniformLocationARB(this->shaderProgram, name); |
---|
| 200 | /* This is EXPENSIVE and should be avoided. */ |
---|
[8255] | 201 | |
---|
[9806] | 202 | if (size == 1) glUniform1fvARB(location, 1, value); |
---|
| 203 | else if (size == 2) glUniform2fvARB(location, 1, value); |
---|
| 204 | else if (size == 3) glUniform3fvARB(location, 1, value); |
---|
| 205 | else if (size == 4) glUniform4fvARB(location, 1, value); |
---|
| 206 | else if (size == 9) glUniformMatrix3fvARB(location, 1, false, value); |
---|
| 207 | else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value); |
---|
[8255] | 208 | |
---|
[9806] | 209 | } |
---|
[8255] | 210 | } |
---|
| 211 | |
---|
[5266] | 212 | void Shader::deactivateShader() |
---|
| 213 | { |
---|
[9806] | 214 | if (storedShader != NULL) |
---|
| 215 | glUseProgramObjectARB(0); |
---|
| 216 | Shader::storedShader = NULL; |
---|
[5266] | 217 | } |
---|
[5262] | 218 | |
---|
[5266] | 219 | |
---|
[8037] | 220 | void Shader::deleteProgram(Shader::Type type) |
---|
[5262] | 221 | { |
---|
[5335] | 222 | GLint status = 0; |
---|
[8037] | 223 | if (type == Shader::Vertex && this->vertexShader != 0) |
---|
[5262] | 224 | { |
---|
[7221] | 225 | this->vertexShaderFile = ""; |
---|
[5321] | 226 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
[5263] | 227 | glDeleteObjectARB(this->vertexShader); |
---|
[5320] | 228 | glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 229 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 230 | Shader::printError(this->vertexShader); |
---|
[5263] | 231 | this->vertexShader = 0; |
---|
[5262] | 232 | } |
---|
[8037] | 233 | else if (type == Shader::Fragment && this->fragmentShader != 0) |
---|
[5262] | 234 | { |
---|
[7221] | 235 | this->fragmentShaderFile = ""; |
---|
[5321] | 236 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
[5263] | 237 | glDeleteObjectARB(this->fragmentShader); |
---|
[5320] | 238 | glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
| 239 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
| 240 | Shader::printError(this->fragmentShader); |
---|
[5263] | 241 | this->fragmentShader = 0; |
---|
[5262] | 242 | } |
---|
| 243 | else |
---|
| 244 | return; |
---|
| 245 | } |
---|
| 246 | |
---|
[5264] | 247 | |
---|
[5319] | 248 | void Shader::printError(GLhandleARB program) |
---|
[5264] | 249 | { |
---|
[5273] | 250 | if (program == 0) |
---|
| 251 | return; |
---|
| 252 | |
---|
[5267] | 253 | int infologLength = 0; |
---|
| 254 | int charsWritten = 0; |
---|
| 255 | char *infoLog; |
---|
| 256 | |
---|
| 257 | glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, |
---|
| 258 | &infologLength); |
---|
| 259 | |
---|
| 260 | if (infologLength > 0) |
---|
| 261 | { |
---|
[5283] | 262 | infoLog = new char[infologLength+1]; |
---|
[5267] | 263 | glGetInfoLogARB(program, infologLength, &charsWritten, infoLog); |
---|
[5269] | 264 | printf("%s\n", infoLog); |
---|
[5283] | 265 | delete[] infoLog; |
---|
[5267] | 266 | } |
---|
[5264] | 267 | } |
---|
| 268 | |
---|
[5333] | 269 | bool Shader::checkShaderAbility() |
---|
| 270 | { |
---|
| 271 | if (GLEW_ARB_shader_objects && |
---|
| 272 | GLEW_ARB_shading_language_100 && |
---|
| 273 | GLEW_ARB_vertex_shader && |
---|
| 274 | GLEW_ARB_fragment_shader) |
---|
| 275 | return true; |
---|
| 276 | else |
---|
| 277 | return false; |
---|
| 278 | } |
---|
[5264] | 279 | |
---|
[5262] | 280 | void Shader::debug() const |
---|
| 281 | { |
---|
| 282 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
---|
| 283 | if (this->vertexShader != 0) |
---|
| 284 | { |
---|
[9806] | 285 | /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
---|
| 286 | if (this->vertexShaderSource != NULL) |
---|
| 287 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
---|
| 288 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
---|
| 289 | } |
---|
| 290 | if (this->fragmentShader != 0) |
---|
| 291 | { |
---|
| 292 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
---|
| 293 | if (this->fragmentShaderSource != NULL) |
---|
| 294 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
---|
| 295 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ |
---|
[5262] | 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|