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 "debug.h" |
---|
24 | |
---|
25 | #include "util/loading/resource_manager.h" |
---|
26 | |
---|
27 | |
---|
28 | #ifndef PARSELINELENGHT |
---|
29 | #define PARSELINELENGHT 512 //!< how many chars to read at once |
---|
30 | #endif |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * standard constructor |
---|
37 | */ |
---|
38 | Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile) |
---|
39 | { |
---|
40 | this->setClassID(CL_SHADER, "Shader"); |
---|
41 | |
---|
42 | this->fragmentShaderFile = ""; |
---|
43 | this->vertexShaderFile = ""; |
---|
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 | GLint status = 0; |
---|
51 | |
---|
52 | this->shaderProgram = glCreateProgramObjectARB(); |
---|
53 | |
---|
54 | if (!vertexShaderFile.empty()) |
---|
55 | this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile); |
---|
56 | if (!fragmentShaderFile.empty()) |
---|
57 | this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile); |
---|
58 | glLinkProgramARB(this->shaderProgram); |
---|
59 | // link error checking |
---|
60 | glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status); |
---|
61 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
62 | this->printError(this->shaderProgram); |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | PRINTF(2)("Shaders are not supported on your hardware\n"); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * standard deconstructor |
---|
73 | */ |
---|
74 | Shader::~Shader () |
---|
75 | { |
---|
76 | if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB)) |
---|
77 | Shader::deactivateShader(); |
---|
78 | |
---|
79 | // delete what has to be deleted here |
---|
80 | this->deleteProgram(SHADER_VERTEX); |
---|
81 | this->deleteProgram(SHADER_FRAGMENT); |
---|
82 | |
---|
83 | if (this->fragmentShader != 0) |
---|
84 | { |
---|
85 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
86 | glDeleteObjectARB(this->fragmentShader); |
---|
87 | } |
---|
88 | if (this->vertexShader != 0) |
---|
89 | { |
---|
90 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
91 | glDeleteObjectARB(this->vertexShader); |
---|
92 | } |
---|
93 | if (this->shaderProgram != 0) |
---|
94 | { |
---|
95 | GLint status = 0; |
---|
96 | //glLinkProgramARB(this->shaderProgram); |
---|
97 | glDeleteObjectARB(this->shaderProgram); |
---|
98 | // link error checking |
---|
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 | } |
---|
103 | } |
---|
104 | |
---|
105 | Shader* Shader::getShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile) |
---|
106 | { |
---|
107 | return (Shader*)ResourceManager::getInstance()->load(vertexShaderFile, SHADER, RP_LEVEL, fragmentShaderFile); |
---|
108 | } |
---|
109 | |
---|
110 | bool Shader::unload(Shader* shader) |
---|
111 | { |
---|
112 | return ResourceManager::getInstance()->unload(shader); |
---|
113 | } |
---|
114 | |
---|
115 | Shader* Shader::storedShader = NULL; |
---|
116 | |
---|
117 | |
---|
118 | bool Shader::loadShaderProgramm(SHADER_TYPE type, const std::string& fileName) |
---|
119 | { |
---|
120 | GLhandleARB shader = 0; |
---|
121 | |
---|
122 | if (type != SHADER_VERTEX && type != SHADER_FRAGMENT) |
---|
123 | return false; |
---|
124 | this->deleteProgram(type); |
---|
125 | |
---|
126 | |
---|
127 | std::vector<char*>* program = fileReadArray(fileName); |
---|
128 | |
---|
129 | if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader) |
---|
130 | { |
---|
131 | this->vertexShaderFile = fileName; |
---|
132 | |
---|
133 | shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
---|
134 | } |
---|
135 | |
---|
136 | if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader) |
---|
137 | { |
---|
138 | this->fragmentShaderFile = fileName; |
---|
139 | |
---|
140 | shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); |
---|
141 | } |
---|
142 | |
---|
143 | if (shader != 0) |
---|
144 | { |
---|
145 | GLint status = 0; |
---|
146 | /// FIXME do it back |
---|
147 | // glShaderSourceARB(shader, program->size(), (const std::string&)&(*program)[0], NULL); |
---|
148 | glCompileShaderARB(shader); |
---|
149 | // checking on error. |
---|
150 | glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); |
---|
151 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
152 | this->printError(shader); |
---|
153 | else |
---|
154 | glAttachObjectARB(this->shaderProgram, shader); |
---|
155 | } |
---|
156 | for (unsigned int i=0; i< program->size(); i++) |
---|
157 | delete[] (*program)[i]; |
---|
158 | delete program; |
---|
159 | } |
---|
160 | |
---|
161 | char* Shader::fileRead(const std::string& fileName) |
---|
162 | { |
---|
163 | FILE* fileHandle; |
---|
164 | char* content = NULL; |
---|
165 | |
---|
166 | int count = 0; |
---|
167 | |
---|
168 | if (fileName.empty()) |
---|
169 | return NULL; |
---|
170 | |
---|
171 | fileHandle = fopen(fileName.c_str(), "rt"); |
---|
172 | |
---|
173 | if (fileHandle == NULL) |
---|
174 | return NULL; |
---|
175 | fseek(fileHandle, 0, SEEK_END); |
---|
176 | count = ftell(fileHandle); |
---|
177 | rewind(fileHandle); |
---|
178 | if (count > 0) { |
---|
179 | content = new char[count+1]; |
---|
180 | count = fread(content, sizeof(char), count, fileHandle); |
---|
181 | content[count] = '\0'; |
---|
182 | } |
---|
183 | fclose(fileHandle); |
---|
184 | return content; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | std::vector<char*>* Shader::fileReadArray(const std::string& fileName) |
---|
189 | { |
---|
190 | FILE* stream; //< The stream we use to read the file. |
---|
191 | |
---|
192 | if( (stream = fopen (fileName.c_str(), "rt")) == NULL) |
---|
193 | { |
---|
194 | PRINTF(1)("Shader could not open %s\n", fileName.c_str()); |
---|
195 | return NULL; |
---|
196 | } |
---|
197 | std::vector<char*>* file = new std::vector<char*>; |
---|
198 | |
---|
199 | char lineBuffer[PARSELINELENGHT]; |
---|
200 | char* addString; |
---|
201 | while(fgets (lineBuffer, PARSELINELENGHT, stream) != NULL) |
---|
202 | { |
---|
203 | addString = new char[strlen(lineBuffer)+1]; |
---|
204 | strcpy(addString, lineBuffer); |
---|
205 | file->push_back(addString); |
---|
206 | } |
---|
207 | fclose(stream); |
---|
208 | return file; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | void Shader::activateShader() |
---|
214 | { |
---|
215 | if (likely (this->shaderProgram != 0)) |
---|
216 | { |
---|
217 | glUseProgramObjectARB(this->shaderProgram); |
---|
218 | Shader::storedShader = this; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | void Shader::bindShader(const char* name, const float* value, size_t size) |
---|
223 | { |
---|
224 | if (likely (this->shaderProgram != 0)) { |
---|
225 | glUseProgramObjectARB(this->shaderProgram); |
---|
226 | |
---|
227 | unsigned int location = glGetUniformLocationARB(this->shaderProgram, name); |
---|
228 | /* This is EXPENSIVE and should be avoided. */ |
---|
229 | |
---|
230 | if (size == 1) glUniform1fvARB(location, 1, value); |
---|
231 | else if (size == 2) glUniform2fvARB(location, 1, value); |
---|
232 | else if (size == 3) glUniform3fvARB(location, 1, value); |
---|
233 | else if (size == 4) glUniform4fvARB(location, 1, value); |
---|
234 | else if (size == 9) glUniformMatrix3fvARB(location, 1, false, value); |
---|
235 | else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value); |
---|
236 | |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | void Shader::deactivateShader() |
---|
241 | { |
---|
242 | if (storedShader != NULL) |
---|
243 | glUseProgramObjectARB(0); |
---|
244 | Shader::storedShader = NULL; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | void Shader::deleteProgram(SHADER_TYPE type) |
---|
249 | { |
---|
250 | GLint status = 0; |
---|
251 | if (type == SHADER_VERTEX && this->vertexShader != 0) |
---|
252 | { |
---|
253 | this->vertexShaderFile = ""; |
---|
254 | glDetachObjectARB(this->shaderProgram, this->vertexShader); |
---|
255 | glDeleteObjectARB(this->vertexShader); |
---|
256 | glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
257 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
258 | Shader::printError(this->vertexShader); |
---|
259 | this->vertexShader = 0; |
---|
260 | } |
---|
261 | else if (type == SHADER_FRAGMENT && this->fragmentShader != 0) |
---|
262 | { |
---|
263 | this->fragmentShaderFile = ""; |
---|
264 | glDetachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
265 | glDeleteObjectARB(this->fragmentShader); |
---|
266 | glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status); |
---|
267 | if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) |
---|
268 | Shader::printError(this->fragmentShader); |
---|
269 | this->fragmentShader = 0; |
---|
270 | } |
---|
271 | else |
---|
272 | return; |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | void Shader::printError(GLhandleARB program) |
---|
277 | { |
---|
278 | if (program == 0) |
---|
279 | return; |
---|
280 | |
---|
281 | int infologLength = 0; |
---|
282 | int charsWritten = 0; |
---|
283 | char *infoLog; |
---|
284 | |
---|
285 | glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, |
---|
286 | &infologLength); |
---|
287 | |
---|
288 | if (infologLength > 0) |
---|
289 | { |
---|
290 | infoLog = new char[infologLength+1]; |
---|
291 | glGetInfoLogARB(program, infologLength, &charsWritten, infoLog); |
---|
292 | printf("%s\n", infoLog); |
---|
293 | delete[] infoLog; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | bool Shader::checkShaderAbility() |
---|
298 | { |
---|
299 | if (GLEW_ARB_shader_objects && |
---|
300 | GLEW_ARB_shading_language_100 && |
---|
301 | GLEW_ARB_vertex_shader && |
---|
302 | GLEW_ARB_fragment_shader) |
---|
303 | return true; |
---|
304 | else |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | void Shader::debug() const |
---|
309 | { |
---|
310 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
---|
311 | if (this->vertexShader != 0) |
---|
312 | { |
---|
313 | /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
---|
314 | if (this->vertexShaderSource != NULL) |
---|
315 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
---|
316 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
---|
317 | } |
---|
318 | if (this->fragmentShader != 0) |
---|
319 | { |
---|
320 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
---|
321 | if (this->fragmentShaderSource != NULL) |
---|
322 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
---|
323 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/ |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|