ShaderVisitor: assume the first texture unit is the diffuseMap

move
scrawl 9 years ago
parent e7682e04ae
commit e25e0a0600

@ -88,6 +88,15 @@ namespace Shader
return newStateSet.get(); return newStateSet.get();
} }
const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap", "envMap", "specularMap" };
bool isTextureNameRecognized(const std::string& name)
{
for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i)
if (name.c_str() == defaultTextures[i])
return true;
return false;
}
void ShaderVisitor::applyStateSet(osg::ref_ptr<osg::StateSet> stateset, osg::Node& node) void ShaderVisitor::applyStateSet(osg::ref_ptr<osg::StateSet> stateset, osg::Node& node)
{ {
osg::StateSet* writableStateSet = NULL; osg::StateSet* writableStateSet = NULL;
@ -107,10 +116,14 @@ namespace Shader
const osg::Texture* texture = attr->asTexture(); const osg::Texture* texture = attr->asTexture();
if (texture) if (texture)
{ {
if (!texture->getName().empty()) std::string texName = texture->getName();
if ((texName.empty() || !isTextureNameRecognized(texName)) && unit == 0)
texName = "diffuseMap";
if (!texName.empty())
{ {
mRequirements.back().mTextures[unit] = texture->getName(); mRequirements.back().mTextures[unit] = texName;
if (texture->getName() == "normalMap") if (texName == "normalMap")
{ {
mRequirements.back().mTexStageRequiringTangents = unit; mRequirements.back().mTexStageRequiringTangents = unit;
mRequirements.back().mShaderRequired = true; mRequirements.back().mShaderRequired = true;
@ -120,9 +133,9 @@ namespace Shader
writableStateSet->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON); writableStateSet->setTextureMode(unit, GL_TEXTURE_2D, osg::StateAttribute::ON);
normalMap = texture; normalMap = texture;
} }
if (texture->getName() == "diffuseMap") if (texName == "diffuseMap")
diffuseMap = texture; diffuseMap = texture;
if (texture->getName() == "specularMap") if (texName == "specularMap")
specularMap = texture; specularMap = texture;
} }
else else
@ -222,7 +235,6 @@ namespace Shader
writableStateSet = getWritableStateSet(node); writableStateSet = getWritableStateSet(node);
ShaderManager::DefineMap defineMap; ShaderManager::DefineMap defineMap;
const char* defaultTextures[] = { "diffuseMap", "normalMap", "emissiveMap", "darkMap", "detailMap", "envMap", "specularMap" };
for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i) for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i)
{ {
defineMap[defaultTextures[i]] = "0"; defineMap[defaultTextures[i]] = "0";

Loading…
Cancel
Save