Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/light.cc @ 9727

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

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

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 "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  printf("Loading Lights\n");
267  LoadParamXML(root, "Lights", this, LightManager, loadLights)
268  .describe("an XML-Element to load lights from.");
269
270  LoadParam(root, "ambient-color", this, LightManager, setAmbientColor)
271  .describe("sets the ambient Color of the Environmental Light");
272}
273
274/**
275* @param root The XML-element to load Lights from
276 */
277void LightManager::loadLights(const TiXmlElement* root)
278{
279  printf("Loading single Lights\n");
280
281  const TiXmlElement* element = root->FirstChildElement();
282
283  while (element != NULL)
284  {
285    Factory::fabricate(element);
286
287    element = element->NextSiblingElement();
288  }
289}
290
291// set Attributes
292/**
293 *  sets the ambient Color of the Scene
294 * @param r red
295 * @param g green
296 * @param b blue
297*/
298void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
299{
300  this->ambientColor[0] = r;
301  this->ambientColor[1] = g;
302  this->ambientColor[2] = b;
303  this->ambientColor[3] = 1.0;
304
305  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
306}
307
308/**
309* @param light the Light to register to the LightManager
310
311  This is done explicitely by the constructor of a Light
312*/
313int LightManager::registerLight(Light* light)
314{
315  for (int i = 0; i < NUMBEROFLIGHTS; i++)
316    if (!this->lights[i])
317    {
318      this->lights[i]=light;
319      return i;
320    }
321  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
322  return -1;
323}
324
325/**
326* @param light The light to unregister from the LightManager
327
328  This is done every time a Light is destroyed explicitely by the Light-destructor
329 */
330void LightManager::unregisterLight(Light* light)
331{
332  for (int i = 0; i < NUMBEROFLIGHTS; i++)
333  {
334    if (this->lights[i] == light)
335    {
336      this->lights[i] = NULL;
337      return;
338    }
339  }
340  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
341  return;
342}
343
344
345Light* LightManager::getLight(int lightNumber) const
346{
347  if( lightNumber < NUMBEROFLIGHTS)
348    return this->lights[lightNumber];
349
350  return NULL;
351}
352
353/**
354 *  draws all the Lights in their appropriate position
355 */
356void LightManager::draw() const
357{
358  PRINTF(4)("Drawing the Lights\n");
359  for (int i = 0; i < NUMBEROFLIGHTS; i++)
360    if (this->lights[i] != NULL)
361      lights[i]->draw();
362}
363
364/**
365 *  outputs debug information about the Class and its lights
366*/
367void LightManager::debug() const
368{
369  PRINT(0)("=================================\n");
370  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
371  PRINT(0)("=================================\n");
372  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
373  if (this->currentLight)
374    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
375  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
376  PRINT(0)("=== Lights ===\n");
377  for (int i = 0; i < NUMBEROFLIGHTS; i++)
378    if (this->lights[i])
379    {
380      this->lights[i]->debug();
381    }
382  PRINT(0)("-----------------------------LM-\n");
383}
Note: See TracBrowser for help on using the repository browser.