Commit d6cc11c9 authored by Jan Möbius's avatar Jan Möbius
Browse files

Updated new Light Node

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@8905 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 16889df0
......@@ -61,17 +61,19 @@
namespace ACG {
namespace SceneGraph {
GLenum nextLight = GL_LIGHT0;
//== IMPLEMENTATION ==========================================================
LightNode::LightNode( BaseNode* _parent,
const std::string& _name)
: BaseNode(_parent, _name)
const std::string& _name)
: BaseNode(_parent, _name),
lightId_(0)
{
lights_.resize(8);
lightsSave_.resize(8);
enable(GL_LIGHT0);;
lightId_ = nextLight;
nextLight++;
//std::cerr << "Maximum light sources : " << GL_MAX_LIGHTS << std::endl;
}
......@@ -79,37 +81,31 @@ LightNode::LightNode( BaseNode* _parent,
void LightNode::enter(GLState& _state, DrawModes::DrawMode /* _drawmode */ )
{
// save old lights
for(unsigned int i=0; i<lightsSave_.size(); i++)
{
// save only if enabled
if(glIsEnabled(index2gl(i)))
{
lightsSave_[i].enabled = true;
/// transfer GL-preferences to lightsSave_
get_parameters(index2gl(i), lightsSave_[i]);
}
else lightsSave_[i].enabled = false;
}
// set new lights
for(unsigned int i=0; i<lights_.size(); i++)
{
if(lights_[i].enabled)
{
// correct Position for fixed Lights
if(lights_[i].fixedPosition)
lights_[i].realPosition =
_state.inverse_modelview()*lights_[i].position;
else lights_[i].realPosition = lights_[i].position;
glEnable(index2gl(i));
set_parameters(index2gl(i), lights_[i]);
/// transfer GL-preferences to lightSave_
getParameters(lightId_, lightSave_);
// save old light
lightSave_.enabled = glIsEnabled(lightId_);
if(light_.enabled) {
// correct Position for fixed Lights
if(light_.fixedPosition) {
light_.realPosition = _state.inverse_modelview() * light_.position;
// std::cerr << "New Light pos :" << _state.inverse_modelview().transform_vector(light_.position) << std::endl;
} else {
light_.realPosition = light_.position;
// std::cerr << "New Light pos :" << light_.position << std::endl;
}
else glDisable(index2gl(i));
}
glEnable(lightId_);
setParameters(lightId_, light_);
} else
glDisable(lightId_);
}
......@@ -118,21 +114,18 @@ void LightNode::enter(GLState& _state, DrawModes::DrawMode /* _drawmode */ )
void LightNode::leave(GLState& /* _state */ , DrawModes::DrawMode /* _drawmode*/ )
{
// restore old enabled lights
for(unsigned int i=0; i<lights_.size(); i++)
// restore old enabled light
if(lightSave_.enabled)
{
if(lightsSave_[i].enabled)
{
glEnable(index2gl(i));
set_parameters(index2gl(i), lightsSave_[i]);
}
else glDisable(index2gl(i));
glEnable(lightId_);
setParameters(lightId_, lightSave_);
}
else glDisable(lightId_);
}
//----------------------------------------------------------------------------
void LightNode::set_parameters(GLenum _index, LightSource& _light)
void LightNode::setParameters(GLenum _index, LightSource& _light)
{
// set preferences of _light for GL_LIGHT#_index
......@@ -148,11 +141,24 @@ void LightNode::set_parameters(GLenum _index, LightSource& _light)
glLightf(_index, GL_CONSTANT_ATTENUATION, _light.constantAttenuation);
glLightf(_index, GL_LINEAR_ATTENUATION, _light.linearAttenuation);
glLightf(_index, GL_QUADRATIC_ATTENUATION, _light.quadraticAttenuation);
// std::cerr << " set GL_AMBIENT : " << _index << " " << _light.ambientColor << std::endl;
// std::cerr << " set GL_DIFFUSE : " << _index << " " << _light.diffuseColor << std::endl;
// std::cerr << " set GL_SPECULAR : " << _index << " " << _light.specularColor << std::endl;
//
// std::cerr << " set GL_POSITION : " << _index << " " << _light.realPosition << std::endl;
// std::cerr << " set GL_SPOT_DIRECTION : " << _index << " " << _light.spotDirection << std::endl;
//
// std::cerr << " set GL_SPOT_EXPONENT : " << _index << " " << _light.spotExponent << std::endl;
// std::cerr << " set GL_SPOT_CUTOFF : " << _index << " " << _light.spotCutoff << std::endl;
// std::cerr << " set GL_CONSTANT_ATTENUATION : " << _index << " " << _light.constantAttenuation << std::endl;
// std::cerr << " set GL_LINEAR_ATTENUATION : " << _index << " " << _light.linearAttenuation << std::endl;
// std::cerr << " set GL_QUADRATIC_ATTENUATION : " << _index << " " << _light.quadraticAttenuation << std::endl;
// std::cerr << "===============================================================================" << std::endl;
}
//----------------------------------------------------------------------------
void LightNode::get_parameters(GLenum _index, LightSource& _light)
void LightNode::getParameters(GLenum _index, LightSource& _light)
{
// get preferences of GL_LIGHT#_index and store them in _light
glGetLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
......@@ -166,6 +172,18 @@ void LightNode::get_parameters(GLenum _index, LightSource& _light)
glGetLightfv(_index, GL_CONSTANT_ATTENUATION, &_light.constantAttenuation);
glGetLightfv(_index, GL_LINEAR_ATTENUATION, &_light.linearAttenuation);
glGetLightfv(_index, GL_QUADRATIC_ATTENUATION, &_light.quadraticAttenuation);
// std::cerr << " get GL_AMBIENT : " << _index << " " << _light.ambientColor << std::endl;
// std::cerr << " get GL_DIFFUSE : " << _index << " " << _light.diffuseColor << std::endl;
// std::cerr << " get GL_SPECULAR : " << _index << " " << _light.specularColor << std::endl;
//
// std::cerr << " get GL_POSITION : " << _index << " " << _light.realPosition << std::endl;
// std::cerr << " get GL_SPOT_DIRECTION : " << _index << " " << _light.spotDirection << std::endl;
//
// std::cerr << " get GL_SPOT_EXPONENT : " << _index << " " << _light.spotExponent << std::endl;
// std::cerr << " get GL_SPOT_CUTOFF : " << _index << " " << _light.spotCutoff << std::endl;
// std::cerr << " get GL_CONSTANT_ATTENUATION : " << _index << " " << _light.constantAttenuation << std::endl;
// std::cerr << " get GL_LINEAR_ATTENUATION : " << _index << " " << _light.linearAttenuation << std::endl;
// std::cerr << " get GL_QUADRATIC_ATTENUATION : " << _index << " " << _light.quadraticAttenuation << std::endl;
}
//=============================================================================
} // namespace SceneGraph
......
......@@ -84,6 +84,8 @@ namespace SceneGraph {
class ACGDLLEXPORT LightNode : public BaseNode
{
public:
/// Structure to hold options for one LightSource
struct LightSource
{
......@@ -94,12 +96,12 @@ class ACGDLLEXPORT LightNode : public BaseNode
enabled = false;
fixedPosition = false;
ambientColor = Vec4f(0.1,0.1,0.1,1);
diffuseColor = Vec4f(1,1,1,1);
specularColor = Vec4f(1,1,1,1);
position = Vec4f(0,0,1,0);
realPosition = Vec4f(0,0,1,0);
spotDirection = Vec3f(0,0,-1);
ambientColor = Vec4f(0.1,0.1,0.1,1.0);
diffuseColor = Vec4f(1.0,1.0,1.0,1.0);
specularColor = Vec4f(1.0,1.0,1.0,1.0);
position = Vec4f(0.0,0.0,1.0,0.0);
realPosition = Vec4f(0.0,0.0,1.0,0.0);
spotDirection = Vec3d(0.0,0.0,-1.0);
spotExponent = 0;
spotCutoff = 180;
......@@ -115,7 +117,7 @@ class ACGDLLEXPORT LightNode : public BaseNode
Vec4f specularColor;
Vec4f position;
Vec4f realPosition;
Vec3f spotDirection;
Vec3d spotDirection;
float spotExponent;
float spotCutoff;
float constantAttenuation;
......@@ -124,7 +126,7 @@ class ACGDLLEXPORT LightNode : public BaseNode
};
public:
/// Default constructor. Applies all properties.
LightNode( BaseNode* _parent = 0,
......@@ -133,7 +135,6 @@ public:
/// Destructor.
virtual ~LightNode() {}
ACG_CLASSNAME(LightNode);
/// set current Light Sources
......@@ -142,64 +143,129 @@ public:
void leave(GLState& _state, DrawModes::DrawMode _drawmode);
/// enable LightSource _nr
void enable(GLenum _nr)
{ lights_[gl2index(_nr)].enabled = true; }
void enable()
{ light_.enabled = true; }
/// disable LightSource _nr
void disable(GLenum _nr)
{ lights_[gl2index(_nr)].enabled = false; }
/// set position ( _pos = 1) or direction ( _pos = 0) of LightSource
void set_position(GLenum _nr, Vec4f _pos)
{ lights_[gl2index(_nr)].position = _pos; }
/// set position for Point-LightSource
void set_position(GLenum _nr, Vec3f _pos)
{ set_position(_nr, Vec4f(_pos[0], _pos[1], _pos[2], 1)); }
void disable()
{ light_.enabled = false; }
bool enabled() {
return light_.enabled;
}
/// set position for LightSource
void position( Vec3d _pos)
{ light_.position = Vec4d( _pos[0],_pos[1],_pos[2],1.0); }
/// Get the position of the LightSource
Vec3d position() {
return Vec3d(light_.position[0],light_.position[1],light_.position[2]);
}
/// set direction for LightSource ( this switches the light source to directional mode
void direction( Vec3d _pos)
{ light_.position = Vec4d( _pos[0],_pos[1],_pos[2],0.0); }
/// set direction for directional LightSource
void set_direction(GLenum _nr, Vec3f _pos)
{ set_position(_nr, Vec4f(_pos[0], _pos[1], _pos[2], 0)); }
void spotDirection( Vec3d _pos)
{ light_.spotDirection = _pos; }
/// get direction for directional LightSource
Vec3d spotDirection( ) {
return Vec3d(light_.spotDirection[0],light_.spotDirection[1],light_.spotDirection[2]);
}
/// set Ambient color for LightSource _nr
void set_ambient_color( GLenum _nr, Vec4f _color)
{ lights_[gl2index(_nr)].ambientColor = _color; }
void ambientColor( Vec4f _color)
{ light_.ambientColor = _color; }
/// get Ambient color for LightSource _nr
Vec4f ambientColor()
{ return light_.ambientColor; }
/// set Diffuse color for LightSource _nr
void set_diffuse_color( GLenum _nr, Vec4f _color)
{ lights_[gl2index(_nr)].diffuseColor = _color; }
void diffuseColor( Vec4f _color)
{ light_.diffuseColor = _color; }
/// get Diffuse color for LightSource _nr
Vec4f diffuseColor()
{ return light_.diffuseColor; }
/// set Specular color for LightSource _nr
void set_specular_color( GLenum _nr, Vec4f _color)
{ lights_[gl2index(_nr)].specularColor = _color; }
void specularColor( Vec4f _color)
{ light_.specularColor = _color; }
/// get Specular color for LightSource _nr
Vec4f specularColor()
{ return light_.specularColor; }
/// make LightSource fixed or moveable with ModelViewMatrix
void fixed_position(GLenum _nr, bool _state)
{ lights_[gl2index(_nr)].fixedPosition = _state; }
void fixedPosition( bool _state)
{ light_.fixedPosition = _state; }
bool fixedPosition() {
return light_.fixedPosition;
}
void spotExponent(float _exponent) {
light_.spotExponent = _exponent;
}
float spotExponent() {
return light_.spotExponent;
}
void spotCutoff(float _cutoff) {
light_.spotCutoff = _cutoff;
}
float spotCutoff() {
return light_.spotCutoff;
}
void constantAttenuation(float _constantAttenuation) {
light_.constantAttenuation = _constantAttenuation;
}
float constantAttenuation() {
return light_.constantAttenuation;
}
void linearAttenuation(float _linearAttenuation) {
light_.linearAttenuation = _linearAttenuation;
}
float linearAttenuation() {
return light_.linearAttenuation;
}
void quadraticAttenuation(float _quadraticAttenuation) {
light_.quadraticAttenuation = _quadraticAttenuation;
}
float quadraticAttenuation() {
return light_.quadraticAttenuation;
}
private:
/// return index in vector for GL_LIGHT*
int gl2index( GLenum _nr)
{ return( _nr - GL_LIGHT0); }
/// return GL_LIGHT* for light _nr
GLenum index2gl( int _nr)
{ return( _nr + GL_LIGHT0); }
/// set _light Options in OpenGL for GL_LIGHT#_index
void set_parameters(GLenum _index, LightSource& _light);
void setParameters(GLenum _index, LightSource& _light);
/// get _light Options in OpenGL for GL_LIGHT#_index
void get_parameters(GLenum _index, LightSource& _light);
void getParameters(GLenum _index, LightSource& _light);
private:
GLenum lightId_;
/// store LightSources of this node
std::vector<LightSource> lights_;
LightSource light_;
/// save old LightSources
std::vector<LightSource> lightsSave_;
LightSource lightSave_;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment