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 | |
---|
21 | #include "array.h" |
---|
22 | #include "stdlibincl.h" |
---|
23 | #include <stdio.h> |
---|
24 | #include "debug.h" |
---|
25 | |
---|
26 | |
---|
27 | #ifndef PARSELINELENGHT |
---|
28 | #define PARSELINELENGHT 512 //!< how many chars to read at once |
---|
29 | #endif |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * standard constructor |
---|
36 | */ |
---|
37 | Shader::Shader (const char* vertexShaderFile, const char* fragmentShaderFile) |
---|
38 | { |
---|
39 | this->setClassID(CL_SHADER, "Shader"); |
---|
40 | |
---|
41 | this->fragmentShaderFile = NULL; |
---|
42 | this->vertexShaderFile = NULL; |
---|
43 | this->fragmentShaderSource = NULL; |
---|
44 | this->vertexShaderSource = NULL; |
---|
45 | this->fragmentShaderLengths = NULL; |
---|
46 | this->vertexShaderLenghts = NULL; |
---|
47 | this->shaderProgram = 0; |
---|
48 | this->vertexShader = 0; |
---|
49 | this->fragmentShader = 0; |
---|
50 | |
---|
51 | if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) |
---|
52 | this->shaderProgram = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
---|
53 | |
---|
54 | if (vertexShaderFile != NULL) |
---|
55 | this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile); |
---|
56 | if (fragmentShaderFile != NULL) |
---|
57 | this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile); |
---|
58 | glLinkProgramARB(this->shaderProgram); |
---|
59 | this->printError(this->shaderProgram); |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * standard deconstructor |
---|
66 | */ |
---|
67 | Shader::~Shader () |
---|
68 | { |
---|
69 | // delete what has to be deleted here |
---|
70 | this->deleteProgram(SHADER_VERTEX); |
---|
71 | this->deleteProgram(SHADER_FRAGMENT); |
---|
72 | |
---|
73 | glDeleteObjectARB(this->shaderProgram); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | bool Shader::loadShaderProgramm(SHADER_TYPE type, const char* fileName) |
---|
78 | { |
---|
79 | if (type != SHADER_VERTEX && type != SHADER_FRAGMENT) |
---|
80 | return false; |
---|
81 | this->deleteProgram(type); |
---|
82 | |
---|
83 | FILE* stream; //< The stream we use to read the file. |
---|
84 | |
---|
85 | if( (stream = fopen (fileName, "r")) == NULL) |
---|
86 | { |
---|
87 | PRINTF(1)("Shader could not open %s\n", fileName); |
---|
88 | return false; |
---|
89 | } |
---|
90 | Array<char*>* program = new Array<char*>; |
---|
91 | Array<int>* lengths = new Array<int>; |
---|
92 | if (type == SHADER_VERTEX) |
---|
93 | { |
---|
94 | this->vertexShaderFile = new char[strlen(fileName)+1]; |
---|
95 | strcpy(this->vertexShaderFile, fileName); |
---|
96 | this->vertexShaderSource = program; |
---|
97 | this->vertexShaderLenghts = lengths; |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | this->fragmentShaderFile = new char[strlen(fileName)+1]; |
---|
102 | strcpy(this->fragmentShaderFile, fileName); |
---|
103 | this->fragmentShaderSource = program; |
---|
104 | this->fragmentShaderLengths = lengths; |
---|
105 | } |
---|
106 | |
---|
107 | char lineBuffer[PARSELINELENGHT]; |
---|
108 | char* addString; |
---|
109 | while( !feof( stream)) |
---|
110 | { |
---|
111 | // get next line |
---|
112 | fgets (lineBuffer, PARSELINELENGHT, stream); |
---|
113 | // if (strchr(lineBuffer, '\n')) |
---|
114 | // { |
---|
115 | // addString = new char[strlen(lineBuffer)]; |
---|
116 | // strncpy(addString, lineBuffer, strlen (lineBuffer)-1); |
---|
117 | // addString[strlen(lineBuffer)-1] ='\0'; |
---|
118 | // } |
---|
119 | // else |
---|
120 | { |
---|
121 | addString = new char[strlen(lineBuffer)+1]; |
---|
122 | strcpy(addString, lineBuffer); |
---|
123 | } |
---|
124 | program->addEntry(addString); |
---|
125 | lengths->addEntry(strlen(addString)); |
---|
126 | } |
---|
127 | fclose(stream); |
---|
128 | |
---|
129 | program->finalizeArray(); |
---|
130 | lengths->finalizeArray(); |
---|
131 | |
---|
132 | if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader) |
---|
133 | { |
---|
134 | this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); |
---|
135 | glShaderSourceARB(this->vertexShader, program->getCount(), (const GLcharARB**)program->getArray(), lengths->getArray()); |
---|
136 | glCompileShaderARB(this->vertexShader); |
---|
137 | glAttachObjectARB(this->shaderProgram, this->vertexShader); |
---|
138 | this->printError(this->vertexShader); |
---|
139 | } |
---|
140 | |
---|
141 | if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader) |
---|
142 | { |
---|
143 | this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); |
---|
144 | glShaderSourceARB(this->fragmentShader, program->getCount(), (const GLcharARB**)program->getArray(), lengths->getArray()); |
---|
145 | glCompileShaderARB(this->fragmentShader); |
---|
146 | glAttachObjectARB(this->shaderProgram, this->fragmentShader); |
---|
147 | this->printError(this->fragmentShader); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | bool Shader::activateShader() |
---|
152 | { |
---|
153 | glUseProgramObjectARB(this->shaderProgram); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void Shader::deleteProgram(SHADER_TYPE type) |
---|
158 | { |
---|
159 | Array<char*>* deleteArray = NULL; |
---|
160 | if (type == SHADER_VERTEX) |
---|
161 | { |
---|
162 | deleteArray = this->vertexShaderSource; |
---|
163 | this->vertexShaderSource = NULL; |
---|
164 | delete this->vertexShaderLenghts; |
---|
165 | this->vertexShaderLenghts = NULL; |
---|
166 | delete[] this->vertexShaderFile; |
---|
167 | this->vertexShaderFile = NULL; |
---|
168 | glDeleteObjectARB(this->vertexShader); |
---|
169 | this->vertexShader = 0; |
---|
170 | } |
---|
171 | else if (type == SHADER_FRAGMENT) |
---|
172 | { |
---|
173 | deleteArray = this->fragmentShaderSource; |
---|
174 | delete this->fragmentShaderLengths; |
---|
175 | this->fragmentShaderLengths = NULL; |
---|
176 | this->fragmentShaderSource = NULL; |
---|
177 | delete[] this->fragmentShaderFile; |
---|
178 | this->fragmentShaderFile = NULL; |
---|
179 | glDeleteObjectARB(this->fragmentShader); |
---|
180 | this->fragmentShader = 0; |
---|
181 | } |
---|
182 | else |
---|
183 | return; |
---|
184 | |
---|
185 | if (deleteArray == NULL) |
---|
186 | return; |
---|
187 | else |
---|
188 | { |
---|
189 | deleteArray->finalizeArray(); |
---|
190 | for (unsigned int i = 0; i < deleteArray->getCount(); i++) |
---|
191 | { |
---|
192 | delete[] deleteArray->getEntry(i); |
---|
193 | } |
---|
194 | delete deleteArray; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | void Shader::printError(GLenum program) |
---|
200 | { |
---|
201 | GLint length = 5000; |
---|
202 | // glGetObjectParameterARBfiv(program, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length); |
---|
203 | char* text = new char[length+1]; |
---|
204 | glGetInfoLogARB(program, length, &length, text); |
---|
205 | printf("%s\n", text); |
---|
206 | delete[] text; |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | void Shader::debug() const |
---|
212 | { |
---|
213 | PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); |
---|
214 | if (this->vertexShader != 0) |
---|
215 | { |
---|
216 | PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile); |
---|
217 | if (this->vertexShaderSource != NULL) |
---|
218 | for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++) |
---|
219 | PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i)); |
---|
220 | } |
---|
221 | if (this->fragmentShader != 0) |
---|
222 | { |
---|
223 | PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile); |
---|
224 | if (this->fragmentShaderSource != NULL) |
---|
225 | for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++) |
---|
226 | PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i)); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | } |
---|
231 | |
---|