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