Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3597 in orxonox.OLD for orxonox/trunk/src/light.cc


Ignore:
Timestamp:
Mar 17, 2005, 11:04:59 PM (20 years ago)
Author:
bensch
Message:

orxonox/trunk: Light is now a World-entity.

File:
1 edited

Legend:

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

    r3590 r3597  
    2727int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
    2828
    29 
     29Light::Light(int lightNumber)
     30{
     31  PRINTF(4)("initializing Light number %d.\n", lightNumber);
     32  // enable The light
     33  glEnable(lightsV[lightNumber]); // postSpawn
     34 
     35  // set values (defaults)
     36  this->lightNumber = lightNumber;
     37  this->setPosition(0.0, 0.0, 0.0);
     38  this->setDiffuseColor(1.0, 1.0, 1.0);
     39  this->setSpecularColor(1.0, 1.0, 1.0);
     40}
     41
     42Light::~Light(void)
     43{
     44  glDisable(lightsV[this->lightNumber]);
     45}
     46
     47/**
     48   \brief Sets a Position for the Light.
     49   \param position The new position of the Light.
     50   \todo patrick: is it ok to set a Light Position even if it is derived from p_node??
     51*/
     52void Light::setPosition(Vector position)
     53{
     54  this->lightPosition[0] = position.x;
     55  this->lightPosition[1] = position.y;
     56  this->lightPosition[2] = position.z;
     57  this->lightPosition[3] = 0.0;
     58
     59  glLightfv (GL_LIGHT0, GL_POSITION, this->lightPosition);
     60}
     61
     62/**
     63   \brief Sets a Position for the Light.
     64   \param x the x-coordinate
     65   \param y the y-coordinate
     66   \param z the z-coordinate
     67*/
     68void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
     69{
     70  this->lightPosition[0] = x;
     71  this->lightPosition[1] = y;
     72  this->lightPosition[2] = z;
     73  this->lightPosition[3] = 0.0;
     74
     75  glLightfv (GL_LIGHT0, GL_POSITION, this->lightPosition);
     76}
     77
     78/**
     79   \brief sets an emitting Diffuse color for the Light
     80   \param r red
     81   \param g green
     82   \param b blue
     83*/
     84void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
     85{
     86  this->diffuseColor[0] = r;
     87  this->diffuseColor[1] = g;
     88  this->diffuseColor[2] = b;
     89  this->diffuseColor[3] = 1.0;
     90
     91  glLightfv (GL_LIGHT0, GL_DIFFUSE, this->diffuseColor);
     92}
     93
     94/**
     95   \brief sets an emitting Ambient color for the Light
     96   \param r red
     97   \param g green
     98   \param b blue
     99*/
     100void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
     101{
     102  this->specularColor[0] = r;
     103  this->specularColor[1] = g;
     104  this->specularColor[2] = b;
     105  this->specularColor[3] = 1.0;
     106
     107  glLightfv (GL_LIGHT0, GL_SPECULAR, this->specularColor);
     108}
     109
     110/**
     111   \brief Sets the AttenuationType of this Light Source
     112   \param type the AttenuationType to set
     113   \param factor the Factor to multipy the attenuation with
     114
     115   this actually just sets the following: glLightf(currentLight, type, factor)
     116*/
     117void Light::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
     118{
     119  this->constantAttenuation  = constantAttenuation;
     120  this->linearAttenuation    = linearAttenuation;
     121  this->quadraticAttenuation = quadraticAttenuation;
     122
     123  glLightf(lightsV[this->lightNumber], GL_CONSTANT_ATTENUATION,  constantAttenuation);
     124  glLightf(lightsV[this->lightNumber], GL_LINEAR_ATTENUATION,    linearAttenuation);
     125  glLightf(lightsV[this->lightNumber], GL_QUADRATIC_ATTENUATION, quadraticAttenuation);
     126}
     127
     128/**
     129   \brief stets the direction of the Spot Light.
     130   \param direction The direction of the Spot Light.
     131*/
     132void Light::setSpotDirection(Vector direction)
     133{
     134  this->spotDirection[0] = direction.x;
     135  this->spotDirection[1] = direction.y;
     136  this->spotDirection[2] = direction.z;
     137
     138  glLightfv(lightsV[this->lightNumber], GL_SPOT_DIRECTION, this->spotDirection);
     139}
     140
     141
     142/**
     143   \brief sets the cutoff angle of the Light.
     144   \param cutoff The cutoff angle.
     145*/
     146void Light::setSpotCutoff(GLfloat cutoff)
     147{
     148  this->spotCutoff = cutoff;
     149  glLightf(lightsV[this->lightNumber], GL_SPOT_CUTOFF, cutoff);
     150}
     151
     152void Light::draw()
     153{
     154  float pos[3] = {this->getAbsCoor ().x, this->getAbsCoor().y, this->getAbsCoor().z};
     155  glLightfv(lightsV[this->lightNumber], GL_POSITION, pos);
     156}
     157
     158void Light::debug(void)
     159{
     160  PRINT(0)(":: %d ::  -- reference %p\n", this->lightNumber, this);
     161  PRINT(0)(" GL-state: ");
     162  GLboolean param;
     163  glGetBooleanv(lightsV[this->lightNumber], &param);
     164  if (param)
     165    PRINT(0)("ON\n");
     166  else
     167    PRINT(0)("OFF\n");
     168 
     169  PRINT(0)(" Position:      %f/%f/%f\n", this->lightPosition[0], this->lightPosition[1], this->lightPosition[2]);
     170  PRINT(0)(" DiffuseColor:  %f/%f/%f\n", this->diffuseColor[0], this->diffuseColor[1], this->diffuseColor[2]);
     171  PRINT(0)(" SpecularColor: %f/%f/%f\n", this->specularColor[0], this->specularColor[1], this->specularColor[2]);
     172  PRINT(0)(" Attenuation: constant=%f linear=%f quadratic=%f\n", this->constantAttenuation, this->linearAttenuation, this->quadraticAttenuation);
     173}
     174
     175
     176/******************
     177** LIGHT-MANAGER **
     178******************/
    30179/**
    31180   \brief standard constructor for a Light
    32181*/
    33 Light::Light ()
    34 {
    35   this->setClassName ("Light");
     182LightManager::LightManager ()
     183{
     184  this->setClassName ("LightManager");
    36185
    37186  glEnable (GL_LIGHTING);
    38187  this->setAmbientColor(.3, .3, .3);
    39   this->lights = new LightValue*[NUMBEROFLIGHTS];
     188  this->lights = new Light*[NUMBEROFLIGHTS];
    40189  for (int i = 0; i < NUMBEROFLIGHTS; i++)
    41190    lights[i] = NULL;
     
    51200   and in the end sets the singleton Reference to zero.
    52201*/
    53 Light::~Light ()
     202LightManager::~LightManager ()
    54203{
    55204  glDisable(GL_LIGHTING);
    56205 
    57   for (int i = 0; i < NUMBEROFLIGHTS; i++)
    58     this->deleteLight(i);
     206  // this will be done either by worldEntity, or by pNode as each light is one of them
     207  //  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     208  //    this->deleteLight(i);
    59209  delete lights;
    60   Light::singletonRef = NULL;
     210  LightManager::singletonRef = NULL;
    61211}
    62212
     
    64214   \brief singleton-Reference to the Light-class
    65215*/
    66 Light* Light::singletonRef = NULL;
     216LightManager* LightManager::singletonRef = NULL;
    67217
    68218/**
    69219   \returns The Instance of the Lights
    70220*/
    71 Light* Light::getInstance(void)
    72 {
    73   if (singletonRef)
    74     return singletonRef;
    75   else
    76     return Light::singletonRef = new Light();
     221LightManager* LightManager::getInstance(void)
     222{
     223  if (!singletonRef)
     224    LightManager::singletonRef = new LightManager();
     225  return singletonRef;
    77226}
    78227
     
    80229   \brief initializes a new Light with default values, and enables GL_LIGHTING
    81230*/
    82 void Light::init(int lightNumber)
    83 {
    84   PRINTF(3)("initializing Light number %d.\n", lightNumber);
    85   // enable The light
    86   glEnable(lightsV[lightNumber]);
    87   this->currentLight = lights[lightNumber] = new LightValue;
    88  
    89   // set default values
    90   this->currentLight->lightNumber = lightNumber;
    91   this->setPosition(0.0, 0.0, 0.0);
    92   this->setDiffuseColor(1.0, 1.0, 1.0);
    93   this->setSpecularColor(1.0, 1.0, 1.0);
     231void LightManager::initLight(int lightNumber)
     232{
     233  lights[lightNumber] = new Light(lightNumber);
    94234}
    95235
     
    99239   if no slot is free error
    100240*/
    101 int Light::addLight(void)
     241int LightManager::addLight(void)
    102242{
    103243  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     
    114254   if the Number is not free: warn, automatically choose another slot.
    115255*/
    116 int Light::addLight(int lightNumber)
     256int LightManager::addLight(int lightNumber)
    117257{
    118258  if (this->lights[lightNumber])
     
    121261      return this->addLight();
    122262    }
    123   this->init(lightNumber);
     263  this->initLight(lightNumber);
    124264  this->currentLight = this->lights[lightNumber];
    125265  return lightNumber;
     
    130270   \param lightNumber the light to work with
    131271*/
    132 void Light::editLightNumber(int lightNumber)
     272void LightManager::editLightNumber(int lightNumber)
    133273{
    134274  if (!this->currentLight)
    135275    {
    136       PRINTF(1)("no Light defined yet\n");
     276      PRINTF(2)("no Light defined yet. Please define at least one light first befor editing.\n");
    137277      return;
    138278    }
    139 
    140279  this->currentLight = lights[lightNumber];
    141280}
     
    144283   \brief Delete the current Light
    145284*/
    146 void Light::deleteLight(void)
     285void LightManager::deleteLight(void)
    147286{
    148287  if (!this->currentLight)
    149288    {
    150       PRINTF(1)("no Light defined yet\n");
     289      PRINTF(1)("no Light defined yet. So you cannot delete any Light right now.\n");
    151290      return;
    152291    }
     
    159298   \param lightNumber the number of the light to delete
    160299*/
    161 void Light::deleteLight(int lightNumber)
     300void LightManager::deleteLight(int lightNumber)
    162301{
    163302  if (this->lights[lightNumber])
    164303    {
    165       PRINTF(3)("deleting Light number %d\n", lightNumber);
     304      PRINTF(4)("deleting Light number %d\n", lightNumber);
    166305      delete this->lights[lightNumber];
    167       glDisable(lightsV[lightNumber]);
    168306      this->lights[lightNumber] = NULL;
    169307    }
     
    171309
    172310// set Attributes
    173 /**
    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 {
    180   if (!this->currentLight)
    181     {
    182       PRINTF(1)("no Light defined yet\n");
    183       return;
    184     }
    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;
    189 
    190   glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
    191 }
    192 
    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 */
    199 void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
    200 {
    201   if (!this->currentLight)
    202     {
    203       PRINTF(1)("no Light defined yet\n");
    204       return;
    205     }
    206 
    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);
    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
    220 */
    221 void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
    222 {
    223   if (!this->currentLight)
    224     {
    225       PRINTF(1)("no Light defined yet\n");
    226       return;
    227     }
    228 
    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;
    233 
    234   glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor);
    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 {
    246   if (!this->currentLight)
    247     {
    248       PRINTF(1)("no Light defined yet\n");
    249       return;
    250     }
    251 
    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;
    256 
    257   glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor);
    258 }
    259 
    260 /**
    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 
    291311/**
    292312   \brief sets the ambient Color of the Scene
     
    295315   \param b blue
    296316*/
    297 void Light::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
     317void LightManager::setAmbientColor(GLfloat r, GLfloat g, GLfloat b)
    298318{
    299319  this->ambientColor[0] = r;
     
    305325}
    306326
    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 
     327void LightManager::setPosition(Vector position)
     328{
     329  this->currentLight->setPosition(position);
     330}
     331void LightManager::setPosition(GLfloat x, GLfloat y, GLfloat z)
     332{
     333  this->currentLight->setPosition(x, y, z);
     334}
     335void LightManager::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
     336{
     337  this->currentLight->setDiffuseColor(r,g,b);
     338}
     339void LightManager::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
     340{
     341  this->currentLight->setSpecularColor(r, g, b);
     342}
     343void LightManager::setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
     344{
     345  this->currentLight->setAttenuation(constantAttenuation, linearAttenuation, quadraticAttenuation);
     346}
     347void LightManager::setSpotDirection(Vector direction)
     348{
     349  this->currentLight->setSpotDirection(direction);
     350}
     351void LightManager::setSpotCutoff(GLfloat cutoff)
     352{
     353  this->currentLight->setSpotCutoff(cutoff);
     354}
    331355
    332356// get Attributes
    333 
    334357/**
    335358   \returns the Position of the Light
    336359*/
    337 Vector Light::getPosition(void)
     360Vector LightManager::getPosition(void)
    338361{
    339362  if (!this->currentLight)
     
    346369}
    347370
    348 
    349 
    350 
    351371/**
    352372   \brief outputs debug information about the Class and its lights
    353373*/
    354 void Light::debug(void)
     374void LightManager::debug(void)
    355375{
    356376  PRINT(0)("=================================\n");
    357377  PRINT(0)("= DEBUG INFORMATION CLASS LIGHT =\n");
    358378  PRINT(0)("=================================\n");
    359   PRINT(0)("Reference: %p\n", Light::singletonRef);
     379  PRINT(0)("Reference: %p\n", LightManager::singletonRef);
    360380  if (this->currentLight)
    361381    PRINT(0)("current Light Nr: %d\n", this->currentLight->lightNumber);
     
    365385    if (this->lights[i])
    366386      {
    367         PRINT(0)(":: %d ::  -- reference %p\n", i, lights[i]);
    368         PRINT(0)(" GL-state: ");
    369         GLboolean param;
    370         glGetBooleanv(lightsV[i], &param);
    371         if (param)
    372           PRINT(0)("ON\n");
    373         else
    374           PRINT(0)("OFF\n");
    375 
    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);
     387        this->lights[i]->debug();
    394388      }
    395389  PRINT(0)("--------------------------------\n");
Note: See TracChangeset for help on using the changeset viewer.