Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4849 was 4836, checked in by bensch, 19 years ago

orxonox/trunk: renamed all the \param → @param and so on in Doxygen tags.
Thanks a lot to the kDevelop team. this took since the last commit :)

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