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
RevLine 
[1853]1
2
[4597]3/*
[1853]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.
[1855]12
13   ### File Specific:
[3436]14   main-programmer: Benjamin Grauer
[1855]15   co-programmer: ...
[1853]16*/
17
[3590]18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LIGHT
[1853]19
[3436]20#include "light.h"
[1853]21
[3436]22#include "glincl.h"
[3608]23#include "vector.h"
[4381]24#include "debug.h"
[4734]25#include "tinyxml.h"
26#include "load_param.h"
27#include "factory.h"
[1853]28
[1856]29using namespace std;
[1853]30
[4734]31CREATE_FACTORY(Light);
32
[3437]33//! Definition of the Lights
[4734]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};
[1856]45
[3598]46/**
[4734]47 * \param root The XML-element to load the Light from
48 */
49 Light::Light(const TiXmlElement* root)
50{
[4736]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
[4734]66  this->loadParams(root);
[4736]67
[4734]68}
69
70/**
71   \brief destroys a Light
72*/
73Light::~Light(void)
74{
75  glDisable(lightsV[this->lightNumber]);
[4736]76
77  LightManager::getInstance()->unregisterLight(this);
[4734]78}
79
80/**
81 * \brief initializes a Light
82 */
[4736]83void Light::init()
[4734]84{
[3597]85}
[3437]86
[3598]87/**
[4734]88 * \param root The XML-element to load the Light from
89 */
90void Light::loadParams(const TiXmlElement* root)
[3597]91{
[4734]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");
[3597]105}
106
[3245]107/**
[3598]108   \brief sets an emitting Diffuse color of this Light
[3597]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
[3598]120  glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor);
[3597]121}
122
123/**
[3598]124   \brief sets an emitting Specular color of this Light
[3597]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
[3598]136  glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor);
[3597]137}
138
[4471]139
[3597]140/**
141   \brief Sets the AttenuationType of this Light Source
[3598]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
[3597]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
[4471]157
[3597]158/**
159   \brief stets the direction of the Spot Light.
160   \param direction The direction of the Spot Light.
161*/
[4734]162void Light::setSpotDirection(const Vector& direction)
[3597]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
[4471]171
[3597]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
[3598]182/**
183   \brief draws this Light. Being a World-entity the possibility to do this lies at hand.
[4597]184*/
[4736]185void Light::draw() const
[3597]186{
[3602]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]);
[3597]189  glLightfv(lightsV[this->lightNumber], GL_POSITION, pos);
190}
191
[4471]192
[3598]193/**
194   \brief Prints out some nice formated debug information about the Light
195*/
[3600]196void Light::debug(void) const
[3597]197{
198  PRINT(0)(":: %d ::  -- reference %p\n", this->lightNumber, this);
199  PRINT(0)(" GL-state: ");
[4597]200  GLboolean param;
[3597]201  glGetBooleanv(lightsV[this->lightNumber], &param);
202  if (param)
203    PRINT(0)("ON\n");
204  else
205    PRINT(0)("OFF\n");
[4597]206
[3597]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/**
[3436]217   \brief standard constructor for a Light
[3245]218*/
[4597]219LightManager::LightManager ()
[3365]220{
[4320]221  this->setClassID(CL_LIGHT_MANAGER, "LightManager");
[1853]222
[3437]223  glEnable (GL_LIGHTING);
[3440]224  this->setAmbientColor(.3, .3, .3);
[3597]225  this->lights = new Light*[NUMBEROFLIGHTS];
[3437]226  for (int i = 0; i < NUMBEROFLIGHTS; i++)
227    lights[i] = NULL;
[3438]228  this->currentLight = NULL;
[3436]229}
[1853]230
[3245]231/**
232   \brief standard deconstructor
[4597]233
[3446]234   first disables Lighting
[3598]235
[3446]236   then deletes the rest of the allocated memory
237   and in the end sets the singleton Reference to zero.
[3245]238*/
[4597]239LightManager::~LightManager ()
[3436]240{
241  glDisable(GL_LIGHTING);
[4597]242
[4736]243  // this will be done either by PNode-tree as each light is one of them
[3597]244  //  for (int i = 0; i < NUMBEROFLIGHTS; i++)
245  //    this->deleteLight(i);
[3437]246  delete lights;
[3597]247  LightManager::singletonRef = NULL;
[3436]248}
[1853]249
[3438]250/**
251   \brief singleton-Reference to the Light-class
252*/
[3597]253LightManager* LightManager::singletonRef = NULL;
[3437]254
[3436]255/**
[4735]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.");
[4471]262
[4735]263  LoadParam<LightManager>(root, "ambient-color", this, &LightManager::setAmbientColor)
264      .describe("sets the ambient Color of the Environmental Light");
265}
266
[3437]267/**
[4735]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
[3436]282// set Attributes
[3245]283/**
[3597]284   \brief sets the ambient Color of the Scene
285   \param r red
286   \param g green
287   \param b blue
[3436]288*/
[3597]289void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
[3436]290{
[3597]291  this->ambientColor[0] = r;
292  this->ambientColor[1] = g;
293  this->ambientColor[2] = b;
294  this->ambientColor[3] = 1.0;
[3245]295
[3597]296  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
[3437]297}
[3436]298
[4736]299int LightManager::registerLight(Light* light)
[3436]300{
[4736]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;
[3436]309}
[3598]310
[4736]311void LightManager::unregisterLight(Light* light)
[3441]312{
[4736]313  for (int i = 0; i < NUMBEROFLIGHTS; i++)
314  {
315    if (this->lights[i] == light)
[4597]316    {
[4736]317      this->lights[i] == NULL;
[3599]318    }
[4736]319  }
320  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
321  return;
[3441]322}
[3598]323
324/**
[4736]325   \brief draws all the Lights in their appropriate position
326 */
327void LightManager::draw() const
[3440]328{
[4736]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();
[3440]335}
[3598]336
337/**
[3442]338   \brief outputs debug information about the Class and its lights
339*/
[3600]340void LightManager::debug(void) const
[3442]341{
342  PRINT(0)("=================================\n");
343  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
344  PRINT(0)("=================================\n");
[3597]345  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
[3442]346  if (this->currentLight)
[3598]347    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
[3442]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      {
[4597]353        this->lights[i]->debug();
[3442]354      }
355  PRINT(0)("--------------------------------\n");
356}
Note: See TracBrowser for help on using the repository browser.