Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: nice doxygen-information about the LightManager

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