Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10002 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
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"
[9869]24#include "util/loading/load_param_xml.h"
[7193]25#include "util/loading/factory.h"
[5129]26#include "debug.h"
[1853]27
[9869]28#include "class_id_DEPRECATED.h"
[4734]29
[9869]30ObjectListDefinitionID(Light, CL_LIGHT);
31CREATE_FACTORY(Light);
32
[4738]33//! Definition of the Lights and their Names
[4734]34int lightsV[] =
[8742]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 */
[8742]51Light::Light(const TiXmlElement* root)
[4734]52{
[4738]53  PRINTF(4)("initializing Light number %d.\n", this->lightNumber);
54
[9869]55  this->registerObject(this, Light::_objectList);
56
[4736]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
[5156]70  if (root != NULL)
71    this->loadParams(root);
[4734]72}
73
74/**
[4836]75 *  destroys a Light
[4734]76*/
[4746]77Light::~Light()
[4734]78{
79  glDisable(lightsV[this->lightNumber]);
[4736]80
81  LightManager::getInstance()->unregisterLight(this);
[4734]82}
83
84/**
[4836]85 * @param root The XML-element to load the Light from
[4734]86 */
87void Light::loadParams(const TiXmlElement* root)
[3597]88{
[6512]89  PNode::loadParams(root);
[4734]90
[5671]91  LoadParam(root, "diffuse-color", this, Light, setDiffuseColor)
[8742]92  .describe("sets the diffuse color of the Light (red [0-1], green [0-1], blue [0-1])");
[4734]93
[5671]94  LoadParam(root, "specular-color", this, Light, setSpecularColor)
[8742]95  .describe("sets the specular color of the Light (red [0-1], green [0-1], blue [0-1])");
[4734]96
[5671]97  LoadParam(root, "attenuation", this, Light, setAttenuation)
[8742]98  .describe("sets the Attenuation of the LightSource (constant Factor, linear Factor, quadratic Factor).");
[4738]99
[5671]100  LoadParam(root, "spot-direction", this, Light, setSpotDirection)
[8742]101  .describe("sets the Direction of the Spot");
[4734]102
[5671]103  LoadParam(root, "spot-cutoff", this, Light, setSpotCutoff)
[8742]104  .describe("the cuttoff of the Spotlight");
[3597]105}
106
[3245]107/**
[4836]108 *  sets an emitting Diffuse color of this Light
109 * @param r red
110 * @param g green
111 * @param b blue
[3597]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/**
[4836]124 *  sets an emitting Specular color of this Light
125 * @param r red
126 * @param g green
127 * @param b blue
[3597]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/**
[4836]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
[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/**
[4836]159 *  stets the direction of the Spot Light.
160 * @param direction The direction of the Spot Light.
[3597]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/**
[4836]173 *  sets the cutoff angle of the Light.
174 * @param cutoff The cutoff angle.
[3597]175*/
176void Light::setSpotCutoff(GLfloat cutoff)
177{
178  this->spotCutoff = cutoff;
179  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
180}
181
[3598]182/**
[4836]183 *  draws this Light. Being a World-entity the possibility to do this lies at hand.
[4597]184*/
[4746]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/**
[4836]194 *  Prints out some nice formated debug information about the Light
[3598]195*/
[4746]196void Light::debug() 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******************/
[9869]216ObjectListDefinition(LightManager);
[3597]217/**
[4836]218 *  standard constructor for a Light
[3245]219*/
[4597]220LightManager::LightManager ()
[3365]221{
[9869]222  this->registerObject(this, LightManager::_objectList);
[1853]223
[3437]224  glEnable (GL_LIGHTING);
[6763]225  glEnable ( GL_COLOR_MATERIAL ) ;
[6764]226  glColorMaterial ( GL_FRONT, GL_DIFFUSE ) ;
[6763]227
[3440]228  this->setAmbientColor(.3, .3, .3);
[3597]229  this->lights = new Light*[NUMBEROFLIGHTS];
[3437]230  for (int i = 0; i < NUMBEROFLIGHTS; i++)
231    lights[i] = NULL;
[3438]232  this->currentLight = NULL;
[3436]233}
[1853]234
[3245]235/**
[4836]236 *  standard deconstructor
[4597]237
[3446]238   first disables Lighting
[3598]239
[3446]240   then deletes the rest of the allocated memory
241   and in the end sets the singleton Reference to zero.
[3245]242*/
[4597]243LightManager::~LightManager ()
[3436]244{
[6763]245  glDisable(GL_COLOR_MATERIAL);
[3436]246  glDisable(GL_LIGHTING);
[4739]247  this->setAmbientColor(.0,.0,.0);
[4597]248
[4737]249  for (int i = 0; i < NUMBEROFLIGHTS; i++)
[5212]250    if (this->lights[i] != NULL)
[4737]251      delete lights[i];
[5211]252  delete[] lights;
[3597]253  LightManager::singletonRef = NULL;
[3436]254}
[1853]255
[3438]256/**
[4836]257 *  singleton-Reference to the Light-class
[3438]258*/
[3597]259LightManager* LightManager::singletonRef = NULL;
[3437]260
[3436]261/**
[4836]262* @param root the XML-element to load the LightManager's settings from
[4735]263 */
264void LightManager::loadParams(const TiXmlElement* root)
265{
[5653]266  LoadParamXML(root, "Lights", this, LightManager, loadLights)
[8742]267  .describe("an XML-Element to load lights from.");
[4471]268
[5671]269  LoadParam(root, "ambient-color", this, LightManager, setAmbientColor)
[8742]270  .describe("sets the ambient Color of the Environmental Light");
[4735]271}
272
[3437]273/**
[4836]274* @param root The XML-element to load Lights from
[4735]275 */
276void LightManager::loadLights(const TiXmlElement* root)
277{
278  const TiXmlElement* element = root->FirstChildElement();
279
280  while (element != NULL)
281  {
[5982]282    Factory::fabricate(element);
[4735]283
284    element = element->NextSiblingElement();
285  }
286}
287
[3436]288// set Attributes
[3245]289/**
[4836]290 *  sets the ambient Color of the Scene
291 * @param r red
292 * @param g green
293 * @param b blue
[3436]294*/
[3597]295void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
[3436]296{
[3597]297  this->ambientColor[0] = r;
298  this->ambientColor[1] = g;
299  this->ambientColor[2] = b;
300  this->ambientColor[3] = 1.0;
[3245]301
[3597]302  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
[3437]303}
[3436]304
[4738]305/**
[4836]306* @param light the Light to register to the LightManager
[4738]307
308  This is done explicitely by the constructor of a Light
309*/
[4736]310int LightManager::registerLight(Light* light)
[3436]311{
[4736]312  for (int i = 0; i < NUMBEROFLIGHTS; i++)
313    if (!this->lights[i])
[8742]314    {
315      this->lights[i]=light;
316      return i;
317    }
[4736]318  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
319  return -1;
[3436]320}
[3598]321
[4738]322/**
[4836]323* @param light The light to unregister from the LightManager
[4738]324
325  This is done every time a Light is destroyed explicitely by the Light-destructor
326 */
[4736]327void LightManager::unregisterLight(Light* light)
[3441]328{
[4736]329  for (int i = 0; i < NUMBEROFLIGHTS; i++)
330  {
331    if (this->lights[i] == light)
[4597]332    {
[4737]333      this->lights[i] = NULL;
334      return;
[3599]335    }
[4736]336  }
337  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
338  return;
[3441]339}
[3598]340
[6884]341
342Light* LightManager::getLight(int lightNumber) const
343{
344  if( lightNumber < NUMBEROFLIGHTS)
345    return this->lights[lightNumber];
346
347  return NULL;
348}
349
[3598]350/**
[4836]351 *  draws all the Lights in their appropriate position
[4736]352 */
353void LightManager::draw() const
[3440]354{
[8742]355  PRINTF(4)("Drawing the Lights\n");
[4736]356  for (int i = 0; i < NUMBEROFLIGHTS; i++)
[7109]357    if (this->lights[i] != NULL)
[4736]358      lights[i]->draw();
[3440]359}
[3598]360
361/**
[4836]362 *  outputs debug information about the Class and its lights
[3442]363*/
[4746]364void LightManager::debug() const
[3442]365{
366  PRINT(0)("=================================\n");
367  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
368  PRINT(0)("=================================\n");
[3597]369  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
[3442]370  if (this->currentLight)
[3598]371    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
[3442]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])
[8742]376    {
377      this->lights[i]->debug();
378    }
[4738]379  PRINT(0)("-----------------------------LM-\n");
[3442]380}
Note: See TracBrowser for help on using the repository browser.