diff --git a/components/shader/shadervisitor.cpp b/components/shader/shadervisitor.cpp index 92bdac7b7..a05988f61 100644 --- a/components/shader/shadervisitor.cpp +++ b/components/shader/shadervisitor.cpp @@ -25,6 +25,7 @@ namespace Shader , mColorMaterial(false) , mVertexColorMode(GL_AMBIENT_AND_DIFFUSE) , mMaterialOverridden(false) + , mNormalHeight(false) , mTexStageRequiringTangents(-1) { } @@ -120,6 +121,12 @@ namespace Shader if ((texName.empty() || !isTextureNameRecognized(texName)) && unit == 0) texName = "diffuseMap"; + if (texName == "normalHeightMap") + { + mRequirements.back().mNormalHeight = true; + texName = "normalMap"; + } + if (!texName.empty()) { mRequirements.back().mTextures[unit] = texName; @@ -133,9 +140,9 @@ namespace Shader writableStateSet->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON); normalMap = texture; } - if (texName == "diffuseMap") + else if (texName == "diffuseMap") diffuseMap = texture; - if (texName == "specularMap") + else if (texName == "specularMap") specularMap = texture; } else @@ -147,10 +154,28 @@ namespace Shader if (mAutoUseNormalMaps && diffuseMap != NULL && normalMap == NULL) { std::string normalMap = diffuseMap->getImage(0)->getFileName(); - boost::replace_last(normalMap, ".", mNormalMapPattern + "."); - if (mImageManager.getVFS()->exists(normalMap)) + + osg::ref_ptr image; + bool normalHeight = false; + std::string normalHeightMap = normalMap; + boost::replace_last(normalHeightMap, ".", mNormalHeightMapPattern + "."); + if (mImageManager.getVFS()->exists(normalHeightMap)) + { + image = mImageManager.getImage(normalHeightMap); + normalHeight = true; + } + else + { + boost::replace_last(normalMap, ".", mNormalMapPattern + "."); + if (mImageManager.getVFS()->exists(normalMap)) + { + image = mImageManager.getImage(normalMap); + } + } + + if (image) { - osg::ref_ptr normalMapTex (new osg::Texture2D(mImageManager.getImage(normalMap))); + osg::ref_ptr normalMapTex (new osg::Texture2D(image)); normalMapTex->setWrap(osg::Texture::WRAP_S, diffuseMap->getWrap(osg::Texture::WRAP_S)); normalMapTex->setWrap(osg::Texture::WRAP_T, diffuseMap->getWrap(osg::Texture::WRAP_T)); normalMapTex->setFilter(osg::Texture::MIN_FILTER, diffuseMap->getFilter(osg::Texture::MIN_FILTER)); @@ -165,6 +190,7 @@ namespace Shader mRequirements.back().mTextures[unit] = "normalMap"; mRequirements.back().mTexStageRequiringTangents = unit; mRequirements.back().mShaderRequired = true; + mRequirements.back().mNormalHeight = normalHeight; } } if (mAutoUseSpecularMaps && diffuseMap != NULL && specularMap == NULL) diff --git a/components/shader/shadervisitor.hpp b/components/shader/shadervisitor.hpp index 682a9c60f..8f4597ff3 100644 --- a/components/shader/shadervisitor.hpp +++ b/components/shader/shadervisitor.hpp @@ -39,6 +39,7 @@ namespace Shader void setAutoUseNormalMaps(bool use); void setNormalMapPattern(const std::string& pattern); + void setNormalHeightMapPattern(const std::string& pattern); void setAutoUseSpecularMaps(bool use); @@ -62,6 +63,7 @@ namespace Shader bool mAutoUseNormalMaps; std::string mNormalMapPattern; + std::string mNormalHeightMapPattern; bool mAutoUseSpecularMaps; std::string mSpecularMapPattern; @@ -83,6 +85,7 @@ namespace Shader // osg::Material::ColorMode int mVertexColorMode; bool mMaterialOverridden; + bool mNormalHeight; // true if normal map has height info in alpha channel // -1 == no tangents required int mTexStageRequiringTangents;