| 1 | /*! |
|---|
| 2 | \file light.h |
|---|
| 3 | \brief Handles Lights. |
|---|
| 4 | |
|---|
| 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. |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | #ifndef _LIGHT_H |
|---|
| 11 | #define _LIGHT_H |
|---|
| 12 | |
|---|
| 13 | #include "p_node.h" |
|---|
| 14 | #include "glincl.h" |
|---|
| 15 | |
|---|
| 16 | //! The maximum number of Lights this OpenGL-implementation supports. |
|---|
| 17 | #define NUMBEROFLIGHTS GL_MAX_LIGHTS |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | // FORWARD DEFINITIONS // |
|---|
| 21 | class Vector; |
|---|
| 22 | class TiXmlElement; |
|---|
| 23 | |
|---|
| 24 | //! A class that handles Lights. The LightManager operates on this. |
|---|
| 25 | class Light : public PNode |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | Light(int lightNumber); |
|---|
| 29 | Light(const TiXmlElement* root); |
|---|
| 30 | virtual ~Light(void); |
|---|
| 31 | |
|---|
| 32 | void init(int lightNumber); |
|---|
| 33 | void loadParams(const TiXmlElement* root); |
|---|
| 34 | |
|---|
| 35 | void setPosition(const Vector& position); |
|---|
| 36 | void setPosition(GLfloat x, GLfloat y, GLfloat z); |
|---|
| 37 | Vector getPosition() const; |
|---|
| 38 | |
|---|
| 39 | void setDiffuseColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 40 | void setSpecularColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 41 | void setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation); |
|---|
| 42 | void setSpotDirection(const Vector& direction); |
|---|
| 43 | void setSpotDirection(float x, float y, float z) { setSpotDirection(Vector(x,y,z)); }; |
|---|
| 44 | void setSpotCutoff(GLfloat cutoff); |
|---|
| 45 | |
|---|
| 46 | /** \returns the lightNumber*/ |
|---|
| 47 | int getLightNumber(void) const {return this->lightNumber;} |
|---|
| 48 | |
|---|
| 49 | virtual void draw(); |
|---|
| 50 | |
|---|
| 51 | void debug(void) const; |
|---|
| 52 | |
|---|
| 53 | // attributes |
|---|
| 54 | private: |
|---|
| 55 | int lightNumber; //!< The number of this Light. |
|---|
| 56 | GLfloat lightPosition[4]; //!< The Position of this Light. |
|---|
| 57 | GLfloat diffuseColor[4]; //!< The Diffuse Color this Light emmits. |
|---|
| 58 | GLfloat specularColor[4]; //!< The specular Color of this Light. |
|---|
| 59 | float constantAttenuation; //!< The Factor of the the Constant Attenuation. |
|---|
| 60 | float linearAttenuation; //!< The Factor of the the Linear Attenuation. |
|---|
| 61 | float quadraticAttenuation; //!< The Factor of the the Quadratic Attenuation. |
|---|
| 62 | GLfloat spotDirection[4]; //!< The direction of the Spot Light. |
|---|
| 63 | GLfloat spotCutoff; //!< The cutoff Angle of the Light Source |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | //! A class that handles Lights |
|---|
| 69 | /** |
|---|
| 70 | A Light is a source that emits light rays (photons) |
|---|
| 71 | |
|---|
| 72 | <b>Usage:</b>\n |
|---|
| 73 | First you have to get the Light Manager up and running by using LightManager::getInstance. |
|---|
| 74 | This automatically initiates the GL_LIGHTING, and sets some default stuff about the light.\n |
|---|
| 75 | Then you will create a new light using: |
|---|
| 76 | \li int addLight(void); |
|---|
| 77 | \li int addLight(int lightNumber); |
|---|
| 78 | |
|---|
| 79 | now you can operate on the light as follows: |
|---|
| 80 | \li void setPosition(Vector position); |
|---|
| 81 | \li void setPosition(GLfloat x, GLfloat y, GLfloat z); |
|---|
| 82 | \li void setDiffuseColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 83 | \li void setSpecularColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 84 | \li void setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation); |
|---|
| 85 | \li void setSpotDirection(Vector direction); |
|---|
| 86 | \li void setSpotCutoff(GLfloat cutoff); |
|---|
| 87 | |
|---|
| 88 | These functions are preatty selv-explaining, but you can read about them below.\n |
|---|
| 89 | If you want to fetch info, or the a Light use: |
|---|
| 90 | \li Vector getPosition(void) const; |
|---|
| 91 | \li Vector getPosition(int lightNumber) const; |
|---|
| 92 | \li Light* getLight(int LightNumber) const; |
|---|
| 93 | |
|---|
| 94 | To redraw the light use |
|---|
| 95 | \li void draw(); |
|---|
| 96 | |
|---|
| 97 | and to delete one just use |
|---|
| 98 | \li void deleteLight(void); |
|---|
| 99 | |
|---|
| 100 | for some nice output just use: |
|---|
| 101 | \li void debug(void) const; |
|---|
| 102 | |
|---|
| 103 | You can also operate on the single Lights themselves, by first retreaving them with |
|---|
| 104 | \li Light* getLight(int LightNumber); |
|---|
| 105 | \n |
|---|
| 106 | and then using the same functions on this to change settings. |
|---|
| 107 | */ |
|---|
| 108 | class LightManager : public BaseObject |
|---|
| 109 | { |
|---|
| 110 | |
|---|
| 111 | public: |
|---|
| 112 | virtual ~LightManager(void); |
|---|
| 113 | /** \returns a Pointer to the only object of this Class */ |
|---|
| 114 | inline static LightManager* getInstance(void) { if (!singletonRef) singletonRef = new LightManager(); return singletonRef; }; |
|---|
| 115 | |
|---|
| 116 | void loadParams(const TiXmlElement* root); |
|---|
| 117 | |
|---|
| 118 | // set Attributes |
|---|
| 119 | int addLight(void); |
|---|
| 120 | int addLight(int lightNumber); |
|---|
| 121 | void editLightNumber(int lightNumber); |
|---|
| 122 | void deleteLight(void); |
|---|
| 123 | void deleteLight(int lightNumber); |
|---|
| 124 | |
|---|
| 125 | void setAmbientColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 126 | |
|---|
| 127 | void draw(); |
|---|
| 128 | |
|---|
| 129 | //set Attributes |
|---|
| 130 | void setPosition(Vector position); |
|---|
| 131 | void setPosition(GLfloat x, GLfloat y, GLfloat z); |
|---|
| 132 | void setDiffuseColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 133 | void setSpecularColor(GLfloat r, GLfloat g, GLfloat b); |
|---|
| 134 | void setAttenuation(float constantAttenuation, float linearAttenuation, float quadraticAttenuation); |
|---|
| 135 | void setSpotDirection(Vector direction); |
|---|
| 136 | void setSpotCutoff(GLfloat cutoff); |
|---|
| 137 | |
|---|
| 138 | // get Attributes |
|---|
| 139 | Vector getPosition(void) const; |
|---|
| 140 | Vector getPosition(int lightNumber) const; |
|---|
| 141 | |
|---|
| 142 | Light* getLight(int lightNumber) const; |
|---|
| 143 | |
|---|
| 144 | void debug(void) const; |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | private: |
|---|
| 148 | LightManager(void); |
|---|
| 149 | void initLight(int LightNumber); |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | private: |
|---|
| 153 | static LightManager* singletonRef; //!< This is the LightHandlers Reference. |
|---|
| 154 | GLfloat ambientColor[4]; //!< The ambient Color of the scene. |
|---|
| 155 | |
|---|
| 156 | Light** lights; //!< An array of Lenght NUMBEROFLIGHTS, that holds pointers to all LightValues. |
|---|
| 157 | Light* currentLight; //!< The current Light, we are working with. |
|---|
| 158 | |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | #endif /* _LIGHT_H */ |
|---|