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) Chunk::Chunk(Ogre::HardwareVertexBufferSharedPtr uvBuffer, const Ogre::AxisAlignedBox& bounds, const LoadResponseData& data)
: mBounds(bounds) : mBounds(bounds)
, mOwnMaterial(false)
{ {
mVertexData = OGRE_NEW Ogre::VertexData; mVertexData = OGRE_NEW Ogre::VertexData;
mVertexData->vertexStart = 0; mVertexData->vertexStart = 0;
@ -57,11 +58,7 @@ namespace Terrain
mVertexData->vertexBufferBinding->setBinding(3, colourBuffer); mVertexData->vertexBufferBinding->setBinding(3, colourBuffer);
// Assign a default material in case terrain material fails to be created // 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 mMaterial = Ogre::MaterialManager::getSingleton().getByName("BaseWhite");
// so the original always stays available.
static int materialCount=0;
mMaterial = Ogre::MaterialManager::getSingleton().getByName("BaseWhite")
->clone("BaseWhite"+Ogre::StringConverter::toString(++materialCount));
mIndexData = OGRE_NEW Ogre::IndexData(); mIndexData = OGRE_NEW Ogre::IndexData();
mIndexData->indexStart = 0; mIndexData->indexStart = 0;
@ -75,7 +72,7 @@ namespace Terrain
Chunk::~Chunk() Chunk::~Chunk()
{ {
if (!mMaterial.isNull()) if (!mMaterial.isNull() && mOwnMaterial)
{ {
#if TERRAIN_USE_SHADER #if TERRAIN_USE_SHADER
sh::Factory::getInstance().destroyMaterialInstance(mMaterial->getName()); sh::Factory::getInstance().destroyMaterialInstance(mMaterial->getName());
@ -86,9 +83,19 @@ namespace Terrain
OGRE_DELETE mIndexData; 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; mMaterial = material;
mOwnMaterial = own;
} }
const Ogre::AxisAlignedBox& Chunk::getBoundingBox(void) const const Ogre::AxisAlignedBox& Chunk::getBoundingBox(void) const

@ -20,8 +20,8 @@ namespace Terrain
virtual ~Chunk(); virtual ~Chunk();
/// @note Takes ownership of \a material /// @param own Should we take ownership of the material?
void setMaterial (const Ogre::MaterialPtr& material); void setMaterial (const Ogre::MaterialPtr& material, bool own=true);
void setIndexBuffer(Ogre::HardwareIndexBufferSharedPtr buffer); void setIndexBuffer(Ogre::HardwareIndexBufferSharedPtr buffer);
@ -43,6 +43,7 @@ namespace Terrain
private: private:
Ogre::AxisAlignedBox mBounds; Ogre::AxisAlignedBox mBounds;
Ogre::MaterialPtr mMaterial; Ogre::MaterialPtr mMaterial;
bool mOwnMaterial; // Should we remove mMaterial on destruction?
Ogre::VertexData* mVertexData; Ogre::VertexData* mVertexData;
Ogre::IndexData* mIndexData; 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"); 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"); 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); 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; static int count = 0;
std::stringstream name; std::stringstream name;
@ -82,7 +75,7 @@ namespace Terrain
if (!mShaders) 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::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::Technique* technique = mat->getTechnique(0); Ogre::Technique* technique = mat->getTechnique(0);
technique->removeAllPasses(); technique->removeAllPasses();

@ -29,23 +29,17 @@ namespace Terrain
void enableSplitShadows(bool splitShadows) { mSplitShadows = splitShadows; } void enableSplitShadows(bool splitShadows) { mSplitShadows = splitShadows; }
/// Creates a material suitable for displaying a chunk of terrain using alpha-blending. /// 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 Ogre::MaterialPtr generate ();
/// a new material is created.
Ogre::MaterialPtr generate (Ogre::MaterialPtr mat);
/// Creates a material suitable for displaying a chunk of terrain using a ready-made composite map. /// 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 Ogre::MaterialPtr generateForCompositeMap ();
/// a new material is created.
Ogre::MaterialPtr generateForCompositeMap (Ogre::MaterialPtr mat);
/// Creates a material suitable for rendering composite maps, i.e. for "baking" several layer textures /// 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. /// 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 Ogre::MaterialPtr generateForCompositeMapRTT ();
/// a new material is created.
Ogre::MaterialPtr generateForCompositeMapRTT (Ogre::MaterialPtr mat);
private: private:
Ogre::MaterialPtr create (Ogre::MaterialPtr mat, bool renderCompositeMap, bool displayCompositeMap); Ogre::MaterialPtr create (bool renderCompositeMap, bool displayCompositeMap);
std::vector<LayerInfo> mLayerList; std::vector<LayerInfo> mLayerList;
std::vector<Ogre::TexturePtr> mBlendmapList; std::vector<Ogre::TexturePtr> mBlendmapList;

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

Loading…
Cancel
Save