material.cpp (#3117)

pull/3124/head
Bo Svensson 3 years ago committed by GitHub
parent 8d86d90782
commit 095f4b2ed5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -166,6 +166,29 @@ namespace
mValue->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); mValue->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
} }
}; };
class UniformCollection
{
public:
static const UniformCollection& value()
{
static UniformCollection instance;
return instance;
}
osg::ref_ptr<osg::Uniform> mDiffuseMap;
osg::ref_ptr<osg::Uniform> mBlendMap;
osg::ref_ptr<osg::Uniform> mNormalMap;
osg::ref_ptr<osg::Uniform> mColorMode;
UniformCollection()
: mDiffuseMap(new osg::Uniform("diffuseMap", 0))
, mBlendMap(new osg::Uniform("blendMap", 1))
, mNormalMap(new osg::Uniform("normalMap", 2))
, mColorMode(new osg::Uniform("colorMode", 2))
{
}
};
} }
namespace Terrain namespace Terrain
@ -199,32 +222,28 @@ namespace Terrain
} }
} }
int texunit = 0;
if (useShaders) if (useShaders)
{ {
stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); stateset->setTextureAttributeAndModes(0, it->mDiffuseMap);
if (layerTileSize != 1.f) if (layerTileSize != 1.f)
stateset->setTextureAttributeAndModes(texunit, LayerTexMat::value(layerTileSize), osg::StateAttribute::ON); stateset->setTextureAttributeAndModes(0, LayerTexMat::value(layerTileSize), osg::StateAttribute::ON);
stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); stateset->addUniform(UniformCollection::value().mDiffuseMap);
if (!blendmaps.empty()) if (!blendmaps.empty())
{ {
++texunit;
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++); osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++);
stateset->setTextureAttributeAndModes(texunit, blendmap.get()); stateset->setTextureAttributeAndModes(1, blendmap.get());
stateset->setTextureAttributeAndModes(texunit, BlendmapTexMat::value(blendmapScale)); stateset->setTextureAttributeAndModes(1, BlendmapTexMat::value(blendmapScale));
stateset->addUniform(new osg::Uniform("blendMap", texunit)); stateset->addUniform(UniformCollection::value().mBlendMap);
} }
if (it->mNormalMap) if (it->mNormalMap)
{ {
++texunit; stateset->setTextureAttributeAndModes(2, it->mNormalMap);
stateset->setTextureAttributeAndModes(texunit, it->mNormalMap); stateset->addUniform(UniformCollection::value().mNormalMap);
stateset->addUniform(new osg::Uniform("normalMap", texunit));
} }
Shader::ShaderManager::DefineMap defineMap; Shader::ShaderManager::DefineMap defineMap;
@ -242,31 +261,27 @@ namespace Terrain
} }
stateset->setAttributeAndModes(shaderManager->getProgram(vertexShader, fragmentShader)); stateset->setAttributeAndModes(shaderManager->getProgram(vertexShader, fragmentShader));
stateset->addUniform(new osg::Uniform("colorMode", 2)); stateset->addUniform(UniformCollection::value().mColorMode);
} }
else else
{ {
// Add the actual layer texture // Add the actual layer texture
osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap; osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap;
stateset->setTextureAttributeAndModes(texunit, tex.get()); stateset->setTextureAttributeAndModes(0, tex.get());
if (layerTileSize != 1.f) if (layerTileSize != 1.f)
stateset->setTextureAttributeAndModes(texunit, LayerTexMat::value(layerTileSize), osg::StateAttribute::ON); stateset->setTextureAttributeAndModes(0, LayerTexMat::value(layerTileSize), osg::StateAttribute::ON);
++texunit;
// Multiply by the alpha map // Multiply by the alpha map
if (!blendmaps.empty()) if (!blendmaps.empty())
{ {
osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++); osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++);
stateset->setTextureAttributeAndModes(texunit, blendmap.get()); stateset->setTextureAttributeAndModes(1, blendmap.get());
// This is to map corner vertices directly to the center of a blendmap texel. // This is to map corner vertices directly to the center of a blendmap texel.
stateset->setTextureAttributeAndModes(texunit, BlendmapTexMat::value(blendmapScale)); stateset->setTextureAttributeAndModes(1, BlendmapTexMat::value(blendmapScale));
stateset->setTextureAttributeAndModes(texunit, TexEnvCombine::value(), osg::StateAttribute::ON); stateset->setTextureAttributeAndModes(1, TexEnvCombine::value(), osg::StateAttribute::ON);
++texunit;
} }
} }

Loading…
Cancel
Save