OpenCS: lighting modes

c++11
scrawl 10 years ago
parent 173887c2d9
commit 00deacc27e

@ -1,4 +1,5 @@
#include "lighting.hpp" #include "lighting.hpp"
#include <osg/LightSource>
CSVRender::Lighting::~Lighting() {} CSVRender::Lighting::~Lighting() {}

@ -1,13 +1,13 @@
#ifndef OPENCS_VIEW_LIGHTING_H #ifndef OPENCS_VIEW_LIGHTING_H
#define OPENCS_VIEW_LIGHTING_H #define OPENCS_VIEW_LIGHTING_H
namespace osgViewer #include <osg/ref_ptr>
{
class View;
}
namespace osg namespace osg
{ {
class Vec4f; class Vec4f;
class LightSource;
class Group;
} }
namespace CSVRender namespace CSVRender
@ -16,14 +16,19 @@ namespace CSVRender
{ {
public: public:
Lighting() : mRootNode(0) {}
virtual ~Lighting(); virtual ~Lighting();
virtual void activate (osgViewer::View* view, virtual void activate (osg::Group* rootNode) = 0;
const osg::Vec4f *defaultAmbient = 0) = 0;
virtual void deactivate() = 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<osg::LightSource> mLightSource;
osg::Group* mRootNode;
}; };
} }

@ -1,30 +1,35 @@
#include "lightingbright.hpp" #include "lightingbright.hpp"
#include <OgreSceneManager.h> #include <osg/LightSource>
#include <osgViewer/View> CSVRender::LightingBright::LightingBright() {}
CSVRender::LightingBright::LightingBright() : mView(NULL) {} void CSVRender::LightingBright::activate (osg::Group* rootNode)
void CSVRender::LightingBright::activate (osgViewer::View* view,
const osg::Vec4f* /*defaultAmbient*/)
{ {
mView = view; mRootNode = rootNode;
// FIXME: ambient should be applied to LightModel instead of the light mLightSource = (new osg::LightSource);
osg::ref_ptr<osg::Light> light (new osg::Light); osg::ref_ptr<osg::Light> 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->setDirection(osg::Vec3f(0.f, 0.f, -1.f));
light->setDiffuse(osg::Vec4f(1.f, 1.f, 1.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() 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);
}

@ -3,27 +3,25 @@
#include "lighting.hpp" #include "lighting.hpp"
namespace Ogre namespace osg
{ {
class Light; class Light;
class Group;
} }
namespace CSVRender namespace CSVRender
{ {
class LightingBright : public Lighting class LightingBright : public Lighting
{ {
osgViewer::View* mView;
public: public:
LightingBright(); LightingBright();
virtual void activate (osgViewer::View* view, virtual void activate (osg::Group* rootNode);
const osg::Vec4f *defaultAmbient = 0);
virtual void deactivate(); virtual void deactivate();
virtual void setDefaultAmbient (const osg::Vec4f& colour); virtual osg::Vec4f getAmbientColour(osg::Vec4f* defaultAmbient);
}; };
} }

@ -1,33 +1,36 @@
#include "lightingday.hpp" #include "lightingday.hpp"
#include <osgViewer/View> #include <osg/LightSource>
CSVRender::LightingDay::LightingDay() : mView(NULL) {} CSVRender::LightingDay::LightingDay(){}
void CSVRender::LightingDay::activate (osgViewer::View* view, void CSVRender::LightingDay::activate (osg::Group* rootNode)
const osg::Vec4f *defaultAmbient)
{ {
mView = view; mRootNode = rootNode;
mLightSource = new osg::LightSource;
osg::ref_ptr<osg::Light> light (new osg::Light); osg::ref_ptr<osg::Light> light (new osg::Light);
light->setDirection(osg::Vec3f(0.f, 0.f, -1.f)); 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->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); light->setConstantAttenuation(1.f);
if (defaultAmbient) mLightSource->setLight(light);
light->setAmbient(*defaultAmbient); mRootNode->addChild(mLightSource);
else
light->setAmbient(osg::Vec4f(0.7f, 0.7f, 0.7f, 1.f));
mView->setLight(light);
} }
void CSVRender::LightingDay::deactivate() 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) if (defaultAmbient)
mView->getLight()->setAmbient(colour); return *defaultAmbient;
else
return osg::Vec4f(0.7f, 0.7f, 0.7f, 1.f);
} }

@ -12,18 +12,15 @@ namespace CSVRender
{ {
class LightingDay : public Lighting class LightingDay : public Lighting
{ {
osgViewer::View* mView;
public: public:
LightingDay(); LightingDay();
virtual void activate (osgViewer::View* view, virtual void activate (osg::Group* rootNode);
const osg::Vec4f *defaultAmbient = 0);
virtual void deactivate(); virtual void deactivate();
virtual void setDefaultAmbient (const osg::Vec4f& colour); virtual osg::Vec4f getAmbientColour(osg::Vec4f *defaultAmbient);
}; };
} }

