From 6ad85fbae9a8108eb20099b76c337ff9882735fe Mon Sep 17 00:00:00 2001 From: frohlich Date: Thu, 23 Nov 2006 17:50:49 +0000 Subject: [PATCH] Modified Files: src/Main/renderer.cxx: Use a LightSource for the Light. --- src/Main/renderer.cxx | 60 ++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx index e60254151..f4cfdf84e 100644 --- a/src/Main/renderer.cxx +++ b/src/Main/renderer.cxx @@ -41,7 +41,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -206,15 +208,14 @@ public: private: }; - -class FGSunLightUpdateCallback : public osg::StateAttribute::Callback { +class FGLightSourceUpdateCallback : public osg::NodeCallback { public: - virtual void operator()(osg::StateAttribute* stateAttribute, - osg::NodeVisitor*) + virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { - assert(dynamic_cast(stateAttribute)); - osg::Light* light = static_cast(stateAttribute); - + assert(dynamic_cast(node)); + osg::LightSource* lightSource = static_cast(node); + osg::Light* light = lightSource->getLight(); + FGLight *l = static_cast(globals->get_subsystem("lighting")); light->setAmbient(l->scene_ambient().osg()); light->setDiffuse(l->scene_diffuse().osg()); @@ -228,6 +229,8 @@ public: light->setConstantAttenuation(1); light->setLinearAttenuation(0); light->setQuadraticAttenuation(0); + + traverse(node, nv); } }; @@ -388,7 +391,6 @@ FGRenderer::init( void ) { sceneView->getCamera()->setClearMask(GL_COLOR_BUFFER_BIT); osg::StateSet* stateSet = mRoot->getOrCreateStateSet(); - stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN); stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); @@ -416,15 +418,6 @@ FGRenderer::init( void ) { // stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON); - // need to update the light on every frame - osg::Light* sunLight = new osg::Light; - sunLight->setLightNum(0); - sunLight->setUpdateCallback(new FGSunLightUpdateCallback); - stateSet->setAttributeAndModes(sunLight, osg::StateAttribute::ON); - osg::LightModel* lightModel = new osg::LightModel; - lightModel->setUpdateCallback(new FGLightModelUpdateCallback); - stateSet->setAttributeAndModes(lightModel, osg::StateAttribute::ON); - // this is the topmost scenegraph node for osg mBackGroundCamera->addChild(thesky->getPreRoot()); mBackGroundCamera->setClearMask(0); @@ -436,6 +429,7 @@ FGRenderer::init( void ) { mBackGroundCamera->setInheritanceMask(inheritanceMask); mBackGroundCamera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); mBackGroundCamera->setCullingMode(osg::CullSettings::NO_CULLING); + mBackGroundCamera->setRenderOrder(osg::CameraNode::NESTED_RENDER); mBackGroundCamera->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); @@ -448,7 +442,23 @@ FGRenderer::init( void ) { mSceneCamera->setInheritanceMask(inheritanceMask); mSceneCamera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); mSceneCamera->setCullingMode(osg::CullSettings::DEFAULT_CULLING); + mSceneCamera->setRenderOrder(osg::CameraNode::NESTED_RENDER); + mRoot->addChild(mSceneCamera.get()); + stateSet = mSceneCamera->getOrCreateStateSet(); + stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); + stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); + + // need to update the light on every frame + osg::LightSource* lightSource = new osg::LightSource; + lightSource->setUpdateCallback(new FGLightSourceUpdateCallback); + // relative because of CameraView being just a clever transform node + lightSource->setReferenceFrame(osg::LightSource::RELATIVE_RF); + lightSource->setLocalStateSetModes(osg::StateAttribute::ON); + mSceneCamera->addChild(lightSource); + + lightSource->addChild(globals->get_scenery()->get_scene_graph()); + lightSource->addChild(thesky->getCloudRoot()); stateSet = globals->get_scenery()->get_scene_graph()->getOrCreateStateSet(); stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); @@ -456,6 +466,12 @@ FGRenderer::init( void ) { stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); + // enable disable specular highlights. + // is the place where we might plug in an other fragment shader ... + osg::LightModel* lightModel = new osg::LightModel; + lightModel->setUpdateCallback(new FGLightModelUpdateCallback); + stateSet->setAttribute(lightModel); + // switch to enable wireframe osg::PolygonMode* polygonMode = new osg::PolygonMode; polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback); @@ -465,8 +481,6 @@ FGRenderer::init( void ) { stateSet->setAttributeAndModes(mFog.get()); stateSet->setUpdateCallback(new FGFogEnableUpdateCallback); - mRoot->addChild(mSceneCamera.get()); - // plug in the GUI osg::CameraNode* guiCamera = new osg::CameraNode; guiCamera->setRenderOrder(osg::CameraNode::POST_RENDER); @@ -483,12 +497,6 @@ FGRenderer::init( void ) { geode->addDrawable(new SGHUDAndPanelDrawable); guiCamera->addChild(geode); - mSceneCamera->addChild(globals->get_scenery()->get_scene_graph()); - - stateSet = mSceneCamera->getOrCreateStateSet(); - stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); - stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); - // this one contains all lights, here we set the light states we did // in the plib case with plain OpenGL osg::Group* lightGroup = new osg::Group; @@ -514,8 +522,6 @@ FGRenderer::init( void ) { mCameraView->addChild(mRoot.get()); sceneView->setSceneData(mCameraView.get()); - mSceneCamera->addChild(thesky->getCloudRoot()); - // sceneView->getState()->setCheckForGLErrors(osg::State::ONCE_PER_ATTRIBUTE); }