63 #include "LightNode.hh"
64 #include <ACG/GL/IRenderer.hh>
67 #define M_PI 3.14159265358979323846
73 namespace SceneGraph {
77 static LightSourceHandle* lightSourceHandle = 0;
84 fixedPosition_ =
false;
86 ambientColor_ =
Vec4f(0.1f,0.1f,0.1f,1.f);
87 diffuseColor_ =
Vec4f(1.f,1.f,1.f,1.f);
88 specularColor_ =
Vec4f(1.f,1.f,1.f,1.f);
90 position_ =
Vec4f(0.f,0.f,1.f,0.f);
91 realPosition_ =
Vec4f(0.f,0.f,1.f,0.f);
93 spotDirection_ =
Vec3d(0.0,0.0,-1.0);
94 realSpotDirection_ =
Vec3d(0.0,0.0,-1.0);
98 initialPosition_ =
Vec4f(0.f, 0.f, 0.f, 0.f);
99 initialSpotDirection_ =
Vec3d(0.0, 0.0, -1.0);
100 initialPositionInit_ =
false;
105 constantAttenuation_ = 1;
106 linearAttenuation_ = 0;
107 quadraticAttenuation_ = 0;
116 position_ =
Vec4d( _pos[0],_pos[1],_pos[2],1.0);
120 return Vec3d( position_[0], position_[1], position_[2]);
126 position_ =
Vec4d( _pos[0],_pos[1],_pos[2],0.0);
130 return Vec3d(position_[0], position_[1], position_[2]);
134 return ( position_[3] == 0.0 );
141 { enabled_ =
false; }
148 { spotDirection_ = _pos; }
151 return Vec3d(spotDirection_[0],spotDirection_[1],spotDirection_[2]);
155 { ambientColor_ = _color; }
158 {
return ambientColor_; }
161 { diffuseColor_ = _color; }
164 {
return diffuseColor_; }
167 { specularColor_ = _color; }
170 {
return specularColor_; }
172 void LightSource::setColor(
const Vec4f& _ambient,
const Vec4f& _diffuse,
const Vec4f& _specular) {
173 ambientColor_ = _ambient;
174 diffuseColor_ = _diffuse;
175 specularColor_ = _specular;
178 void LightSource::fixedPosition(
bool _state)
179 { fixedPosition_ = _state; }
181 bool LightSource::fixedPosition()
const {
182 return fixedPosition_;
185 void LightSource::spotExponent(
float _exponent) {
186 spotExponent_ = _exponent;
189 float LightSource::spotExponent()
const {
190 return spotExponent_;
193 void LightSource::spotCutoff(
float _cutoff) {
194 spotCutoff_ = _cutoff;
197 float LightSource::spotCutoff()
const {
201 void LightSource::constantAttenuation(
float _constantAttenuation) {
202 constantAttenuation_ = _constantAttenuation;
205 float LightSource::constantAttenuation()
const {
206 return constantAttenuation_;
209 void LightSource::linearAttenuation(
float _linearAttenuation) {
210 linearAttenuation_ = _linearAttenuation;
213 float LightSource::linearAttenuation()
const {
214 return linearAttenuation_;
217 void LightSource::quadraticAttenuation(
float _quadraticAttenuation) {
218 quadraticAttenuation_ = _quadraticAttenuation;
221 float LightSource::quadraticAttenuation()
const {
222 return quadraticAttenuation_;
225 void LightSource::brightness(
float _brightness) {
226 brightness_ = _brightness;
229 float LightSource::brightness()
const {
234 const std::string& _name)
237 lightId_(GL_INVALID_ENUM) {
239 if(lightSourceHandle == 0) {
244 cone_ =
new ACG::GLCone(10, 10, 1.0f, 1.0f,
false,
true);
279 (double)
light_.realPosition_[1],
280 (
double)
light_.realPosition_[2]);
297 if(
light_.fixedPosition_ && !
light_.initialPositionInit_) {
300 light_.initialPositionInit_ =
true;
303 if(
light_.fixedPosition_) {
322 for(
int i = 0; i < 3; ++i) {
323 if(ac[i] > max) max = ac[i];
327 for(
int i = 0; i < 3; ++i) {
328 if(dc[i] > max) max = dc[i];
332 for(
int i = 0; i < 3; ++i) {
333 if(sc[i] > max) max = sc[i];
338 GLboolean lighting_backup;
346 glGetBooleanv(GL_LIGHTING, &lighting_backup);
355 lightId_ = lightSourceHandle->getLight(
this);
368 glLightf(
lightId_, GL_SPOT_EXPONENT, 0.0f);
370 pos [0] = backup_position[0];
371 pos [1] = backup_position[1];
372 pos [2] = backup_position[2];
375 glLightfv(
lightId_, GL_POSITION, pos);
378 float gl_ac[] = {ac[0], ac[1], ac[2], ac[3]};
379 glLightfv(
lightId_, GL_AMBIENT, gl_ac);
380 float gl_dc[] = {dc[0], dc[1], dc[2], dc[3]};
381 glLightfv(
lightId_, GL_DIFFUSE, gl_dc);
382 float gl_sc[] = {sc[0], sc[1], sc[2], sc[3]};
383 glLightfv(
lightId_, GL_SPECULAR, gl_sc);
390 if(
light_.spotCutoff() < 180.0f) {
397 float angle = acos((z | spot)/(z.norm()*spot.norm()));
398 angle = angle*360/(2*M_PI);
400 _state.
rotate(angle, rA[0], rA[1], rA[2]);
403 cone_->setNormalOrientation(ACG::GLPrimitive::INSIDE);
410 cone_->setTopRadius(0.0f);
415 lightSourceHandle->removeLight(
this);
419 if(!backup_directional) {
443 if(
light_.fixedPosition_ && !
light_.initialPositionInit_) {
446 light_.initialPositionInit_ =
true;
449 if(
light_.fixedPosition_) {
458 glPushAttrib(GL_DEPTH_BUFFER_BIT);
478 if(
light_.spotCutoff() < 180.0f) {
485 float angle = acos((z | spot)/(z.norm()*spot.norm()));
486 angle = angle*360/(2*M_PI);
488 _state.
rotate(angle, rA[0], rA[1], rA[2]);
490 cone_->setNormalOrientation(ACG::GLPrimitive::OUTSIDE);
497 cone_->setTopRadius(0.0f);
514 if(
light_.fixedPosition_ && !
light_.initialPositionInit_) {
517 light_.initialPositionInit_ =
true;
521 lightId_ = lightSourceHandle->getLight(
this);
524 if(
lightId_ == GL_INVALID_ENUM)
return;
534 if(
light_.fixedPosition_) {
565 if(
lightId_ == GL_INVALID_ENUM)
return;
577 lightSourceHandle->removeLight(
this);
586 Vec4f& a = _light.ambientColor_;
587 GLfloat ambient[4] = {a[0]*_light.brightness_,
588 a[1]*_light.brightness_,
589 a[2]*_light.brightness_,
590 a[3]*_light.brightness_};
592 Vec4f& d = _light.diffuseColor_;
593 GLfloat diffuse[4] = {d[0]*_light.brightness_,
594 d[1]*_light.brightness_,
595 d[2]*_light.brightness_,
596 d[3]*_light.brightness_};
598 Vec4f& s = _light.specularColor_;
599 GLfloat specular[4] = {s[0]*_light.brightness_,
600 s[1]*_light.brightness_,
601 s[2]*_light.brightness_,
602 s[3]*_light.brightness_};
605 glLightfv(_index, GL_AMBIENT, ambient);
606 glLightfv(_index, GL_DIFFUSE, diffuse);
607 glLightfv(_index, GL_SPECULAR, specular);
609 Vec3d& sd = _light.realSpotDirection_;
613 Vec4f& p = _light.realPosition_;
614 GLfloat realPos[4] = {(float)p[0], (
float)p[1], (float)p[2], (directional ? 0.0f : 1.0f)};
616 glLightfv(_index, GL_POSITION, realPos);
619 GLfloat dir[3] = {(float)sd[0], (
float)sd[1], (float)sd[2]};
620 glLightfv(_index, GL_SPOT_DIRECTION, dir);
623 if(!directional) glLightf(_index, GL_SPOT_EXPONENT, _light.spotExponent_);
624 if(!directional) glLightf(_index, GL_SPOT_CUTOFF, _light.spotCutoff_);
626 glLightf(_index, GL_CONSTANT_ATTENUATION, _light.constantAttenuation_);
627 glLightf(_index, GL_LINEAR_ATTENUATION, _light.linearAttenuation_);
628 glLightf(_index, GL_QUADRATIC_ATTENUATION, _light.quadraticAttenuation_);
636 glGetLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor_.data());
637 glGetLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor_.data());
638 glGetLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor_.data());
639 glGetLightfv(_index, GL_POSITION, (GLfloat *)_light.position_.data());
640 glGetLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection_.data());
642 glGetLightfv(_index, GL_SPOT_EXPONENT, &_light.spotExponent_);
643 glGetLightfv(_index, GL_SPOT_CUTOFF, &_light.spotCutoff_);
644 glGetLightfv(_index, GL_CONSTANT_ATTENUATION, &_light.constantAttenuation_);
645 glGetLightfv(_index, GL_LINEAR_ATTENUATION, &_light.linearAttenuation_);
646 glGetLightfv(_index, GL_QUADRATIC_ATTENUATION, &_light.quadraticAttenuation_);
657 light.ltype = ACG::SG_LIGHT_DIRECTIONAL;
659 light.ltype = ACG::SG_LIGHT_POINT;
661 light.ltype = ACG::SG_LIGHT_SPOT;
663 #define V4toV3(v) (
ACG::Vec3f(v[0], v[1], v[2]))
Vec4f specularColor() const
get Specular color for LightSource
bool enabled() const
Get light source status.
void disable()
disable LightSource
LightSource lightSave_
save old LightSources
Namespace providing different geometric functions concerning angles.
static void enable(GLenum _cap)
replaces glEnable, but supports locking
void pick_set_name(unsigned int _idx)
sets the current name/color (like glLoadName(_idx))
PickTarget
What target to use for picking.
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
Draw light source node.
static void disable(GLenum _cap)
replaces glDisable, but supports locking
bool pick_set_maximum(unsigned int _idx)
Set the maximal number of primitives/components of your object.
picks faces (should be implemented for all nodes)
pick any of the prior targets (should be implemented for all nodes)
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
LightSource()
Default Constructor.
void rotate(double _angle, double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
rotate around axis (_x, _y, _z) by _angle
void diffuseColor(Vec4f _color)
set Diffuse color for LightSource
virtual void addLight(const LightData &_light)
Callback for the scenegraph nodes, which send new lights to the renderer via this function...
VectorT< float, 4 > Vec4f
void fixedPosition(bool _state)
make LightSource fixed or moveable with ModelViewMatrix
VectorT< double, 3 > Vec3d
const GLMatrixd & modelview() const
get modelview matrix
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x',y',z',0) = A * (x,y,z,0)
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat)
Add this light to shader render interface.
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
restores original Light Sources
virtual ~LightNode()
Destructor.
void push_modelview_matrix()
push modelview matrix
Vec3d position() const
Get the position of the LightSource.
const GLenum & depthFunc() const
get glDepthFunc() that is supposed to be active
const GLMatrixd & inverse_modelview() const
get inverse modelview matrix
VectorT< float, 3 > Vec3f
Vec3d direction() const
Get direction of the light source.
void getLightSource(LightSource *_light) const
Get the light source parameters.
Vec4f ambientColor() const
get Ambient color for LightSource
void pop_modelview_matrix()
pop modelview matrix
LightSource transformedLight_
pretransformed light position and direction in view space
void setParameters(GLenum _index, LightSource &_light)
set _light Options in OpenGL for GL_LIGHT::_index
LightNode(BaseNode *_parent=0, const std::string &_name="<LightNode>")
Default constructor. Applies all properties.
LightSource light_
store LightSources of this node
void getParameters(GLenum _index, LightSource &_light)
get _light Options in OpenGL for GL_LIGHT::_index
VectorT< double, 4 > Vec4d
bool visualize_
Indicates whether light node should be visualized or not.
void pick(GLState &_state, PickTarget _target)
Picking.
Structure to hold options for one LightSource.
bool directional() const
Check if the light source is a directional light source.
void specularColor(Vec4f _color)
set Specular color for LightSource
GLenum lightId_
Internal light id.
void enable()
enable LightSource
Vec4f diffuseColor() const
get Diffuse color for LightSource
const GLenum & depthFunc() const
get glDepthFunc() that is supposed to be active
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set current Light Sources
void boundingBox(ACG::Vec3d &, ACG::Vec3d &)
Get bounding box (for visualization purposes)
float radius() const
Get light source radius.
void ambientColor(Vec4f _color)
set Ambient color for LightSource
Vec3d spotDirection() const
get spot direction
void position(Vec3d _pos)
Set position for LightSource.
void spotDirection(Vec3d _pos)
Set spot direction.
void getLightSourceViewSpace(LightSource *_light) const