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

Moved Light Node to LightObject

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@8935 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 56041d62
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 8548 $ *
* $Author: moebius $ *
* $Date: 2010-02-12 14:40:13 +0100 (Fr, 12. Feb 2010) $ *
* *
\*===========================================================================*/
//=============================================================================
//
// CLASS LightNode - IMPLEMENTATION
//
//=============================================================================
//== INCLUDES =================================================================
#include "LightNode.hh"
//== NAMESPACES ===============================================================
namespace ACG {
namespace SceneGraph {
GLenum nextLight = GL_LIGHT0;
//== IMPLEMENTATION ==========================================================
LightNode::LightNode( BaseNode* _parent,
const std::string& _name)
: BaseNode(_parent, _name),
lightId_(0)
{
lightId_ = nextLight;
nextLight++;
//std::cerr << "Maximum light sources : " << GL_MAX_LIGHTS << std::endl;
}
//----------------------------------------------------------------------------
void LightNode::enter(GLState& _state, DrawModes::DrawMode /* _drawmode */ )
{
/// 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;
}
glEnable(lightId_);
setParameters(lightId_, light_);
} else
glDisable(lightId_);
}
//----------------------------------------------------------------------------
void LightNode::leave(GLState& /* _state */ , DrawModes::DrawMode /* _drawmode*/ )
{
// restore old enabled light
if(lightSave_.enabled)
{
glEnable(lightId_);
setParameters(lightId_, lightSave_);
}
else glDisable(lightId_);
}
//----------------------------------------------------------------------------
void LightNode::setParameters(GLenum _index, LightSource& _light)
{
// set preferences of _light for GL_LIGHT#_index
glLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
glLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor.data());
glLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor.data());
glLightfv(_index, GL_POSITION, (GLfloat *)_light.realPosition.data());
glLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection.data());
glLightf(_index, GL_SPOT_EXPONENT, _light.spotExponent);
glLightf(_index, GL_SPOT_CUTOFF, _light.spotCutoff);
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::getParameters(GLenum _index, LightSource& _light)
{
// get preferences of GL_LIGHT#_index and store them in _light
glGetLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
glGetLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor.data());
glGetLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor.data());
glGetLightfv(_index, GL_POSITION, (GLfloat *)_light.position.data());
glGetLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection.data());
glGetLightfv(_index, GL_SPOT_EXPONENT, &_light.spotExponent);
glGetLightfv(_index, GL_SPOT_CUTOFF, &_light.spotCutoff);
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
} // namespace ACG
//=============================================================================
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 8548 $ *
* $Author: moebius $ *
* $Date: 2010-02-12 14:40:13 +0100 (Fr, 12. Feb 2010) $ *
* *
\*===========================================================================*/
//=============================================================================
//
// CLASS LightNode
//
//=============================================================================
#ifndef ACG_LIGHT_NODE_HH
#define ACG_LIGHT_NODE_HH
//== INCLUDES =================================================================
#include "BaseNode.hh"
#include "../GL/gl.hh"
#include <string>
#include <vector>
//== NAMESPACES ===============================================================
namespace ACG {
namespace SceneGraph {
//== CLASS DEFINITION =========================================================
/** \class LightNode LightNode.hh <ACG/Scenegraph/LightNode.hh>
Set LightSources (0 to 7) for this node and all its children.
All changes will be done in the enter() method undone
in the leave() method.
**/
class ACGDLLEXPORT LightNode : public BaseNode
{
public:
/// Structure to hold options for one LightSource
struct LightSource
{
//default Constructor
LightSource()
{
// set OpenGL defaults
enabled = false;
fixedPosition = false;
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;
constantAttenuation = 1;
linearAttenuation = 0;
quadraticAttenuation = 0;
}
bool enabled;
bool fixedPosition;
Vec4f ambientColor;
Vec4f diffuseColor;
Vec4f specularColor;
Vec4f position;
Vec4f realPosition;
Vec3d spotDirection;
float spotExponent;
float spotCutoff;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
};
/// Default constructor. Applies all properties.
LightNode( BaseNode* _parent = 0,
const std::string& _name = "<LightNode>");
/// Destructor.
virtual ~LightNode() {}
ACG_CLASSNAME(LightNode);
/// set current Light Sources
void enter(GLState& _state, DrawModes::DrawMode _drawmode);
/// restores original Light Sources
void leave(GLState& _state, DrawModes::DrawMode _drawmode);
/// enable LightSource _nr
void enable()
{ light_.enabled = true; }
/// disable LightSource _nr
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 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 ambientColor( Vec4f _color)
{ light_.ambientColor = _color; }
/// get Ambient color for LightSource _nr
Vec4f ambientColor()
{ return light_.ambientColor; }
/// set Diffuse color for LightSource _nr
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 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 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:
/// set _light Options in OpenGL for GL_LIGHT#_index
void setParameters(GLenum _index, LightSource& _light);
/// get _light Options in OpenGL for GL_LIGHT#_index
void getParameters(GLenum _index, LightSource& _light);
private:
GLenum lightId_;
/// store LightSources of this node
LightSource light_;
/// save old LightSources
LightSource lightSave_;
};
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================
#endif // ACG_LIGHT_NODE_HH defined
//=============================================================================
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