Addition to 7c9c0830a96: don't create useless BaseWhite clones

deque
scrawl 11 years ago
parent dcc0e2d105
commit c018319940

@ -16,6 +16,7 @@ namespace Terrain
Chunk::Chunk(Ogre::HardwareVertexBufferSharedPtr uvBuffer, const Ogre::AxisAlignedBox& bounds, const LoadResponseData& data)
: mBounds(bounds)
, mOwnMaterial(false)
{
mVertexData = OGRE_NEW Ogre::VertexData;
mVertexData->vertexStart = 0;
@ -57,11 +58,7 @@ namespace Terrain
mVertexData->vertexBufferBinding->setBinding(3, colourBuffer);
// Assign a default material in case terrain material fails to be created
// Since we are removing this material in the destructor, it must be cloned from BaseWhite
// so the original always stays available.
static int materialCount=0;
mMaterial = Ogre::MaterialManager::getSingleton().getByName("BaseWhite")
->clone("BaseWhite"+Ogre::StringConverter::toString(++materialCount));
mMaterial = Ogre::MaterialManager::getSingleton().getByName("BaseWhite");
mIndexData = OGRE_NEW Ogre::IndexData();
mIndexData->indexStart = 0;
@ -75,7 +72,7 @@ namespace Terrain
Chunk::~Chunk()
{
if (!mMaterial.isNull())
if (!mMaterial.isNull() && mOwnMaterial)
{
#if TERRAIN_USE_SHADER
sh::Factory::getInstance().destroyMaterialInstance(mMaterial->getName());
@ -86,9 +83,19 @@ namespace Terrain
OGRE_DELETE mIndexData;
}
void Chunk::setMaterial(const Ogre::MaterialPtr &material)
void Chunk::setMaterial(const Ogre::MaterialPtr &material, bool own)
{
// Clean up the previous material, if we own it
if (!mMaterial.isNull() && mOwnMaterial)
{
#if TERRAIN_USE_SHADER
sh::Factory::getInstance().destroyMaterialInstance(mMaterial->getName());
#endif
Ogre::MaterialManager::getSingleton().remove(mMaterial->getName());
}
mMaterial = material;
mOwnMaterial = own;
}
const Ogre::AxisAlignedBox& Chunk::getBoundingBox(void) const

@ -20,8 +20,8 @@ namespace Terrain
virtual ~Chunk();
/// @note Takes ownership of \a material
void setMaterial (const Ogre::MaterialPtr& material);
/// @param own Should we take ownership of the material?
void setMaterial (const Ogre::MaterialPtr& material, bool own=true);
void setIndexBuffer(Ogre::HardwareIndexBufferSharedPtr buffer);
@ -43,6 +43,7 @@ namespace Terrain
private:
Ogre::AxisAlignedBox mBounds;
Ogre::MaterialPtr mMaterial;
bool mOwnMaterial; // Should we remove mMaterial on destruction?
Ogre::VertexData* mVertexData;
Ogre::IndexData* mIndexData;

@ -46,35 +46,28 @@ namespace Terrain
}
Ogre::MaterialPtr MaterialGenerator::generate(Ogre::MaterialPtr mat)
Ogre::MaterialPtr MaterialGenerator::generate()
{
assert(!mLayerList.empty() && "Can't create material with no layers");
return create(mat, false, false);
return create(false, false);
}
Ogre::MaterialPtr MaterialGenerator::generateForCompositeMapRTT(Ogre::MaterialPtr mat)
Ogre::MaterialPtr MaterialGenerator::generateForCompositeMapRTT()
{
assert(!mLayerList.empty() && "Can't create material with no layers");
return create(mat, true, false);
return create(true, false);
}
Ogre::MaterialPtr MaterialGenerator::generateForCompositeMap(Ogre::MaterialPtr mat)
Ogre::MaterialPtr MaterialGenerator::generateForCompositeMap()
{
return create(mat, false, true);
return create(false, true);
}
Ogre::MaterialPtr MaterialGenerator::create(Ogre::MaterialPtr mat, bool renderCompositeMap, bool displayCompositeMap)
Ogre::MaterialPtr MaterialGenerator::create(bool renderCompositeMap, bool displayCompositeMap)
{
assert(!renderCompositeMap || !displayCompositeMap);
if (!mat.isNull())
{
#if TERRAIN_USE_SHADER
sh::Factory::getInstance().destroyMaterialInstance(mat->getName());
#endif
Ogre::MaterialManager::getSingleton().remove(mat->getName());
}
static int count = 0;
std::stringstream name;
@ -82,7 +75,7 @@ namespace Terrain
if (!mShaders)
{
mat = Ogre::MaterialManager::getSingleton().create(name.str(),
Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create(name.str(),
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::Technique* technique = mat->getTechnique(0);
technique->removeAllPasses();

@ -29,23 +29,17 @@ namespace Terrain
void enableSplitShadows(bool splitShadows) { mSplitShadows = splitShadows; }
/// Creates a material suitable for displaying a chunk of terrain using alpha-blending.
/// @param mat Material that will be replaced by the generated material. May be empty as well, in which case
/// a new material is created.
Ogre::MaterialPtr generate (Ogre::MaterialPtr mat);
Ogre::MaterialPtr generate ();
/// Creates a material suitable for displaying a chunk of terrain using a ready-made composite map.
/// @param mat Material that will be replaced by the generated material. May be empty as well, in which case
/// a new material is created.
Ogre::MaterialPtr generateForCompositeMap (Ogre::MaterialPtr mat);
Ogre::MaterialPtr generateForCompositeMap ();
/// Creates a material suitable for rendering composite maps, i.e. for "baking" several layer textures
/// into one. The main difference compared to a normal material is that no shading is applied at this point.
/// @param mat Material that will be replaced by the generated material. May be empty as well, in which case
/// a new material is created.
Ogre::MaterialPtr generateForCompositeMapRTT (Ogre::MaterialPtr mat);
Ogre::MaterialPtr generateForCompositeMapRTT ();
private:
Ogre::MaterialPtr create (Ogre::MaterialPtr mat, bool renderCompositeMap, bool displayCompositeMap);
Ogre::MaterialPtr create (bool renderCompositeMap, bool displayCompositeMap);
std::vector<LayerInfo> mLayerList;
std::vector<Ogre::TexturePtr> mBlendmapList;

@ -400,13 +400,13 @@ void QuadTreeNode::load(const LoadResponseData &data)
{
if (mSize == 1)
{
mChunk->setMaterial(mMaterialGenerator->generate(mChunk->getMaterial()));
mChunk->setMaterial(mMaterialGenerator->generate());
}
else
{
ensureCompositeMap();
mMaterialGenerator->setCompositeMap(mCompositeMap->getName());
mChunk->setMaterial(mMaterialGenerator->generateForCompositeMap(mChunk->getMaterial()));
mChunk->setMaterial(mMaterialGenerator->generateForCompositeMap());
}
}
// else: will be loaded in loadMaterials() after background thread has finished loading layers
@ -532,13 +532,13 @@ void QuadTreeNode::loadMaterials()
{
if (mSize == 1)
{
mChunk->setMaterial(mMaterialGenerator->generate(mChunk->getMaterial()));
mChunk->setMaterial(mMaterialGenerator->generate());
}
else
{
ensureCompositeMap();
mMaterialGenerator->setCompositeMap(mCompositeMap->getName());
mChunk->setMaterial(mMaterialGenerator->generateForCompositeMap(mChunk->getMaterial()));
mChunk->setMaterial(mMaterialGenerator->generateForCompositeMap());
}
}
}
@ -554,7 +554,7 @@ void QuadTreeNode::prepareForCompositeMap(Ogre::TRect<float> area)
std::vector<LayerInfo> layer;
layer.push_back(mTerrain->getStorage()->getDefaultLayer());
matGen.setLayerList(layer);
makeQuad(sceneMgr, area.left, area.top, area.right, area.bottom, matGen.generateForCompositeMapRTT(Ogre::MaterialPtr()));
makeQuad(sceneMgr, area.left, area.top, area.right, area.bottom, matGen.generateForCompositeMapRTT());
return;
}
if (mSize > 1)
@ -577,7 +577,7 @@ void QuadTreeNode::prepareForCompositeMap(Ogre::TRect<float> area)
else
{
// TODO: when to destroy?
Ogre::MaterialPtr material = mMaterialGenerator->generateForCompositeMapRTT(Ogre::MaterialPtr());
Ogre::MaterialPtr material = mMaterialGenerator->generateForCompositeMapRTT();
makeQuad(sceneMgr, area.left, area.top, area.right, area.bottom, material);
}
}
@ -612,9 +612,9 @@ void QuadTreeNode::applyMaterials()
mMaterialGenerator->enableShadows(mTerrain->getShadowsEnabled());
mMaterialGenerator->enableSplitShadows(mTerrain->getSplitShadowsEnabled());
if (mSize <= 1)
mChunk->setMaterial(mMaterialGenerator->generate(Ogre::MaterialPtr()));
mChunk->setMaterial(mMaterialGenerator->generate());
else
mChunk->setMaterial(mMaterialGenerator->generateForCompositeMap(Ogre::MaterialPtr()));
mChunk->setMaterial(mMaterialGenerator->generateForCompositeMap());
}
if (hasChildren())
for (int i=0; i<4; ++i)

Loading…
Cancel
Save