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