Commit 3049a2df authored by Mike Kremer's avatar Mike Kremer
Browse files

Light sources are now automatically added each time the scene is cleared.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@8966 383ad7c9-94d9-4d36-a494-682f7c89f535
parent fc89c6da
......@@ -61,7 +61,7 @@
namespace ACG {
namespace SceneGraph {
GLenum nextLight = GL_LIGHT0;
static LightSourceHandle lightSourceHandle;
//== IMPLEMENTATION ==========================================================
......@@ -195,22 +195,29 @@ float LightSource::quadraticAttenuation() {
LightNode::LightNode( BaseNode* _parent,
const std::string& _name)
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;
lightId_(0) {
lightId_ = lightSourceHandle.getLight(this);
//std::cerr << "Getting light id: " << lightId_ << std::endl;
//std::cerr << "GL_MAX_LIGHTS: " << GL_MAX_LIGHTS << std::endl;
}
//----------------------------------------------------------------------------
LightNode::~LightNode() {
lightSourceHandle.removeLight(this);
}
//----------------------------------------------------------------------------
void LightNode::enter(GLState& _state, DrawModes::DrawMode /* _drawmode */ )
{
// Return if we don't have a valid light source
if(lightId_ == GL_INVALID_ENUM) return;
/// transfer GL-preferences to lightSave_
getParameters(lightId_, lightSave_);
......@@ -243,6 +250,9 @@ void LightNode::enter(GLState& _state, DrawModes::DrawMode /* _drawmode */ )
void LightNode::leave(GLState& /* _state */ , DrawModes::DrawMode /* _drawmode*/ )
{
// Return if we don't have a valid light source
if(lightId_ == GL_INVALID_ENUM) return;
// restore old enabled light
if(lightSave_.enabled_)
{
......
......@@ -70,7 +70,6 @@ namespace SceneGraph {
//== CLASS DEFINITION =========================================================
/** \class LightNode LightNode.hh <ACG/Scenegraph/LightNode.hh>
Set LightSources (0 to 7) for this node and all its children.
......@@ -219,7 +218,7 @@ public:
const std::string& _name = "<LightNode>");
/// Destructor.
virtual ~LightNode() {}
virtual ~LightNode();
/// Set the light source parameters
void setLightSource(LightSource _light ) { light_ = _light; };
......@@ -253,6 +252,51 @@ private:
LightSource lightSave_;
};
/**
* \class LightSourceHandle
*
* Assign a unique OpenGL light source identifier to each of the
* light source nodes in the scene. These relations are
* stored in a map. If there is no free light source left,
* return GL_FALSE as light source enumerator.
*/
class DLLEXPORT LightSourceHandle {
public:
LightSourceHandle() {
for(int i = 0; i < GL_MAX_LIGHTS; ++i) {
lights_.insert(std::pair<GLenum, LightNode*>(GL_LIGHT0 + i, 0));
}
};
GLenum getLight(LightNode* _node) {
GLenum light = GL_INVALID_ENUM;
for(std::map<GLenum, LightNode*>::iterator it = lights_.begin();
it != lights_.end(); ++it) {
if(it->second == 0) {
lights_[it->first] = _node;
light = it->first;
break;
}
}
return light;
};
void removeLight(LightNode* _node) {
for(std::map<GLenum, LightNode*>::iterator it = lights_.begin();
it != lights_.end(); ++it) {
if(it->second == _node) {
lights_[it->first] = 0;
break;
}
}
};
private:
std::map<GLenum, LightNode*> lights_;
};
//=============================================================================
} // namespace SceneGraph
......
......@@ -57,8 +57,20 @@ LightObject::~LightObject()
// perObjectData.
deleteData();
// No need to delete the scenegraph Nodes as this will be managed by baseplugin
lightNode_ = NULL;
// Move children to parent
BaseNode* parent = lightNode_->parent();
// First, collect all children as the iterator will get invalid if we delete while iterating!
std::vector< BaseNode*> children;
for (BaseNode::ChildIter cIt=lightNode_->childrenBegin(); cIt!=lightNode_->childrenEnd(); ++cIt)
children.push_back( (*cIt) );
// Move the children
for (unsigned int i = 0 ; i < children.size(); ++i )
children[i]->set_parent(parent);
// Delete the scenegraph node
delete lightNode_;
}
/** Cleanup Function for Light Objects. Deletes the contents of the whole object and
......
......@@ -67,72 +67,98 @@ bool TypeLightPlugin::registerType() {
return true;
}
void TypeLightPlugin::pluginsInitialized(){
// Disable the build in light management and use this plugins light handling
PluginFunctions::disableExaminerLightHandling();
// Generate the default lights
int objectId0 = addEmpty();
BaseObjectData* obj0(0);
if(PluginFunctions::getObject( objectId0, obj0))
{
LightObject* lightObject0 = PluginFunctions::lightObject(obj0);
LightSource* light0 = PluginFunctions::lightSource(lightObject0);
void TypeLightPlugin::slotAllCleared() {
if( light0 ) {
light0->direction(ACG::Vec3d(0.0,0.0,1.0) );
light0->diffuseColor( ACG::Vec4f(0.7,0.7,0.7,0.0));
light0->specularColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
light0->enable();
light0->fixedPosition(true);
// Add default lights to active scene
addDefaultLights();
}
void TypeLightPlugin::addDefaultLights() {
// Test if light sources already exist
int light0 = PluginFunctions::getObjectId("Default Light 0");
int light1 = PluginFunctions::getObjectId("Default Light 1");
int light2 = PluginFunctions::getObjectId("Default Light 2");
if(light0 == -1) {
// Create light 0
light0 = addEmpty();
BaseObjectData* obj0(0);
if(PluginFunctions::getObject( light0, obj0 )) {
LightObject* lightObject0 = PluginFunctions::lightObject(obj0);
LightSource* lightSrc0 = PluginFunctions::lightSource(lightObject0);
if( lightSrc0 ) {
lightSrc0->direction(ACG::Vec3d(0.0,0.0,1.0) );
lightSrc0->diffuseColor( ACG::Vec4f(0.7,0.7,0.7,0.0));
lightSrc0->specularColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
lightSrc0->enable();
lightSrc0->fixedPosition(true);
lightObject0->setName("Default Light 0");
}
emit updatedObject(light0, UPDATE_ALL);
}
}
}
emit updatedObject(objectId0,UPDATE_ALL);
// Generate the default lights
int objectId1 = addEmpty();
BaseObjectData* obj1(0);
if(PluginFunctions::getObject( objectId1, obj1))
{
LightObject* lightObject1 = PluginFunctions::lightObject(obj1);
LightSource* light1 = PluginFunctions::lightSource(lightObject1);
if(light1 == -1) {
// Create light 1
light1 = addEmpty();
BaseObjectData* obj1(0);
if(PluginFunctions::getObject( light1, obj1 )) {
LightObject* lightObject1 = PluginFunctions::lightObject(obj1);
LightSource* lightSrc1 = PluginFunctions::lightSource(lightObject1);
if( light1 ) {
light1->direction(ACG::Vec3d(-1.0, 1.0, 0.7) );
light1->diffuseColor( ACG::Vec4f(0.7,0.7,0.7,0.0));
light1->specularColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
light1->enable();
light1->fixedPosition(true);
if( lightSrc1 ) {
lightSrc1->direction(ACG::Vec3d(-1.0, 1.0, 0.7) );
lightSrc1->diffuseColor( ACG::Vec4f(0.7,0.7,0.7,0.0));
lightSrc1->specularColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
lightSrc1->enable();
lightSrc1->fixedPosition(true);
lightObject1->setName("Default Light 1");
}
emit updatedObject(light1, UPDATE_ALL);
}
}
}
emit updatedObject(objectId1,UPDATE_ALL);
//Generate the default lights
int objectId2 = addEmpty();
if(light2 == -1) {
//Generate the default lights
light2 = addEmpty();
BaseObjectData* obj2(0);
if(PluginFunctions::getObject( objectId2, obj2))
{
LightObject* lightObject2 = PluginFunctions::lightObject(obj2);
LightSource* light2 = PluginFunctions::lightSource(lightObject2);
BaseObjectData* obj2(0);
if(PluginFunctions::getObject( light2, obj2 )) {
LightObject* lightObject2 = PluginFunctions::lightObject(obj2);
LightSource* lightSrc2 = PluginFunctions::lightSource(lightObject2);
if( light2 ) {
light2->direction(ACG::Vec3d( 1.0, 1.0, 0.7) );
light2->diffuseColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
light2->specularColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
light2->enable();
light2->fixedPosition(true);
if( lightSrc2 ) {
lightSrc2->direction(ACG::Vec3d( 1.0, 1.0, 0.7) );
lightSrc2->diffuseColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
lightSrc2->specularColor(ACG::Vec4f(0.7,0.7,0.7,0.0));
lightSrc2->enable();
lightSrc2->fixedPosition(true);
lightObject2->setName("Default Light 2");
}
emit updatedObject(light2, UPDATE_ALL);
}
}
}
}
void TypeLightPlugin::pluginsInitialized(){
emit updatedObject(objectId2,UPDATE_ALL);
// Disable the build in light management and use this plugins light handling
PluginFunctions::disableExaminerLightHandling();
// Add default light sources to active scene
addDefaultLights();
}
int TypeLightPlugin::addEmpty(){
......
......@@ -77,6 +77,12 @@ class TypeLightPlugin : public QObject, BaseInterface, LoadSaveInterface, Loggin
private slots:
void noguiSupported( ) {} ;
// Is called whenever the scene is cleared
void slotAllCleared();
// Adds three default light sources to scene
void addDefaultLights();
public :
......
Supports Markdown
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