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