[1853] | 1 | |
---|
| 2 | |
---|
[4597] | 3 | /* |
---|
[1853] | 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 | |
---|
[5357] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
[1853] | 19 | |
---|
[3436] | 20 | #include "light.h" |
---|
[1853] | 21 | |
---|
[3436] | 22 | #include "glincl.h" |
---|
[3608] | 23 | #include "vector.h" |
---|
[5944] | 24 | #include "parser/tinyxml/tinyxml.h" |
---|
[7193] | 25 | #include "util/loading/load_param.h" |
---|
| 26 | #include "util/loading/factory.h" |
---|
[5129] | 27 | #include "debug.h" |
---|
[1853] | 28 | |
---|
[9716] | 29 | #include "class_id_DEPRECATED.h" |
---|
[9685] | 30 | |
---|
[9715] | 31 | ObjectListDefinitionID(Light, CL_LIGHT); |
---|
[9709] | 32 | CREATE_FACTORY(Light); |
---|
[4734] | 33 | |
---|
[4738] | 34 | //! Definition of the Lights and their Names |
---|
[4734] | 35 | int lightsV[] = |
---|
[8742] | 36 | { |
---|
| 37 | GL_LIGHT0, |
---|
| 38 | GL_LIGHT1, |
---|
| 39 | GL_LIGHT2, |
---|
| 40 | GL_LIGHT3, |
---|
| 41 | GL_LIGHT4, |
---|
| 42 | GL_LIGHT5, |
---|
| 43 | GL_LIGHT6, |
---|
| 44 | GL_LIGHT7 |
---|
| 45 | }; |
---|
[1856] | 46 | |
---|
[3598] | 47 | /** |
---|
[4836] | 48 | * @param root The XML-element to load the Light from |
---|
[4738] | 49 | |
---|
[4836] | 50 | @todo what to do, if no Light-Slots are open anymore ??? |
---|
[4734] | 51 | */ |
---|
[8742] | 52 | Light::Light(const TiXmlElement* root) |
---|
[4734] | 53 | { |
---|
[4738] | 54 | PRINTF(4)("initializing Light number %d.\n", this->lightNumber); |
---|
| 55 | |
---|
[9685] | 56 | this->registerObject(this, Light::_objectList); |
---|
| 57 | |
---|
[4736] | 58 | this->lightNumber = LightManager::getInstance()->registerLight(this); |
---|
| 59 | |
---|
| 60 | char tmpName[10]; |
---|
| 61 | sprintf(tmpName, "Light[%d]", this->lightNumber); |
---|
| 62 | this->setName(tmpName); |
---|
| 63 | |
---|
| 64 | // enable The light |
---|
| 65 | glEnable(lightsV[this->lightNumber]); // postSpawn |
---|
| 66 | |
---|
| 67 | // set values (defaults) |
---|
| 68 | this->setDiffuseColor(1.0, 1.0, 1.0); |
---|
| 69 | this->setSpecularColor(1.0, 1.0, 1.0); |
---|
| 70 | |
---|
[5156] | 71 | if (root != NULL) |
---|
| 72 | this->loadParams(root); |
---|
[4734] | 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
[4836] | 76 | * destroys a Light |
---|
[4734] | 77 | */ |
---|
[4746] | 78 | Light::~Light() |
---|
[4734] | 79 | { |
---|
| 80 | glDisable(lightsV[this->lightNumber]); |
---|
[4736] | 81 | |
---|
| 82 | LightManager::getInstance()->unregisterLight(this); |
---|
[4734] | 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
[4836] | 86 | * @param root The XML-element to load the Light from |
---|
[4734] | 87 | */ |
---|
| 88 | void Light::loadParams(const TiXmlElement* root) |
---|
[3597] | 89 | { |
---|
[6512] | 90 | PNode::loadParams(root); |
---|
[4734] | 91 | |
---|
[5671] | 92 | LoadParam(root, "diffuse-color", this, Light, setDiffuseColor) |
---|
[8742] | 93 | .describe("sets the diffuse color of the Light (red [0-1], green [0-1], blue [0-1])"); |
---|
[4734] | 94 | |
---|
[5671] | 95 | LoadParam(root, "specular-color", this, Light, setSpecularColor) |
---|
[8742] | 96 | .describe("sets the specular color of the Light (red [0-1], green [0-1], blue [0-1])"); |
---|
[4734] | 97 | |
---|
[5671] | 98 | LoadParam(root, "attenuation", this, Light, setAttenuation) |
---|
[8742] | 99 | .describe("sets the Attenuation of the LightSource (constant Factor, linear Factor, quadratic Factor)."); |
---|
[4738] | 100 | |
---|
[5671] | 101 | LoadParam(root, "spot-direction", this, Light, setSpotDirection) |
---|
[8742] | 102 | .describe("sets the Direction of the Spot"); |
---|
[4734] | 103 | |
---|
[5671] | 104 | LoadParam(root, "spot-cutoff", this, Light, setSpotCutoff) |
---|
[8742] | 105 | .describe("the cuttoff of the Spotlight"); |
---|
[3597] | 106 | } |
---|
| 107 | |
---|
[3245] | 108 | /** |
---|
[4836] | 109 | * sets an emitting Diffuse color of this Light |
---|
| 110 | * @param r red |
---|
| 111 | * @param g green |
---|
| 112 | * @param b blue |
---|
[3597] | 113 | */ |
---|
| 114 | void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b) |
---|
| 115 | { |
---|
| 116 | this->diffuseColor[0] = r; |
---|
| 117 | this->diffuseColor[1] = g; |
---|
| 118 | this->diffuseColor[2] = b; |
---|
| 119 | this->diffuseColor[3] = 1.0; |
---|
| 120 | |
---|
[3598] | 121 | glLightfv (lightsV[this->lightNumber], GL_DIFFUSE, this->diffuseColor); |
---|
[3597] | 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
[4836] | 125 | * sets an emitting Specular color of this Light |
---|
| 126 | * @param r red |
---|
| 127 | * @param g green |
---|
| 128 | * @param b blue |
---|
[3597] | 129 | */ |
---|
| 130 | void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b) |
---|
| 131 | { |
---|
| 132 | this->specularColor[0] = r; |
---|
| 133 | this->specularColor[1] = g; |
---|
| 134 | this->specularColor[2] = b; |
---|
| 135 | this->specularColor[3] = 1.0; |
---|
| 136 | |
---|
[3598] | 137 | glLightfv (lightsV[this->lightNumber], GL_SPECULAR, this->specularColor); |
---|
[3597] | 138 | } |
---|
| 139 | |
---|
[4471] | 140 | |
---|
[3597] | 141 | /** |
---|
[4836] | 142 | * Sets the AttenuationType of this Light Source |
---|
| 143 | * @param constantAttenuation The Constant Attenuation of the Light |
---|
| 144 | * @param linearAttenuation The Linear Attenuation of the Light |
---|
| 145 | * @param quadraticAttenuation The Quadratic Attenuation of the Light |
---|
[3597] | 146 | */ |
---|
| 147 | void Light::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation) |
---|
| 148 | { |
---|
| 149 | this->constantAttenuation = constantAttenuation; |
---|
| 150 | this->linearAttenuation = linearAttenuation; |
---|
| 151 | this->quadraticAttenuation = quadraticAttenuation; |
---|
| 152 | |
---|
| 153 | glLightf(lightsV[this->lightNumber], GL_CONSTANT_ATTENUATION, constantAttenuation); |
---|
| 154 | glLightf(lightsV[this->lightNumber], GL_LINEAR_ATTENUATION, linearAttenuation); |
---|
| 155 | glLightf(lightsV[this->lightNumber], GL_QUADRATIC_ATTENUATION, quadraticAttenuation); |
---|
| 156 | } |
---|
| 157 | |
---|
[4471] | 158 | |
---|
[3597] | 159 | /** |
---|
[4836] | 160 | * stets the direction of the Spot Light. |
---|
| 161 | * @param direction The direction of the Spot Light. |
---|
[3597] | 162 | */ |
---|
[4734] | 163 | void Light::setSpotDirection(const Vector& direction) |
---|
[3597] | 164 | { |
---|
| 165 | this->spotDirection[0] = direction.x; |
---|
| 166 | this->spotDirection[1] = direction.y; |
---|
| 167 | this->spotDirection[2] = direction.z; |
---|
| 168 | |
---|
| 169 | glLightfv(lightsV[this->lightNumber], GL_SPOT_DIRECTION, this->spotDirection); |
---|
| 170 | } |
---|
| 171 | |
---|
[4471] | 172 | |
---|
[3597] | 173 | /** |
---|
[4836] | 174 | * sets the cutoff angle of the Light. |
---|
| 175 | * @param cutoff The cutoff angle. |
---|
[3597] | 176 | */ |
---|
| 177 | void Light::setSpotCutoff(GLfloat cutoff) |
---|
| 178 | { |
---|
| 179 | this->spotCutoff = cutoff; |
---|
| 180 | glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff); |
---|
| 181 | } |
---|
| 182 | |
---|
[3598] | 183 | /** |
---|
[4836] | 184 | * draws this Light. Being a World-entity the possibility to do this lies at hand. |
---|
[4597] | 185 | */ |
---|
[4746] | 186 | void Light::draw() const |
---|
[3597] | 187 | { |
---|
[3602] | 188 | float pos[4] = {this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z, 1.0}; |
---|
| 189 | PRINTF(4)("Drawing The Lights new Position at %f %f %f\n", pos[0], pos[1], pos[2]); |
---|
[3597] | 190 | glLightfv(lightsV[this->lightNumber], GL_POSITION, pos); |
---|
| 191 | } |
---|
| 192 | |
---|
[4471] | 193 | |
---|
[3598] | 194 | /** |
---|
[4836] | 195 | * Prints out some nice formated debug information about the Light |
---|
[3598] | 196 | */ |
---|
[4746] | 197 | void Light::debug() const |
---|
[3597] | 198 | { |
---|
| 199 | PRINT(0)(":: %d :: -- reference %p\n", this->lightNumber, this); |
---|
| 200 | PRINT(0)(" GL-state: "); |
---|
[4597] | 201 | GLboolean param; |
---|
[3597] | 202 | glGetBooleanv(lightsV[this->lightNumber], ¶m); |
---|
| 203 | if (param) |
---|
| 204 | PRINT(0)("ON\n"); |
---|
| 205 | else |
---|
| 206 | PRINT(0)("OFF\n"); |
---|
[4597] | 207 | |
---|
[3597] | 208 | PRINT(0)(" DiffuseColor: %f/%f/%f\n", this->diffuseColor[0], this->diffuseColor[1], this->diffuseColor[2]); |
---|
| 209 | PRINT(0)(" SpecularColor: %f/%f/%f\n", this->specularColor[0], this->specularColor[1], this->specularColor[2]); |
---|
| 210 | PRINT(0)(" Attenuation: constant=%f linear=%f quadratic=%f\n", this->constantAttenuation, this->linearAttenuation, this->quadraticAttenuation); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | /****************** |
---|
| 215 | ** LIGHT-MANAGER ** |
---|
| 216 | ******************/ |
---|
[9715] | 217 | ObjectListDefinition(LightManager); |
---|
[3597] | 218 | /** |
---|
[4836] | 219 | * standard constructor for a Light |
---|
[3245] | 220 | */ |
---|
[4597] | 221 | LightManager::LightManager () |
---|
[3365] | 222 | { |
---|
[9685] | 223 | this->registerObject(this, LightManager::_objectList); |
---|
[1853] | 224 | |
---|
[3437] | 225 | glEnable (GL_LIGHTING); |
---|
[6763] | 226 | glEnable ( GL_COLOR_MATERIAL ) ; |
---|
[6764] | 227 | glColorMaterial ( GL_FRONT, GL_DIFFUSE ) ; |
---|
[6763] | 228 | |
---|
[3440] | 229 | this->setAmbientColor(.3, .3, .3); |
---|
[3597] | 230 | this->lights = new Light*[NUMBEROFLIGHTS]; |
---|
[3437] | 231 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 232 | lights[i] = NULL; |
---|
[3438] | 233 | this->currentLight = NULL; |
---|
[3436] | 234 | } |
---|
[1853] | 235 | |
---|
[3245] | 236 | /** |
---|
[4836] | 237 | * standard deconstructor |
---|
[4597] | 238 | |
---|
[3446] | 239 | first disables Lighting |
---|
[3598] | 240 | |
---|
[3446] | 241 | then deletes the rest of the allocated memory |
---|
| 242 | and in the end sets the singleton Reference to zero. |
---|
[3245] | 243 | */ |
---|
[4597] | 244 | LightManager::~LightManager () |
---|
[3436] | 245 | { |
---|
[6763] | 246 | glDisable(GL_COLOR_MATERIAL); |
---|
[3436] | 247 | glDisable(GL_LIGHTING); |
---|
[4739] | 248 | this->setAmbientColor(.0,.0,.0); |
---|
[4597] | 249 | |
---|
[4737] | 250 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
[5212] | 251 | if (this->lights[i] != NULL) |
---|
[4737] | 252 | delete lights[i]; |
---|
[5211] | 253 | delete[] lights; |
---|
[3597] | 254 | LightManager::singletonRef = NULL; |
---|
[3436] | 255 | } |
---|
[1853] | 256 | |
---|
[3438] | 257 | /** |
---|
[4836] | 258 | * singleton-Reference to the Light-class |
---|
[3438] | 259 | */ |
---|
[3597] | 260 | LightManager* LightManager::singletonRef = NULL; |
---|
[3437] | 261 | |
---|
[3436] | 262 | /** |
---|
[4836] | 263 | * @param root the XML-element to load the LightManager's settings from |
---|
[4735] | 264 | */ |
---|
| 265 | void LightManager::loadParams(const TiXmlElement* root) |
---|
| 266 | { |
---|
[5653] | 267 | LoadParamXML(root, "Lights", this, LightManager, loadLights) |
---|
[8742] | 268 | .describe("an XML-Element to load lights from."); |
---|
[4471] | 269 | |
---|
[5671] | 270 | LoadParam(root, "ambient-color", this, LightManager, setAmbientColor) |
---|
[8742] | 271 | .describe("sets the ambient Color of the Environmental Light"); |
---|
[4735] | 272 | } |
---|
| 273 | |
---|
[3437] | 274 | /** |
---|
[4836] | 275 | * @param root The XML-element to load Lights from |
---|
[4735] | 276 | */ |
---|
| 277 | void LightManager::loadLights(const TiXmlElement* root) |
---|
| 278 | { |
---|
| 279 | const TiXmlElement* element = root->FirstChildElement(); |
---|
| 280 | |
---|
| 281 | while (element != NULL) |
---|
| 282 | { |
---|
[5982] | 283 | Factory::fabricate(element); |
---|
[4735] | 284 | |
---|
| 285 | element = element->NextSiblingElement(); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
[3436] | 289 | // set Attributes |
---|
[3245] | 290 | /** |
---|
[4836] | 291 | * sets the ambient Color of the Scene |
---|
| 292 | * @param r red |
---|
| 293 | * @param g green |
---|
| 294 | * @param b blue |
---|
[3436] | 295 | */ |
---|
[3597] | 296 | void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b) |
---|
[3436] | 297 | { |
---|
[3597] | 298 | this->ambientColor[0] = r; |
---|
| 299 | this->ambientColor[1] = g; |
---|
| 300 | this->ambientColor[2] = b; |
---|
| 301 | this->ambientColor[3] = 1.0; |
---|
[3245] | 302 | |
---|
[3597] | 303 | glLightfv (GL_LIGHT0, GL_AMBIENT, this->ambientColor); |
---|
[3437] | 304 | } |
---|
[3436] | 305 | |
---|
[4738] | 306 | /** |
---|
[4836] | 307 | * @param light the Light to register to the LightManager |
---|
[4738] | 308 | |
---|
| 309 | This is done explicitely by the constructor of a Light |
---|
| 310 | */ |
---|
[4736] | 311 | int LightManager::registerLight(Light* light) |
---|
[3436] | 312 | { |
---|
[4736] | 313 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 314 | if (!this->lights[i]) |
---|
[8742] | 315 | { |
---|
| 316 | this->lights[i]=light; |
---|
| 317 | return i; |
---|
| 318 | } |
---|
[4736] | 319 | PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS); |
---|
| 320 | return -1; |
---|
[3436] | 321 | } |
---|
[3598] | 322 | |
---|
[4738] | 323 | /** |
---|
[4836] | 324 | * @param light The light to unregister from the LightManager |
---|
[4738] | 325 | |
---|
| 326 | This is done every time a Light is destroyed explicitely by the Light-destructor |
---|
| 327 | */ |
---|
[4736] | 328 | void LightManager::unregisterLight(Light* light) |
---|
[3441] | 329 | { |
---|
[4736] | 330 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 331 | { |
---|
| 332 | if (this->lights[i] == light) |
---|
[4597] | 333 | { |
---|
[4737] | 334 | this->lights[i] = NULL; |
---|
| 335 | return; |
---|
[3599] | 336 | } |
---|
[4736] | 337 | } |
---|
| 338 | PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)"); |
---|
| 339 | return; |
---|
[3441] | 340 | } |
---|
[3598] | 341 | |
---|
[6884] | 342 | |
---|
| 343 | Light* LightManager::getLight(int lightNumber) const |
---|
| 344 | { |
---|
| 345 | if( lightNumber < NUMBEROFLIGHTS) |
---|
| 346 | return this->lights[lightNumber]; |
---|
| 347 | |
---|
| 348 | return NULL; |
---|
| 349 | } |
---|
| 350 | |
---|
[3598] | 351 | /** |
---|
[4836] | 352 | * draws all the Lights in their appropriate position |
---|
[4736] | 353 | */ |
---|
| 354 | void LightManager::draw() const |
---|
[3440] | 355 | { |
---|
[8742] | 356 | PRINTF(4)("Drawing the Lights\n"); |
---|
[4736] | 357 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
[7109] | 358 | if (this->lights[i] != NULL) |
---|
[4736] | 359 | lights[i]->draw(); |
---|
[3440] | 360 | } |
---|
[3598] | 361 | |
---|
| 362 | /** |
---|
[4836] | 363 | * outputs debug information about the Class and its lights |
---|
[3442] | 364 | */ |
---|
[4746] | 365 | void LightManager::debug() const |
---|
[3442] | 366 | { |
---|
| 367 | PRINT(0)("=================================\n"); |
---|
| 368 | PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n"); |
---|
| 369 | PRINT(0)("=================================\n"); |
---|
[3597] | 370 | PRINT(0)("Reference: %p\n", LightManager::singletonRef); |
---|
[3442] | 371 | if (this->currentLight) |
---|
[3598] | 372 | PRINT(0)("current Light Nr: %d\n", this->currentLight->getLightNumber()); |
---|
[3442] | 373 | PRINT(0)("Ambient Color: %f:%f:%f\n", this->ambientColor[0], this->ambientColor[0], this->ambientColor[0]); |
---|
| 374 | PRINT(0)("=== Lights ===\n"); |
---|
| 375 | for (int i = 0; i < NUMBEROFLIGHTS; i++) |
---|
| 376 | if (this->lights[i]) |
---|
[8742] | 377 | { |
---|
| 378 | this->lights[i]->debug(); |
---|
| 379 | } |
---|
[4738] | 380 | PRINT(0)("-----------------------------LM-\n"); |
---|
[3442] | 381 | } |
---|