Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4736 in orxonox.OLD for orxonox/trunk/src/lib/graphics/light.cc


Ignore:
Timestamp:
Jun 30, 2005, 1:06:53 AM (20 years ago)
Author:
bensch
Message:

orxonox/trunk: cleaned up LightManager, this was quite messy….

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/graphics/light.cc

    r4735 r4736  
    4545
    4646/**
    47    \param lightNumber the Light Number to initiate
    48 */
    49 Light::Light(int lightNumber)
    50 {
    51   this->init(lightNumber);
    52 }
    53 
    54 /**
    5547 * \param root The XML-element to load the Light from
    5648 */
    5749 Light::Light(const TiXmlElement* root)
    5850{
    59   this->init(1);
    60   this->loadParams(root);
    61 }
    62 
    63 /**
    64    \brief destroys a Light
    65 */
    66 Light::~Light(void)
    67 {
    68   glDisable(lightsV[this->lightNumber]);
    69 }
    70 
    71 /**
    72  * \brief initializes a Light
    73  */
    74 void Light::init(int lightNumber)
    75 {
     51  this->lightNumber = LightManager::getInstance()->registerLight(this);
     52
    7653  this->setClassID(CL_LIGHT, "Light");
    7754  char tmpName[10];
    78   sprintf(tmpName, "Light[%d]", lightNumber);
     55  sprintf(tmpName, "Light[%d]", this->lightNumber);
    7956  this->setName(tmpName);
    8057
    81   PRINTF(4)("initializing Light number %d.\n", lightNumber);
     58  PRINTF(4)("initializing Light number %d.\n", this->lightNumber);
    8259  // enable The light
    83   glEnable(lightsV[lightNumber]); // postSpawn
     60  glEnable(lightsV[this->lightNumber]); // postSpawn
    8461
    8562  // set values (defaults)
    86   this->lightNumber = lightNumber;
    8763  this->setDiffuseColor(1.0, 1.0, 1.0);
    8864  this->setSpecularColor(1.0, 1.0, 1.0);
     65
     66  this->loadParams(root);
     67
     68}
     69
     70/**
     71   \brief destroys a Light
     72*/
     73Light::~Light(void)
     74{
     75  glDisable(lightsV[this->lightNumber]);
     76
     77  LightManager::getInstance()->unregisterLight(this);
     78}
     79
     80/**
     81 * \brief initializes a Light
     82 */
     83void Light::init()
     84{
    8985}
    9086
     
    187183   \brief draws this Light. Being a World-entity the possibility to do this lies at hand.
    188184*/
    189 void Light::draw()
     185void Light::draw() const
    190186{
    191187  float pos[4] = {this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z, 1.0};
     
    245241  glDisable(GL_LIGHTING);
    246242
    247   // this will be done either by worldEntity, or by pNode as each light is one of them
     243  // this will be done either by PNode-tree as each light is one of them
    248244  //  for (int i = 0; i < NUMBEROFLIGHTS; i++)
    249245  //    this->deleteLight(i);
     
    252248}
    253249
    254 
    255250/**
    256251   \brief singleton-Reference to the Light-class
    257252*/
    258253LightManager* LightManager::singletonRef = NULL;
    259 
    260 /**
    261    \brief initializes a new Light with default values, and enables GL_LIGHTING
    262 */
    263 void LightManager::initLight(int lightNumber)
    264 {
    265   lights[lightNumber] = new Light(lightNumber);
    266 }
    267254
    268255/**
     
    292279  }
    293280}
    294 
    295 /**
    296    \brief Adds a new Light, by selecting the First free slot.
    297 
    298    if no slot is free error
    299 */
    300 int LightManager::addLight(void)
    301 {
    302   for (int i = 0; i < NUMBEROFLIGHTS; i++)
    303     if (!this->lights[i])
    304       return addLight(i);
    305   PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
    306   return -1;
    307 }
    308 
    309 
    310 /**
    311    \brief Adds a new Light
    312    \param lightNumber The number of the light to add.
    313 
    314    if the Number is not free: warn, automatically choose another slot.
    315 */
    316 int LightManager::addLight(int lightNumber)
    317 {
    318   if (this->lights[lightNumber])
    319     {
    320       PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
    321       return this->addLight();
    322     }
    323   this->initLight(lightNumber);
    324   this->currentLight = this->lights[lightNumber];
    325   return lightNumber;
    326 }
    327 
    328 
    329 /**
    330    \brief select the light to work with
    331    \param lightNumber the light to work with
    332 */
    333 void LightManager::editLightNumber(int lightNumber)
    334 {
    335   if (!this->currentLight)
    336     {
    337       PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    338       return;
    339     }
    340   this->currentLight = lights[lightNumber];
    341 }
    342 
    343 
    344 /**
    345    \brief Delete the current Light
    346 */
    347 void LightManager::deleteLight(void)
    348 {
    349   if (!this->currentLight)
    350     {
    351       PRINTF(1)("no Light defined yet. So you cannot delete any Light right now.\n");
    352       return;
    353     }
    354 
    355   this->deleteLight(this->currentLight->getLightNumber());
    356 }
    357 
    358 
    359 /**
    360    \brief delete light.
    361    \param lightNumber the number of the light to delete
    362 */
    363 void LightManager::deleteLight(int lightNumber)
    364 {
    365   if (this->lights[lightNumber])
    366     {
    367       PRINTF(4)("deleting Light number %d\n", lightNumber);
    368       delete this->lights[lightNumber];
    369       this->lights[lightNumber] = NULL;
    370     }
    371 }
    372 
    373 
    374 /**
    375    \brief draws all the Lights in their appropriate position
    376 */
    377 void LightManager::draw()
    378 {
    379   glMatrixMode(GL_MODELVIEW);
    380   glLoadIdentity();
    381   PRINTF(4)("Drawing the Lights\n");
    382   for (int i = 0; i < NUMBEROFLIGHTS; i++)
    383     if (this->lights[i])
    384       lights[i]->draw();
    385 }
    386 
    387 
    388281
    389282// set Attributes
     
    404297}
    405298
    406 /**
    407    \brief sets an emitting Diffuse color of the currently selected Light
    408    \param r red
    409    \param g green
    410    \param b blue
    411 */
    412 void LightManager::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
    413 {
    414   if (!this->currentLight)
     299int LightManager::registerLight(Light* light)
     300{
     301  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     302    if (!this->lights[i])
     303  {
     304    this->lights[i]=light;
     305    return i;
     306  }
     307  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
     308  return -1;
     309}
     310
     311void LightManager::unregisterLight(Light* light)
     312{
     313  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     314  {
     315    if (this->lights[i] == light)
    415316    {
    416       PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    417       return;
     317      this->lights[i] == NULL;
    418318    }
    419   this->currentLight->setDiffuseColor(r,g,b);
    420 }
    421 
    422 
    423 /**
    424    \brief sets an emitting Specular color of the currently selected Light
    425    \param r red
    426    \param g green
    427    \param b blue
    428 */
    429 void LightManager::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
    430 {
    431   if (!this->currentLight)
    432     {
    433       PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    434       return;
    435     }
    436   this->currentLight->setSpecularColor(r, g, b);
    437 }
    438 
    439 
    440 /**
    441    \brief Sets the AttenuationType of th currently selecte LightSource Light Source
    442    \param constantAttenuation The Constant Attenuation of the Light
    443    \param linearAttenuation The Linear Attenuation of the Light
    444    \param quadraticAttenuation The Quadratic Attenuation of the Light
    445 */
    446 void LightManager::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
    447 {
    448   if (!this->currentLight)
    449     {
    450       PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    451       return;
    452     }
    453   this->currentLight->setAttenuation(constantAttenuation, linearAttenuation, quadraticAttenuation);
    454 }
    455 
    456 
    457 /**
    458    \brief stets the direction of the Spot Light.
    459    \param direction The direction of the Spot Light.
    460 */
    461 void LightManager::setSpotDirection(Vector direction)
    462 {
    463   if (!this->currentLight)
    464     {
    465       PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    466       return;
    467     }
    468   this->currentLight->setSpotDirection(direction);
    469 }
    470 
    471 
    472 /**
    473    \brief sets the cutoff angle of the Light.
    474    \param cutoff The cutoff angle.
    475 */
    476 void LightManager::setSpotCutoff(GLfloat cutoff)
    477 {
    478   if (!this->currentLight)
    479     {
    480       PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    481       return;
    482     }
    483   this->currentLight->setSpotCutoff(cutoff);
    484 }
    485 
    486 /**
    487    \returns a pointer to a Light
    488    \param lightNumber The light to request the pointer from
    489 */
    490 Light* LightManager::getLight(int lightNumber) const
    491 {
    492   return this->lights[lightNumber];
    493 }
    494 
     319  }
     320  PRINTF(2)("Light %p could not be unloaded (this should not heappen\n)");
     321  return;
     322}
     323
     324/**
     325   \brief draws all the Lights in their appropriate position
     326 */
     327void LightManager::draw() const
     328{
     329  glMatrixMode(GL_MODELVIEW);
     330  glLoadIdentity();
     331  PRINTF(4)("Drawing the Lights\n");
     332  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     333    if (this->lights[i])
     334      lights[i]->draw();
     335}
    495336
    496337/**
Note: See TracChangeset for help on using the changeset viewer.