Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some minor cleanup, of the mess i made with AutoMake-sh

File size: 9.5 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
[5357]18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]19
[3436]20#include "light.h"
[1853]21
[3436]22#include "glincl.h"
[3608]23#include "vector.h"
[4734]24#include "tinyxml.h"
25#include "load_param.h"
26#include "factory.h"
[5129]27#include "debug.h"
[1853]28
[1856]29using namespace std;
[1853]30
[4734]31CREATE_FACTORY(Light);
32
[4738]33//! Definition of the Lights and their Names
[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/**
[4836]47 * @param root The XML-element to load the Light from
[4738]48
[4836]49  @todo what to do, if no Light-Slots are open anymore ???
[4734]50 */
51 Light::Light(const TiXmlElement* root)
52{
[4738]53  PRINTF(4)("initializing Light number %d.\n", this->lightNumber);
54
[4736]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
[5156]69  if (root != NULL)
70    this->loadParams(root);
[4734]71}
72
73/**
[4836]74 *  destroys a Light
[4734]75*/
[4746]76Light::~Light()
[4734]77{
78  glDisable(lightsV[this->lightNumber]);
[4736]79
80  LightManager::getInstance()->unregisterLight(this);
[4734]81}
82
83/**
[4836]84 * @param root The XML-element to load the Light from
[4734]85 */
86void Light::loadParams(const TiXmlElement* root)
[3597]87{
[4734]88  static_cast<PNode*>(this)->loadParams(root);
89
90  LoadParam<Light>(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<Light>(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
[4738]96  LoadParam<Light>(root, "attenuation", this, &Light::setAttenuation)
97      .describe("sets the Attenuation of the LightSource (constant Factor, linear Factor, quadratic Factor).");
98
[4734]99  LoadParam<Light>(root, "spot-direction", this, &Light::setSpotDirection)
100      .describe("sets the Direction of the Spot");
101
102  LoadParam<Light>(root, "spot-cutoff", this, &Light::setSpotCutoff)
103      .describe("the cuttoff of the Spotlight");
[3597]104}
105
[3245]106/**
[4836]107 *  sets an emitting Diffuse color of this Light
108 * @param r red
109 * @param g green
110 * @param b blue
[3597]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
[3598]119  glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor);
[3597]120}
121
122/**
[4836]123 *  sets an emitting Specular color of this Light
124 * @param r red
125 * @param g green
126 * @param b blue
[3597]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
[3598]135  glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor);
[3597]136}
137
[4471]138
[3597]139/**
[4836]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
[3597]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
[4471]156
[3597]157/**
[4836]158 *  stets the direction of the Spot Light.
159 * @param direction The direction of the Spot Light.
[3597]160*/
[4734]161void Light::setSpotDirection(const Vector& direction)
[3597]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
[4471]170
[3597]171/**
[4836]172 *  sets the cutoff angle of the Light.
173 * @param cutoff The cutoff angle.
[3597]174*/
175void Light::setSpotCutoff(GLfloat cutoff)
176{
177  this->spotCutoff = cutoff;
178  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
179}
180
[3598]181/**
[4836]182 *  draws this Light. Being a World-entity the possibility to do this lies at hand.
[4597]183*/
[4746]184void Light::draw() const
[3597]185{
[3602]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]);
[3597]188  glLightfv(lightsV[this->lightNumber], GL_POSITION, pos);
189}
190
[4471]191
[3598]192/**
[4836]193 *  Prints out some nice formated debug information about the Light
[3598]194*/
[4746]195void Light::debug() const
[3597]196{
197  PRINT(0)(":: %d ::  -- reference %p\n", this->lightNumber, this);
198  PRINT(0)(" GL-state: ");
[4597]199  GLboolean param;
[3597]200  glGetBooleanv(lightsV[this->lightNumber], &param);
201  if (param)
202    PRINT(0)("ON\n");
203  else
204    PRINT(0)("OFF\n");
[4597]205
[3597]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/**
[4836]216 *  standard constructor for a Light
[3245]217*/
[4597]218LightManager::LightManager ()
[3365]219{
[4320]220  this->setClassID(CL_LIGHT_MANAGER, "LightManager");
[1853]221
[3437]222  glEnable (GL_LIGHTING);
[3440]223  this->setAmbientColor(.3, .3, .3);
[3597]224  this->lights = new Light*[NUMBEROFLIGHTS];
[3437]225  for (int i = 0; i < NUMBEROFLIGHTS; i++)
226    lights[i] = NULL;
[3438]227  this->currentLight = NULL;
[3436]228}
[1853]229
[3245]230/**
[4836]231 *  standard deconstructor
[4597]232
[3446]233   first disables Lighting
[3598]234
[3446]235   then deletes the rest of the allocated memory
236   and in the end sets the singleton Reference to zero.
[3245]237*/
[4597]238LightManager::~LightManager ()
[3436]239{
240  glDisable(GL_LIGHTING);
[4739]241  this->setAmbientColor(.0,.0,.0);
[4597]242
[4737]243  for (int i = 0; i < NUMBEROFLIGHTS; i++)
[5212]244    if (this->lights[i] != NULL)
[4737]245      delete lights[i];
[5211]246  delete[] lights;
[3597]247  LightManager::singletonRef = NULL;
[3436]248}
[1853]249
[3438]250/**
[4836]251 *  singleton-Reference to the Light-class
[3438]252*/
[3597]253LightManager* LightManager::singletonRef = NULL;
[3437]254
[3436]255/**
[4836]256* @param root the XML-element to load the LightManager's settings from
[4735]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/**
[4836]268* @param root The XML-element to load Lights from
[4735]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/**
[4836]284 *  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
[4738]299/**
[4836]300* @param light the Light to register to the LightManager
[4738]301
302  This is done explicitely by the constructor of a Light
303*/
[4736]304int LightManager::registerLight(Light* light)
[3436]305{
[4736]306  for (int i = 0; i < NUMBEROFLIGHTS; i++)
307    if (!this->lights[i])
308  {
309    this->lights[i]=light;
310    return i;
311  }
312  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
313  return -1;
[3436]314}
[3598]315
[4738]316/**
[4836]317* @param light The light to unregister from the LightManager
[4738]318
319  This is done every time a Light is destroyed explicitely by the Light-destructor
320 */
[4736]321void LightManager::unregisterLight(Light* light)
[3441]322{
[4736]323  for (int i = 0; i < NUMBEROFLIGHTS; i++)
324  {
325    if (this->lights[i] == light)
[4597]326    {
[4737]327      this->lights[i] = NULL;
328      return;
[3599]329    }
[4736]330  }
331  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
332  return;
[3441]333}
[3598]334
335/**
[4836]336 *  draws all the Lights in their appropriate position
[4736]337 */
338void LightManager::draw() const
[3440]339{
[4736]340  glMatrixMode(GL_MODELVIEW);
341  glLoadIdentity();
342  PRINTF(4)("Drawing the Lights\n");
343  for (int i = 0; i < NUMBEROFLIGHTS; i++)
344    if (this->lights[i])
345      lights[i]->draw();
[3440]346}
[3598]347
348/**
[4836]349 *  outputs debug information about the Class and its lights
[3442]350*/
[4746]351void LightManager::debug() const
[3442]352{
353  PRINT(0)("=================================\n");
354  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
355  PRINT(0)("=================================\n");
[3597]356  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
[3442]357  if (this->currentLight)
[3598]358    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
[3442]359  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
360  PRINT(0)("=== Lights ===\n");
361  for (int i = 0; i < NUMBEROFLIGHTS; i++)
362    if (this->lights[i])
363      {
[4597]364        this->lights[i]->debug();
[3442]365      }
[4738]366  PRINT(0)("-----------------------------LM-\n");
[3442]367}
Note: See TracBrowser for help on using the repository browser.