diff --git a/apps/openmw/mwscript/miscextensions.cpp b/apps/openmw/mwscript/miscextensions.cpp index 509b1feafe..dff07b78fa 100644 --- a/apps/openmw/mwscript/miscextensions.cpp +++ b/apps/openmw/mwscript/miscextensions.cpp @@ -47,7 +47,7 @@ #include #include -#include +#include #include #include "../mwbase/environment.hpp" @@ -107,12 +107,7 @@ namespace std::string fileName; if (image) fileName = image->getFileName(); - const osg::StateAttribute* type - = stateset->getTextureAttribute(i, SceneUtil::TextureType::AttributeType); - if (type) - mTextures.emplace_back(static_cast(type)->getName(), fileName); - else - mTextures.emplace_back(texture->getName(), fileName); + mTextures.emplace_back(SceneUtil::getTextureType(*stateset, *texture, i), fileName); } } diff --git a/components/sceneutil/util.cpp b/components/sceneutil/util.cpp index 8d7c585d94..2ad4c92ab5 100644 --- a/components/sceneutil/util.cpp +++ b/components/sceneutil/util.cpp @@ -408,4 +408,12 @@ namespace SceneUtil return osg::Image::computePixelFormat(format); } + const std::string& getTextureType(const osg::StateSet& stateset, const osg::Texture& texture, unsigned int texUnit) + { + const osg::StateAttribute* type = stateset.getTextureAttribute(texUnit, SceneUtil::TextureType::AttributeType); + if (type) + return static_cast(type)->getName(); + + return texture.getName(); + } } diff --git a/components/sceneutil/util.hpp b/components/sceneutil/util.hpp index b76f46a688..b320195eff 100644 --- a/components/sceneutil/util.hpp +++ b/components/sceneutil/util.hpp @@ -116,6 +116,10 @@ namespace SceneUtil // Compute the unsized format equivalent to the given pixel format // Unlike osg::Image::computePixelFormat, this also covers compressed formats GLenum computeUnsizedPixelFormat(GLenum format); + + // Recover the presumed texture type for the given texture unit + // It may be set as a state attribute or it may come from the used texture's name + const std::string& getTextureType(const osg::StateSet& stateset, const osg::Texture& texture, unsigned int texUnit); } #endif diff --git a/components/shader/shadervisitor.cpp b/components/shader/shadervisitor.cpp index 263ca0f2bb..2e4035a990 100644 --- a/components/shader/shadervisitor.cpp +++ b/components/shader/shadervisitor.cpp @@ -330,14 +330,7 @@ namespace Shader const osg::Texture* texture = attr->asTexture(); if (texture) { - std::string texName; - const osg::StateAttribute* type - = stateset->getTextureAttribute(unit, SceneUtil::TextureType::AttributeType); - if (type) - texName = static_cast(type)->getName(); - else - texName = texture->getName(); - + std::string texName = SceneUtil::getTextureType(*stateset, *texture, unit); if ((texName.empty() || !isTextureNameRecognized(texName)) && unit == 0) texName = "diffuseMap";