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