Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/light.cc @ 3595

Last change on this file since 3595 was 3590, checked in by bensch, 20 years ago

orxonox/trunk: updated debug.h: now possibility to log per module (compile-time)

  1. write in the cc-file at the beginnig !!BEFORE ANY INCLUDES!!

#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_[MODULENAME]
where [MODULNAME] is a name of a module that can be defined in debug.h

  1. define a new MODULE: easy just write a new one under the other ones in DEBUG.h
  1. if you do not wish special loggin everything stays as is, and you do not have to worry. (then de verbose will be set from orxonox.cc: int verbose)
File size: 9.7 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
24using namespace std;
25
26//! Definition of the Lights
27int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
28
29
30/**
31   \brief standard constructor for a Light
32*/
33Light::Light () 
34{
35  this->setClassName ("Light");
36
37  glEnable (GL_LIGHTING);
38  this->setAmbientColor(.3, .3, .3);
39  this->lights = new LightValue*[NUMBEROFLIGHTS];
40  for (int i = 0; i < NUMBEROFLIGHTS; i++)
41    lights[i] = NULL;
42  this->currentLight = NULL;
43}
44
45/**
46   \brief standard deconstructor
47   
48   first disables Lighting
49   then deletes all the lights
50   then deletes the rest of the allocated memory
51   and in the end sets the singleton Reference to zero.
52*/
53Light::~Light () 
54{
55  glDisable(GL_LIGHTING);
56 
57  for (int i = 0; i < NUMBEROFLIGHTS; i++)
58    this->deleteLight(i);
59  delete lights;
60  Light::singletonRef = NULL;
61}
62
63/**
64   \brief singleton-Reference to the Light-class
65*/
66Light* Light::singletonRef = NULL;
67
68/**
69   \returns The Instance of the Lights
70*/
71Light* Light::getInstance(void)
72{
73  if (singletonRef)
74    return singletonRef;
75  else
76    return Light::singletonRef = new Light();
77}
78
79/**
80   \brief initializes a new Light with default values, and enables GL_LIGHTING
81*/
82void Light::init(int lightNumber)
83{
84  PRINTF(3)("initializing Light number %d.\n", lightNumber);
85  // enable The light
86  glEnable(lightsV[lightNumber]);
87  this->currentLight = lights[lightNumber] = new LightValue;
88 
89  // set default values
90  this->currentLight->lightNumber = lightNumber;
91  this->setPosition(0.0, 0.0, 0.0);
92  this->setDiffuseColor(1.0, 1.0, 1.0);
93  this->setSpecularColor(1.0, 1.0, 1.0);
94}
95
96/**
97   \brief Adds a new Light, by selecting the First free slot.
98
99   if no slot is free error
100*/
101int Light::addLight(void)
102{
103  for (int i = 0; i < NUMBEROFLIGHTS; i++)
104    if (!this->lights[i])
105      return addLight(i); 
106  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
107  return -1;
108}
109
110/**
111   \brief Adds a new Light
112   \param lightNumber The number of the light to add.
113
114   if the Number is not free: warn, automatically choose another slot.
115*/
116int Light::addLight(int lightNumber)
117{
118  if (this->lights[lightNumber])
119    {
120      PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
121      return this->addLight();
122    }
123  this->init(lightNumber);
124  this->currentLight = this->lights[lightNumber];
125  return lightNumber;
126}
127
128/**
129   \brief select the light to work with
130   \param lightNumber the light to work with
131*/
132void Light::editLightNumber(int lightNumber)
133{
134  if (!this->currentLight)
135    { 
136      PRINTF(1)("no Light defined yet\n");
137      return;
138    }
139
140  this->currentLight = lights[lightNumber];
141}
142
143/**
144   \brief Delete the current Light
145*/
146void Light::deleteLight(void)
147{
148  if (!this->currentLight)
149    { 
150      PRINTF(1)("no Light defined yet\n");
151      return;
152    }
153
154  this->deleteLight(this->currentLight->lightNumber);
155}
156
157/**
158   \brief delete light.
159   \param lightNumber the number of the light to delete
160*/
161void Light::deleteLight(int lightNumber)
162{
163  if (this->lights[lightNumber])
164    {
165      PRINTF(3)("deleting Light number %d\n", lightNumber);
166      delete this->lights[lightNumber];
167      glDisable(lightsV[lightNumber]);
168      this->lights[lightNumber] = NULL;
169    }
170}
171
172// set Attributes
173/**
174   \brief Sets a Position for the Light.
175   \param position The new position of the Light.
176   \todo patrick: is it ok to set a Light Position even if it is derived from p_node??
177*/
178void Light::setPosition(Vector position)
179{
180  if (!this->currentLight)
181    { 
182      PRINTF(1)("no Light defined yet\n");
183      return;
184    }
185  this->currentLight->lightPosition[0] = position.x;
186  this->currentLight->lightPosition[1] = position.y;
187  this->currentLight->lightPosition[2] = position.z;
188  this->currentLight->lightPosition[3] = 0.0;
189
190  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
191}
192
193/**
194   \brief Sets a Position for the Light.
195   \param x the x-coordinate
196   \param y the y-coordinate
197   \param z the z-coordinate
198*/
199void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
200{
201  if (!this->currentLight)
202    { 
203      PRINTF(1)("no Light defined yet\n");
204      return;
205    }
206
207  this->currentLight->lightPosition[0] = x;
208  this->currentLight->lightPosition[1] = y;
209  this->currentLight->lightPosition[2] = z;
210  this->currentLight->lightPosition[3] = 0.0;
211
212  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
213}
214
215/**
216   \brief sets an emitting Diffuse color for the Light
217   \param r red
218   \param g green
219   \param b blue
220*/
221void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
222{
223  if (!this->currentLight)
224    { 
225      PRINTF(1)("no Light defined yet\n");
226      return;
227    }
228
229  this->currentLight->diffuseColor[0] = r;
230  this->currentLight->diffuseColor[1] = g;
231  this->currentLight->diffuseColor[2] = b;
232  this->currentLight->diffuseColor[3] = 1.0;
233
234  glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor);
235}
236
237
238/**
239   \brief sets an emitting Ambient color for the Light
240   \param r red
241   \param g green
242   \param b blue
243*/
244void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
245{
246  if (!this->currentLight)
247    { 
248      PRINTF(1)("no Light defined yet\n");
249      return;
250    }
251
252  this->currentLight->specularColor[0] = r;
253  this->currentLight->specularColor[1] = g;
254  this->currentLight->specularColor[2] = b;
255  this->currentLight->specularColor[3] = 1.0;
256
257  glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor);
258}
259
260/**
261   \brief Sets the AttenuationType of this Light Source
262   \param type the AttenuationType to set
263   \param factor the Factor to multipy the attenuation with
264
265   this actually just sets the following: glLightf(currentLight, type, factor)
266*/
267void Light::setAttenuation(AttenuationType type, float factor)
268{
269  if (!this->currentLight)
270    { 
271      PRINTF(1)("no Light defined yet\n");
272      return;
273    }
274  this->currentLight->attenuationFactor = factor;
275  this->currentLight->attenuationType = type;
276  switch (type)
277    {
278    case CONSTANT:
279      glLightf(lightsV[this->currentLight->lightNumber], GL_CONSTANT_ATTENUATION, factor);
280      break;
281    case LINEAR:
282      glLightf(lightsV[this->currentLight->lightNumber], GL_LINEAR_ATTENUATION, factor);
283      break;
284    case QUADRATIC:
285      glLightf(lightsV[this->currentLight->lightNumber], GL_QUADRATIC_ATTENUATION, factor);
286      break;
287    }
288}
289
290
291/**
292   \brief sets the ambient Color of the Scene
293   \param r red
294   \param g green
295   \param b blue
296*/
297void Light::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
298{
299  this->ambientColor[0] = r;
300  this->ambientColor[1] = g;
301  this->ambientColor[2] = b;
302  this->ambientColor[3] = 1.0;
303
304  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
305}
306
307/**
308   \brief stets the direction of the Spot Light.
309   \param direction The direction of the Spot Light.
310*/
311void Light::setSpotDirection(Vector direction)
312{
313  this->currentLight->spotDirection[0] = direction.x;
314  this->currentLight->spotDirection[1] = direction.y;
315  this->currentLight->spotDirection[2] = direction.z;
316
317  glLightfv(lightsV[this->currentLight->lightNumber], GL_SPOT_DIRECTION, this->currentLight->spotDirection);
318}
319
320
321/**
322   \brief sets the cutoff angle of the Light.
323   \param cutoff The cutoff angle.
324*/
325void Light::setSpotCutoff(GLfloat cutoff)
326{
327  this->currentLight->spotCutoff = cutoff;
328  glLightf(lightsV[this->currentLight->lightNumber], GL_SPOT_CUTOFF, cutoff);
329}
330
331
332// get Attributes
333
334/**
335   \returns the Position of the Light
336*/
337Vector Light::getPosition(void)
338{
339  if (!this->currentLight)
340    { 
341      PRINTF(1)("no Light defined yet\n");
342      return Vector(.0, .0, .0);
343    }
344  else
345    return getPosition(currentLight->lightNumber);
346}
347
348
349
350
351/**
352   \brief outputs debug information about the Class and its lights
353*/
354void Light::debug(void)
355{
356  PRINT(0)("=================================\n");
357  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
358  PRINT(0)("=================================\n");
359  PRINT(0)("Reference: %p\n", Light::singletonRef);
360  if (this->currentLight)
361    PRINT(0)("current Light Nr: %d\n", this->currentLight->lightNumber);
362  PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]);
363  PRINT(0)("=== Lights ===\n");
364  for (int i = 0; i < NUMBEROFLIGHTS; i++)
365    if (this->lights[i])
366      {
367        PRINT(0)(":: %d ::  -- reference %p\n", i, lights[i]);
368        PRINT(0)(" GL-state: ");
369        GLboolean param; 
370        glGetBooleanv(lightsV[i], &param);
371        if (param)
372          PRINT(0)("ON\n");
373        else
374          PRINT(0)("OFF\n");
375
376        if (i != lights[i]->lightNumber)
377          PRINTF(1)(" Lights are out of sync, this really should not happen,\n   %d % should be equal.\n", i, lights[i]->lightNumber);
378        PRINT(0)(" Position:      %f/%f/%f\n", lights[i]->lightPosition[0], lights[i]->lightPosition[1], lights[i]->lightPosition[2]);
379        PRINT(0)(" DiffuseColor:  %f/%f/%f\n", lights[i]->diffuseColor[0], lights[i]->diffuseColor[1], lights[i]->diffuseColor[2]);
380        PRINT(0)(" SpecularColor: %f/%f/%f\n", lights[i]->specularColor[0], lights[i]->specularColor[1], lights[i]->specularColor[2]);
381        PRINT(0)(" Attenuation:   ");
382        switch (lights[i]->attenuationType)
383          {
384          case CONSTANT:
385            PRINT(0)("constant");
386          case LINEAR:
387            PRINT(0)("linear");
388            break;
389          case QUADRATIC:
390            PRINT(0)("quadratic");
391            break;
392          }
393        PRINT(0)(" with Factor %f\n", lights[i]->attenuationFactor);
394      }
395  PRINT(0)("--------------------------------\n");
396}
Note: See TracBrowser for help on using the repository browser.