Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new style for resources (prework/movement)

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