Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: better switch with keys

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"
[5944]24#include "parser/tinyxml/tinyxml.h"
[7193]25#include "util/loading/load_param.h"
26#include "util/loading/factory.h"
[5129]27#include "debug.h"
[1853]28
[5750]29CREATE_FACTORY(Light, CL_LIGHT);
[4734]30
[4738]31//! Definition of the Lights and their Names
[4734]32int lightsV[] =
[8742]33  {
34    GL_LIGHT0,
35    GL_LIGHT1,
36    GL_LIGHT2,
37    GL_LIGHT3,
38    GL_LIGHT4,
39    GL_LIGHT5,
40    GL_LIGHT6,
41    GL_LIGHT7
42  };
[1856]43
[3598]44/**
[4836]45 * @param root The XML-element to load the Light from
[4738]46
[4836]47  @todo what to do, if no Light-Slots are open anymore ???
[4734]48 */
[8742]49Light::Light(const TiXmlElement* root)
[4734]50{
[4738]51  PRINTF(4)("initializing Light number %d.\n", this->lightNumber);
52
[4736]53  this->lightNumber = LightManager::getInstance()->registerLight(this);
54
55  this->setClassID(CL_LIGHT, "Light");
56  char tmpName[10];
57  sprintf(tmpName, "Light[%d]", this->lightNumber);
58  this->setName(tmpName);
59
60  // enable The light
61  glEnable(lightsV[this->lightNumber]); // postSpawn
62
63  // set values (defaults)
64  this->setDiffuseColor(1.0, 1.0, 1.0);
65  this->setSpecularColor(1.0, 1.0, 1.0);
66
[5156]67  if (root != NULL)
68    this->loadParams(root);
[4734]69}
70
71/**
[4836]72 *  destroys a Light
[4734]73*/
[4746]74Light::~Light()
[4734]75{
76  glDisable(lightsV[this->lightNumber]);
[4736]77
78  LightManager::getInstance()->unregisterLight(this);
[4734]79}
80
81/**
[4836]82 * @param root The XML-element to load the Light from
[4734]83 */
84void Light::loadParams(const TiXmlElement* root)
[3597]85{
[6512]86  PNode::loadParams(root);
[4734]87
[5671]88  LoadParam(root, "diffuse-color", this, Light, setDiffuseColor)
[8742]89  .describe("sets the diffuse color of the Light (red [0-1], green [0-1], blue [0-1])");
[4734]90
[5671]91  LoadParam(root, "specular-color", this, Light, setSpecularColor)
[8742]92  .describe("sets the specular color of the Light (red [0-1], green [0-1], blue [0-1])");
[4734]93
[5671]94  LoadParam(root, "attenuation", this, Light, setAttenuation)
[8742]95  .describe("sets the Attenuation of the LightSource (constant Factor, linear Factor, quadratic Factor).");
[4738]96
[5671]97  LoadParam(root, "spot-direction", this, Light, setSpotDirection)
[8742]98  .describe("sets the Direction of the Spot");
[4734]99
[5671]100  LoadParam(root, "spot-cutoff", this, Light, setSpotCutoff)
[8742]101  .describe("the cuttoff of the Spotlight");
[3597]102}
103
[3245]104/**
[4836]105 *  sets an emitting Diffuse color of this Light
106 * @param r red
107 * @param g green
108 * @param b blue
[3597]109*/
110void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
111{
112  this->diffuseColor[0] = r;
113  this->diffuseColor[1] = g;
114  this->diffuseColor[2] = b;
115  this->diffuseColor[3] = 1.0;
116
[3598]117  glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor);
[3597]118}
119
120/**
[4836]121 *  sets an emitting Specular color of this Light
122 * @param r red
123 * @param g green
124 * @param b blue
[3597]125*/
126void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
127{
128  this->specularColor[0] = r;
129  this->specularColor[1] = g;
130  this->specularColor[2] = b;
131  this->specularColor[3] = 1.0;
132
[3598]133  glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor);
[3597]134}
135
[4471]136
[3597]137/**
[4836]138 *  Sets the AttenuationType of this Light Source
139 * @param constantAttenuation The Constant Attenuation of the Light
140 * @param linearAttenuation The Linear Attenuation of the Light
141 * @param quadraticAttenuation The Quadratic Attenuation of the Light
[3597]142*/
143void Light::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
144{
145  this->constantAttenuation  = constantAttenuation;
146  this->linearAttenuation    = linearAttenuation;
147  this->quadraticAttenuation = quadraticAttenuation;
148
149  glLightf(lightsV[this->lightNumber], GL_CONSTANT_ATTENUATION,  constantAttenuation);
150  glLightf(lightsV[this->lightNumber], GL_LINEAR_ATTENUATION,    linearAttenuation);
151  glLightf(lightsV[this->lightNumber], GL_QUADRATIC_ATTENUATION, quadraticAttenuation);
152}
153
[4471]154
[3597]155/**
[4836]156 *  stets the direction of the Spot Light.
157 * @param direction The direction of the Spot Light.
[3597]158*/
[4734]159void Light::setSpotDirection(const Vector& direction)
[3597]160{
161  this->spotDirection[0] = direction.x;
162  this->spotDirection[1] = direction.y;
163  this->spotDirection[2] = direction.z;
164
165  glLightfv(lightsV[this->lightNumber], GL_SPOT_DIRECTION, this->spotDirection);
166}
167
[4471]168
[3597]169/**
[4836]170 *  sets the cutoff angle of the Light.
171 * @param cutoff The cutoff angle.
[3597]172*/
173void Light::setSpotCutoff(GLfloat cutoff)
174{
175  this->spotCutoff = cutoff;
176  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
177}
178
[3598]179/**
[4836]180 *  draws this Light. Being a World-entity the possibility to do this lies at hand.
[4597]181*/
[4746]182void Light::draw() const
[3597]183{
[3602]184  float pos[4] = {this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z, 1.0};
185  PRINTF(4)("Drawing The Lights new Position at %f %f %f\n", pos[0], pos[1], pos[2]);
[3597]186  glLightfv(lightsV[this->lightNumber], GL_POSITION, pos);
187}
188
[4471]189
[3598]190/**
[4836]191 *  Prints out some nice formated debug information about the Light
[3598]192*/
[4746]193void Light::debug() const
[3597]194{
195  PRINT(0)(":: %d ::  -- reference %p\n", this->lightNumber, this);
196  PRINT(0)(" GL-state: ");
[4597]197  GLboolean param;
[3597]198  glGetBooleanv(lightsV[this->lightNumber], &param);
199  if (param)
200    PRINT(0)("ON\n");
201  else
202    PRINT(0)("OFF\n");
[4597]203
[3597]204  PRINT(0)(" DiffuseColor:  %f/%f/%f\n", this->diffuseColor[0], this->diffuseColor[1], this->diffuseColor[2]);
205  PRINT(0)(" SpecularColor: %f/%f/%f\n", this->specularColor[0], this->specularColor[1], this->specularColor[2]);
206  PRINT(0)(" Attenuation: constant=%f linear=%f quadratic=%f\n", this->constantAttenuation, this->linearAttenuation, this->quadraticAttenuation);
207}
208
209
210/******************
211** LIGHT-MANAGER **
212******************/
213/**
[4836]214 *  standard constructor for a Light
[3245]215*/
[4597]216LightManager::LightManager ()
[3365]217{
[4320]218  this->setClassID(CL_LIGHT_MANAGER, "LightManager");
[1853]219
[3437]220  glEnable (GL_LIGHTING);
[6763]221  glEnable ( GL_COLOR_MATERIAL ) ;
[6764]222  glColorMaterial ( GL_FRONT, GL_DIFFUSE ) ;
[6763]223
[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/**
[4836]232 *  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{
[6763]241  glDisable(GL_COLOR_MATERIAL);
[3436]242  glDisable(GL_LIGHTING);
[4739]243  this->setAmbientColor(.0,.0,.0);
[4597]244
[4737]245  for (int i = 0; i < NUMBEROFLIGHTS; i++)
[5212]246    if (this->lights[i] != NULL)
[4737]247      delete lights[i];
[5211]248  delete[] lights;
[3597]249  LightManager::singletonRef = NULL;
[3436]250}
[1853]251
[3438]252/**
[4836]253 *  singleton-Reference to the Light-class
[3438]254*/
[3597]255LightManager* LightManager::singletonRef = NULL;
[3437]256
[3436]257/**
[4836]258* @param root the XML-element to load the LightManager's settings from
[4735]259 */
260void LightManager::loadParams(const TiXmlElement* root)
261{
[5653]262  LoadParamXML(root, "Lights", this, LightManager, loadLights)
[8742]263  .describe("an XML-Element to load lights from.");
[4471]264
[5671]265  LoadParam(root, "ambient-color", this, LightManager, setAmbientColor)
[8742]266  .describe("sets the ambient Color of the Environmental Light");
[4735]267}
268
[3437]269/**
[4836]270* @param root The XML-element to load Lights from
[4735]271 */
272void LightManager::loadLights(const TiXmlElement* root)
273{
274  const TiXmlElement* element = root->FirstChildElement();
275
276  while (element != NULL)
277  {
[5982]278    Factory::fabricate(element);
[4735]279
280    element = element->NextSiblingElement();
281  }
282}
283
[3436]284// set Attributes
[3245]285/**
[4836]286 *  sets the ambient Color of the Scene
287 * @param r red
288 * @param g green
289 * @param b blue
[3436]290*/
[3597]291void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
[3436]292{
[3597]293  this->ambientColor[0] = r;
294  this->ambientColor[1] = g;
295  this->ambientColor[2] = b;
296  this->ambientColor[3] = 1.0;
[3245]297
[3597]298  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
[3437]299}
[3436]300
[4738]301/**
[4836]302* @param light the Light to register to the LightManager
[4738]303
304  This is done explicitely by the constructor of a Light
305*/
[4736]306int LightManager::registerLight(Light* light)
[3436]307{
[4736]308  for (int i = 0; i < NUMBEROFLIGHTS; i++)
309    if (!this->lights[i])
[8742]310    {
311      this->lights[i]=light;
312      return i;
313    }
[4736]314  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
315  return -1;
[3436]316}
[3598]317
[4738]318/**
[4836]319* @param light The light to unregister from the LightManager
[4738]320
321  This is done every time a Light is destroyed explicitely by the Light-destructor
322 */
[4736]323void LightManager::unregisterLight(Light* light)
[3441]324{
[4736]325  for (int i = 0; i < NUMBEROFLIGHTS; i++)
326  {
327    if (this->lights[i] == light)
[4597]328    {
[4737]329      this->lights[i] = NULL;
330      return;
[3599]331    }
[4736]332  }
333  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
334  return;
[3441]335}
[3598]336
[6884]337
338Light* LightManager::getLight(int lightNumber) const
339{
340  if( lightNumber < NUMBEROFLIGHTS)
341    return this->lights[lightNumber];
342
343  return NULL;
344}
345
[3598]346/**
[4836]347 *  draws all the Lights in their appropriate position
[4736]348 */
349void LightManager::draw() const
[3440]350{
[8742]351  PRINTF(4)("Drawing the Lights\n");
[4736]352  for (int i = 0; i < NUMBEROFLIGHTS; i++)
[7109]353    if (this->lights[i] != NULL)
[4736]354      lights[i]->draw();
[3440]355}
[3598]356
357/**
[4836]358 *  outputs debug information about the Class and its lights
[3442]359*/
[4746]360void LightManager::debug() const
[3442]361{
362  PRINT(0)("=================================\n");
363  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
364  PRINT(0)("=================================\n");
[3597]365  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
[3442]366  if (this->currentLight)
[3598]367    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
[3442]368  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
369  PRINT(0)("=== Lights ===\n");
370  for (int i = 0; i < NUMBEROFLIGHTS; i++)
371    if (this->lights[i])
[8742]372    {
373      this->lights[i]->debug();
374    }
[4738]375  PRINT(0)("-----------------------------LM-\n");
[3442]376}
Note: See TracBrowser for help on using the repository browser.