Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Light: added ambientColor, removed fancy effects :)

File size: 6.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
19#include "light.h"
20
21#include "glincl.h"
22
23using namespace std;
24
25//! Definition of the Lights
26int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
27
28
29/**
30   \brief standard constructor for a Light
31*/
32Light::Light () 
33{
34  this->setClassName ("Light");
35
36  glEnable (GL_LIGHTING);
37  this->setAmbientColor(.3, .3, .3);
38  this->lights = new LightValue*[NUMBEROFLIGHTS];
39  for (int i = 0; i < NUMBEROFLIGHTS; i++)
40    lights[i] = NULL;
41  this->currentLight = NULL;
42}
43
44/**
45   \brief standard deconstructor
46   \todo this deconstructor is not jet implemented - do it
47
48*/
49Light::~Light () 
50{
51  glDisable(GL_LIGHTING);
52 
53  for (int i = 0; i < NUMBEROFLIGHTS; i++)
54    this->deleteLight(i);
55  delete lights;
56  Light::singletonRef = NULL;
57}
58
59/**
60   \brief singleton-Reference to the Light-class
61*/
62Light* Light::singletonRef = NULL;
63
64/**
65   \returns The Instance of the Lights
66*/
67Light* Light::getInstance(void)
68{
69  if (singletonRef)
70    return singletonRef;
71  else
72    return Light::singletonRef = new Light();
73}
74
75/**
76   \brief initializes a new Light with default values, and enables GL_LIGHTING
77*/
78void Light::init(int lightNumber)
79{
80  PRINTF(3)("initializing Light number %d.\n", lightNumber);
81  // enable The light
82  glEnable(lightsV[lightNumber]);
83  this->currentLight = lights[lightNumber] = new LightValue;
84 
85  // set default values
86  this->currentLight->lightNumber = lightNumber;
87  this->setPosition(Vector(0.0, 0.0, 0.0));
88  this->setDiffuseColor(1.0, 1.0, 1.0);
89  this->setSpecularColor(1.0, 1.0, 1.0);
90  //  lmodelAmbient[] = {.1, .1, .1, 1.0};
91}
92
93/**
94   \brief Adds a new Light, by selecting the First free slot.
95
96   if no slot is free error
97*/
98int Light::addLight(void)
99{
100  for (int i = 0; i < NUMBEROFLIGHTS; i++)
101    if (!this->lights[i])
102      return addLight(i); 
103  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
104  return -1;
105}
106
107/**
108   \brief Adds a new Light
109   \param lightNumber The number of the light to add.
110
111   if the Number is not free: warn, automatically choose another slot.
112*/
113int Light::addLight(int lightNumber)
114{
115  if (this->lights[lightNumber])
116    {
117      PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
118      return this->addLight();
119    }
120  this->init(lightNumber);
121  this->currentLight = this->lights[lightNumber];
122  return lightNumber;
123}
124
125/**
126   \brief select the light to work with
127   \param lightNumber the light to work with
128*/
129void Light::editLightNumber(int lightNumber)
130{
131  if (!this->currentLight)
132    { 
133      PRINTF(1)("no Light defined yet\n");
134      return;
135    }
136
137  this->currentLight = lights[lightNumber];
138}
139
140/**
141   \brief Delete the current Light
142*/
143void Light::deleteLight(void)
144{
145  if (!this->currentLight)
146    { 
147      PRINTF(1)("no Light defined yet\n");
148      return;
149    }
150
151  this->deleteLight(this->currentLight->lightNumber);
152}
153
154/**
155   \brief delete light.
156   \param lightnumber the number of the light to delete
157*/
158void Light::deleteLight(int lightNumber)
159{
160  if (this->lights[lightNumber])
161    {
162      PRINTF(3)("deleting Light number %d\n", lightNumber);
163      delete this->lights[lightNumber];
164      glDisable(lightsV[lightNumber]);
165      this->lights[lightNumber] = NULL;
166    }
167}
168
169// set Attributes
170/**
171   \brief Sets a Position for the Light.
172   \param position The new position of the Light.
173   \todo patrick: is it ok to set a Light Position even if it is derived from p_node??
174*/
175void Light::setPosition(Vector position)
176{
177  if (!this->currentLight)
178    { 
179      PRINTF(1)("no Light defined yet\n");
180      return;
181    }
182  this->currentLight->lightPosition[0] = position.x;
183  this->currentLight->lightPosition[1] = position.y;
184  this->currentLight->lightPosition[2] = position.z;
185  this->currentLight->lightPosition[3] = 0.0;
186
187  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
188}
189
190void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
191{
192  if (!this->currentLight)
193    { 
194      PRINTF(1)("no Light defined yet\n");
195      return;
196    }
197
198  this->currentLight->lightPosition[0] = x;
199  this->currentLight->lightPosition[1] = y;
200  this->currentLight->lightPosition[2] = z;
201  this->currentLight->lightPosition[3] = 0.0;
202
203  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
204}
205
206/**
207   \brief sets an emitting Diffuse color for the Light
208   \param r red
209   \param g green
210   \param b blue
211*/
212void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
213{
214  if (!this->currentLight)
215    { 
216      PRINTF(1)("no Light defined yet\n");
217      return;
218    }
219
220  this->currentLight->diffuseColor[0] = r;
221  this->currentLight->diffuseColor[1] = g;
222  this->currentLight->diffuseColor[2] = b;
223  this->currentLight->diffuseColor[3] = 1.0;
224
225  glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor);
226}
227
228
229/**
230   \brief sets an emitting Ambient color for the Light
231   \param r red
232   \param g green
233   \param b blue
234*/
235void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
236{
237  if (!this->currentLight)
238    { 
239      PRINTF(1)("no Light defined yet\n");
240      return;
241    }
242
243  this->currentLight->specularColor[0] = r;
244  this->currentLight->specularColor[1] = g;
245  this->currentLight->specularColor[2] = b;
246  this->currentLight->specularColor[3] = 1.0;
247
248  glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor);
249}
250
251/**
252   \brief sets the ambient Color of the Scene
253   \param r red
254   \param g green
255   \param b blue
256*/
257void Light::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
258{
259  this->ambientColor[0] = r;
260  this->ambientColor[1] = g;
261  this->ambientColor[2] = b;
262  this->ambientColor[3] = 1.0;
263
264  glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor);
265
266}
267
268// get Attributes
269
270/**
271   \returns the Position of the Light
272*/
273Vector Light::getPosition(void)
274{
275  if (!this->currentLight)
276    { 
277      PRINTF(1)("no Light defined yet\n");
278      return Vector(.0, .0, .0);
279    }
280  else
281    return Vector(this->currentLight->lightPosition[0], this->currentLight->lightPosition[1], this->currentLight->lightPosition[2]);
282}
Note: See TracBrowser for help on using the repository browser.