Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: light is now more like a lightManager. It is a Class, that handles many lights, and as such also glEnable(GL_LIGHTING)…
Until now It is not possible to initialize more than the first Light, but this will be fixed soon :)

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