@ -1,33 +1,37 @@
#include "lightingnight.hpp" #include "lightingnight.hpp"
#include <osgViewer/View> #include <osg/LightSource>
CSVRender::LightingNight::LightingNight() : mView(NULL) {} CSVRender::LightingNight::LightingNight() {}
void CSVRender::LightingNight::activate (osgViewer::View* view, void CSVRender::LightingNight::activate (osg::Group* rootNode)
const osg::Vec4f *defaultAmbient)
{ {
mView = view; mRootNode = rootNode;
mLightSource = new osg::LightSource;
osg::ref_ptr<osg::Light> light (new osg::Light); osg::ref_ptr<osg::Light> light (new osg::Light);
light->setDirection(osg::Vec3f(0.f, 0.f, -1.f)); 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->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); light->setConstantAttenuation(1.f);
if (defaultAmbient) mLightSource->setLight(light);
light->setAmbient(*defaultAmbient);
else
light->setAmbient(osg::Vec4f(0.2f, 0.2f, 0.2f, 1.f));
mView->setLight(light); mRootNode->addChild(mLightSource);
} }
void CSVRender::LightingNight::deactivate() 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) if (defaultAmbient)
mView->getLight()->setAmbient(colour); return *defaultAmbient;
else
return osg::Vec4f(0.2f, 0.2f, 0.2f, 1.f);
} }

@ -12,18 +12,14 @@ namespace CSVRender
{ {
class LightingNight : public Lighting class LightingNight : public Lighting
{ {
osgViewer::View* mView;
public: public:
LightingNight(); LightingNight();
virtual void activate (osgViewer::View* view, virtual void activate (osg::Group* rootNode);
const osg::Vec4f *defaultAmbient = 0);
virtual void deactivate(); virtual void deactivate();
virtual void setDefaultAmbient (const osg::Vec4f& colour); virtual osg::Vec4f getAmbientColour(osg::Vec4f *defaultAmbient);
}; };
} }

@ -10,6 +10,8 @@
#include <osg/GraphicsContext> #include <osg/GraphicsContext>
#include <osgViewer/CompositeViewer> #include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
#include <osg/LightModel>
#include <osg/io_utils>
#include <components/resource/scenemanager.hpp> #include <components/resource/scenemanager.hpp>
@ -61,7 +63,8 @@ RenderWidget::RenderWidget(QWidget *parent, Qt::WindowFlags f)
mRootNode = new osg::Group; 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); mView->setSceneData(mRootNode);
@ -75,12 +78,6 @@ RenderWidget::RenderWidget(QWidget *parent, Qt::WindowFlags f)
viewer.realize(); 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() RenderWidget::~RenderWidget()
{ {
CompositeViewer::get().removeView(mView); CompositeViewer::get().removeView(mView);
@ -138,8 +135,10 @@ SceneWidget::SceneWidget(Resource::SceneManager* sceneManager, QWidget *parent,
: RenderWidget(parent, f) : RenderWidget(parent, f)
, mSceneManager(sceneManager) , mSceneManager(sceneManager)
, mLighting(NULL) , mLighting(NULL)
, mHasDefaultAmbient(false)
{ {
//mView->setLightingMode(osgViewer::View::NO_LIGHT); // we handle lighting manually
mView->setLightingMode(osgViewer::View::NO_LIGHT);
setLighting(&mLightingDay); setLighting(&mLightingDay);
} }
@ -156,11 +155,25 @@ void SceneWidget::setLighting(Lighting *lighting)
mLighting->deactivate(); mLighting->deactivate();
mLighting = lighting; mLighting = lighting;
mLighting->activate (mView.get(), mHasDefaultAmbient ? &mDefaultAmbient : 0); mLighting->activate (mRootNode);
osg::Vec4f ambient = mLighting->getAmbientColour(mHasDefaultAmbient ? &mDefaultAmbient : 0);
setAmbient(ambient);
flagAsModified(); flagAsModified();
} }
void SceneWidget::setAmbient(const osg::Vec4f& ambient)
{
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
osg::ref_ptr<osg::LightModel> 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) void SceneWidget::selectLightingMode (const std::string& mode)
{ {
if (mode=="day") if (mode=="day")
@ -204,8 +217,7 @@ void SceneWidget::setDefaultAmbient (const osg::Vec4f& colour)
mDefaultAmbient = colour; mDefaultAmbient = colour;
mHasDefaultAmbient = true; mHasDefaultAmbient = true;
if (mLighting) setAmbient(mLighting->getAmbientColour(&mDefaultAmbient));
mLighting->setDefaultAmbient (colour);
} }
} }

@ -46,8 +46,6 @@ namespace CSVRender
protected: protected:
void addDefaultRootState(osg::StateSet* stateset);
osg::ref_ptr<osgViewer::View> mView; osg::ref_ptr<osgViewer::View> mView;
osg::Group* mRootNode; osg::Group* mRootNode;
@ -74,6 +72,8 @@ namespace CSVRender
void setLighting (Lighting *lighting); void setLighting (Lighting *lighting);
///< \attention The ownership of \a lighting is not transferred to *this. ///< \attention The ownership of \a lighting is not transferred to *this.
void setAmbient(const osg::Vec4f& ambient);
Resource::SceneManager* mSceneManager; Resource::SceneManager* mSceneManager;
Lighting* mLighting; Lighting* mLighting;

Loading…
Cancel
Save