Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: light: comments, errormessages, light up again

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