| [4734] | 1 | /*! | 
|---|
| [8255] | 2 | * @file light.h | 
|---|
 | 3 | *  Handles Lights. | 
|---|
| [3329] | 4 |  | 
|---|
| [8255] | 5 | A Light is one of the more important things in a 3D-environment, | 
|---|
 | 6 | without it one sees nothing :) | 
|---|
 | 7 | It is here for diffuse-, specular- and Bump-Mappings. | 
|---|
| [3245] | 8 | */ | 
|---|
| [1853] | 9 |  | 
|---|
| [3436] | 10 | #ifndef _LIGHT_H | 
|---|
 | 11 | #define _LIGHT_H | 
|---|
| [1853] | 12 |  | 
|---|
| [4338] | 13 | #include "p_node.h" | 
|---|
| [3436] | 14 | #include "glincl.h" | 
|---|
| [1853] | 15 |  | 
|---|
| [3598] | 16 | //! The maximum number of Lights this OpenGL-implementation supports. | 
|---|
| [3437] | 17 | #define NUMBEROFLIGHTS GL_MAX_LIGHTS | 
|---|
 | 18 |  | 
|---|
| [4469] | 19 |  | 
|---|
| [5405] | 20 | // FORWARD DECLARATIONS // | 
|---|
| [3436] | 21 | class Vector; | 
|---|
| [4734] | 22 | class TiXmlElement; | 
|---|
| [2036] | 23 |  | 
|---|
| [3598] | 24 | //! A class that handles Lights. The LightManager operates on this. | 
|---|
| [4338] | 25 | class Light : public PNode | 
|---|
| [3597] | 26 | { | 
|---|
| [9869] | 27 |   ObjectListDeclaration(Light); | 
|---|
 | 28 | public: | 
|---|
 | 29 |   Light(const TiXmlElement* root = NULL); | 
|---|
 | 30 |   virtual ~Light(); | 
|---|
 | 31 |  | 
|---|
 | 32 |   virtual void loadParams(const TiXmlElement* root); | 
|---|
 | 33 |  | 
|---|
 | 34 |   void setDiffuseColor(GLfloat r, GLfloat g, GLfloat b); | 
|---|
 | 35 |   void setSpecularColor(GLfloat r, GLfloat g, GLfloat b); | 
|---|
 | 36 |   void setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation); | 
|---|
 | 37 |   void setSpotDirection(const Vector& direction); | 
|---|
 | 38 |   void setSpotDirection(float x, float y, float z) { setSpotDirection(Vector(x,y,z)); }; | 
|---|
 | 39 |   void setSpotCutoff(GLfloat cutoff); | 
|---|
 | 40 |  | 
|---|
 | 41 |   /** @returns the lightNumber*/ | 
|---|
 | 42 |   int getLightNumber() const {return this->lightNumber;} | 
|---|
 | 43 |  | 
|---|
 | 44 |   virtual void draw() const; | 
|---|
 | 45 |  | 
|---|
 | 46 |   void debug() const; | 
|---|
 | 47 |  | 
|---|
 | 48 |   // attributes | 
|---|
 | 49 | private: | 
|---|
 | 50 |   int              lightNumber;               //!< The number of this Light. | 
|---|
 | 51 |   GLfloat          diffuseColor[4];           //!< The Diffuse Color this Light emmits. | 
|---|
 | 52 |   GLfloat          specularColor[4];          //!< The specular Color of this Light. | 
|---|
 | 53 |   float            constantAttenuation;       //!< The Factor of the the Constant Attenuation. | 
|---|
 | 54 |   float            linearAttenuation;         //!< The Factor of the the Linear Attenuation. | 
|---|
 | 55 |   float            quadraticAttenuation;      //!< The Factor of the the Quadratic Attenuation. | 
|---|
 | 56 |   GLfloat          spotDirection[4];          //!< The direction of the Spot Light. | 
|---|
 | 57 |   GLfloat          spotCutoff;                //!< The cutoff Angle of the Light Source | 
|---|
| [3597] | 58 | }; | 
|---|
| [9869] | 59 |  | 
|---|
 | 60 |  | 
|---|
 | 61 |  | 
