00001 #include "light.h"
00002
00015 #include <cstring>
00016 #include <cassert>
00017 using namespace std;
00018
00019 #include <glt/error.h>
00020
00021
00022
00023 const GltColor GltLight::_ambientDefault (0,0,0,1);
00024 const GltColor GltLight::_diffuseDefault (1,1,1,1);
00025 const GltColor GltLight::_specularDefault (1,1,1,1);
00026 const Vector GltLight::_positionDefault (0,0,1);
00027 const Vector GltLight::_spotDirectionDefault(0,0,-1);
00028 const GLfloat GltLight::_spotExponentDefault (0.0);
00029 const GLfloat GltLight::_spotCutoffDefault (180.0);
00030 const GLfloat GltLight::_attenuationConstantDefault(1);
00031 const GLfloat GltLight::_attenuationLinearDefault(0);
00032 const GLfloat GltLight::_attenuationQuadraticDefault(0);
00033
00034 GltLight::GltLight(const GLenum light,const bool getIt)
00035 :
00036 _light(light),
00037 _enabled(false),
00038 _ambient(_ambientDefault),
00039 _diffuse(_diffuseDefault),
00040 _specular(_specularDefault),
00041 _position(_positionDefault),
00042 _spotDirection(_spotDirectionDefault),
00043 _spotExponent(_spotExponentDefault),
00044 _spotCutoff(_spotCutoffDefault),
00045 _attenuationConstant(_attenuationConstantDefault),
00046 _attenuationLinear(_attenuationLinearDefault),
00047 _attenuationQuadratic(_attenuationQuadraticDefault)
00048 {
00049 assert(_light>=GL_LIGHT0 && _light<=GL_LIGHT7);
00050
00051 if (getIt)
00052 get();
00053 }
00054
00055 GltLight::~GltLight()
00056 {
00057 }
00058
00059 void
00060 GltLight::get()
00061 {
00062 assert(_light>=GL_LIGHT0 && _light<=GL_LIGHT7);
00063
00064 GLERROR
00065
00066 _enabled = glIsEnabled(_light)!=0;
00067
00068 _ambient .glGetLight(_light,GL_AMBIENT);
00069 _diffuse .glGetLight(_light,GL_DIFFUSE);
00070 _specular.glGetLight(_light,GL_SPECULAR);
00071
00072 GltColor tmp;
00073
00074 tmp.glGetLight(_light,GL_POSITION); _position = tmp;
00075 tmp.glGetLight(_light,GL_SPOT_DIRECTION); _spotDirection = tmp;
00076
00077 glGetLightfv(_light,GL_SPOT_EXPONENT ,&_spotExponent);
00078 glGetLightfv(_light,GL_SPOT_CUTOFF ,&_spotCutoff);
00079
00080 glGetLightfv(_light,GL_CONSTANT_ATTENUATION ,&_attenuationConstant);
00081 glGetLightfv(_light,GL_LINEAR_ATTENUATION ,&_attenuationLinear);
00082 glGetLightfv(_light,GL_QUADRATIC_ATTENUATION,&_attenuationQuadratic);
00083
00084 GLERROR
00085 }
00086
00087 void
00088 GltLight::set() const
00089 {
00090 assert(_light>=GL_LIGHT0 && _light<=GL_LIGHT7);
00091
00092 if (_enabled)
00093 glEnable(_light);
00094 else
00095 glDisable(_light);
00096
00097 _ambient .glLight(_light,GL_AMBIENT);
00098 _diffuse .glLight(_light,GL_DIFFUSE);
00099 _specular.glLight(_light,GL_SPECULAR);
00100
00101 GltColor tmp;
00102 tmp = _position; tmp.glLight(_light,GL_POSITION);
00103 tmp = _spotDirection; tmp.glLight(_light,GL_SPOT_DIRECTION);
00104
00105 glLightf (_light,GL_SPOT_EXPONENT ,_spotExponent);
00106 glLightf (_light,GL_SPOT_CUTOFF ,_spotCutoff);
00107
00108 glLightf (_light,GL_CONSTANT_ATTENUATION ,_attenuationConstant);
00109 glLightf (_light,GL_LINEAR_ATTENUATION ,_attenuationLinear);
00110 glLightf (_light,GL_QUADRATIC_ATTENUATION,_attenuationQuadratic);
00111 }
00112
00113 GLenum &GltLight::light() { return _light; }
00114 bool &GltLight::enabled() { return _enabled; }
00115 GltColor &GltLight::ambient() { return _ambient; }
00116 GltColor &GltLight::diffuse() { return _diffuse; }
00117 GltColor &GltLight::specular() { return _specular; }
00118 Vector &GltLight::position() { return _position; }
00119 Vector &GltLight::spotDirection() { return _spotDirection; }
00120 GLfloat &GltLight::spotExponent() { return _spotExponent; }
00121 GLfloat &GltLight::spotCutoff() { return _spotCutoff; }
00122
00123 GLfloat &GltLight::attenutationConstant() { return _attenuationConstant; }
00124 GLfloat &GltLight::attenutationLinear() { return _attenuationLinear; }
00125 GLfloat &GltLight::attenutationQuadratic() { return _attenuationQuadratic; }
00126
00127 const GLenum &GltLight::light() const { return _light; }
00128 const bool &GltLight::enabled() const { return _enabled; }
00129 const GltColor &GltLight::ambient() const { return _ambient; }
00130 const GltColor &GltLight::diffuse() const { return _diffuse; }
00131 const GltColor &GltLight::specular() const { return _specular; }
00132 const Vector &GltLight::position() const { return _position; }
00133 const Vector &GltLight::spotDirection() const { return _spotDirection; }
00134 const GLfloat &GltLight::spotExponent() const { return _spotExponent; }
00135 const GLfloat &GltLight::spotCutoff() const { return _spotCutoff; }
00136
00137 const GLfloat &GltLight::attenutationConstant() const { return _attenuationConstant; }
00138 const GLfloat &GltLight::attenutationLinear() const { return _attenuationLinear; }
00139 const GLfloat &GltLight::attenutationQuadratic() const { return _attenuationQuadratic; }
00140