Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Light is now a World-entity.

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