Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3437 in orxonox.OLD


Ignore:
Timestamp:
Mar 1, 2005, 8:11:31 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: light is now more like a lightManager. It is a Class, that handles many lights, and as such also glEnable(GL_LIGHTING)…
Until now It is not possible to initialize more than the first Light, but this will be fixed soon :)

Location:
orxonox/trunk/src
Files:
4 edited

Legend:

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

    r3436 r3437  
    2323using namespace std;
    2424
     25//! Definition of the Lights
     26int lightsV[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};
     27
    2528
    2629/**
     
    2932Light::Light ()
    3033{
    31   this->init();
    32 }
    33 
    34 Light::Light(Vector position)
    35 {
    36   this->init();
    37   this->setPosition(position);
     34  this->setClassName ("Light");
     35
     36  glEnable (GL_LIGHTING);
     37  this->lights = new LightValue*[NUMBEROFLIGHTS];
     38  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     39    lights[i] = NULL;
     40  this->currentLight = new LightValue;
    3841}
    3942
     
    4649{
    4750  glDisable(GL_LIGHTING);
    48   glDisable(GL_LIGHT0);
     51 
     52  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     53    this->deleteLight(i);
     54  delete lights;
     55  Light::singletonRef = NULL;
     56}
     57
     58Light* Light::singletonRef = NULL;
     59
     60/**
     61   \returns The Instance of the Lights
     62*/
     63Light* Light::getInstance(void)
     64{
     65  if (singletonRef)
     66    return singletonRef;
     67  else
     68    return Light::singletonRef = new Light();
    4969}
    5070
     
    5272   \brief initializes a new Light with default values, and enables GL_LIGHTING
    5373*/
    54 void Light::init()
    55 {
    56   this->setClassName ("Light");
    57   glEnable (GL_LIGHTING);
    58   glEnable (GL_LIGHT0);
     74void Light::init(int lightNumber)
     75{
     76  PRINTF(3)("initializing Light number %d.\n", lightNumber);
     77  // enable The light
     78  glEnable(lightsV[lightNumber]);
     79  this->currentLight = lights[lightNumber] = new LightValue;
    5980 
     81  // set default values
     82  this->currentLight->lightNumber = lightNumber;
    6083  this->setPosition(Vector(0.0, 0.0, 0.0));
    6184  this->setDiffuseColor(1.0, 1.0, 1.0);
    6285  this->setSpecularColor(1.0, 1.0, 1.0);
    6386  //  lmodelAmbient[] = {.1, .1, .1, 1.0};
    64  
    65 }
     87}
     88
     89/**
     90   \brief Adds a new Light, by selecting the First free slot.
     91
     92   if no slot is free error
     93*/
     94int Light::addLight(void)
     95{
     96  for (int i = 0; i < NUMBEROFLIGHTS; i++)
     97    if (!this->lights[i])
     98      return addLight(i);
     99  PRINTF(1)("no more light slots availiable. All %d already taken\n", NUMBEROFLIGHTS);
     100  return -1;
     101}
     102
     103/**
     104   \brief Adds a new Light
     105   \param lightNumber The number of the light to add.
     106
     107   if the Number is not free: warn, automatically choose another slot.
     108*/
     109int Light::addLight(int lightNumber)
     110{
     111  if (this->lights[lightNumber])
     112    {
     113      PRINTF(2)("Lightslot %d is allready taken, trying another one\n", lightNumber);
     114      return this->addLight();
     115    }
     116  this->init(lightNumber);
     117  this->currentLight = this->lights[lightNumber];
     118  return lightNumber;
     119}
     120
     121/**
     122   \brief select the light to work with
     123   \param lightNumber the light to work with
     124*/
     125void Light::editLightNumber(int lightNumber)
     126{
     127  this->currentLight = lights[lightNumber];
     128}
     129
     130/**
     131   \brief Delete the current Light
     132*/
     133void Light::deleteLight(void)
     134{
     135  this->deleteLight(this->currentLight->lightNumber);
     136}
     137
     138/**
     139   \brief delete light.
     140   \param lightnumber the number of the light to delete
     141*/
     142void Light::deleteLight(int lightNumber)
     143{
     144  if (this->lights[lightNumber])
     145    {
     146      PRINTF(3)("deleting Light number %d\n", lightNumber);
     147      delete this->lights[lightNumber];
     148      glDisable(lightsV[lightNumber]);
     149      this->lights[lightNumber] = NULL;
     150    }
     151}
     152
     153
    66154
    67155// set Attributes
     
    73161void Light::setPosition(Vector position)
    74162{
    75   this->lightPosition[0] = position.x;
    76   this->lightPosition[1] = position.y;
    77   this->lightPosition[2] = position.z;
    78   this->lightPosition[3] = 0.0;
    79 
    80   glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
    81 
     163  this->currentLight->lightPosition[0] = position.x;
     164  this->currentLight->lightPosition[1] = position.y;
     165  this->currentLight->lightPosition[2] = position.z;
     166  this->currentLight->lightPosition[3] = 0.0;
     167
     168  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
     169}
     170
     171void Light::setPosition(GLfloat x, GLfloat y, GLfloat z)
     172{
     173  this->currentLight->lightPosition[0] = x;
     174  this->currentLight->lightPosition[1] = y;
     175  this->currentLight->lightPosition[2] = z;
     176  this->currentLight->lightPosition[3] = 0.0;
     177
     178  glLightfv (GL_LIGHT0, GL_POSITION, this->currentLight->lightPosition);
    82179}
    83180
     
    90187void Light::setDiffuseColor(GLfloat r, GLfloat g, GLfloat b)
    91188{
    92   this->diffuseColor[0] = r;
    93   this->diffuseColor[1] = g;
    94   this->diffuseColor[2] = b;
    95   this->diffuseColor[3] = 1.0;
    96 
    97   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuseColor);
     189  this->currentLight->diffuseColor[0] = r;
     190  this->currentLight->diffuseColor[1] = g;
     191  this->currentLight->diffuseColor[2] = b;
     192  this->currentLight->diffuseColor[3] = 1.0;
     193
     194  glLightfv (GL_LIGHT0, GL_DIFFUSE, this->currentLight->diffuseColor);
    98195}
    99196
     
    107204void Light::setSpecularColor(GLfloat r, GLfloat g, GLfloat b)
    108205{
    109   this->specularColor[0] = r;
    110   this->specularColor[1] = g;
    111   this->specularColor[2] = b;
    112   this->specularColor[3] = 1.0;
    113 
    114   glLightfv (GL_LIGHT0, GL_SPECULAR, specularColor);
     206  this->currentLight->specularColor[0] = r;
     207  this->currentLight->specularColor[1] = g;
     208  this->currentLight->specularColor[2] = b;
     209  this->currentLight->specularColor[3] = 1.0;
     210
     211  glLightfv (GL_LIGHT0, GL_SPECULAR, this->currentLight->specularColor);
    115212}
    116213
     
    122219Vector Light::getPosition(void)
    123220{
    124   Vector tmpPosition(lightPosition[0], lightPosition[1], lightPosition[2]);
     221  Vector tmpPosition(this->currentLight->lightPosition[0], this->currentLight->lightPosition[1], this->currentLight->lightPosition[2]);
    125222  return tmpPosition;
    126223}
  • orxonox/trunk/src/light.h

    r3436 r3437  
    1414#include "glincl.h"
    1515
     16#define NUMBEROFLIGHTS GL_MAX_LIGHTS
     17
    1618// FORWARD DEFINITIONS //
    1719class Vector;
     
    2123   A Light is a source that emits light rays (photons)
    2224*/
    23 class Light : public WorldEntity 
     25class Light : public WorldEntity
    2426{
     27 private:
     28  //! A struct that holds information about a Light
     29  struct LightValue
     30  {
     31    int lightNumber;            //!< The number of this Light.
     32    GLfloat lightPosition[4];   //!< The Position of this Light.
     33    GLfloat lmodelAmbient[4];   //!< The general Ambient Color.
     34    GLfloat diffuseColor[4];    //!< The Diffuse Color this Light emmits.
     35    GLfloat specularColor[4];   //!< The specular Color of this Light.
     36
     37    LightValue* next;
     38  };
     39
     40  static Light* singletonRef;    //!< This is the LightHandlers Reference.
     41  Light(void);
     42
     43  void init(int LightNumber); 
     44  LightValue** lights;
     45  LightValue* currentLight;
    2546 
    26 
    2747 public:
    28   Light(void);
    29   Light(Vector position);
     48  static Light* getInstance();
    3049  ~Light(void);
    3150
    3251  // set Attributes
     52  int addLight(void);
     53  int addLight(int lightNumber);
     54  void editLightNumber(int lightNumber);
     55  void deleteLight(void);
     56  void deleteLight(int lightNumber);
     57
    3358  void setPosition(Vector position);
     59  void setPosition(GLfloat x, GLfloat y, GLfloat z);
    3460  void setDiffuseColor(GLfloat r, GLfloat g, GLfloat b);
    3561  void setSpecularColor(GLfloat r, GLfloat g, GLfloat b);
    36  
    3762  // get Attributes
    3863  Vector getPosition(void);
    39  private:
    40   GLfloat lightPosition[4];   //!< The Position of this Light
    41   GLfloat lmodelAmbient[4];   //!< The general Ambient Color
    42   GLfloat diffuseColor[4];    //!< The Diffuse Color this Light emmits
    43   GLfloat specularColor[4];   //!< The specular Color of this Light
    44 
    45   void init();
    4664};
    4765
  • orxonox/trunk/src/world.cc

    r3436 r3437  
    172172void World::load()
    173173{
    174   light0 = new Light(Vector(10.0, 10.0, 19.0));
     174  light = Light::getInstance();
     175  light->addLight(1);
     176  light->setPosition(10.0, 10.0, 19.0);
    175177
    176178  //  BezierCurve* tmpCurve = new BezierCurve();
  • orxonox/trunk/src/world.h

    r3436 r3437  
    7777  SDL_Surface *loadImage;
    7878  Skysphere* skySphere;
    79   Light* light0;
     79  Light* light;
    8080
    8181  WorldEntity* localPlayer;
Note: See TracChangeset for help on using the changeset viewer.