Accurate handling of fog depth/density (Fixes #2752)

This commit is contained in:
scrawl 2015-07-01 03:21:39 +02:00
parent 6691891bee
commit dcb8fbc69c
2 changed files with 15 additions and 3 deletions

View file

@ -46,7 +46,8 @@ namespace MWRender
{ {
public: public:
StateUpdater() StateUpdater()
: mFogEnd(0.f) : mFogStart(0.f)
, mFogEnd(0.f)
, mWireframe(false) , mWireframe(false)
{ {
} }
@ -56,7 +57,6 @@ namespace MWRender
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; osg::Fog* fog = new osg::Fog;
fog->setStart(1);
fog->setMode(osg::Fog::LINEAR); fog->setMode(osg::Fog::LINEAR);
stateset->setAttributeAndModes(fog, osg::StateAttribute::ON); stateset->setAttributeAndModes(fog, osg::StateAttribute::ON);
if (mWireframe) if (mWireframe)
@ -75,6 +75,7 @@ namespace MWRender
lightModel->setAmbientIntensity(mAmbientColor); lightModel->setAmbientIntensity(mAmbientColor);
osg::Fog* fog = static_cast<osg::Fog*>(stateset->getAttribute(osg::StateAttribute::FOG)); osg::Fog* fog = static_cast<osg::Fog*>(stateset->getAttribute(osg::StateAttribute::FOG));
fog->setColor(mFogColor); fog->setColor(mFogColor);
fog->setStart(mFogStart);
fog->setEnd(mFogEnd); fog->setEnd(mFogEnd);
} }
@ -88,6 +89,11 @@ namespace MWRender
mFogColor = col; mFogColor = col;
} }
void setFogStart(float start)
{
mFogStart = start;
}
void setFogEnd(float end) void setFogEnd(float end)
{ {
mFogEnd = end; mFogEnd = end;
@ -110,6 +116,7 @@ namespace MWRender
private: private:
osg::Vec4f mAmbientColor; osg::Vec4f mAmbientColor;
osg::Vec4f mFogColor; osg::Vec4f mFogColor;
float mFogStart;
float mFogEnd; float mFogEnd;
bool mWireframe; bool mWireframe;
}; };
@ -118,6 +125,7 @@ namespace MWRender
: mViewer(viewer) : mViewer(viewer)
, mRootNode(rootNode) , mRootNode(rootNode)
, mResourceSystem(resourceSystem) , mResourceSystem(resourceSystem)
, mFogDepth(0.f)
, mNightEyeFactor(0.f) , mNightEyeFactor(0.f)
{ {
osg::ref_ptr<SceneUtil::LightManager> lightRoot = new SceneUtil::LightManager; osg::ref_ptr<SceneUtil::LightManager> lightRoot = new SceneUtil::LightManager;
@ -338,8 +346,9 @@ namespace MWRender
configureFog (cell->mAmbi.mFogDensity, color); configureFog (cell->mAmbi.mFogDensity, color);
} }
void RenderingManager::configureFog(float /* fogDepth */, const osg::Vec4f &color) void RenderingManager::configureFog(float fogDepth, const osg::Vec4f &color)
{ {
mFogDepth = fogDepth;
mFogColor = color; mFogColor = color;
} }
@ -364,11 +373,13 @@ namespace MWRender
if (mWater->isUnderwater(cameraPos)) if (mWater->isUnderwater(cameraPos))
{ {
setFogColor(osg::Vec4f(0.090195f, 0.115685f, 0.12745f, 1.f)); setFogColor(osg::Vec4f(0.090195f, 0.115685f, 0.12745f, 1.f));
mStateUpdater->setFogStart(0.f);
mStateUpdater->setFogEnd(1000); mStateUpdater->setFogEnd(1000);
} }
else else
{ {
setFogColor(mFogColor); setFogColor(mFogColor);
mStateUpdater->setFogStart(mViewDistance * (1 - mFogDepth));
mStateUpdater->setFogEnd(mViewDistance); mStateUpdater->setFogEnd(mViewDistance);
} }
} }

View file

@ -190,6 +190,7 @@ namespace MWRender
osg::ref_ptr<StateUpdater> mStateUpdater; osg::ref_ptr<StateUpdater> mStateUpdater;
float mFogDepth;
osg::Vec4f mFogColor; osg::Vec4f mFogColor;
osg::Vec4f mAmbientColor; osg::Vec4f mAmbientColor;