Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/light.cc @ 3442

Last change on this file since 3442 was 3442, checked in by bensch, 19 years ago

orxonox/trunk: light: added debug-method.
There is something strange about adding multiple lights. I have to read about this some more.

File size: 8.7 KB
Line 
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
23using namespace std;
24
25//! Definition of the Lights
26int 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*/
32Light::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   \todo this deconstructor is not jet implemented - do it
47
48*/
49Light::~Light () 
50{
51  glDisable(GL_LIGHTING);
52 
53  for (int i = 0; i < NUMBEROFLIGHTS; i++)
54    this->deleteLight(i);
55  delete lights;
56  Light::singletonRef = NULL;
57}
58
59/**
60   \brief singleton-Reference to the Light-class
61*/
62Light* Light::singletonRef = NULL;
63
64/**
65   \returns The Instance of the Lights
66*/
67Light* Light::getInstance(void)
68{
69  if (singletonRef)
70    return singletonRef;
71  else
72    return Light::singletonRef = new Light();
73}
74
75/**
76   \brief initializes a new Light with default values, and enables GL_LIGHTING
77*/
78void Light::init(int lightNumber)
79{
80  PRINTF(3)("initializing Light number %d.\n", lightNumber);
81  // enable The light
82  glEnable(lightsV[lightNumber]);
83  this->currentLight = lights[lightNumber] = new LightValue;
84 
85  // set default values
86  this->currentLight->lightNumber = lightNumber;
87  this->setPosition(Vector(0.0, 0.0, 0.0));
88  this->setDiffuseColor(1.0, 1.0, 1.0);
89  this->setSpecularColor(1.0, 1.0, 1.0);
90  //  lmodelAmbient[] = {.1, .1, .1, 1.0};
91}
92
93/**
94   \brief Adds a new Light, by selecting the First free slot.
95
96   if no slot is free error
97*/
98int Light::addLight(void)
99{
100  for (int i = 0; i < NUMBEROFLIGHTS; i++)
101    if (!this->lights[i])
102      return addLight(i); 
103  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
104  return -1;
105}
106
107/**
108   \brief Adds a new Light
109   \param lightNumber The number of the light to add.
110
111   if the Number is not free: warn, automatically choose another slot.
112*/
113int Light::addLight(int lightNumber)
114{
115  if (this->lights[lightNumber])
116    {
117      PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
118      return this->addLight();
119    }
120  this->init(lightNumber);
121  this->currentLight = this->lights[lightNumber];
122  return lightNumber;
123}
124
125/**
126   \brief select the light to work with
127   \param lightNumber the light to work with
128*/
129void Light::editLightNumber(int lightNumber)
130{
131  if (!this->currentLight)
132    { 
133      PRINTF(1)("no Light defined yet\n");
134      return;
135    }
136
137  this->currentLight = lights[lightNumber];
138}
139
140/**
141   \brief Delete the current Light
142*/
143void Light::deleteLight(void)
144{
145  if (!this->currentLight)
146    { 
147      PRINTF(1)("no Light defined yet\n");
148      return;
149    }
150
151  this->deleteLight(this->currentLight->lightNumber);
152}
153
154/**
155   \brief delete light.
156   \param lightnumber the number of the light to delete
157*/
158void Light::deleteLight(int lightNumber)
159{
160  if (this->lights[lightNumber])
161    {
162      PRINTF(3)("deleting Light number %d\n", lightNumber);
163      delete this->lights[lightNumber];
164      glDisable(lightsV[lightNumber]);
165      this->lights[lightNumber] = NULL;
166    }
167}
168
169// set Attributes
170/**
171   \brief Sets a Position for the Light.
172   \param position The new position of the Light.
173   \todo patrick: is it ok to set a Light Position even if it is derived from p_node??
174*/
175void Light::setPosition(Vector position)
176{
177  if (!this->currentLight)
178    { 
179      PRINTF(1)("no Light defined yet\n");
180      return;
181    }
182  this->currentLight->lightPosition[0] = position.x;
183  this->currentLight->lightPosition[1] = position.y;
184  this->currentLight->lightPosition[2] = position.z;
185  this->currentLight->lightPosition[3] = 0.0;
186
187  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
188}
189
190void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
191{
192  if (!this->currentLight)
193    { 
194      PRINTF(1)("no Light defined yet\n");
195      return;
196    }
197
198  this->currentLight->lightPosition[0] = x;
199  this->currentLight->lightPosition[1] = y;
200  this->currentLight->lightPosition[2] = z;
201  this->currentLight->lightPosition[3] = 0.0;
202
203  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
204}
205
206/**
207   \brief sets an emitting Diffuse color for the Light
208   \param r red
209   \param g green
210   \param b blue
211*/
212void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
213{
214  if (!this->currentLight)
215    { 
216      PRINTF(1)("no Light defined yet\n");
217      return;
218    }
219
220  this->currentLight->diffuseColor[0] = r;
221  this->currentLight->diffuseColor[1] = g;
222  this->currentLight->diffuseColor[2] = b;
223  this->currentLight->diffuseColor[3] = 1.0;
224
225  glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor);
226}
227
228
229/**
230   \brief sets an emitting Ambient color for the Light
231   \param r red
232   \param g green
233   \param b blue
234*/
235void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
236{
237  if (!this->currentLight)
238    { 
239      PRINTF(1)("no Light defined yet\n");
240      return;
241    }
242
243  this->currentLight->specularColor[0] = r;
244  this->currentLight->specularColor[1] = g;
245  this->currentLight->specularColor[2] = b;
246  this->currentLight->specularColor[3] = 1.0;
247
248  glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor);
249}
250
251/**
252   \brief Sets the AttenuationType of this Light Source
253   \param type the AttenuationType to set
254   \param factor the Factor to multipy the attenuation with
255
256   this actually just sets the following: glLightf(currentLight, type, factor)
257*/
258void Light::setAttenuation(AttenuationType type, float factor)
259{
260  if (!this->currentLight)
261    { 
262      PRINTF(1)("no Light defined yet\n");
263      return;
264    }
265  this->currentLight->attenuationFactor = factor;
266  this->currentLight->attenuationType = type;
267  switch (type)
268    {
269    case CONSTANT:
270      glLightf(lightsV[this->currentLight->lightNumber], GL_CONSTANT_ATTENUATION, factor);
271      break;
272    case LINEAR:
273      glLightf(lightsV[this->currentLight->lightNumber], GL_LINEAR_ATTENUATION, factor);
274      break;
275    case QUADRATIC:
276      glLightf(lightsV[this->currentLight->lightNumber], GL_QUADRATIC_ATTENUATION, factor);
277      break;
278    }
279}
280
281
282/**
283   \brief sets the ambient Color of the Scene
284   \param r red
285   \param g green
286   \param b blue
287*/
288void Light::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
289{
290  this->ambientColor[0] = r;
291  this->ambientColor[1] = g;
292  this->ambientColor[2] = b;
293  this->ambientColor[3] = 1.0;
294
295  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
296}
297
298// get Attributes
299
300/**
301   \returns the Position of the Light
302*/
303Vector Light::getPosition(void)
304{
305  if (!this->currentLight)
306    { 
307      PRINTF(1)("no Light defined yet\n");
308      return Vector(.0, .0, .0);
309    }
310  else
311    return Vector(this->currentLight->lightPosition[0], this->currentLight->lightPosition[1], this->currentLight->lightPosition[2]);
312}
313
314
315/**
316   \brief outputs debug information about the Class and its lights
317*/
318void Light::debug(void)
319{
320  PRINT(0)("=================================\n");
321  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
322  PRINT(0)("=================================\n");
323  PRINT(0)("Reference: %p\n", Light::singletonRef);
324  if (this->currentLight)
325    PRINT(0)("current Light Nr: %d\n", this->currentLight->lightNumber);
326  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
327  PRINT(0)("=== Lights ===\n");
328  for (int i = 0; i < NUMBEROFLIGHTS; i++)
329    if (this->lights[i])
330      {
331        PRINT(0)(":: %d ::  -- reference %p\n", i, lights[i]);
332        if (i != lights[i]->lightNumber)
333          PRINTF(1)(" Lights are out of sync, this really should not happen,\n   %d % should be equal.\n", i, lights[i]->lightNumber);
334        PRINT(0)(" Position:      %f/%f/%f\n", lights[i]->lightPosition[0], lights[i]->lightPosition[1], lights[i]->lightPosition[2]);
335        PRINT(0)(" DiffuseColor:  %f/%f/%f\n", lights[i]->diffuseColor[0], lights[i]->diffuseColor[1], lights[i]->diffuseColor[2]);
336        PRINT(0)(" SpecularColor: %f/%f/%f\n", lights[i]->specularColor[0], lights[i]->specularColor[1], lights[i]->specularColor[2]);
337        PRINT(0)(" Attenuation:   ");
338        switch (lights[i]->attenuationType)
339          {
340          case CONSTANT:
341            PRINT(0)("constant");
342          case LINEAR:
343            PRINT(0)("linear");
344            break;
345          case QUADRATIC:
346            PRINT(0)("quadratic");
347            break;
348          }
349        PRINT(0)(" with Factor %f\n", lights[i]->attenuationFactor);
350      }
351  PRINT(0)("--------------------------------\n");
352}
Note: See TracBrowser for help on using the repository browser.