Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4692 was 4597, checked in by bensch, 19 years ago

orxonox/trunk: setClassID implemented in all files

File size: 13.2 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"
[1853]25
[1856]26using namespace std;
[1853]27
[3437]28//! Definition of the Lights
29int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
[1856]30
[3598]31
32/**
33   \param lightNumber the Light Number to initiate
34*/
[3597]35Light::Light(int lightNumber)
36{
[4320]37  this->setClassID(CL_LIGHT, "Light");
[4597]38  char tmpName[10];
39  sprintf(tmpName, "Light[%d]", lightNumber);
[3602]40  this->setName(tmpName);
41
[3597]42  PRINTF(4)("initializing Light number %d.\n", lightNumber);
43  // enable The light
44  glEnable(lightsV[lightNumber]); // postSpawn
[4597]45
[3597]46  // set values (defaults)
47  this->lightNumber = lightNumber;
48  this->setPosition(0.0, 0.0, 0.0);
49  this->setDiffuseColor(1.0, 1.0, 1.0);
50  this->setSpecularColor(1.0, 1.0, 1.0);
51}
[3437]52
[4471]53
[3598]54/**
55   \brief destroys a Light
56*/
[3597]57Light::~Light(void)
58{
59  glDisable(lightsV[this->lightNumber]);
60}
61
[4471]62
[3245]63/**
[3597]64   \brief Sets a Position for the Light.
65   \param position The new position of the Light.
66   \todo patrick: is it ok to set a Light Position even if it is derived from p_node??
67*/
68void Light::setPosition(Vector position)
69{
70  this->lightPosition[0] = position.x;
71  this->lightPosition[1] = position.y;
72  this->lightPosition[2] = position.z;
73  this->lightPosition[3] = 0.0;
74
[3809]75  this->setAbsCoor(position);
[3598]76
77  glLightfv (lightsV[this->lightNumber], GL_POSITION, this->lightPosition);
[3597]78}
79
[4471]80
[3597]81/**
[3598]82   \brief Sets a Position of this Light.
[3597]83   \param x the x-coordinate
84   \param y the y-coordinate
85   \param z the z-coordinate
86*/
87void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
88{
[3598]89  this->setPosition(Vector(x, y, z));
[3597]90}
91
[4471]92
[3597]93/**
[3598]94   \brief sets an emitting Diffuse color of this Light
[3597]95   \param r red
96   \param g green
97   \param b blue
98*/
99void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
100{
101  this->diffuseColor[0] = r;
102  this->diffuseColor[1] = g;
103  this->diffuseColor[2] = b;
104  this->diffuseColor[3] = 1.0;
105
[3598]106  glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor);
[3597]107}
108
[4471]109
[3597]110/**
[3598]111   \brief sets an emitting Specular color of this Light
[3597]112   \param r red
113   \param g green
114   \param b blue
115*/
116void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
117{
118  this->specularColor[0] = r;
119  this->specularColor[1] = g;
120  this->specularColor[2] = b;
121  this->specularColor[3] = 1.0;
122
[3598]123  glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor);
[3597]124}
125
[4471]126
[3597]127/**
128   \brief Sets the AttenuationType of this Light Source
[3598]129   \param constantAttenuation The Constant Attenuation of the Light
130   \param linearAttenuation The Linear Attenuation of the Light
131   \param quadraticAttenuation The Quadratic Attenuation of the Light
[3597]132*/
133void Light::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
134{
135  this->constantAttenuation  = constantAttenuation;
136  this->linearAttenuation    = linearAttenuation;
137  this->quadraticAttenuation = quadraticAttenuation;
138
139  glLightf(lightsV[this->lightNumber], GL_CONSTANT_ATTENUATION,  constantAttenuation);
140  glLightf(lightsV[this->lightNumber], GL_LINEAR_ATTENUATION,    linearAttenuation);
141  glLightf(lightsV[this->lightNumber], GL_QUADRATIC_ATTENUATION, quadraticAttenuation);
142}
143
[4471]144
[3597]145/**
146   \brief stets the direction of the Spot Light.
147   \param direction The direction of the Spot Light.
148*/
149void Light::setSpotDirection(Vector direction)
150{
151  this->spotDirection[0] = direction.x;
152  this->spotDirection[1] = direction.y;
153  this->spotDirection[2] = direction.z;
154
155  glLightfv(lightsV[this->lightNumber], GL_SPOT_DIRECTION, this->spotDirection);
156}
157
[4471]158
[3597]159/**
160   \brief sets the cutoff angle of the Light.
161   \param cutoff The cutoff angle.
162*/
163void Light::setSpotCutoff(GLfloat cutoff)
164{
165  this->spotCutoff = cutoff;
166  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
167}
168
[4471]169
[3598]170/**
171   \returns the Position of the Light
172*/
[3600]173Vector Light::getPosition() const
[3598]174{
175  return Vector(this->lightPosition[0], this->lightPosition[1], this->lightPosition[2]);
176}
177
[4471]178
[3598]179/**
180   \brief draws this Light. Being a World-entity the possibility to do this lies at hand.
[4597]181*/
[3597]182void Light::draw()
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/**
191   \brief Prints out some nice formated debug information about the Light
192*/
[3600]193void Light::debug(void) 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)(" Position:      %f/%f/%f\n", this->lightPosition[0], this->lightPosition[1], this->lightPosition[2]);
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/**
[3436]215   \brief 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/**
230   \brief 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);
[4597]240
[3597]241  // this will be done either by worldEntity, or by pNode as each light is one of them
242  //  for (int i = 0; i < NUMBEROFLIGHTS; i++)
243  //    this->deleteLight(i);
[3437]244  delete lights;
[3597]245  LightManager::singletonRef = NULL;
[3436]246}
[1853]247
[4471]248
[3438]249/**
250   \brief singleton-Reference to the Light-class
251*/
[3597]252LightManager* LightManager::singletonRef = NULL;
[3437]253
[3436]254/**
255   \brief initializes a new Light with default values, and enables GL_LIGHTING
256*/
[3597]257void LightManager::initLight(int lightNumber)
[3436]258{
[3597]259  lights[lightNumber] = new Light(lightNumber);
[3436]260}
[1853]261
[4471]262
[3437]263/**
264   \brief Adds a new Light, by selecting the First free slot.
265
266   if no slot is free error
267*/
[3597]268int LightManager::addLight(void)
[3437]269{
270  for (int i = 0; i < NUMBEROFLIGHTS; i++)
271    if (!this->lights[i])
[4597]272      return addLight(i);
[3437]273  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
274  return -1;
275}
276
[4471]277
[3437]278/**
279   \brief Adds a new Light
280   \param lightNumber The number of the light to add.
281
282   if the Number is not free: warn, automatically choose another slot.
283*/
[3597]284int LightManager::addLight(int lightNumber)
[3437]285{
286  if (this->lights[lightNumber])
287    {
288      PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
289      return this->addLight();
290    }
[3597]291  this->initLight(lightNumber);
[3437]292  this->currentLight = this->lights[lightNumber];
293  return lightNumber;
294}
295
[4471]296
[3437]297/**
298   \brief select the light to work with
299   \param lightNumber the light to work with
300*/
[3597]301void LightManager::editLightNumber(int lightNumber)
[3437]302{
[3438]303  if (!this->currentLight)
[4597]304    {
[3597]305      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
[3438]306      return;
307    }
[3437]308  this->currentLight = lights[lightNumber];
309}
310
[4471]311
[3437]312/**
313   \brief Delete the current Light
314*/
[3597]315void LightManager::deleteLight(void)
[3437]316{
[3438]317  if (!this->currentLight)
[4597]318    {
[3597]319      PRINTF(1)("no Light defined yet. So you cannot delete any Light right now.\n");
[3438]320      return;
321    }
322
[3598]323  this->deleteLight(this->currentLight->getLightNumber());
[3437]324}
325
[4471]326
[3437]327/**
328   \brief delete light.
[3446]329   \param lightNumber the number of the light to delete
[3437]330*/
[3597]331void LightManager::deleteLight(int lightNumber)
[3437]332{
333  if (this->lights[lightNumber])
334    {
[3597]335      PRINTF(4)("deleting Light number %d\n", lightNumber);
[3437]336      delete this->lights[lightNumber];
337      this->lights[lightNumber] = NULL;
338    }
339}
340
[4471]341
[3598]342/**
343   \brief draws all the Lights in their appropriate position
344*/
345void LightManager::draw()
346{
347  glMatrixMode(GL_MODELVIEW);
348  glLoadIdentity();
[3602]349  PRINTF(4)("Drawing the Lights\n");
350  for (int i = 0; i < NUMBEROFLIGHTS; i++)
[3598]351    if (this->lights[i])
[3602]352      lights[i]->draw();
[3598]353}
354
355
356
[3436]357// set Attributes
[3245]358/**
[3597]359   \brief sets the ambient Color of the Scene
360   \param r red
361   \param g green
362   \param b blue
[3436]363*/
[3597]364void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
[3436]365{
[3597]366  this->ambientColor[0] = r;
367  this->ambientColor[1] = g;
368  this->ambientColor[2] = b;
369  this->ambientColor[3] = 1.0;
[3245]370
[3597]371  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
[3437]372}
[3436]373
[4471]374
[3598]375/**
376   \brief sets The Position of the currently selected Light
377   \param position the new Position
378*/
[3597]379void LightManager::setPosition(Vector position)
[3437]380{
[3597]381  this->currentLight->setPosition(position);
[3436]382}
[3598]383
[4471]384
[3598]385/**
386   \brief Sets a Position for the currently selected Light.
387   \param x the x-coordinate
388   \param y the y-coordinate
389   \param z the z-coordinate
390*/
[3597]391void LightManager::setPosition(GLfloat x, GLfloat y, GLfloat z)
[3436]392{
[3599]393  if (!this->currentLight)
[4597]394    {
[3599]395      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
396      return;
397    }
[3597]398  this->currentLight->setPosition(x, y, z);
[3436]399}
[3598]400
[4471]401
[3598]402/**
403   \brief sets an emitting Diffuse color of the currently selected Light
404   \param r red
405   \param g green
406   \param b blue
407*/
[3597]408void LightManager::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
[3436]409{
[3599]410  if (!this->currentLight)
[4597]411    {
[3599]412      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
413      return;
414    }
[3597]415  this->currentLight->setDiffuseColor(r,g,b);
[3436]416}
[3598]417
[4471]418
[3598]419/**
420   \brief sets an emitting Specular color of the currently selected Light
421   \param r red
422   \param g green
423   \param b blue
424*/
[3597]425void LightManager::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
[3441]426{
[3599]427  if (!this->currentLight)
[4597]428    {
[3599]429      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
430      return;
431    }
[3597]432  this->currentLight->setSpecularColor(r, g, b);
[3441]433}
[3598]434
[4471]435
[3598]436/**
437   \brief Sets the AttenuationType of th currently selecte LightSource Light Source
438   \param constantAttenuation The Constant Attenuation of the Light
439   \param linearAttenuation The Linear Attenuation of the Light
440   \param quadraticAttenuation The Quadratic Attenuation of the Light
441*/
[3597]442void LightManager::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
[3440]443{
[3599]444  if (!this->currentLight)
[4597]445    {
[3599]446      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
447      return;
448    }
[3597]449  this->currentLight->setAttenuation(constantAttenuation, linearAttenuation, quadraticAttenuation);
[3440]450}
[3598]451
452
453/**
454   \brief stets the direction of the Spot Light.
455   \param direction The direction of the Spot Light.
456*/
[3597]457void LightManager::setSpotDirection(Vector direction)
[3453]458{
[3599]459  if (!this->currentLight)
[4597]460    {
[3599]461      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
462      return;
463    }
[3597]464  this->currentLight->setSpotDirection(direction);
[3453]465}
[3598]466
[4471]467
[3598]468/**
469   \brief sets the cutoff angle of the Light.
470   \param cutoff The cutoff angle.
471*/
[3597]472void LightManager::setSpotCutoff(GLfloat cutoff)
[3453]473{
[3599]474  if (!this->currentLight)
[4597]475    {
[3599]476      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
477      return;
478    }
[3597]479  this->currentLight->setSpotCutoff(cutoff);
[3453]480}
481
[4471]482
[3436]483// get Attributes
484/**
485   \returns the Position of the Light
486*/
[3600]487Vector LightManager::getPosition(void) const
[3436]488{
[3438]489  if (!this->currentLight)
[4597]490    {
[3599]491      PRINTF(2)("no Light defined yet\n");
[3438]492      return Vector(.0, .0, .0);
493    }
494  else
[3598]495    return this->currentLight->getPosition();
[3436]496}
[3442]497
[4471]498
[3442]499/**
[4597]500   \returns the Position of Light
[3598]501   \param lightNumber lightnumber
502*/
[3600]503Vector LightManager::getPosition(int lightNumber) const
[3598]504{
505  if (!this->lights[lightNumber])
[3599]506    {
507      PRINTF(2)("no Light defined yet\n");
508      return Vector(.0,.0,.0);
509    }
[3598]510  else
511    return this->lights[lightNumber]->getPosition();
512}
513
[4471]514
[3600]515/**
516   \returns a pointer to a Light
517   \param lightNumber The light to request the pointer from
518*/
519Light* LightManager::getLight(int lightNumber) const
520{
521  return this->lights[lightNumber];
522}
[3598]523
[4471]524
[3598]525/**
[3442]526   \brief outputs debug information about the Class and its lights
527*/
[3600]528void LightManager::debug(void) const
[3442]529{
530  PRINT(0)("=================================\n");
531  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
532  PRINT(0)("=================================\n");
[3597]533  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
[3442]534  if (this->currentLight)
[3598]535    PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber());
[3442]536  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
537  PRINT(0)("=== Lights ===\n");
538  for (int i = 0; i < NUMBEROFLIGHTS; i++)
539    if (this->lights[i])
540      {
[4597]541        this->lights[i]->debug();
[3442]542      }
543  PRINT(0)("--------------------------------\n");
544}
Note: See TracBrowser for help on using the repository browser.