|---|
| [3436] | 62 | //! A class that handles Lights | 
|---|
| [3329] | 63 | /** | 
|---|
| [8255] | 64 | A Light is a source that emits light rays (photons) | 
|---|
| [3600] | 65 |  | 
|---|
| [8255] | 66 | <b>Usage:</b>\n | 
|---|
 | 67 | First you have to get the Light Manager up and running by using LightManager::getInstance(). | 
|---|
 | 68 | This automatically initiates the GL_LIGHTING, and sets some default stuff about the light.\n | 
|---|
 | 69 | Then you will create a new light using: | 
|---|
 | 70 | \li new Light(); | 
|---|
| [3600] | 71 |  | 
|---|
| [8255] | 72 | if you want to operate on this Light just apply the following functions onto it. | 
|---|
 | 73 | (You can also optain the Light-pointer with LightManager::getInstance()->getLight(lightNumber)) | 
|---|
| [4738] | 74 |  | 
|---|
| [8255] | 75 | now you can operate on the light as follows: | 
|---|
 | 76 | \li void setDiffuseColor(GLfloat r, GLfloat g, GLfloat b); | 
|---|
 | 77 | \li void setSpecularColor(GLfloat r, GLfloat g, GLfloat b); | 
|---|
 | 78 | \li void setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation); | 
|---|
 | 79 | \li void setSpotDirection(Vector direction); | 
|---|
 | 80 | \li void setSpotCutoff(GLfloat cutoff); | 
|---|
 | 81 | \li all PNode stuff also works | 
|---|
| [3600] | 82 |  | 
|---|
| [8255] | 83 | To redraw the light use | 
|---|
 | 84 | \li void draw() const; (this is automatically done by the LightManager) | 
|---|
| [4734] | 85 |  | 
|---|
| [8255] | 86 | for some nice output just use: | 
|---|
 | 87 | \li void debug() const; (either on LightManager for a resume or on any Light for single information.) | 
|---|
| [3329] | 88 | */ | 
|---|
| [3597] | 89 | class LightManager : public BaseObject | 
|---|
| [9869] | 90 | { | 
|---|
 | 91 |   ObjectListDeclaration(LightManager); | 
|---|
| [3440] | 92 |  | 
|---|
| [9869] | 93 |   friend class Light; | 
|---|
 | 94 | public: | 
|---|
 | 95 |   virtual ~LightManager(); | 
|---|
 | 96 |   /** @returns a Pointer to the only object of this Class */ | 
|---|
 | 97 |   inline static LightManager* getInstance() { if (!singletonRef) singletonRef = new LightManager();  return singletonRef; }; | 
|---|
| [1853] | 98 |  | 
|---|
| [9869] | 99 |   virtual void loadParams(const TiXmlElement* root); | 
|---|
 | 100 |   void loadLights(const TiXmlElement* root); | 
|---|
| [4734] | 101 |  | 
|---|
| [9869] | 102 |   void setAmbientColor(GLfloat r, GLfloat g, GLfloat b); | 
|---|
 | 103 |   // HACK: Assuming r = g = b values | 
|---|
 | 104 | inline GLfloat getAmbientColor() {  return this->ambientColor[0]; } | 
|---|
| [3597] | 105 |  | 
|---|
| [9869] | 106 |   Light* getLight(int lightNumber) const; | 
|---|
 | 107 |   inline Light* getLight() const { return this->currentLight; }; | 
|---|
| [3598] | 108 |  | 
|---|
| [9869] | 109 |   void draw() const; | 
|---|
| [3453] | 110 |  | 
|---|
| [9869] | 111 |   void debug() const; | 
|---|
| [4469] | 112 |  | 
|---|
| [9869] | 113 | private: | 
|---|
 | 114 |   LightManager(); | 
|---|
| [4469] | 115 |  | 
|---|
| [9869] | 116 |   int  registerLight(Light* light); | 
|---|
 | 117 |   void unregisterLight(Light* light); | 
|---|
| [4734] | 118 |  | 
|---|
| [9869] | 119 | private: | 
|---|
 | 120 |   static LightManager*    singletonRef;       //!< This is the LightHandlers Reference. | 
|---|
 | 121 |   GLfloat                 ambientColor[4];    //!< The ambient Color of the scene. | 
|---|
| [4469] | 122 |  | 
|---|
| [9869] | 123 |   Light**                 lights;             //!< An array of Lenght NUMBEROFLIGHTS, that holds pointers to all LightValues. | 
|---|
 | 124 |   Light*                  currentLight;       //!< The current Light, we are working with. | 
|---|
| [4734] | 125 |  | 
|---|
| [1853] | 126 | }; | 
|---|
 | 127 |  | 
|---|
| [3436] | 128 | #endif /* _LIGHT_H */ | 
|---|