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

Added Light Object

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@8936 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 5b5f329f
/*===========================================================================*\
* *
* 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 ==========================================================
//default Constructor
LightSource::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;
}
void LightSource::position( Vec3d _pos){
// Set homogeneous coordinte to 1.0 to get a positional light source
position_ = Vec4d( _pos[0],_pos[1],_pos[2],1.0);
}
Vec3d LightSource::position() {
return Vec3d( position_[0], position_[1], position_[2]);
}
void LightSource::direction( Vec3d _pos) {
// Set homogeneous coordinate of position to 0.0 to tell OpenGL
// that this is a directional light source
position_ = Vec4d( _pos[0],_pos[1],_pos[2],0.0);
}
bool LightSource::directional() {
return ( position_[3] == 0.0 );
}
void LightSource::enable()
{ enabled_ = true; }
void LightSource::disable()
{ enabled_ = false; }
bool LightSource::enabled() {
return enabled_;
}
void LightSource::spotDirection( Vec3d _pos)
{ spotDirection_ = _pos; }
Vec3d LightSource::spotDirection( ) {
return Vec3d(spotDirection_[0],spotDirection_[1],spotDirection_[2]);
}
void LightSource::ambientColor( Vec4f _color)
{ ambientColor_ = _color; }
Vec4f LightSource::ambientColor()
{ return ambientColor_; }
void LightSource::diffuseColor( Vec4f _color)
{ diffuseColor_ = _color; }
Vec4f LightSource::diffuseColor()
{ return diffuseColor_; }
void LightSource::specularColor( Vec4f _color)
{ specularColor_ = _color; }
Vec4f LightSource::specularColor()
{ return specularColor_; }
void LightSource::fixedPosition( bool _state)
{ fixedPosition_ = _state; }
bool LightSource::fixedPosition() {
return fixedPosition_;
}
void LightSource::spotExponent(float _exponent) {
spotExponent_ = _exponent;
}
float LightSource::spotExponent() {
return spotExponent_;
}
void LightSource::spotCutoff(float _cutoff) {
spotCutoff_ = _cutoff;
}
float LightSource::spotCutoff() {
return spotCutoff_;
}
void LightSource::constantAttenuation(float _constantAttenuation) {
constantAttenuation_ = _constantAttenuation;
}
float LightSource::constantAttenuation() {
return constantAttenuation_;
}
void LightSource::linearAttenuation(float _linearAttenuation) {
linearAttenuation_ = _linearAttenuation;
}
float LightSource::linearAttenuation() {
return linearAttenuation_;
}
void LightSource::quadraticAttenuation(float _quadraticAttenuation) {
quadraticAttenuation_ = _quadraticAttenuation;
}
float LightSource::quadraticAttenuation() {
return quadraticAttenuation_;
}
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 "ACG/Scenegraph/BaseNode.hh"
#include "ACG/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.
**/
/// Structure to hold options for one LightSource
class LightSource
{
// Declare Node as friend for direct access to memberss
friend class LightNode;
public:
/// Default Constructor
LightSource();
/** \brief Set position for LightSource
*
* This function sets the position of a light source. OpenGL handles
* a position for a light source if the homogeneous coordinate is
* 1.0. If it is 0.0 the light source will be directional. So a
* 1.0 is set in this function to get a positioned light source.
*/
void position( Vec3d _pos);
/** \brief Get the position of the LightSource
*
* Be careful with this function. Check if the light source is not directional with
* the directional() function
*/
Vec3d position();
/** \brief Check if the light source is a directional light source
*
* If this light Source is a directional light source the function will return true.
* Otherwise false as the light source has a position.
*/
bool directional();
/** \brief Set the direction of the light source
*
* This function creates a directional light source. The position is not used by OpenGL.
* Internally the positions homogeneous coordinate is set to 0.0.
*/
void direction( Vec3d _pos);
/// enable LightSource
void enable();
/// disable LightSource
void disable();
/// Get light source status
bool enabled();
/** \brief Set spot direction
*
* \todo Improve documentation
*/
void spotDirection( Vec3d _pos);
/** \brief get spot direction
*
* \todo Improve documentation
*/
Vec3d spotDirection( );
/// set Ambient color for LightSource
void ambientColor( Vec4f _color);
/// get Ambient color for LightSource
Vec4f ambientColor();
/// set Diffuse color for LightSource
void diffuseColor( Vec4f _color);
/// get Diffuse color for LightSource
Vec4f diffuseColor();
/// set Specular color for LightSource
void specularColor( Vec4f _color);
/// get Specular color for LightSource
Vec4f specularColor();
/// make LightSource fixed or moveable with ModelViewMatrix
void fixedPosition( bool _state);
bool fixedPosition();
void spotExponent(float _exponent);
float spotExponent();
void spotCutoff(float _cutoff);
float spotCutoff();
void constantAttenuation(float _constantAttenuation);
float constantAttenuation();
void linearAttenuation(float _linearAttenuation);
float linearAttenuation();
void quadraticAttenuation(float _quadraticAttenuation);
float quadraticAttenuation();
protected:
Vec4f position_;
bool enabled_;
bool fixedPosition_;
Vec4f ambientColor_;
Vec4f diffuseColor_;
Vec4f specularColor_;
Vec4f realPosition_;
Vec3d spotDirection_;
float spotExponent_;
float spotCutoff_;
float constantAttenuation_;
float linearAttenuation_;
float quadraticAttenuation_;
};
class ACGDLLEXPORT LightNode : public BaseNode
{
public:
/// Default constructor. Applies all properties.
LightNode( BaseNode* _parent = 0,
const std::string& _name = "<LightNode>");
/// Destructor.
virtual ~LightNode() {}
/// Set the light source parameters
void setLightSource(LightSource _light ) { light_ = _light; };
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);
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
//=============================================================================
......@@ -93,7 +93,7 @@ void LightObject::init(LightNode* _light) {
lightNode_ = new LightNode( 0 , "NEW LightNode");
// Light nodes have to be on top of all other nodes.
PluginFunctions::addObjectRenderingNode(lightNode_);
PluginFunctions::addGlobalStatusNode(lightNode_);
/*
......@@ -149,7 +149,7 @@ QString LightObject::getObjectinfo() {
output += "Object Contains Light : ";
///\TODO Output info
ACG::Vec3d pos = lightNode_->position();
ACG::Vec3d pos = lightSource_.position();
// ACG::Vec3f nor = lightNode_->normal();
output += " Position ( " + QString::number(pos[0]) + ", " + QString::number(pos[1]) + ", " + QString::number(pos[2]) + ")";
......@@ -210,6 +210,15 @@ void LightObject::visible(bool _visible) {
hide();
}
LightSource* LightObject::lightSource() {
return &lightSource_;
}
void LightObject::update(UpdateType _type) {
lightNode_->setLightSource(lightSource_);
std::cerr << "Update" << std::endl;
}
//=============================================================================
......@@ -62,7 +62,28 @@ class DLLEXPORT LightObject : public BaseObjectData {
/// Set the name of the Object
void setName( QString _name );
/** @} */
//===========================================================================
/** @name Contents
* @{ */
//===========================================================================
public:
LightSource* lightSource();
/** \brief Update the Light Object
*