2011-10-20 19:02:19 +00:00
|
|
|
#include "renderingmanager.hpp"
|
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
#include <stdexcept>
|
2011-10-20 22:15:30 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
#include <osg/io_utils>
|
|
|
|
#include <osg/Light>
|
|
|
|
#include <osg/LightModel>
|
|
|
|
#include <osg/Group>
|
2011-10-20 22:15:30 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
#include <osgViewer/Viewer>
|
2013-07-29 00:32:08 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
#include <components/sceneutil/util.hpp>
|
2012-07-03 20:48:16 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
#include <components/sceneutil/lightmanager.hpp>
|
2013-02-07 05:47:09 +00:00
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
#include <components/sceneutil/statesetupdater.hpp>
|
2015-04-14 13:55:56 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
#include <components/esm/loadcell.hpp>
|
2013-08-16 11:01:52 +00:00
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
#include "sky.hpp"
|
2015-04-19 15:55:56 +00:00
|
|
|
#include "effectmanager.hpp"
|
2015-04-14 13:55:56 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
namespace MWRender
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2012-03-13 21:14:35 +00:00
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
class StateUpdater : public SceneUtil::StateSetUpdater
|
2015-04-14 13:55:56 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void setDefaults(osg::StateSet *stateset)
|
|
|
|
{
|
|
|
|
osg::LightModel* lightModel = new osg::LightModel;
|
|
|
|
stateset->setAttribute(lightModel, osg::StateAttribute::ON);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(osg::StateSet* stateset, osg::NodeVisitor*)
|
|
|
|
{
|
|
|
|
osg::LightModel* lightModel = static_cast<osg::LightModel*>(stateset->getAttribute(osg::StateAttribute::LIGHTMODEL));
|
|
|
|
lightModel->setAmbientIntensity(mAmbientColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAmbientColor(osg::Vec4f col)
|
|
|
|
{
|
|
|
|
mAmbientColor = col;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
osg::Vec4f mAmbientColor;
|
|
|
|
};
|
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
RenderingManager::RenderingManager(osgViewer::Viewer &viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem)
|
|
|
|
: mViewer(viewer)
|
|
|
|
, mRootNode(rootNode)
|
|
|
|
, mResourceSystem(resourceSystem)
|
2014-06-22 14:10:52 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
osg::ref_ptr<SceneUtil::LightManager> lightRoot = new SceneUtil::LightManager;
|
|
|
|
lightRoot->setStartLight(1);
|
2012-07-20 14:44:03 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
mRootNode->addChild(lightRoot);
|
2011-11-04 03:47:15 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
mObjects.reset(new Objects(mResourceSystem, lightRoot));
|
2012-02-26 12:13:29 +00:00
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
mSky.reset(new SkyManager(mRootNode, resourceSystem->getSceneManager()));
|
|
|
|
|
2015-04-19 15:55:56 +00:00
|
|
|
mEffectManager.reset(new EffectManager(mRootNode, mResourceSystem));
|
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
mViewer.setLightingMode(osgViewer::View::NO_LIGHT);
|
2011-11-04 03:47:15 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
osg::ref_ptr<osg::LightSource> source = new osg::LightSource;
|
|
|
|
mSunLight = new osg::Light;
|
|
|
|
source->setLight(mSunLight);
|
|
|
|
mSunLight->setDiffuse(osg::Vec4f(0,0,0,1));
|
|
|
|
mSunLight->setAmbient(osg::Vec4f(0,0,0,1));
|
|
|
|
mSunLight->setConstantAttenuation(1.f);
|
|
|
|
lightRoot->addChild(source);
|
2011-11-04 03:47:15 +00:00
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
mRootNode->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
|
|
|
|
mRootNode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
|
|
|
|
mRootNode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
source->setStateSetModes(*mRootNode->getOrCreateStateSet(), osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
mStateUpdater = new StateUpdater;
|
|
|
|
mRootNode->addUpdateCallback(mStateUpdater);
|
2011-11-04 03:47:15 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
// for consistent benchmarks against the ogre branch. remove later
|
|
|
|
osg::CullStack::CullingMode cullingMode = viewer.getCamera()->getCullingMode();
|
|
|
|
cullingMode &= ~(osg::CullStack::SMALL_FEATURE_CULLING);
|
|
|
|
viewer.getCamera()->setCullingMode( cullingMode );
|
2015-04-14 13:55:56 +00:00
|
|
|
|
|
|
|
double fovy, aspect, zNear, zFar;
|
|
|
|
mViewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar);
|
|
|
|
fovy = 55.f;
|
|
|
|
mViewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar);
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderingManager::~RenderingManager()
|
|
|
|
{
|
2013-02-03 14:46:23 +00:00
|
|
|
}
|
2012-02-24 15:12:43 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
MWRender::Objects& RenderingManager::getObjects()
|
2014-03-05 20:45:43 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
return *mObjects.get();
|
2014-03-05 20:45:43 +00:00
|
|
|
}
|
2014-02-18 15:44:37 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
MWRender::Actors& RenderingManager::getActors()
|
2013-08-16 11:01:52 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
throw std::runtime_error("unimplemented");
|
2013-08-16 11:01:52 +00:00
|
|
|
}
|
2012-05-14 19:37:43 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
Resource::ResourceSystem* RenderingManager::getResourceSystem()
|
2012-05-14 19:37:43 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
return mResourceSystem;
|
2012-05-14 19:37:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
void RenderingManager::setAmbientColour(const osg::Vec4f &colour)
|
|
|
|
{
|
|
|
|
mStateUpdater->setAmbientColor(colour);
|
|
|
|
}
|
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
void RenderingManager::configureAmbient(const ESM::Cell *cell)
|
2012-05-22 23:32:36 +00:00
|
|
|
{
|
2015-04-14 13:55:56 +00:00
|
|
|
setAmbientColour(SceneUtil::colourFromRGB(cell->mAmbi.mAmbient));
|
2012-05-29 02:54:54 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
mSunLight->setDiffuse(SceneUtil::colourFromRGB(cell->mAmbi.mSunlight));
|
|
|
|
mSunLight->setDirection(osg::Vec3f(1.f,-1.f,-1.f));
|
2012-05-27 19:39:18 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
void RenderingManager::setSunColour(const osg::Vec4f &colour)
|
|
|
|
{
|
|
|
|
mSunLight->setDiffuse(colour);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderingManager::setSunDirection(const osg::Vec3f &direction)
|
|
|
|
{
|
|
|
|
mSunLight->setDirection(direction*-1);
|
|
|
|
|
|
|
|
mSky->setSunDirection(direction*-1);
|
|
|
|
}
|
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
osg::Vec3f RenderingManager::getEyePos()
|
2012-05-27 19:39:18 +00:00
|
|
|
{
|
2015-04-14 13:55:56 +00:00
|
|
|
osg::Vec3d eye = mViewer.getCameraManipulator()->getMatrix().getTrans();
|
2015-04-12 13:34:50 +00:00
|
|
|
return eye;
|
2012-05-22 23:32:36 +00:00
|
|
|
}
|
2012-05-29 04:45:44 +00:00
|
|
|
|
2015-04-12 13:34:50 +00:00
|
|
|
void RenderingManager::removeCell(const MWWorld::CellStore *store)
|
2013-08-20 07:52:27 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
mObjects->removeCell(store);
|
2013-08-20 07:52:27 +00:00
|
|
|
}
|
2012-05-22 23:32:36 +00:00
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
void RenderingManager::setSkyEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
mSky->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderingManager::configureFog(float fogDepth, const osg::Vec4f &colour)
|
|
|
|
{
|
|
|
|
mViewer.getCamera()->setClearColor(colour);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkyManager* RenderingManager::getSkyManager()
|
|
|
|
{
|
|
|
|
return mSky.get();
|
|
|
|
}
|
|
|
|
|
2015-04-18 23:57:52 +00:00
|
|
|
void RenderingManager::update(float dt, bool paused)
|
|
|
|
{
|
|
|
|
mObjects->update(dt);
|
2015-04-19 15:55:56 +00:00
|
|
|
mEffectManager->update(dt);
|
2015-04-19 18:07:18 +00:00
|
|
|
mSky->update(dt);
|
2015-04-19 15:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderingManager::spawnEffect(const std::string &model, const std::string &texture, const osg::Vec3f &worldPosition, float scale)
|
|
|
|
{
|
|
|
|
mEffectManager->addEffect(model, texture, worldPosition, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderingManager::notifyWorldSpaceChanged()
|
|
|
|
{
|
|
|
|
mEffectManager->clear();
|
|
|
|
//mWater->clearRipples();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderingManager::clear()
|
|
|
|
{
|
|
|
|
//mLocalMap->clear();
|
|
|
|
notifyWorldSpaceChanged();
|
2015-04-18 23:57:52 +00:00
|
|
|
}
|
|
|
|
|
2012-05-27 19:39:18 +00:00
|
|
|
}
|