Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability.old/src/lib/graphics/light.cc @ 10610

Last change on this file since 10610 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 9.6 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 "util/loading/load_param_xml.h"
25#include "util/loading/factory.h"
26#include "debug.h"
27
28#include "class_id_DEPRECATED.h"
29
30ObjectListDefinitionID(Light, CL_LIGHT);
31CREATE_FACTORY(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 */
51Light::Light(const TiXmlElement* root)
52{
53  PRINTF(4)("initializing Light number %d.\n", this->lightNumber);
54
55  this->registerObject(this, Light::_objectList);
56
57  this->lightNumber = LightManager::getInstance()->registerLight(this);
58
59  char tmpName[10];
60  sprintf(tmpName, "Light[%d]", this->lightNumber);
61  this->setName(tmpName);
62
63  // enable The light
64  glEnable(lightsV[this->lightNumber]); // postSpawn
65
66  // set values (defaults)
67  this->setDiffuseColor(1.0, 1.0, 1.0);
68  this->setSpecularColor(1.0, 1.0, 1.0);
69
70  if (root != NULL)
71    this->loadParams(root);
72}
73
74/**
75 *  destroys a Light
76*/
77Light::~Light()
78{
79  glDisable(lightsV[this->lightNumber]);
80
81  LightManager::getInstance()->unregisterLight(this);
82}
83
84/**
85 * @param root The XML-element to load the Light from
86 */
87void Light::loadParams(const TiXmlElement* root)
88{
89  PNode::loadParams(root);
90
91  LoadParam(root, "diffuse-color", this, Light, setDiffuseColor)
92  .describe("sets the diffuse color of the Light (red [0-1], green [0-1], blue [0-1])");
93
94  LoadParam(root, "specular-color", this, Light, setSpecularColor)
95  .describe("sets the specular color of the Light (red [0-1], green [0-1], blue [0-1])");
96
97  LoadParam(root, "attenuation", this, Light, setAttenuation)
98  .describe("sets the Attenuation of the LightSource (constant Factor, linear Factor, quadratic Factor).");
99
100  LoadParam(root, "spot-direction", this, Light, setSpotDirection)
101  .describe("sets the Direction of the Spot");
102
103  LoadParam(root, "spot-cutoff", this, Light, setSpotCutoff)
104  .describe("the cuttoff of the Spotlight");
105}
106
107/**
108 *  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 *  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 *  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 *  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 *  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 *  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 *  Prints out some nice formated debug information about the Light
195*/
196void Light::debug() 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******************/
216ObjectListDefinition(LightManager);
217/**
218 *  standard constructor for a Light
219*/
220LightManager::LightManager ()
221{
222  this->registerObject(this, LightManager::_objectList);
223
224  glEnable (GL_LIGHTING);
225  glEnable ( GL_COLOR_MATERIAL ) ;
226  glColorMaterial ( GL_FRONT, GL_DIFFUSE ) ;
227
228  this->setAmbientColor(.3, .3, .3);
229  this->lights = new Light*[NUMBEROFLIGHTS];
230  for (int i = 0; i < NUMBEROFLIGHTS; i++)
231    lights[i] = NULL;
232  this->currentLight = NULL;
233}
234
235/**
236 *  standard deconstructor
237
238   first disables Lighting
239
240   then deletes the rest of the allocated memory
241   and in the end sets the singleton Reference to zero.
242*/
243LightManager::~LightManager ()
244{
245  glDisable(GL_COLOR_MATERIAL);
246  glDisable(GL_LIGHTING);
247  this->setAmbientColor(.0,.0,.0);
248
249  for (int i = 0; i < NUMBEROFLIGHTS; i++)
250    if (this->lights[i] != NULL)
251      delete lights[i];
252  delete[] lights;
253  LightManager::singletonRef = NULL;
254}
255
256/**
257 *  singleton-Reference to the Light-class
258*/
259LightManager* LightManager::singletonRef = NULL;
260
261/**
262* @param root the XML-element to load the LightManager's settings from
263 */
264void LightManager::loadParams(const TiXmlElement* root)
265{
266  LoadParamXML(root, "Lights", this, LightManager, loadLights)
267  .describe("an XML-Element to load lights from.");
268
269  LoadParam(root, "ambient-color", this, LightManager, setAmbientColor)
270  .describe("sets the ambient Color of the Environmental Light");
271}
272
273/**
274* @param root The XML-element to load Lights from
275 */
276void LightManager::loadLights(const TiXmlElement* root)
277{
278  const TiXmlElement* element = root->FirstChildElement();
279
280  while (element != NULL)
281  {
282    Factory::fabricate(element);
283
284    element = element->NextSiblingElement();
285  }
286}
287
288// set Attributes
289/**
290 *  sets the ambient Color of the Scene
291 * @param r red
292 * @param g green
293 * @param b blue
294*/
295void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
296{
297  this->ambientColor[0] = r;
298  this->ambientColor[1] = g;
299  this->ambientColor[2] = b;
300  this->ambientColor[3] = 1.0;
301
302  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
303}
304
305/**
306* @param light the Light to register to the LightManager
307
308  This is done explicitely by the constructor of a Light
309*/
310int LightManager::registerLight(Light* light)
311{
312  for (int i = 0; i < NUMBEROFLIGHTS; i++)
313    if (!this->lights[i])
314    {
315      this->lights[i]=light;
316      return i;
317    }
318  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
319  return -1;
320}
321
322/**
323* @param light The light to unregister from the LightManager
324
325  This is done every time a Light is destroyed explicitely by the Light-destructor
326 */
327void LightManager::unregisterLight(Light* light)
328{
329  for (int i = 0; i < NUMBEROFLIGHTS; i++)
330  {
331    if (this->lights[i] == light)
332    {
333      this->lights[i] = NULL;
334      return;
335    }
336  }
337  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
338  return;
339}
340
341
342Light* LightManager::getLight(int lightNumber) const
343{
344  if( lightNumber < NUMBEROFLIGHTS)
345    return this->lights[lightNumber];
346
347  return NULL;
348}
349
350/**
351 *  draws all the Lights in their appropriate position
352 */
353void LightManager::draw() const
354{
355  PRINTF(4)("Drawing the Lights\n");
356  for (int i = 0; i < NUMBEROFLIGHTS; i++)
357    if (this->lights[i] != NULL)
358      lights[i]->draw();
359}
360
361/**
362 *  outputs debug information about the Class and its lights
363*/
364void LightManager::debug() const
365{
366  PRINT(0)("=================================\n");
367  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
368  PRINT(0)("=================================\n");
369  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
370  if (this->currentLight)
371    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
372  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
373  PRINT(0)("=== Lights ===\n");
374  for (int i = 0; i < NUMBEROFLIGHTS; i++)
375    if (this->lights[i])
376    {
377      this->lights[i]->debug();
378    }
379  PRINT(0)("-----------------------------LM-\n");
380}
Note: See TracBrowser for help on using the repository browser.