Add fog, view distance, and far plane culling

c++11
scrawl 10 years ago
parent a254877abe
commit 9e177df61b

@ -5,10 +5,13 @@
#include <osg/io_utils> #include <osg/io_utils>
#include <osg/Light> #include <osg/Light>
#include <osg/LightModel> #include <osg/LightModel>
#include <osg/Fog>
#include <osg/Group> #include <osg/Group>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <components/settings/settings.hpp>
#include <components/sceneutil/util.hpp> #include <components/sceneutil/util.hpp>
#include <components/sceneutil/lightmanager.hpp> #include <components/sceneutil/lightmanager.hpp>
@ -26,25 +29,49 @@ namespace MWRender
class StateUpdater : public SceneUtil::StateSetUpdater class StateUpdater : public SceneUtil::StateSetUpdater
{ {
public: public:
StateUpdater()
: mFogEnd(0.f)
{
}
virtual void setDefaults(osg::StateSet *stateset) virtual void setDefaults(osg::StateSet *stateset)
{ {
osg::LightModel* lightModel = new osg::LightModel; osg::LightModel* lightModel = new osg::LightModel;
stateset->setAttribute(lightModel, osg::StateAttribute::ON); stateset->setAttribute(lightModel, osg::StateAttribute::ON);
osg::Fog* fog = new osg::Fog;
fog->setStart(1);
stateset->setAttributeAndModes(fog, osg::StateAttribute::ON);
} }
virtual void apply(osg::StateSet* stateset, osg::NodeVisitor*) virtual void apply(osg::StateSet* stateset, osg::NodeVisitor*)
{ {
osg::LightModel* lightModel = static_cast<osg::LightModel*>(stateset->getAttribute(osg::StateAttribute::LIGHTMODEL)); osg::LightModel* lightModel = static_cast<osg::LightModel*>(stateset->getAttribute(osg::StateAttribute::LIGHTMODEL));
lightModel->setAmbientIntensity(mAmbientColor); lightModel->setAmbientIntensity(mAmbientColor);
osg::Fog* fog = static_cast<osg::Fog*>(stateset->getAttribute(osg::StateAttribute::FOG));
fog->setColor(mFogColor);
fog->setEnd(mFogEnd);
fog->setMode(osg::Fog::LINEAR);
} }
void setAmbientColor(osg::Vec4f col) void setAmbientColor(const osg::Vec4f& col)
{ {
mAmbientColor = col; mAmbientColor = col;
} }
void setFogColor(const osg::Vec4f& col)
{
mFogColor = col;
}
void setFogEnd(float end)
{
mFogEnd = end;
}
private: private:
osg::Vec4f mAmbientColor; osg::Vec4f mAmbientColor;
osg::Vec4f mFogColor;
float mFogEnd;
}; };
RenderingManager::RenderingManager(osgViewer::Viewer &viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem) RenderingManager::RenderingManager(osgViewer::Viewer &viewer, osg::ref_ptr<osg::Group> rootNode, Resource::ResourceSystem* resourceSystem)
@ -80,16 +107,25 @@ namespace MWRender
source->setStateSetModes(*mRootNode->getOrCreateStateSet(), osg::StateAttribute::ON); source->setStateSetModes(*mRootNode->getOrCreateStateSet(), osg::StateAttribute::ON);
mStateUpdater = new StateUpdater; mStateUpdater = new StateUpdater;
mRootNode->addUpdateCallback(mStateUpdater); lightRoot->addUpdateCallback(mStateUpdater);
osg::Camera::CullingMode cullingMode = osg::Camera::DEFAULT_CULLING|osg::Camera::FAR_PLANE_CULLING;
// for consistent benchmarks against the ogre branch. remove later // for consistent benchmarks against the ogre branch. remove later
osg::CullStack::CullingMode cullingMode = viewer.getCamera()->getCullingMode();
cullingMode &= ~(osg::CullStack::SMALL_FEATURE_CULLING); cullingMode &= ~(osg::CullStack::SMALL_FEATURE_CULLING);
viewer.getCamera()->setCullingMode( cullingMode ); viewer.getCamera()->setCullingMode( cullingMode );
mViewer.getCamera()->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);
mViewer.getCamera()->setCullingMode(cullingMode);
mViewDistance = Settings::Manager::getFloat("viewing distance", "Viewing distance");
double fovy, aspect, zNear, zFar; double fovy, aspect, zNear, zFar;
mViewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar); mViewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar);
fovy = 55.f; fovy = 55.f;
zNear = 5.f;
zFar = mViewDistance;
mViewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar); mViewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspect, zNear, zFar);
} }
@ -153,9 +189,19 @@ namespace MWRender
mSky->setEnabled(enabled); mSky->setEnabled(enabled);
} }
void RenderingManager::configureFog(float fogDepth, const osg::Vec4f &colour) void RenderingManager::configureFog(const ESM::Cell *cell)
{
osg::Vec4f color = SceneUtil::colourFromRGB(cell->mAmbi.mFog);
configureFog (cell->mAmbi.mFogDensity, color);
}
void RenderingManager::configureFog(float /* fogDepth */, const osg::Vec4f &colour)
{ {
mViewer.getCamera()->setClearColor(colour); mViewer.getCamera()->setClearColor(colour);
mStateUpdater->setFogColor(colour);
mStateUpdater->setFogEnd(mViewDistance);
} }
SkyManager* RenderingManager::getSkyManager() SkyManager* RenderingManager::getSkyManager()

@ -53,7 +53,7 @@ namespace MWRender
void setSunColour(const osg::Vec4f& colour); void setSunColour(const osg::Vec4f& colour);
void configureAmbient(const ESM::Cell* cell); void configureAmbient(const ESM::Cell* cell);
void configureFog(const ESM::Cell* cell);
void configureFog(float fogDepth, const osg::Vec4f& colour); void configureFog(float fogDepth, const osg::Vec4f& colour);
void removeCell(const MWWorld::CellStore* store); void removeCell(const MWWorld::CellStore* store);
@ -87,6 +87,8 @@ namespace MWRender
osg::ref_ptr<StateUpdater> mStateUpdater; osg::ref_ptr<StateUpdater> mStateUpdater;
float mViewDistance;
void operator = (const RenderingManager&); void operator = (const RenderingManager&);
RenderingManager(const RenderingManager&); RenderingManager(const RenderingManager&);
}; };

@ -511,7 +511,7 @@ namespace MWWorld
changePlayerCell(cell, position, true); changePlayerCell(cell, position, true);
// adjust fog // adjust fog
//mRendering.configureFog(*mCurrentCell); mRendering.configureFog(mCurrentCell->getCell());
// Sky system // Sky system
MWBase::Environment::get().getWorld()->adjustSky(); MWBase::Environment::get().getWorld()->adjustSky();

Loading…
Cancel
Save