[1853] | 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. |
---|
[1855] | 12 | |
---|
| 13 | ### File Specific: |
---|
[3436] | 14 | main-programmer: Benjamin Grauer |
---|
[1855] | 15 | co-programmer: ... |
---|
[1853] | 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
[3436] | 19 | #include "light.h" |
---|
[1853] | 20 | |
---|
[3436] | 21 | #include "glincl.h" |
---|
[1853] | 22 | |
---|
[1856] | 23 | using namespace std; |
---|
[1853] | 24 | |
---|
[3437] | 25 | //! Definition of the Lights |
---|
| 26 | int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7}; |
---|
[1856] | 27 | |
---|
[3437] | 28 | |
---|
[3245] | 29 | /** |
---|
[3436] | 30 | \brief standard constructor for a Light |
---|
[3245] | 31 | */ |
---|
[3436] | 32 | Light::Light () |
---|
[3365] | 33 | { |
---|
[3437] | 34 | this->setClassName ("Light"); |
---|
[1853] | 35 | |
---|
[3437] | 36 | glEnable (GL_LIGHTING); |
---|
| 37 | this->lights = new LightValue*[NUMBEROFLIGHTS]; |
---|
| 38 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 39 | lights[i] = NULL; |
---|
[3438] | 40 | this->currentLight = NULL; |
---|
[3436] | 41 | } |
---|
[1853] | 42 | |
---|
[3245] | 43 | /** |
---|
| 44 | \brief standard deconstructor |
---|
[3329] | 45 | \todo this deconstructor is not jet implemented - do it |
---|
[1853] | 46 | |
---|
[3245] | 47 | */ |
---|
[3436] | 48 | Light::~Light () |
---|
| 49 | { |
---|
| 50 | glDisable(GL_LIGHTING); |
---|
[3437] | 51 | |
---|
| 52 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 53 | this->deleteLight(i); |
---|
| 54 | delete lights; |
---|
| 55 | Light::singletonRef = NULL; |
---|
[3436] | 56 | } |
---|
[1853] | 57 | |
---|
[3438] | 58 | /** |
---|
| 59 | \brief singleton-Reference to the Light-class |
---|
| 60 | */ |
---|
[3437] | 61 | Light* Light::singletonRef = NULL; |
---|
| 62 | |
---|
[3436] | 63 | /** |
---|
[3437] | 64 | \returns The Instance of the Lights |
---|
| 65 | */ |
---|
| 66 | Light* Light::getInstance(void) |
---|
| 67 | { |
---|
| 68 | if (singletonRef) |
---|
| 69 | return singletonRef; |
---|
| 70 | else |
---|
| 71 | return Light::singletonRef = new Light(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
[3436] | 75 | \brief initializes a new Light with default values, and enables GL_LIGHTING |
---|
| 76 | */ |
---|
[3437] | 77 | void Light::init(int lightNumber) |
---|
[3436] | 78 | { |
---|
[3437] | 79 | PRINTF(3)("initializing Light number %d.\n", lightNumber); |
---|
| 80 | // enable The light |
---|
| 81 | glEnable(lightsV[lightNumber]); |
---|
| 82 | this->currentLight = lights[lightNumber] = new LightValue; |
---|
[3436] | 83 | |
---|
[3437] | 84 | // set default values |
---|
| 85 | this->currentLight->lightNumber = lightNumber; |
---|
[3436] | 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 | } |
---|
[1853] | 91 | |
---|
[3437] | 92 | /** |
---|
| 93 | \brief Adds a new Light, by selecting the First free slot. |
---|
| 94 | |
---|
| 95 | if no slot is free error |
---|
| 96 | */ |
---|
| 97 | int 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 | */ |
---|
| 112 | int 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 | */ |
---|
| 128 | void Light::editLightNumber(int lightNumber) |
---|
| 129 | { |
---|
[3438] | 130 | if (!this->currentLight) |
---|
| 131 | { |
---|
| 132 | PRINTF(1)("no Light defined yet\n"); |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | |
---|
[3437] | 136 | this->currentLight = lights[lightNumber]; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | \brief Delete the current Light |
---|
| 141 | */ |
---|
| 142 | void Light::deleteLight(void) |
---|
| 143 | { |
---|
[3438] | 144 | if (!this->currentLight) |
---|
| 145 | { |
---|
| 146 | PRINTF(1)("no Light defined yet\n"); |
---|
| 147 | return; |
---|
| 148 | } |
---|
| 149 | |
---|
[3437] | 150 | this->deleteLight(this->currentLight->lightNumber); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | \brief delete light. |
---|
| 155 | \param lightnumber the number of the light to delete |
---|
| 156 | */ |
---|
| 157 | void 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 | |
---|
[3436] | 168 | // set Attributes |
---|
[3245] | 169 | /** |
---|
[3436] | 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 | */ |
---|
| 174 | void Light::setPosition(Vector position) |
---|
| 175 | { |
---|
[3438] | 176 | if (!this->currentLight) |
---|
| 177 | { |
---|
| 178 | PRINTF(1)("no Light defined yet\n"); |
---|
| 179 | return; |
---|
| 180 | } |
---|
[3437] | 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; |
---|
[3245] | 185 | |
---|
[3437] | 186 | glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition); |
---|
| 187 | } |
---|
[3436] | 188 | |
---|
[3437] | 189 | void Light::setPosition(GLfloat x, GLfloat y, GLfloat z) |
---|
| 190 | { |
---|
[3438] | 191 | if (!this->currentLight) |
---|
| 192 | { |
---|
| 193 | PRINTF(1)("no Light defined yet\n"); |
---|
| 194 | return; |
---|
| 195 | } |
---|
| 196 | |
---|
[3437] | 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); |
---|
[3436] | 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 |
---|
[3245] | 210 | */ |
---|
[3436] | 211 | void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b) |
---|
| 212 | { |
---|
[3438] | 213 | if (!this->currentLight) |
---|
| 214 | { |
---|
| 215 | PRINTF(1)("no Light defined yet\n"); |
---|
| 216 | return; |
---|
| 217 | } |
---|
| 218 | |
---|
[3437] | 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; |
---|
[3436] | 223 | |
---|
[3437] | 224 | glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor); |
---|
[3436] | 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 | */ |
---|
| 234 | void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b) |
---|
| 235 | { |
---|
[3438] | 236 | if (!this->currentLight) |
---|
| 237 | { |
---|
| 238 | PRINTF(1)("no Light defined yet\n"); |
---|
| 239 | return; |
---|
| 240 | } |
---|
| 241 | |
---|
[3437] | 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; |
---|
[3436] | 246 | |
---|
[3437] | 247 | glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor); |
---|
[3436] | 248 | } |
---|
| 249 | |
---|
| 250 | // get Attributes |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | \returns the Position of the Light |
---|
| 254 | */ |
---|
| 255 | Vector Light::getPosition(void) |
---|
| 256 | { |
---|
[3438] | 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]); |
---|
[3436] | 264 | } |
---|