1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Benjamin Grauer |
---|
15 | co-programmer: ... |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | #include "light.h" |
---|
20 | |
---|
21 | #include "glincl.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | //! Definition of the Lights |
---|
26 | int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7}; |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | \brief standard constructor for a Light |
---|
31 | */ |
---|
32 | Light::Light () |
---|
33 | { |
---|
34 | this->setClassName ("Light"); |
---|
35 | |
---|
36 | glEnable (GL_LIGHTING); |
---|
37 | this->setAmbientColor(.3, .3, .3); |
---|
38 | this->lights = new LightValue*[NUMBEROFLIGHTS]; |
---|
39 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
40 | lights[i] = NULL; |
---|
41 | this->currentLight = NULL; |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | \brief standard deconstructor |
---|
46 | |
---|
47 | first disables Lighting |
---|
48 | then deletes all the lights |
---|
49 | then deletes the rest of the allocated memory |
---|
50 | and in the end sets the singleton Reference to zero. |
---|
51 | */ |
---|
52 | Light::~Light () |
---|
53 | { |
---|
54 | glDisable(GL_LIGHTING); |
---|
55 | |
---|
56 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
57 | this->deleteLight(i); |
---|
58 | delete lights; |
---|
59 | Light::singletonRef = NULL; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | \brief singleton-Reference to the Light-class |
---|
64 | */ |
---|
65 | Light* Light::singletonRef = NULL; |
---|
66 | |
---|
67 | /** |
---|
68 | \returns The Instance of the Lights |
---|
69 | */ |
---|
70 | Light* Light::getInstance(void) |
---|
71 | { |
---|
72 | if (singletonRef) |
---|
73 | return singletonRef; |
---|
74 | else |
---|
75 | return Light::singletonRef = new Light(); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | \brief initializes a new Light with default values, and enables GL_LIGHTING |
---|
80 | */ |
---|
81 | void Light::init(int lightNumber) |
---|
82 | { |
---|
83 | PRINTF(3)("initializing Light number %d.\n", lightNumber); |
---|
84 | // enable The light |
---|
85 | glEnable(lightsV[lightNumber]); |
---|
86 | this->currentLight = lights[lightNumber] = new LightValue; |
---|
87 | |
---|
88 | // set default values |
---|
89 | this->currentLight->lightNumber = lightNumber; |
---|
90 | this->setPosition(0.0, 0.0, 0.0); |
---|
91 | this->setDiffuseColor(1.0, 1.0, 1.0); |
---|
92 | this->setSpecularColor(1.0, 1.0, 1.0); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | \brief Adds a new Light, by selecting the First free slot. |
---|
97 | |
---|
98 | if no slot is free error |
---|
99 | */ |
---|
100 | int Light::addLight(void) |
---|
101 | { |
---|
102 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
103 | if (!this->lights[i]) |
---|
104 | return addLight(i); |
---|
105 | PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS); |
---|
106 | return -1; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | \brief Adds a new Light |
---|
111 | \param lightNumber The number of the light to add. |
---|
112 | |
---|
113 | if the Number is not free: warn, automatically choose another slot. |
---|
114 | */ |
---|
115 | int Light::addLight(int lightNumber) |
---|
116 | { |
---|
117 | if (this->lights[lightNumber]) |
---|
118 | { |
---|
119 | PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber); |
---|
120 | return this->addLight(); |
---|
121 | } |
---|
122 | this->init(lightNumber); |
---|
123 | this->currentLight = this->lights[lightNumber]; |
---|
124 | return lightNumber; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | \brief select the light to work with |
---|
129 | \param lightNumber the light to work with |
---|
130 | */ |
---|
131 | void Light::editLightNumber(int lightNumber) |
---|
132 | { |
---|
133 | if (!this->currentLight) |
---|
134 | { |
---|
135 | PRINTF(1)("no Light defined yet\n"); |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | this->currentLight = lights[lightNumber]; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | \brief Delete the current Light |
---|
144 | */ |
---|
145 | void Light::deleteLight(void) |
---|
146 | { |
---|
147 | if (!this->currentLight) |
---|
148 | { |
---|
149 | PRINTF(1)("no Light defined yet\n"); |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | this->deleteLight(this->currentLight->lightNumber); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | \brief delete light. |
---|
158 | \param lightNumber the number of the light to delete |
---|
159 | */ |
---|
160 | void Light::deleteLight(int lightNumber) |
---|
161 | { |
---|
162 | if (this->lights[lightNumber]) |
---|
163 | { |
---|
164 | PRINTF(3)("deleting Light number %d\n", lightNumber); |
---|
165 | delete this->lights[lightNumber]; |
---|
166 | glDisable(lightsV[lightNumber]); |
---|
167 | this->lights[lightNumber] = NULL; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | // set Attributes |
---|
172 | /** |
---|
173 | \brief Sets a Position for the Light. |
---|
174 | \param position The new position of the Light. |
---|
175 | \todo patrick: is it ok to set a Light Position even if it is derived from p_node?? |
---|
176 | */ |
---|
177 | void Light::setPosition(Vector position) |
---|
178 | { |
---|
179 | if (!this->currentLight) |
---|
180 | { |
---|
181 | PRINTF(1)("no Light defined yet\n"); |
---|
182 | return; |
---|
183 | } |
---|
184 | this->currentLight->lightPosition[0] = position.x; |
---|
185 | this->currentLight->lightPosition[1] = position.y; |
---|
186 | this->currentLight->lightPosition[2] = position.z; |
---|
187 | this->currentLight->lightPosition[3] = 0.0; |
---|
188 | |
---|
189 | glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | \brief Sets a Position for the Light. |
---|
194 | \param x the x-coordinate |
---|
195 | \param y the y-coordinate |
---|
196 | \param z the z-coordinate |
---|
197 | */ |
---|
198 | void Light::setPosition(GLfloat x, GLfloat y, GLfloat z) |
---|
199 | { |
---|
200 | if (!this->currentLight) |
---|
201 | { |
---|
202 | PRINTF(1)("no Light defined yet\n"); |
---|
203 | return; |
---|
204 | } |
---|
205 | |
---|
206 | this->currentLight->lightPosition[0] = x; |
---|
207 | this->currentLight->lightPosition[1] = y; |
---|
208 | this->currentLight->lightPosition[2] = z; |
---|
209 | this->currentLight->lightPosition[3] = 0.0; |
---|
210 | |
---|
211 | glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | \brief sets an emitting Diffuse color for the Light |
---|
216 | \param r red |
---|
217 | \param g green |
---|
218 | \param b blue |
---|
219 | */ |
---|
220 | void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b) |
---|
221 | { |
---|
222 | if (!this->currentLight) |
---|
223 | { |
---|
224 | PRINTF(1)("no Light defined yet\n"); |
---|
225 | return; |
---|
226 | } |
---|
227 | |
---|
228 | this->currentLight->diffuseColor[0] = r; |
---|
229 | this->currentLight->diffuseColor[1] = g; |
---|
230 | this->currentLight->diffuseColor[2] = b; |
---|
231 | this->currentLight->diffuseColor[3] = 1.0; |
---|
232 | |
---|
233 | glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor); |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | /** |
---|
238 | \brief sets an emitting Ambient color for the Light |
---|
239 | \param r red |
---|
240 | \param g green |
---|
241 | \param b blue |
---|
242 | */ |
---|
243 | void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b) |
---|
244 | { |
---|
245 | if (!this->currentLight) |
---|
246 | { |
---|
247 | PRINTF(1)("no Light defined yet\n"); |
---|
248 | return; |
---|
249 | } |
---|
250 | |
---|
251 | this->currentLight->specularColor[0] = r; |
---|
252 | this->currentLight->specularColor[1] = g; |
---|
253 | this->currentLight->specularColor[2] = b; |
---|
254 | this->currentLight->specularColor[3] = 1.0; |
---|
255 | |
---|
256 | glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor); |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | \brief Sets the AttenuationType of this Light Source |
---|
261 | \param type the AttenuationType to set |
---|
262 | \param factor the Factor to multipy the attenuation with |
---|
263 | |
---|
264 | this actually just sets the following: glLightf(currentLight, type, factor) |
---|
265 | */ |
---|
266 | void Light::setAttenuation(AttenuationType type, float factor) |
---|
267 | { |
---|
268 | if (!this->currentLight) |
---|
269 | { |
---|
270 | PRINTF(1)("no Light defined yet\n"); |
---|
271 | return; |
---|
272 | } |
---|
273 | this->currentLight->attenuationFactor = factor; |
---|
274 | this->currentLight->attenuationType = type; |
---|
275 | switch (type) |
---|
276 | { |
---|
277 | case CONSTANT: |
---|
278 | glLightf(lightsV[this->currentLight->lightNumber], GL_CONSTANT_ATTENUATION, factor); |
---|
279 | break; |
---|
280 | case LINEAR: |
---|
281 | glLightf(lightsV[this->currentLight->lightNumber], GL_LINEAR_ATTENUATION, factor); |
---|
282 | break; |
---|
283 | case QUADRATIC: |
---|
284 | glLightf(lightsV[this->currentLight->lightNumber], GL_QUADRATIC_ATTENUATION, factor); |
---|
285 | break; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | /** |
---|
291 | \brief sets the ambient Color of the Scene |
---|
292 | \param r red |
---|
293 | \param g green |
---|
294 | \param b blue |
---|
295 | */ |
---|
296 | void Light::setAmbientColor(GLfloat r, GLfloat g, GLfloat b) |
---|
297 | { |
---|
298 | this->ambientColor[0] = r; |
---|
299 | this->ambientColor[1] = g; |
---|
300 | this->ambientColor[2] = b; |
---|
301 | this->ambientColor[3] = 1.0; |
---|
302 | |
---|
303 | glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor); |
---|
304 | } |
---|
305 | |
---|
306 | // get Attributes |
---|
307 | |
---|
308 | /** |
---|
309 | \returns the Position of the Light |
---|
310 | */ |
---|
311 | Vector Light::getPosition(void) |
---|
312 | { |
---|
313 | if (!this->currentLight) |
---|
314 | { |
---|
315 | PRINTF(1)("no Light defined yet\n"); |
---|
316 | return Vector(.0, .0, .0); |
---|
317 | } |
---|
318 | else |
---|
319 | return getPosition(currentLight->lightNumber); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | /** |
---|
326 | \brief outputs debug information about the Class and its lights |
---|
327 | */ |
---|
328 | void Light::debug(void) |
---|
329 | { |
---|
330 | PRINT(0)("=================================\n"); |
---|
331 | PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n"); |
---|
332 | PRINT(0)("=================================\n"); |
---|
333 | PRINT(0)("Reference: %p\n", Light::singletonRef); |
---|
334 | if (this->currentLight) |
---|
335 | PRINT(0)("current Light Nr: %d\n", this->currentLight->lightNumber); |
---|
336 | PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]); |
---|
337 | PRINT(0)("=== Lights ===\n"); |
---|
338 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
339 | if (this->lights[i]) |
---|
340 | { |
---|
341 | PRINT(0)(":: %d :: -- reference %p\n", i, lights[i]); |
---|
342 | PRINT(0)(" GL-state: "); |
---|
343 | GLboolean param; |
---|
344 | glGetBooleanv(lightsV[i], ¶m); |
---|
345 | if (param) |
---|
346 | PRINT(0)("ON\n"); |
---|
347 | else |
---|
348 | PRINT(0)("OFF\n"); |
---|
349 | |
---|
350 | if (i != lights[i]->lightNumber) |
---|
351 | PRINTF(1)(" Lights are out of sync, this really should not happen,\n %d % should be equal.\n", i, lights[i]->lightNumber); |
---|
352 | PRINT(0)(" Position: %f/%f/%f\n", lights[i]->lightPosition[0], lights[i]->lightPosition[1], lights[i]->lightPosition[2]); |
---|
353 | PRINT(0)(" DiffuseColor: %f/%f/%f\n", lights[i]->diffuseColor[0], lights[i]->diffuseColor[1], lights[i]->diffuseColor[2]); |
---|
354 | PRINT(0)(" SpecularColor: %f/%f/%f\n", lights[i]->specularColor[0], lights[i]->specularColor[1], lights[i]->specularColor[2]); |
---|
355 | PRINT(0)(" Attenuation: "); |
---|
356 | switch (lights[i]->attenuationType) |
---|
357 | { |
---|
358 | case CONSTANT: |
---|
359 | PRINT(0)("constant"); |
---|
360 | case LINEAR: |
---|
361 | PRINT(0)("linear"); |
---|
362 | break; |
---|
363 | case QUADRATIC: |
---|
364 | PRINT(0)("quadratic"); |
---|
365 | break; |
---|
366 | } |
---|
367 | PRINT(0)(" with Factor %f\n", lights[i]->attenuationFactor); |
---|
368 | } |
---|
369 | PRINT(0)("--------------------------------\n"); |
---|
370 | } |
---|