Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/light.cc @ 4320

Last change on this file since 4320 was 4320, checked in by patrick, 19 years ago

orxonox/trunk: now string name of a class works parallel to the int id representation and is only used for documentation purposes

File size: 13.3 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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LIGHT
19
20#include "light.h"
21
22#include "glincl.h"
23#include "vector.h"
24
25using namespace std;
26
27//! Definition of the Lights
28int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
29
30
31/**
32   \param lightNumber the Light Number to initiate
33*/
34Light::Light(int lightNumber)
35{
36  this->setClassID(CL_LIGHT, "Light");
37  char tmpName[7];
38  sprintf(tmpName, "Light%d", lightNumber);
39  this->setName(tmpName);
40
41  PRINTF(4)("initializing Light number %d.\n", lightNumber);
42  // enable The light
43  glEnable(lightsV[lightNumber]); // postSpawn
44 
45  // set values (defaults)
46  this->lightNumber = lightNumber;
47  this->setPosition(0.0, 0.0, 0.0);
48  this->setDiffuseColor(1.0, 1.0, 1.0);
49  this->setSpecularColor(1.0, 1.0, 1.0);
50}
51
52/**
53   \brief destroys a Light
54*/
55Light::~Light(void)
56{
57  glDisable(lightsV[this->lightNumber]);
58}
59
60/**
61   \brief Sets a Position for the Light.
62   \param position The new position of the Light.
63   \todo patrick: is it ok to set a Light Position even if it is derived from p_node??
64*/
65void Light::setPosition(Vector position)
66{
67  this->lightPosition[0] = position.x;
68  this->lightPosition[1] = position.y;
69  this->lightPosition[2] = position.z;
70  this->lightPosition[3] = 0.0;
71
72  this->setAbsCoor(position);
73
74  glLightfv (lightsV[this->lightNumber], GL_POSITION, this->lightPosition);
75}
76
77/**
78   \brief Sets a Position of this Light.
79   \param x the x-coordinate
80   \param y the y-coordinate
81   \param z the z-coordinate
82*/
83void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
84{
85  this->setPosition(Vector(x, y, z));
86}
87
88/**
89   \brief sets an emitting Diffuse color of this Light
90   \param r red
91   \param g green
92   \param b blue
93*/
94void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
95{
96  this->diffuseColor[0] = r;
97  this->diffuseColor[1] = g;
98  this->diffuseColor[2] = b;
99  this->diffuseColor[3] = 1.0;
100
101  glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor);
102}
103
104/**
105   \brief sets an emitting Specular color of this Light
106   \param r red
107   \param g green
108   \param b blue
109*/
110void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
111{
112  this->specularColor[0] = r;
113  this->specularColor[1] = g;
114  this->specularColor[2] = b;
115  this->specularColor[3] = 1.0;
116
117  glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor);
118}
119
120/**
121   \brief Sets the AttenuationType of this Light Source
122   \param constantAttenuation The Constant Attenuation of the Light
123   \param linearAttenuation The Linear Attenuation of the Light
124   \param quadraticAttenuation The Quadratic Attenuation of the Light
125*/
126void Light::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
127{
128  this->constantAttenuation  = constantAttenuation;
129  this->linearAttenuation    = linearAttenuation;
130  this->quadraticAttenuation = quadraticAttenuation;
131
132  glLightf(lightsV[this->lightNumber], GL_CONSTANT_ATTENUATION,  constantAttenuation);
133  glLightf(lightsV[this->lightNumber], GL_LINEAR_ATTENUATION,    linearAttenuation);
134  glLightf(lightsV[this->lightNumber], GL_QUADRATIC_ATTENUATION, quadraticAttenuation);
135}
136
137/**
138   \brief stets the direction of the Spot Light.
139   \param direction The direction of the Spot Light.
140*/
141void Light::setSpotDirection(Vector direction)
142{
143  this->spotDirection[0] = direction.x;
144  this->spotDirection[1] = direction.y;
145  this->spotDirection[2] = direction.z;
146
147  glLightfv(lightsV[this->lightNumber], GL_SPOT_DIRECTION, this->spotDirection);
148}
149
150/**
151   \brief sets the cutoff angle of the Light.
152   \param cutoff The cutoff angle.
153*/
154void Light::setSpotCutoff(GLfloat cutoff)
155{
156  this->spotCutoff = cutoff;
157  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
158}
159
160/**
161   \returns the Position of the Light
162*/
163Vector Light::getPosition() const
164{
165  return Vector(this->lightPosition[0], this->lightPosition[1], this->lightPosition[2]);
166}
167
168/**
169   \brief draws this Light. Being a World-entity the possibility to do this lies at hand.
170*/ 
171void Light::draw()
172{
173  float pos[4] = {this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z, 1.0};
174  PRINTF(4)("Drawing The Lights new Position at %f %f %f\n", pos[0], pos[1], pos[2]);
175  glLightfv(lightsV[this->lightNumber], GL_POSITION, pos);
176}
177
178/**
179   \brief Prints out some nice formated debug information about the Light
180*/
181void Light::debug(void) const
182{
183  PRINT(0)(":: %d ::  -- reference %p\n", this->lightNumber, this);
184  PRINT(0)(" GL-state: ");
185  GLboolean param; 
186  glGetBooleanv(lightsV[this->lightNumber], &param);
187  if (param)
188    PRINT(0)("ON\n");
189  else
190    PRINT(0)("OFF\n");
191 
192  PRINT(0)(" Position:      %f/%f/%f\n", this->lightPosition[0], this->lightPosition[1], this->lightPosition[2]);
193  PRINT(0)(" DiffuseColor:  %f/%f/%f\n", this->diffuseColor[0], this->diffuseColor[1], this->diffuseColor[2]);
194  PRINT(0)(" SpecularColor: %f/%f/%f\n", this->specularColor[0], this->specularColor[1], this->specularColor[2]);
195  PRINT(0)(" Attenuation: constant=%f linear=%f quadratic=%f\n", this->constantAttenuation, this->linearAttenuation, this->quadraticAttenuation);
196}
197
198
199/******************
200** LIGHT-MANAGER **
201******************/
202/**
203   \brief standard constructor for a Light
204*/
205LightManager::LightManager () 
206{
207  this->setClassID(CL_LIGHT_MANAGER, "LightManager");
208
209  glEnable (GL_LIGHTING);
210  this->setAmbientColor(.3, .3, .3);
211  this->lights = new Light*[NUMBEROFLIGHTS];
212  for (int i = 0; i < NUMBEROFLIGHTS; i++)
213    lights[i] = NULL;
214  this->currentLight = NULL;
215}
216
217/**
218   \brief standard deconstructor
219   
220   first disables Lighting
221
222   then deletes the rest of the allocated memory
223   and in the end sets the singleton Reference to zero.
224*/
225LightManager::~LightManager () 
226{
227  glDisable(GL_LIGHTING);
228 
229  // this will be done either by worldEntity, or by pNode as each light is one of them
230  //  for (int i = 0; i < NUMBEROFLIGHTS; i++)
231  //    this->deleteLight(i);
232  delete lights;
233  LightManager::singletonRef = NULL;
234}
235
236/**
237   \brief singleton-Reference to the Light-class
238*/
239LightManager* LightManager::singletonRef = NULL;
240
241/**
242   \returns The Instance of the Lights
243*/
244LightManager* LightManager::getInstance(void)
245{
246  if (!singletonRef)
247    LightManager::singletonRef = new LightManager();
248  return singletonRef;
249}
250
251/**
252   \brief initializes a new Light with default values, and enables GL_LIGHTING
253*/
254void LightManager::initLight(int lightNumber)
255{
256  lights[lightNumber] = new Light(lightNumber);
257}
258
259/**
260   \brief Adds a new Light, by selecting the First free slot.
261
262   if no slot is free error
263*/
264int LightManager::addLight(void)
265{
266  for (int i = 0; i < NUMBEROFLIGHTS; i++)
267    if (!this->lights[i])
268      return addLight(i); 
269  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
270  return -1;
271}
272
273/**
274   \brief Adds a new Light
275   \param lightNumber The number of the light to add.
276
277   if the Number is not free: warn, automatically choose another slot.
278*/
279int LightManager::addLight(int lightNumber)
280{
281  if (this->lights[lightNumber])
282    {
283      PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
284      return this->addLight();
285    }
286  this->initLight(lightNumber);
287  this->currentLight = this->lights[lightNumber];
288  return lightNumber;
289}
290
291/**
292   \brief select the light to work with
293   \param lightNumber the light to work with
294*/
295void LightManager::editLightNumber(int lightNumber)
296{
297  if (!this->currentLight)
298    { 
299      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
300      return;
301    }
302  this->currentLight = lights[lightNumber];
303}
304
305/**
306   \brief Delete the current Light
307*/
308void LightManager::deleteLight(void)
309{
310  if (!this->currentLight)
311    { 
312      PRINTF(1)("no Light defined yet. So you cannot delete any Light right now.\n");
313      return;
314    }
315
316  this->deleteLight(this->currentLight->getLightNumber());
317}
318
319/**
320   \brief delete light.
321   \param lightNumber the number of the light to delete
322*/
323void LightManager::deleteLight(int lightNumber)
324{
325  if (this->lights[lightNumber])
326    {
327      PRINTF(4)("deleting Light number %d\n", lightNumber);
328      delete this->lights[lightNumber];
329      this->lights[lightNumber] = NULL;
330    }
331}
332
333/**
334   \brief draws all the Lights in their appropriate position
335*/
336void LightManager::draw()
337{
338  glMatrixMode(GL_MODELVIEW);
339  glLoadIdentity();
340  PRINTF(4)("Drawing the Lights\n");
341  for (int i = 0; i < NUMBEROFLIGHTS; i++)
342    if (this->lights[i])
343      lights[i]->draw();
344}
345
346
347
348// set Attributes
349/**
350   \brief sets the ambient Color of the Scene
351   \param r red
352   \param g green
353   \param b blue
354*/
355void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
356{
357  this->ambientColor[0] = r;
358  this->ambientColor[1] = g;
359  this->ambientColor[2] = b;
360  this->ambientColor[3] = 1.0;
361
362  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
363}
364
365/**
366   \brief sets The Position of the currently selected Light
367   \param position the new Position
368*/
369void LightManager::setPosition(Vector position)
370{
371  this->currentLight->setPosition(position);
372}
373
374/**
375   \brief Sets a Position for the currently selected Light.
376   \param x the x-coordinate
377   \param y the y-coordinate
378   \param z the z-coordinate
379*/
380void LightManager::setPosition(GLfloat x, GLfloat y, GLfloat z)
381{
382  if (!this->currentLight)
383    { 
384      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
385      return;
386    }
387  this->currentLight->setPosition(x, y, z);
388}
389
390/**
391   \brief sets an emitting Diffuse color of the currently selected Light
392   \param r red
393   \param g green
394   \param b blue
395*/
396void LightManager::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
397{
398  if (!this->currentLight)
399    { 
400      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
401      return;
402    }
403  this->currentLight->setDiffuseColor(r,g,b);
404}
405
406/**
407   \brief sets an emitting Specular color of the currently selected Light
408   \param r red
409   \param g green
410   \param b blue
411*/
412void LightManager::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
413{
414  if (!this->currentLight)
415    { 
416      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
417      return;
418    }
419  this->currentLight->setSpecularColor(r, g, b);
420}
421
422/**
423   \brief Sets the AttenuationType of th currently selecte LightSource Light Source
424   \param constantAttenuation The Constant Attenuation of the Light
425   \param linearAttenuation The Linear Attenuation of the Light
426   \param quadraticAttenuation The Quadratic Attenuation of the Light
427*/
428void LightManager::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
429{
430  if (!this->currentLight)
431    { 
432      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
433      return;
434    }
435  this->currentLight->setAttenuation(constantAttenuation, linearAttenuation, quadraticAttenuation);
436}
437
438
439/**
440   \brief stets the direction of the Spot Light.
441   \param direction The direction of the Spot Light.
442*/
443void LightManager::setSpotDirection(Vector direction)
444{
445  if (!this->currentLight)
446    { 
447      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
448      return;
449    }
450  this->currentLight->setSpotDirection(direction);
451}
452
453/**
454   \brief sets the cutoff angle of the Light.
455   \param cutoff The cutoff angle.
456*/
457void LightManager::setSpotCutoff(GLfloat cutoff)
458{
459  if (!this->currentLight)
460    { 
461      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
462      return;
463    }
464  this->currentLight->setSpotCutoff(cutoff);
465}
466
467// get Attributes
468/**
469   \returns the Position of the Light
470*/
471Vector LightManager::getPosition(void) const
472{
473  if (!this->currentLight)
474    { 
475      PRINTF(2)("no Light defined yet\n");
476      return Vector(.0, .0, .0);
477    }
478  else
479    return this->currentLight->getPosition();
480}
481
482/**
483   \returns the Position of Light
484   \param lightNumber lightnumber
485*/
486Vector LightManager::getPosition(int lightNumber) const
487{
488  if (!this->lights[lightNumber])
489    {
490      PRINTF(2)("no Light defined yet\n");
491      return Vector(.0,.0,.0);
492    }
493  else
494    return this->lights[lightNumber]->getPosition();
495}
496
497/**
498   \returns a pointer to a Light
499   \param lightNumber The light to request the pointer from
500*/
501Light* LightManager::getLight(int lightNumber) const
502{
503  return this->lights[lightNumber];
504}
505
506/**
507   \brief outputs debug information about the Class and its lights
508*/
509void LightManager::debug(void) const
510{
511  PRINT(0)("=================================\n");
512  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
513  PRINT(0)("=================================\n");
514  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
515  if (this->currentLight)
516    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
517  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
518  PRINT(0)("=== Lights ===\n");
519  for (int i = 0; i < NUMBEROFLIGHTS; i++)
520    if (this->lights[i])
521      {
522        this->lights[i]->debug();
523      }
524  PRINT(0)("--------------------------------\n");
525}
Note: See TracBrowser for help on using the repository browser.