mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-31 20:15:32 +00:00
OpenCS: lighting modes
This commit is contained in:
parent
173887c2d9
commit
00deacc27e
10 changed files with 98 additions and 77 deletions
|
@ -1,4 +1,5 @@
|
|||
|
||||
#include "lighting.hpp"
|
||||
|
||||
#include <osg/LightSource>
|
||||
|
||||
CSVRender::Lighting::~Lighting() {}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
#ifndef OPENCS_VIEW_LIGHTING_H
|
||||
#define OPENCS_VIEW_LIGHTING_H
|
||||
|
||||
namespace osgViewer
|
||||
{
|
||||
class View;
|
||||
}
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
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<osg::LightSource> mLightSource;
|
||||
osg::Group* mRootNode;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,30 +1,35 @@
|
|||
|
||||
#include "lightingbright.hpp"
|
||||
|
||||
#include <OgreSceneManager.h>
|
||||
#include <osg/LightSource>
|
||||
|
||||
#include <osgViewer/View>
|
||||
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<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->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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,33 +1,36 @@
|
|||
#include "lightingday.hpp"
|
||||
|
||||
#include <osgViewer/View>
|
||||
#include <osg/LightSource>
|
||||
|
||||
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<osg::Light> 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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,33 +1,37 @@
|
|||
#include "lightingnight.hpp"
|
||||
|
||||
#include <osgViewer/View>
|
||||
#include <osg/LightSource>
|
||||
|
||||
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<osg::Light> 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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <osg/GraphicsContext>
|
||||
#include <osgViewer/CompositeViewer>
|
||||
#include <osgViewer/ViewerEventHandlers>
|
||||
#include <osg/LightModel>
|
||||
#include <osg/io_utils>
|
||||
|
||||
#include <components/resource/scenemanager.hpp>
|
||||
|
||||
|
@ -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<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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,8 +46,6 @@ namespace CSVRender
|
|||
|
||||
protected:
|
||||
|
||||
void addDefaultRootState(osg::StateSet* stateset);
|
||||
|
||||
osg::ref_ptr<osgViewer::View> 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;
|
||||
|
|
Loading…
Reference in a new issue