diff --git a/components/terrain/cellborder.cpp b/components/terrain/cellborder.cpp index 31a162af7..f9af60b75 100644 --- a/components/terrain/cellborder.cpp +++ b/components/terrain/cellborder.cpp @@ -12,8 +12,7 @@ namespace MWRender CellBorder::CellBorder(Terrain::World *world, osg::Group *root): mWorld(world), - mRoot(root), - mBorderRoot(0) + mRoot(root) { } @@ -71,8 +70,6 @@ void CellBorder::createCellBorderGeometry(int x, int y) mRoot->addChild(borderGeode); - mBorderRoot = borderGeode; - mCellBorderNodes[std::make_pair(x,y)] = borderGeode; } @@ -84,9 +81,15 @@ void CellBorder::destroyCellBorderGeometry(int x, int y) return; osg::ref_ptr borderNode = it->second; - mBorderRoot->removeChild(borderNode); + mRoot->removeChild(borderNode); mCellBorderNodes.erase(it); } +void CellBorder::destroyCellBorderGeometry() +{ + for (CellGrid::iterator it = mCellBorderNodes.begin(); it != mCellBorderNodes.end(); ++it) + destroyCellBorderGeometry(it->first.first,it->first.second); +} + } diff --git a/components/terrain/cellborder.hpp b/components/terrain/cellborder.hpp index 6144f8c32..a505aec9c 100644 --- a/components/terrain/cellborder.hpp +++ b/components/terrain/cellborder.hpp @@ -24,10 +24,14 @@ namespace MWRender void createCellBorderGeometry(int x, int y); void destroyCellBorderGeometry(int x, int y); + /** + Destroys the geometry for all borders. + */ + void destroyCellBorderGeometry(); + protected: Terrain::World *mWorld; osg::Group *mRoot; - osg::Group *mBorderRoot; CellGrid mCellBorderNodes; }; diff --git a/components/terrain/terraingrid.cpp b/components/terrain/terraingrid.cpp index f5ab6f64b..edd7de5da 100644 --- a/components/terrain/terraingrid.cpp +++ b/components/terrain/terraingrid.cpp @@ -19,8 +19,9 @@ public: TerrainGrid::TerrainGrid(osg::Group* parent, osg::Group* compileRoot, Resource::ResourceSystem* resourceSystem, Storage* storage, int nodeMask, int preCompileMask, int borderMask) : Terrain::World(parent, compileRoot, resourceSystem, storage, nodeMask, preCompileMask, borderMask) - , mNumSplits(4) + , mNumSplits(4), mBorderVisible(false) { + mCellBorder.reset(new MWRender::CellBorder(this,mTerrainRoot.get())); } TerrainGrid::~TerrainGrid() @@ -65,6 +66,19 @@ osg::ref_ptr TerrainGrid::buildTerrain (osg::Group* parent, float chu } } +void TerrainGrid::setBordersVisible(bool visible) +{ + mBorderVisible = visible; + + if (visible) + { + for (MWRender::CellBorder::CellGrid::iterator it = mGrid.begin(); it != mGrid.end(); ++it) + mCellBorder->createCellBorderGeometry(it->first.first,it->first.second); + } + else + mCellBorder->destroyCellBorderGeometry(); +} + void TerrainGrid::loadCell(int x, int y) { if (mGrid.find(std::make_pair(x, y)) != mGrid.end()) @@ -75,7 +89,8 @@ void TerrainGrid::loadCell(int x, int y) if (!terrainNode) return; // no terrain defined - Terrain::World::loadCell(x,y); + if (mBorderVisible) + mCellBorder->createCellBorderGeometry(x,y); mTerrainRoot->addChild(terrainNode); @@ -84,12 +99,13 @@ void TerrainGrid::loadCell(int x, int y) void TerrainGrid::unloadCell(int x, int y) { - World::unloadCell(x,y); - MWRender::CellBorder::CellGrid::iterator it = mGrid.find(std::make_pair(x,y)); if (it == mGrid.end()) return; + if (mBorderVisible) + mCellBorder->destroyCellBorderGeometry(x,y); + osg::ref_ptr terrainNode = it->second; mTerrainRoot->removeChild(terrainNode); diff --git a/components/terrain/terraingrid.hpp b/components/terrain/terraingrid.hpp index f21dd39d3..164f09bd6 100644 --- a/components/terrain/terraingrid.hpp +++ b/components/terrain/terraingrid.hpp @@ -29,6 +29,7 @@ namespace Terrain View* createView(); + virtual void setBordersVisible(bool visible) override; private: osg::ref_ptr buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter); @@ -36,8 +37,11 @@ namespace Terrain unsigned int mNumSplits; MWRender::CellBorder::CellGrid mGrid; - }; + std::unique_ptr mCellBorder; + + bool mBorderVisible; + }; } #endif diff --git a/components/terrain/world.cpp b/components/terrain/world.cpp index d871d141b..b88e3f157 100644 --- a/components/terrain/world.cpp +++ b/components/terrain/world.cpp @@ -28,12 +28,6 @@ World::World(osg::Group* parent, osg::Group* compileRoot, Resource::ResourceSyst mTerrainRoot->setName("Terrain Root"); - mBorderRoot = new osg::Switch; - mBorderRoot->setName("Border Root"); - mBorderRoot->setNodeMask(borderMask); - - mTerrainRoot->addChild(mBorderRoot); - osg::ref_ptr compositeCam = new osg::Camera; compositeCam->setRenderOrder(osg::Camera::PRE_RENDER, -1); compositeCam->setProjectionMatrix(osg::Matrix::identity()); @@ -53,30 +47,8 @@ World::World(osg::Group* parent, osg::Group* compileRoot, Resource::ResourceSyst mTextureManager.reset(new TextureManager(mResourceSystem->getSceneManager())); mChunkManager.reset(new ChunkManager(mStorage, mResourceSystem->getSceneManager(), mTextureManager.get(), mCompositeMapRenderer)); - mCellBorder.reset(new MWRender::CellBorder(this,mTerrainRoot.get())); - mResourceSystem->addResourceManager(mChunkManager.get()); mResourceSystem->addResourceManager(mTextureManager.get()); - - setBordersVisible(false); -} - -void World::setBordersVisible(bool visible) -{ - if (visible) - mBorderRoot->setAllChildrenOn(); - else - mBorderRoot->setAllChildrenOff(); -} - -void World::loadCell(int x, int y) -{ - mCellBorder->createCellBorderGeometry(x,y); -} - -void World::unloadCell(int x, int y) -{ - mCellBorder->destroyCellBorderGeometry(x,y); } World::~World() diff --git a/components/terrain/world.hpp b/components/terrain/world.hpp index b847d08ab..77aa1ee28 100644 --- a/components/terrain/world.hpp +++ b/components/terrain/world.hpp @@ -4,14 +4,11 @@ #include #include #include -#include #include #include "defs.hpp" -#include "cellborder.hpp" - namespace osg { class Group; @@ -79,15 +76,15 @@ namespace Terrain /// Load the cell into the scene graph. /// @note Not thread safe. - virtual void loadCell(int x, int y); + virtual void loadCell(int x, int y) {} /// Remove the cell from the scene graph. /// @note Not thread safe. - virtual void unloadCell(int x, int y); + virtual void unloadCell(int x, int y) {} virtual void enable(bool enabled) {} - void setBordersVisible(bool visible); + virtual void setBordersVisible(bool visible) {} /// Create a View to use with preload feature. The caller is responsible for deleting the view. /// @note Thread safe. @@ -104,14 +101,10 @@ namespace Terrain Storage* getStorage() { return mStorage; } protected: - void createCellBorderGeometry(int x, int y); - void destroyCellBorderGeometry(int x, int y); - Storage* mStorage; osg::ref_ptr mParent; osg::ref_ptr mTerrainRoot; - osg::ref_ptr mBorderRoot; osg::ref_ptr mCompositeMapCamera; osg::ref_ptr mCompositeMapRenderer; @@ -120,8 +113,6 @@ namespace Terrain std::unique_ptr mTextureManager; std::unique_ptr mChunkManager; - - std::unique_ptr mCellBorder; }; }