| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_LIGHT |
|---|
| 15 | #define OSG_LIGHT 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/StateAttribute> |
|---|
| 18 | #include <osg/Vec3> |
|---|
| 19 | #include <osg/Vec4> |
|---|
| 20 | |
|---|
| 21 | #ifndef GL_LIGHT0 |
|---|
| 22 | #define GL_LIGHT0 0x4000 |
|---|
| 23 | #define GL_LIGHT1 0x4001 |
|---|
| 24 | #define GL_LIGHT2 0x4002 |
|---|
| 25 | #define GL_LIGHT3 0x4003 |
|---|
| 26 | #define GL_LIGHT4 0x4004 |
|---|
| 27 | #define GL_LIGHT5 0x4005 |
|---|
| 28 | #define GL_LIGHT6 0x4006 |
|---|
| 29 | #define GL_LIGHT7 0x4007 |
|---|
| 30 | #endif |
|---|
| 31 | |
|---|
| 32 | #ifndef GL_LIGHTING |
|---|
| 33 | #define GL_LIGHTING 0x0B50 |
|---|
| 34 | #endif |
|---|
| 35 | |
|---|
| 36 | namespace osg { |
|---|
| 37 | |
|---|
| 38 | /** Light state class which encapsulates OpenGL glLight() functionality. */ |
|---|
| 39 | class OSG_EXPORT Light : public StateAttribute |
|---|
| 40 | { |
|---|
| 41 | public : |
|---|
| 42 | |
|---|
| 43 | Light(); |
|---|
| 44 | |
|---|
| 45 | Light(unsigned int lightnum); |
|---|
| 46 | |
|---|
| 47 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 48 | Light(const Light& light,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 49 | StateAttribute(light,copyop), |
|---|
| 50 | _lightnum(light._lightnum), |
|---|
| 51 | _ambient(light._ambient), |
|---|
| 52 | _diffuse(light._diffuse), |
|---|
| 53 | _specular(light._specular), |
|---|
| 54 | _position(light._position), |
|---|
| 55 | _direction(light._direction), |
|---|
| 56 | _constant_attenuation(light._constant_attenuation), |
|---|
| 57 | _linear_attenuation(light._linear_attenuation), |
|---|
| 58 | _quadratic_attenuation(light._quadratic_attenuation), |
|---|
| 59 | _spot_exponent(light._spot_exponent), |
|---|
| 60 | _spot_cutoff(light._spot_cutoff) {} |
|---|
| 61 | |
|---|
| 62 | virtual osg::Object* cloneType() const { return new Light(_lightnum); } |
|---|
| 63 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Light(*this,copyop); } |
|---|
| 64 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Light *>(obj)!=NULL; } |
|---|
| 65 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 66 | virtual const char* className() const { return "Light"; } |
|---|
| 67 | virtual Type getType() const { return LIGHT; } |
|---|
| 68 | |
|---|
| 69 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 70 | virtual int compare(const StateAttribute& sa) const |
|---|
| 71 | { |
|---|
| 72 | // check the types are equal and then create the rhs variable |
|---|
| 73 | // used by the COMPARE_StateAttribute_Parameter macros below. |
|---|
| 74 | COMPARE_StateAttribute_Types(Light,sa) |
|---|
| 75 | |
|---|
| 76 | // compare each parameter in turn against the rhs. |
|---|
| 77 | COMPARE_StateAttribute_Parameter(_lightnum) |
|---|
| 78 | COMPARE_StateAttribute_Parameter(_ambient) |
|---|
| 79 | COMPARE_StateAttribute_Parameter(_diffuse) |
|---|
| 80 | COMPARE_StateAttribute_Parameter(_specular) |
|---|
| 81 | COMPARE_StateAttribute_Parameter(_position) |
|---|
| 82 | COMPARE_StateAttribute_Parameter(_direction) |
|---|
| 83 | COMPARE_StateAttribute_Parameter(_constant_attenuation) |
|---|
| 84 | COMPARE_StateAttribute_Parameter(_linear_attenuation) |
|---|
| 85 | COMPARE_StateAttribute_Parameter(_quadratic_attenuation) |
|---|
| 86 | COMPARE_StateAttribute_Parameter(_spot_exponent) |
|---|
| 87 | COMPARE_StateAttribute_Parameter(_spot_cutoff) |
|---|
| 88 | |
|---|
| 89 | return 0; // passed all the above comparison macros, must be equal. |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | virtual unsigned int getMember() const { return _lightnum; } |
|---|
| 93 | |
|---|
| 94 | virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const |
|---|
| 95 | { |
|---|
| 96 | usage.usesMode(GL_LIGHT0+_lightnum); |
|---|
| 97 | return true; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** Set which OpenGL light to operate on. */ |
|---|
| 103 | void setLightNum(int num); |
|---|
| 104 | |
|---|
| 105 | /** Get which OpenGL light this osg::Light operates on. */ |
|---|
| 106 | int getLightNum() const { return _lightnum; } |
|---|
| 107 | |
|---|
| 108 | /** Set the ambient component of the light. */ |
|---|
| 109 | inline void setAmbient( const Vec4& ambient ) { _ambient = ambient; } |
|---|
| 110 | |
|---|
| 111 | /** Get the ambient component of the light. */ |
|---|
| 112 | inline const Vec4& getAmbient() const { return _ambient; } |
|---|
| 113 | |
|---|
| 114 | /** Set the diffuse component of the light. */ |
|---|
| 115 | inline void setDiffuse( const Vec4& diffuse ) { _diffuse = diffuse; } |
|---|
| 116 | |
|---|
| 117 | /** Get the diffuse component of the light. */ |
|---|
| 118 | inline const Vec4& getDiffuse() const { return _diffuse; } |
|---|
| 119 | |
|---|
| 120 | /** Set the specular component of the light. */ |
|---|
| 121 | inline void setSpecular( const Vec4& specular ) { _specular = specular; } |
|---|
| 122 | |
|---|
| 123 | /** Get the specular component of the light. */ |
|---|
| 124 | inline const Vec4& getSpecular() const { return _specular; } |
|---|
| 125 | |
|---|
| 126 | /** Set the position of the light. */ |
|---|
| 127 | inline void setPosition( const Vec4& position ) { _position = position; } |
|---|
| 128 | |
|---|
| 129 | /** Get the position of the light. */ |
|---|
| 130 | inline const Vec4& getPosition() const { return _position; } |
|---|
| 131 | |
|---|
| 132 | /** Set the direction of the light. */ |
|---|
| 133 | inline void setDirection( const Vec3& direction ) { _direction = direction; } |
|---|
| 134 | |
|---|
| 135 | /** Get the direction of the light. */ |
|---|
| 136 | inline const Vec3& getDirection() const { return _direction; } |
|---|
| 137 | |
|---|
| 138 | /** Set the constant attenuation of the light. */ |
|---|
| 139 | inline void setConstantAttenuation( float constant_attenuation ) { _constant_attenuation = constant_attenuation; } |
|---|
| 140 | |
|---|
| 141 | /** Get the constant attenuation of the light. */ |
|---|
| 142 | inline float getConstantAttenuation() const { return _constant_attenuation; } |
|---|
| 143 | |
|---|
| 144 | /** Set the linear attenuation of the light. */ |
|---|
| 145 | inline void setLinearAttenuation ( float linear_attenuation ) { _linear_attenuation = linear_attenuation; } |
|---|
| 146 | |
|---|
| 147 | /** Get the linear attenuation of the light. */ |
|---|
| 148 | inline float getLinearAttenuation () const { return _linear_attenuation; } |
|---|
| 149 | |
|---|
| 150 | /** Set the quadratic attenuation of the light. */ |
|---|
| 151 | inline void setQuadraticAttenuation ( float quadratic_attenuation ) { _quadratic_attenuation = quadratic_attenuation; } |
|---|
| 152 | |
|---|
| 153 | /** Get the quadratic attenuation of the light. */ |
|---|
| 154 | inline float getQuadraticAttenuation() const { return _quadratic_attenuation; } |
|---|
| 155 | |
|---|
| 156 | /** Set the spot exponent of the light. */ |
|---|
| 157 | inline void setSpotExponent( float spot_exponent ) { _spot_exponent = spot_exponent; } |
|---|
| 158 | |
|---|
| 159 | /** Get the spot exponent of the light. */ |
|---|
| 160 | inline float getSpotExponent() const { return _spot_exponent; } |
|---|
| 161 | |
|---|
| 162 | /** Set the spot cutoff of the light. */ |
|---|
| 163 | inline void setSpotCutoff( float spot_cutoff ) { _spot_cutoff = spot_cutoff; } |
|---|
| 164 | |
|---|
| 165 | /** Get the spot cutoff of the light. */ |
|---|
| 166 | inline float getSpotCutoff() const { return _spot_cutoff; } |
|---|
| 167 | |
|---|
| 168 | /** Capture the lighting settings of the current OpenGL state |
|---|
| 169 | * and store them in this object. |
|---|
| 170 | */ |
|---|
| 171 | void captureLightState(); |
|---|
| 172 | |
|---|
| 173 | /** Apply the light's state to the OpenGL state machine. */ |
|---|
| 174 | virtual void apply(State& state) const; |
|---|
| 175 | |
|---|
| 176 | protected : |
|---|
| 177 | |
|---|
| 178 | virtual ~Light(); |
|---|
| 179 | |
|---|
| 180 | /** Initialize the light's settings with some decent defaults. */ |
|---|
| 181 | void init(); |
|---|
| 182 | |
|---|
| 183 | int _lightnum; // OpenGL light number |
|---|
| 184 | |
|---|
| 185 | Vec4 _ambient; // r, g, b, w |
|---|
| 186 | Vec4 _diffuse; // r, g, b, w |
|---|
| 187 | Vec4 _specular; // r, g, b, w |
|---|
| 188 | Vec4 _position; // x, y, z, w |
|---|
| 189 | Vec3 _direction; // x, y, z |
|---|
| 190 | float _constant_attenuation; // constant |
|---|
| 191 | float _linear_attenuation; // linear |
|---|
| 192 | float _quadratic_attenuation; // quadratic |
|---|
| 193 | float _spot_exponent; // exponent |
|---|
| 194 | float _spot_cutoff; // spread |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | #endif |
|---|