Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cleaned up LightManager, this was quite messy….

File size: 9.1 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#include "debug.h"
25#include "tinyxml.h"
26#include "load_param.h"
27#include "factory.h"
28
29using namespace std;
30
31CREATE_FACTORY(Light);
32
33//! Definition of the Lights
34int lightsV[] =
35{
36                  GL_LIGHT0,
37                  GL_LIGHT1,
38                  GL_LIGHT2,
39                  GL_LIGHT3,
40                  GL_LIGHT4,
41                  GL_LIGHT5,
42                  GL_LIGHT6,
43                  GL_LIGHT7
44};
45
46/**
47 * \param root The XML-element to load the Light from
48 */
49 Light::Light(const TiXmlElement* root)
50{
51  this->lightNumber = LightManager::getInstance()->registerLight(this);
52
53  this->setClassID(CL_LIGHT, "Light");
54  char tmpName[10];
55  sprintf(tmpName, "Light[%d]", this->lightNumber);
56  this->setName(tmpName);
57
58  PRINTF(4)("initializing Light number %d.\n", this->lightNumber);
59  // enable The light
60  glEnable(lightsV[this->lightNumber]); // postSpawn
61
62  // set values (defaults)
63  this->setDiffuseColor(1.0, 1.0, 1.0);
64  this->setSpecularColor(1.0, 1.0, 1.0);
65
66  this->loadParams(root);
67
68}
69
70/**
71   \brief destroys a Light
72*/
73Light::~Light(void)
74{
75  glDisable(lightsV[this->lightNumber]);
76
77  LightManager::getInstance()->unregisterLight(this);
78}
79
80/**
81 * \brief initializes a Light
82 */
83void Light::init()
84{
85}
86
87/**
88 * \param root The XML-element to load the Light from
89 */
90void Light::loadParams(const TiXmlElement* root)
91{
92  static_cast<PNode*>(this)->loadParams(root);
93
94  LoadParam<Light>(root, "diffuse-color", this, &Light::setDiffuseColor)
95      .describe("sets the diffuse color of the Light (red [0-1], green [0-1], blue [0-1])");
96
97  LoadParam<Light>(root, "specular-color", this, &Light::setSpecularColor)
98      .describe("sets the specular color of the Light (red [0-1], green [0-1], blue [0-1])");
99
100  LoadParam<Light>(root, "spot-direction", this, &Light::setSpotDirection)
101      .describe("sets the Direction of the Spot");
102
103  LoadParam<Light>(root, "spot-cutoff", this, &Light::setSpotCutoff)
104      .describe("the cuttoff of the Spotlight");
105}
106
107/**
108   \brief sets an emitting Diffuse color of this Light
109   \param r red
110   \param g green
111   \param b blue
112*/
113void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
114{
115  this->diffuseColor[0] = r;
116  this->diffuseColor[1] = g;
117  this->diffuseColor[2] = b;
118  this->diffuseColor[3] = 1.0;
119
120  glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor);
121}
122
123/**
124   \brief sets an emitting Specular color of this Light
125   \param r red
126   \param g green
127   \param b blue
128*/
129void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
130{
131  this->specularColor[0] = r;
132  this->specularColor[1] = g;
133  this->specularColor[2] = b;
134  this->specularColor[3] = 1.0;
135
136  glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor);
137}
138
139
140/**
141   \brief Sets the AttenuationType of this Light Source
142   \param constantAttenuation The Constant Attenuation of the Light
143   \param linearAttenuation The Linear Attenuation of the Light
144   \param quadraticAttenuation The Quadratic Attenuation of the Light
145*/
146void Light::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
147{
148  this->constantAttenuation  = constantAttenuation;
149  this->linearAttenuation    = linearAttenuation;
150  this->quadraticAttenuation = quadraticAttenuation;
151
152  glLightf(lightsV[this->lightNumber], GL_CONSTANT_ATTENUATION,  constantAttenuation);
153  glLightf(lightsV[this->lightNumber], GL_LINEAR_ATTENUATION,    linearAttenuation);
154  glLightf(lightsV[this->lightNumber], GL_QUADRATIC_ATTENUATION, quadraticAttenuation);
155}
156
157
158/**
159   \brief stets the direction of the Spot Light.
160   \param direction The direction of the Spot Light.
161*/
162void Light::setSpotDirection(const Vector& direction)
163{
164  this->spotDirection[0] = direction.x;
165  this->spotDirection[1] = direction.y;
166  this->spotDirection[2] = direction.z;
167
168  glLightfv(lightsV[this->lightNumber], GL_SPOT_DIRECTION, this->spotDirection);
169}
170
171
172/**
173   \brief sets the cutoff angle of the Light.
174   \param cutoff The cutoff angle.
175*/
176void Light::setSpotCutoff(GLfloat cutoff)
177{
178  this->spotCutoff = cutoff;
179  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
180}
181
182/**
183   \brief draws this Light. Being a World-entity the possibility to do this lies at hand.
184*/
185void Light::draw() const
186{
187  float pos[4] = {this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z, 1.0};
188  PRINTF(4)("Drawing The Lights new Position at %f %f %f\n", pos[0], pos[1], pos[2]);
189  glLightfv(lightsV[this->lightNumber], GL_POSITION, pos);
190}
191
192
193/**
194   \brief Prints out some nice formated debug information about the Light
195*/
196void Light::debug(void) const
197{
198  PRINT(0)(":: %d ::  -- reference %p\n", this->lightNumber, this);
199  PRINT(0)(" GL-state: ");
200  GLboolean param;
201  glGetBooleanv(lightsV[this->lightNumber], &param);
202  if (param)
203    PRINT(0)("ON\n");
204  else
205    PRINT(0)("OFF\n");
206
207  PRINT(0)(" DiffuseColor:  %f/%f/%f\n", this->diffuseColor[0], this->diffuseColor[1], this->diffuseColor[2]);
208  PRINT(0)(" SpecularColor: %f/%f/%f\n", this->specularColor[0], this->specularColor[1], this->specularColor[2]);
209  PRINT(0)(" Attenuation: constant=%f linear=%f quadratic=%f\n", this->constantAttenuation, this->linearAttenuation, this->quadraticAttenuation);
210}
211
212
213/******************
214** LIGHT-MANAGER **
215******************/
216/**
217   \brief standard constructor for a Light
218*/
219LightManager::LightManager ()
220{
221  this->setClassID(CL_LIGHT_MANAGER, "LightManager");
222
223  glEnable (GL_LIGHTING);
224  this->setAmbientColor(.3, .3, .3);
225  this->lights = new Light*[NUMBEROFLIGHTS];
226  for (int i = 0; i < NUMBEROFLIGHTS; i++)
227    lights[i] = NULL;
228  this->currentLight = NULL;
229}
230
231/**
232   \brief standard deconstructor
233
234   first disables Lighting
235
236   then deletes the rest of the allocated memory
237   and in the end sets the singleton Reference to zero.
238*/
239LightManager::~LightManager ()
240{
241  glDisable(GL_LIGHTING);
242
243  // this will be done either by PNode-tree as each light is one of them
244  //  for (int i = 0; i < NUMBEROFLIGHTS; i++)
245  //    this->deleteLight(i);
246  delete lights;
247  LightManager::singletonRef = NULL;
248}
249
250/**
251   \brief singleton-Reference to the Light-class
252*/
253LightManager* LightManager::singletonRef = NULL;
254
255/**
256  \param root the XML-element to load the LightManager's settings from
257 */
258void LightManager::loadParams(const TiXmlElement* root)
259{
260  LoadParam<LightManager>(root, "Lights", this, &LightManager::loadLights)
261      .describe("an XML-Element to load lights from.");
262
263  LoadParam<LightManager>(root, "ambient-color", this, &LightManager::setAmbientColor)
264      .describe("sets the ambient Color of the Environmental Light");
265}
266
267/**
268  \param root The XML-element to load Lights from
269 */
270void LightManager::loadLights(const TiXmlElement* root)
271{
272  const TiXmlElement* element = root->FirstChildElement();
273
274  while (element != NULL)
275  {
276    Factory::getFirst()->fabricate(element);
277
278    element = element->NextSiblingElement();
279  }
280}
281
282// set Attributes
283/**
284   \brief sets the ambient Color of the Scene
285   \param r red
286   \param g green
287   \param b blue
288*/
289void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
290{
291  this->ambientColor[0] = r;
292  this->ambientColor[1] = g;
293  this->ambientColor[2] = b;
294  this->ambientColor[3] = 1.0;
295
296  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
297}
298
299int LightManager::registerLight(Light* light)
300{
301  for (int i = 0; i < NUMBEROFLIGHTS; i++)
302    if (!this->lights[i])
303  {
304    this->lights[i]=light;
305    return i;
306  }
307  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
308  return -1;
309}
310
311void LightManager::unregisterLight(Light* light)
312{
313  for (int i = 0; i < NUMBEROFLIGHTS; i++)
314  {
315    if (this->lights[i] == light)
316    {
317      this->lights[i] == NULL;
318    }
319  }
320  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
321  return;
322}
323
324/**
325   \brief draws all the Lights in their appropriate position
326 */
327void LightManager::draw() const
328{
329  glMatrixMode(GL_MODELVIEW);
330  glLoadIdentity();
331  PRINTF(4)("Drawing the Lights\n");
332  for (int i = 0; i < NUMBEROFLIGHTS; i++)
333    if (this->lights[i])
334      lights[i]->draw();
335}
336
337/**
338   \brief outputs debug information about the Class and its lights
339*/
340void LightManager::debug(void) const
341{
342  PRINT(0)("=================================\n");
343  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
344  PRINT(0)("=================================\n");
345  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
346  if (this->currentLight)
347    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
348  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
349  PRINT(0)("=== Lights ===\n");
350  for (int i = 0; i < NUMBEROFLIGHTS; i++)
351    if (this->lights[i])
352      {
353        this->lights[i]->debug();
354      }
355  PRINT(0)("--------------------------------\n");
356}
Note: See TracBrowser for help on using the repository browser.