diff --git a/apps/opencs/view/render/lighting.cpp b/apps/opencs/view/render/lighting.cpp index 3553ef58c..8e068168f 100644 --- a/apps/opencs/view/render/lighting.cpp +++ b/apps/opencs/view/render/lighting.cpp @@ -1,4 +1,5 @@ - #include "lighting.hpp" +#include + CSVRender::Lighting::~Lighting() {} diff --git a/apps/opencs/view/render/lighting.hpp b/apps/opencs/view/render/lighting.hpp index eb8c97b12..a4315d02f 100644 --- a/apps/opencs/view/render/lighting.hpp +++ b/apps/opencs/view/render/lighting.hpp @@ -1,13 +1,13 @@ #ifndef OPENCS_VIEW_LIGHTING_H #define OPENCS_VIEW_LIGHTING_H -namespace osgViewer -{ - class View; -} +#include + namespace osg { class Vec4f; + class LightSource; + class Group; } namespace CSVRender @@ -16,14 +16,19 @@ namespace CSVRender { public: + Lighting() : mRootNode(0) {} virtual ~Lighting(); - virtual void activate (osgViewer::View* view, - const osg::Vec4f *defaultAmbient = 0) = 0; + virtual void activate (osg::Group* rootNode) = 0; virtual void deactivate() = 0; - virtual void setDefaultAmbient (const osg::Vec4f& colour) = 0; + virtual osg::Vec4f getAmbientColour(osg::Vec4f* defaultAmbient) = 0; + + protected: + + osg::ref_ptr mLightSource; + osg::Group* mRootNode; }; } diff --git a/apps/opencs/view/render/lightingbright.cpp b/apps/opencs/view/render/lightingbright.cpp index 6d3d4d790..035e57c56 100644 --- a/apps/opencs/view/render/lightingbright.cpp +++ b/apps/opencs/view/render/lightingbright.cpp @@ -1,30 +1,35 @@ #include "lightingbright.hpp" -#include +#include -#include +CSVRender::LightingBright::LightingBright() {} -CSVRender::LightingBright::LightingBright() : mView(NULL) {} - -void CSVRender::LightingBright::activate (osgViewer::View* view, - const osg::Vec4f* /*defaultAmbient*/) +void CSVRender::LightingBright::activate (osg::Group* rootNode) { - mView = view; + mRootNode = rootNode; - // FIXME: ambient should be applied to LightModel instead of the light + mLightSource = (new osg::LightSource); osg::ref_ptr light (new osg::Light); - light->setConstantAttenuation(1.f); + light->setAmbient(osg::Vec4f(0.f, 0.f, 0.f, 1.f)); light->setDirection(osg::Vec3f(0.f, 0.f, -1.f)); light->setDiffuse(osg::Vec4f(1.f, 1.f, 1.f, 1.f)); - light->setAmbient(osg::Vec4f(1.f, 1.f, 1.f, 1.f)); + light->setSpecular(osg::Vec4f(0.f, 0.f, 0.f, 0.f)); + light->setConstantAttenuation(1.f); - mView->setLight(light); + mLightSource->setLight(light); + + mRootNode->addChild(mLightSource); } void CSVRender::LightingBright::deactivate() { + if (mRootNode && mLightSource.get()) + mRootNode->removeChild(mLightSource); } -void CSVRender::LightingBright::setDefaultAmbient (const osg::Vec4f& colour) {} +osg::Vec4f CSVRender::LightingBright::getAmbientColour(osg::Vec4f* /*defaultAmbient*/) +{ + return osg::Vec4f(1.f, 1.f, 1.f, 1.f); +} diff --git a/apps/opencs/view/render/lightingbright.hpp b/apps/opencs/view/render/lightingbright.hpp index e7ef63f0a..bc6422814 100644 --- a/apps/opencs/view/render/lightingbright.hpp +++ b/apps/opencs/view/render/lightingbright.hpp @@ -3,27 +3,25 @@ #include "lighting.hpp" -namespace Ogre +namespace osg { class Light; + class Group; } namespace CSVRender { class LightingBright : public Lighting { - osgViewer::View* mView; - public: LightingBright(); - virtual void activate (osgViewer::View* view, - const osg::Vec4f *defaultAmbient = 0); + virtual void activate (osg::Group* rootNode); virtual void deactivate(); - virtual void setDefaultAmbient (const osg::Vec4f& colour); + virtual osg::Vec4f getAmbientColour(osg::Vec4f* defaultAmbient); }; } diff --git a/apps/opencs/view/render/lightingday.cpp b/apps/opencs/view/render/lightingday.cpp index 1d8444bc3..376f3e432 100644 --- a/apps/opencs/view/render/lightingday.cpp +++ b/apps/opencs/view/render/lightingday.cpp @@ -1,33 +1,36 @@ #include "lightingday.hpp" -#include +#include -CSVRender::LightingDay::LightingDay() : mView(NULL) {} +CSVRender::LightingDay::LightingDay(){} -void CSVRender::LightingDay::activate (osgViewer::View* view, - const osg::Vec4f *defaultAmbient) +void CSVRender::LightingDay::activate (osg::Group* rootNode) { - mView = view; + mRootNode = rootNode; + + mLightSource = new osg::LightSource; osg::ref_ptr light (new osg::Light); light->setDirection(osg::Vec3f(0.f, 0.f, -1.f)); + light->setAmbient(osg::Vec4f(0.f, 0.f, 0.f, 1.f)); light->setDiffuse(osg::Vec4f(1.f, 1.f, 1.f, 1.f)); + light->setSpecular(osg::Vec4f(0.f, 0.f, 0.f, 0.f)); light->setConstantAttenuation(1.f); - if (defaultAmbient) - light->setAmbient(*defaultAmbient); - else - light->setAmbient(osg::Vec4f(0.7f, 0.7f, 0.7f, 1.f)); - - mView->setLight(light); + mLightSource->setLight(light); + mRootNode->addChild(mLightSource); } void CSVRender::LightingDay::deactivate() { + if (mRootNode && mLightSource.get()) + mRootNode->removeChild(mLightSource); } -void CSVRender::LightingDay::setDefaultAmbient (const osg::Vec4f& colour) +osg::Vec4f CSVRender::LightingDay::getAmbientColour(osg::Vec4f *defaultAmbient) { - if (mView) - mView->getLight()->setAmbient(colour); + if (defaultAmbient) + return *defaultAmbient; + else + return osg::Vec4f(0.7f, 0.7f, 0.7f, 1.f); } diff --git a/apps/opencs/view/render/lightingday.hpp b/apps/opencs/view/render/lightingday.hpp index a0f39b866..407933ec6 100644 --- a/apps/opencs/view/render/lightingday.hpp +++ b/apps/opencs/view/render/lightingday.hpp @@ -12,18 +12,15 @@ namespace CSVRender { class LightingDay : public Lighting { - osgViewer::View* mView; - public: LightingDay(); - virtual void activate (osgViewer::View* view, - const osg::Vec4f *defaultAmbient = 0); + virtual void activate (osg::Group* rootNode); virtual void deactivate(); - virtual void setDefaultAmbient (const osg::Vec4f& colour); + virtual osg::Vec4f getAmbientColour(osg::Vec4f *defaultAmbient); }; } diff --git a/apps/opencs/view/render/lightingnight.cpp b/apps/opencs/view/render/lightingnight.cpp index 81236ec13..18a12d63d 100644 --- a/apps/opencs/view/render/lightingnight.cpp +++ b/apps/opencs/view/render/lightingnight.cpp @@ -1,33 +1,37 @@ #include "lightingnight.hpp" -#include +#include -CSVRender::LightingNight::LightingNight() : mView(NULL) {} +CSVRender::LightingNight::LightingNight() {} -void CSVRender::LightingNight::activate (osgViewer::View* view, - const osg::Vec4f *defaultAmbient) +void CSVRender::LightingNight::activate (osg::Group* rootNode) { - mView = view; + mRootNode = rootNode; + + mLightSource = new osg::LightSource; osg::ref_ptr light (new osg::Light); light->setDirection(osg::Vec3f(0.f, 0.f, -1.f)); + light->setAmbient(osg::Vec4f(0.f, 0.f, 0.f, 1.f)); light->setDiffuse(osg::Vec4f(0.2f, 0.2f, 0.2f, 1.f)); + light->setSpecular(osg::Vec4f(0.f, 0.f, 0.f, 0.f)); light->setConstantAttenuation(1.f); - if (defaultAmbient) - light->setAmbient(*defaultAmbient); - else - light->setAmbient(osg::Vec4f(0.2f, 0.2f, 0.2f, 1.f)); + mLightSource->setLight(light); - mView->setLight(light); + mRootNode->addChild(mLightSource); } void CSVRender::LightingNight::deactivate() { + if (mRootNode && mLightSource.get()) + mRootNode->removeChild(mLightSource); } -void CSVRender::LightingNight::setDefaultAmbient (const osg::Vec4f& colour) +osg::Vec4f CSVRender::LightingNight::getAmbientColour(osg::Vec4f *defaultAmbient) { - if (mView) - mView->getLight()->setAmbient(colour); + if (defaultAmbient) + return *defaultAmbient; + else + return osg::Vec4f(0.2f, 0.2f, 0.2f, 1.f); } diff --git a/apps/opencs/view/render/lightingnight.hpp b/apps/opencs/view/render/lightingnight.hpp index b2fd17893..8743cc438 100644 --- a/apps/opencs/view/render/lightingnight.hpp +++ b/apps/opencs/view/render/lightingnight.hpp @@ -12,18 +12,14 @@ namespace CSVRender { class LightingNight : public Lighting { - osgViewer::View* mView; - public: LightingNight(); - virtual void activate (osgViewer::View* view, - const osg::Vec4f *defaultAmbient = 0); - + virtual void activate (osg::Group* rootNode); virtual void deactivate(); - virtual void setDefaultAmbient (const osg::Vec4f& colour); + virtual osg::Vec4f getAmbientColour(osg::Vec4f *defaultAmbient); }; } diff --git a/apps/opencs/view/render/scenewidget.cpp b/apps/opencs/view/render/scenewidget.cpp index ef2d701c6..593775970 100644 --- a/apps/opencs/view/render/scenewidget.cpp +++ b/apps/opencs/view/render/scenewidget.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include @@ -61,7 +63,8 @@ RenderWidget::RenderWidget(QWidget *parent, Qt::WindowFlags f) mRootNode = new osg::Group; - addDefaultRootState(mRootNode->getOrCreateStateSet()); + mView->getCamera()->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); + mView->getCamera()->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON); mView->setSceneData(mRootNode); @@ -75,12 +78,6 @@ RenderWidget::RenderWidget(QWidget *parent, Qt::WindowFlags f) viewer.realize(); } -void RenderWidget::addDefaultRootState(osg::StateSet* stateset) -{ - stateset->setMode(GL_NORMALIZE, osg::StateAttribute::ON); - stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON); -} - RenderWidget::~RenderWidget() { CompositeViewer::get().removeView(mView); @@ -138,8 +135,10 @@ SceneWidget::SceneWidget(Resource::SceneManager* sceneManager, QWidget *parent, : RenderWidget(parent, f) , mSceneManager(sceneManager) , mLighting(NULL) + , mHasDefaultAmbient(false) { - //mView->setLightingMode(osgViewer::View::NO_LIGHT); + // we handle lighting manually + mView->setLightingMode(osgViewer::View::NO_LIGHT); setLighting(&mLightingDay); } @@ -156,11 +155,25 @@ void SceneWidget::setLighting(Lighting *lighting) mLighting->deactivate(); mLighting = lighting; - mLighting->activate (mView.get(), mHasDefaultAmbient ? &mDefaultAmbient : 0); + mLighting->activate (mRootNode); + + osg::Vec4f ambient = mLighting->getAmbientColour(mHasDefaultAmbient ? &mDefaultAmbient : 0); + setAmbient(ambient); flagAsModified(); } +void SceneWidget::setAmbient(const osg::Vec4f& ambient) +{ + osg::ref_ptr stateset = new osg::StateSet; + osg::ref_ptr lightmodel = new osg::LightModel; + lightmodel->setAmbientIntensity(ambient); + stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON); + stateset->setMode(GL_LIGHT0, osg::StateAttribute::ON); + stateset->setAttributeAndModes(lightmodel, osg::StateAttribute::ON); + mRootNode->setStateSet(stateset); +} + void SceneWidget::selectLightingMode (const std::string& mode) { if (mode=="day") @@ -204,8 +217,7 @@ void SceneWidget::setDefaultAmbient (const osg::Vec4f& colour) mDefaultAmbient = colour; mHasDefaultAmbient = true; - if (mLighting) - mLighting->setDefaultAmbient (colour); + setAmbient(mLighting->getAmbientColour(&mDefaultAmbient)); } } diff --git a/apps/opencs/view/render/scenewidget.hpp b/apps/opencs/view/render/scenewidget.hpp index 8580a2bb1..acfc0bbd4 100644 --- a/apps/opencs/view/render/scenewidget.hpp +++ b/apps/opencs/view/render/scenewidget.hpp @@ -46,8 +46,6 @@ namespace CSVRender protected: - void addDefaultRootState(osg::StateSet* stateset); - osg::ref_ptr mView; osg::Group* mRootNode; @@ -74,6 +72,8 @@ namespace CSVRender void setLighting (Lighting *lighting); ///< \attention The ownership of \a lighting is not transferred to *this. + void setAmbient(const osg::Vec4f& ambient); + Resource::SceneManager* mSceneManager; Lighting* mLighting;