From 5e12a1b4ef904c35c976526542ee306c0dc5787e Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 18 Feb 2016 23:05:44 +0100 Subject: [PATCH] Add enchanted item glow to the shader --- apps/openmw/mwrender/animation.cpp | 20 +++++++++++++++----- components/shader/shadervisitor.cpp | 2 +- files/shaders/objects_fragment.glsl | 14 ++++++++++++-- files/shaders/objects_vertex.glsl | 15 +++++++++++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index 3d030febf..ec0dab082 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -52,8 +52,8 @@ namespace class GlowUpdater : public SceneUtil::StateSetUpdater { public: - GlowUpdater(osg::Vec4f color, const std::vector >& textures) - : mTexUnit(1) // FIXME: might not always be 1 + GlowUpdater(int texUnit, osg::Vec4f color, const std::vector >& textures) + : mTexUnit(texUnit) , mColor(color) , mTextures(textures) { @@ -1055,17 +1055,27 @@ namespace MWRender osg::ref_ptr image = mResourceSystem->getImageManager()->getImage(stream.str()); osg::ref_ptr tex (new osg::Texture2D(image)); + tex->setName("envMap"); tex->setWrap(osg::Texture::WRAP_S, osg::Texture2D::REPEAT); tex->setWrap(osg::Texture::WRAP_T, osg::Texture2D::REPEAT); mResourceSystem->getSceneManager()->applyFilterSettings(tex); textures.push_back(tex); } - osg::ref_ptr glowupdater (new GlowUpdater(glowColor, textures)); + int texUnit = 1; // FIXME: might not always be 1 + osg::ref_ptr glowupdater (new GlowUpdater(texUnit, glowColor, textures)); node->addUpdateCallback(glowupdater); - // TODO: regenerate shader - // allowedToModifyStatesets = false + // set a texture now so that the ShaderVisitor can find it + osg::ref_ptr writableStateSet = NULL; + if (!node->getStateSet()) + writableStateSet = node->getOrCreateStateSet(); + else + writableStateSet = osg::clone(node->getStateSet(), osg::CopyOp::SHALLOW_COPY); + writableStateSet->setTextureAttributeAndModes(texUnit, textures.front(), osg::StateAttribute::ON); + writableStateSet->addUniform(new osg::Uniform("envMapColor", glowColor)); + + mResourceSystem->getSceneManager()->recreateShaders(node); } // TODO: Should not be here diff --git a/components/shader/shadervisitor.cpp b/components/shader/shadervisitor.cpp index fcb9b8057..331beab34 100644 --- a/components/shader/shadervisitor.cpp +++ b/components/shader/shadervisitor.cpp @@ -152,7 +152,7 @@ namespace Shader writableStateSet = getWritableStateSet(node); ShaderManager::DefineMap defineMap; - const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap" }; + const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap", "envMap" }; for (unsigned int i=0; i