| [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" |
|---|
| [5262] | 22 | #include <stdio.h> |
|---|
| 23 | #include "debug.h" |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | #ifndef PARSELINELENGHT |
|---|
| 27 | #define PARSELINELENGHT 512 //!< how many chars to read at once |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| [1856] | 30 | using namespace std; |
|---|
| [1853] | 31 | |
|---|
| [1856] | 32 | |
|---|
| [3245] | 33 | /** |
|---|
| [4838] | 34 | * standard constructor |
|---|
| [3245] | 35 | */ |
|---|
| [5262] | 36 | Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile) |
|---|
| [3365] | 37 | { |
|---|
| [5261] | 38 | this->setClassID(CL_SHADER, "Shader"); |
|---|
| [4320] | 39 | |
|---|
| [5261] | 40 | this->fragmentShaderFile = NULL; |
|---|
| 41 | this->vertexShaderFile = NULL; |
|---|
| 42 | this->shaderProgram = 0; |
|---|
| 43 | this->vertexShader = 0; |
|---|
| 44 | this->fragmentShader = 0; |
|---|
| [5262] | 45 | |
|---|
| [5263] | 46 | if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) |
|---|
| [5273] | 47 | { |
|---|
| 48 | this->shaderProgram = glCreateProgramObjectARB(); |
|---|
| [5263] | 49 | |
|---|
| [5273] | 50 | if (vertexShaderFile != NULL) |
|---|
| [5285] | 51 | this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile); |
|---|
| [5273] | 52 | if (fragmentShaderFile != NULL) |
|---|
| [5285] | 53 | this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile); |
|---|
| [5273] | 54 | try { |
|---|
| [5285] | 55 | glLinkProgramARB(this->shaderProgram); } |
|---|
| [5273] | 56 | catch(GLenum errorCode) { |
|---|
| [5285] | 57 | this->printError(this->shaderProgram); } |
|---|
| [5273] | 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 |
|---|
| [3245] | 68 | */ |
|---|
| [5261] | 69 | Shader::~Shader () |
|---|
| [3543] | 70 | { |
|---|
| 71 | // delete what has to be deleted here |
|---|
| [5262] | 72 | this->deleteProgram(SHADER_VERTEX); |
|---|
| 73 | this->deleteProgram(SHADER_FRAGMENT); |
|---|
| [5263] | 74 | |
|---|
| [5273] | 75 | if (this->fragmentShader != 0) |
|---|
| 76 | glDeleteObjectARB(this->fragmentShader); |
|---|
| 77 | if (this->vertexShader != 0) |
|---|
| 78 | glDeleteObjectARB(this->vertexShader); |
|---|
| 79 | if (this->shaderProgram != 0) |
|---|
| 80 | glDeleteObjectARB(this->shaderProgram); |
|---|
| [3543] | 81 | } |
|---|
| [5261] | 82 | |
|---|
| 83 | |
|---|
| 84 | bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName) |
|---|
| 85 | { |
|---|
| [5285] | 86 | GLenum shader = 0; |
|---|
| 87 | |
|---|
| [5262] | 88 | if (type != SHADER_VERTEX && type != SHADER_FRAGMENT) |
|---|
| [5261] | 89 | return false; |
|---|
| [5262] | 90 | this->deleteProgram(type); |
|---|
| [5261] | 91 | |
|---|
| 92 | |
|---|
| [5266] | 93 | const char* program = fileRead(fileName); |
|---|
| [5271] | 94 | if (program == NULL) |
|---|
| 95 | return false; |
|---|
| [5266] | 96 | if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader) |
|---|
| [5262] | 97 | { |
|---|
| 98 | this->vertexShaderFile = new char[strlen(fileName)+1]; |
|---|
| 99 | strcpy(this->vertexShaderFile, fileName); |
|---|
| 100 | |
|---|
| [5269] | 101 | shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
|---|
| [5263] | 102 | } |
|---|
| [5262] | 103 | |
|---|
| [5263] | 104 | if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader) |
|---|
| 105 | { |
|---|
| [5266] | 106 | this->fragmentShaderFile = new char[strlen(fileName)+1]; |
|---|
| 107 | strcpy(this->fragmentShaderFile, fileName); |
|---|
| 108 | |
|---|
| [5269] | 109 | shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); |
|---|
| [5263] | 110 | } |
|---|
| [5273] | 111 | |
|---|
| 112 | if (shader != 0) |
|---|
| 113 | { |
|---|
| 114 | glShaderSourceARB(shader, 1, (const GLcharARB**)&program, NULL); |
|---|
| 115 | try { |
|---|
| [5285] | 116 | glCompileShaderARB(shader); |
|---|
| [5273] | 117 | } |
|---|
| 118 | catch (...) |
|---|
| [5285] | 119 | { |
|---|
| 120 | this->printError(shader); |
|---|
| 121 | } |
|---|
| [5273] | 122 | glAttachObjectARB(this->shaderProgram, shader); |
|---|
| 123 | delete[] program; |
|---|
| 124 | } |
|---|
| [5261] | 125 | } |
|---|
| 126 | |
|---|
| [5266] | 127 | char* Shader::fileRead(const char* fileName) |
|---|
| [5261] | 128 | { |
|---|
| [5266] | 129 | FILE* fileHandle; |
|---|
| 130 | char* content = NULL; |
|---|
| 131 | |
|---|
| 132 | int count = 0; |
|---|
| 133 | |
|---|
| [5271] | 134 | if (fileName == NULL) |
|---|
| 135 | return NULL; |
|---|
| [5266] | 136 | |
|---|
| [5271] | 137 | fileHandle = fopen(fileName, "rt"); |
|---|
| [5266] | 138 | |
|---|
| [5271] | 139 | if (fileHandle == NULL) |
|---|
| 140 | return NULL; |
|---|
| 141 | fseek(fileHandle, 0, SEEK_END); |
|---|
| 142 | count = ftell(fileHandle); |
|---|
| 143 | rewind(fileHandle); |
|---|
| 144 | if (count > 0) { |
|---|
| 145 | content = new char[count+1]; |
|---|
| 146 | count = fread(content, sizeof(char), count, fileHandle); |
|---|
| 147 | content[count] = '\0'; |
|---|
| 148 | } |
|---|
| 149 | fclose(fileHandle); |
|---|
| 150 | return content; |
|---|
| [5266] | 151 | } |
|---|
| 152 | |
|---|
| 153 | void Shader::activateShader() |
|---|
| 154 | { |
|---|
| [5273] | 155 | if (likely (this->shaderProgram != 0)) |
|---|
| 156 | glUseProgramObjectARB(this->shaderProgram); |
|---|
| [5261] | 157 | } |
|---|
| [5262] | 158 | |
|---|
| [5266] | 159 | void Shader::deactivateShader() |
|---|
| 160 | { |
|---|
| 161 | glUseProgramObjectARB(0); |
|---|
| 162 | } |
|---|
| [5262] | 163 | |
|---|
| [5266] | 164 | |
|---|
| [5262] | 165 | void Shader::deleteProgram(SHADER_TYPE type) |
|---|
| 166 | { |
|---|
| [5273] | 167 | if (type == SHADER_VERTEX && this->vertexShader != 0) |
|---|
| [5262] | 168 | { |
|---|
| 169 | delete[] this->vertexShaderFile; |
|---|
| 170 | this->vertexShaderFile = NULL; |
|---|
| [5263] | 171 | glDeleteObjectARB(this->vertexShader); |
|---|
| 172 | this->vertexShader = 0; |
|---|
| [5262] | 173 | } |
|---|
| [5273] | 174 | else if (type == SHADER_FRAGMENT && this->fragmentShader != 0) |
|---|
| [5262] | 175 | { |
|---|
| 176 | delete[] this->fragmentShaderFile; |
|---|
| 177 | this->fragmentShaderFile = NULL; |
|---|
| [5263] | 178 | glDeleteObjectARB(this->fragmentShader); |
|---|
| 179 | this->fragmentShader = 0; |
|---|
| [5262] | 180 | } |
|---|
| 181 | else |
|---|
| 182 | return; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| [5264] | 185 | |
|---|
| 186 | void Shader::printError(GLenum program) |
|---|
| 187 | { |
|---|
| [5273] | 188 | if (program == 0) |
|---|
| 189 | return; |
|---|
| 190 | |
|---|
| [5267] | 191 | int infologLength = 0; |
|---|
| 192 | int charsWritten = 0; |
|---|
| 193 | char *infoLog; |
|---|
| 194 | |
|---|
| 195 | glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, |
|---|
| 196 | &infologLength); |
|---|
| 197 | |
|---|
| 198 | if (infologLength > 0) |
|---|
| 199 | { |
|---|
| [5283] | 200 | infoLog = new char[infologLength+1]; |
|---|
| [5267] | 201 | glGetInfoLogARB(program, infologLength, &charsWritten, infoLog); |
|---|
| [5269] | 202 | printf("%s\n", infoLog); |
|---|
| [5283] | 203 | delete[] infoLog; |
|---|
| [5267] | 204 | } |
|---|
| [5264] | 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| [5262] | 209 | void Shader::debug() const |
|---|
| 210 | { |
|---|
| 211 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
|---|
| 212 | if (this->vertexShader != 0) |
|---|
| 213 | { |
|---|
| [5266] | 214 | /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
|---|
| [5262] | 215 | if (this->vertexShaderSource != NULL) |
|---|
| 216 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
|---|
| 217 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
|---|
| 218 | } |
|---|
| 219 | if (this->fragmentShader != 0) |
|---|
| 220 | { |
|---|
| 221 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
|---|
| 222 | if (this->fragmentShaderSource != NULL) |
|---|
| 223 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
|---|
| [5266] | 224 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ |
|---|
| [5262] | 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|