Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4385 was 4381, checked in by bensch, 20 years ago

orxonox/trunk: made include more local. stdincl.h not in base_object.h anymore

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