From d812434feeb8b32aa1edada2e240994e5715cd43 Mon Sep 17 00:00:00 2001 From: scrawl Date: Mon, 21 Sep 2015 17:32:57 +0200 Subject: [PATCH] Add a subtle fading effect to the sun flash texture --- apps/openmw/mwrender/sky.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index 86a9a1765..7f5666b17 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -385,6 +385,7 @@ public: alphaFunc->setFunction(osg::AlphaFunc::GREATER, 0.8); queryNode->getOrCreateStateSet()->setAttributeAndModes(alphaFunc, osg::StateAttribute::ON); queryNode->getOrCreateStateSet()->setTextureAttributeAndModes(0, sunTex, osg::StateAttribute::ON); + queryNode->getOrCreateStateSet()->setAttributeAndModes(createUnlitMaterial(), osg::StateAttribute::ON); mTransform->addChild(queryNode); @@ -407,6 +408,8 @@ public: mUpdater->mColor.a() = ratio; if (mSunGlareCallback) mSunGlareCallback->setGlareView(ratio); + if (mSunFlashCallback) + mSunFlashCallback->setGlareView(ratio); } void setDirection(const osg::Vec3f& direction) @@ -505,8 +508,6 @@ private: osg::StateSet* stateset = geode->getOrCreateStateSet(); stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON); - stateset->setAttributeAndModes(createUnlitMaterial(), - osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); stateset->setRenderBinDetails(RenderBin_SunGlare, "RenderBin"); stateset->setNestRenderBins(false); @@ -639,6 +640,7 @@ private: public: SunFlashCallback(osg::ref_ptr oqnVisible, osg::ref_ptr oqnTotal) : OcclusionCallback(oqnVisible, oqnTotal) + , mGlareView(1.f) { } @@ -648,8 +650,20 @@ private: float visibleRatio = getVisibleRatio(cv->getCurrentCamera()); + osg::ref_ptr stateset; + if (visibleRatio > 0.f) { + const float fadeThreshold = 0.1; + if (visibleRatio < fadeThreshold) + { + float fade = 1.f - (fadeThreshold - visibleRatio) / fadeThreshold; + osg::ref_ptr mat (createUnlitMaterial()); + mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,fade*mGlareView)); + stateset = new osg::StateSet; + stateset->setAttributeAndModes(mat, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); + } + const float threshold = 0.6; visibleRatio = visibleRatio * (1.f - threshold) + threshold; } @@ -667,13 +681,27 @@ private: modelView.preMultScale(osg::Vec3f(visibleRatio, visibleRatio, visibleRatio)); + if (stateset) + cv->pushStateSet(stateset); + cv->pushModelViewMatrix(new osg::RefMatrix(modelView), osg::Transform::RELATIVE_RF); traverse(node, nv); cv->popModelViewMatrix(); + + if (stateset) + cv->popStateSet(); } } + + void setGlareView(float value) + { + mGlareView = value; + } + + private: + float mGlareView; }; @@ -728,10 +756,8 @@ private: for (int i=0; i<3; ++i) sunGlareFaderColor[i] = std::min(1.f, sunGlareFaderColor[i]); - mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,1)); mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,fade)); mat->setEmission(osg::Material::FRONT_AND_BACK, sunGlareFaderColor); - mat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,0)); stateset->setAttributeAndModes(mat, osg::StateAttribute::ON);