From 6ccb6009eee61b92dfe1072d6efffef6b1ede145 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 9 Mar 2017 20:52:50 +0100 Subject: [PATCH] Use the View-based preloading for TerrainGrid as well --- apps/openmw/mwworld/cellpreloader.cpp | 6 +++++- components/terrain/terraingrid.cpp | 17 +++++++++++++++-- components/terrain/terraingrid.hpp | 7 +++---- components/terrain/world.cpp | 5 ----- components/terrain/world.hpp | 6 ++---- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/apps/openmw/mwworld/cellpreloader.cpp b/apps/openmw/mwworld/cellpreloader.cpp index 5d4a2717f..d3affecc4 100644 --- a/apps/openmw/mwworld/cellpreloader.cpp +++ b/apps/openmw/mwworld/cellpreloader.cpp @@ -60,6 +60,8 @@ namespace MWWorld , mPreloadInstances(preloadInstances) , mAbort(false) { + mTerrainView = mTerrain->createView(); + ListModelsVisitor visitor (mMeshes); if (cell->getState() == MWWorld::CellStore::State_Loaded) { @@ -92,7 +94,7 @@ namespace MWWorld { try { - mPreloadedObjects.push_back(mTerrain->cacheCell(mX, mY)); + mTerrain->cacheCell(mTerrainView.get(), mX, mY); mPreloadedObjects.push_back(mLandManager->getLand(mX, mY)); } catch(std::exception& e) @@ -160,6 +162,8 @@ namespace MWWorld volatile bool mAbort; + osg::ref_ptr mTerrainView; + // keep a ref to the loaded objects to make sure it stays loaded as long as this cell is in the preloaded state std::vector > mPreloadedObjects; }; diff --git a/components/terrain/terraingrid.cpp b/components/terrain/terraingrid.cpp index 04b29e169..466cddddc 100644 --- a/components/terrain/terraingrid.cpp +++ b/components/terrain/terraingrid.cpp @@ -9,6 +9,14 @@ namespace Terrain { +class MyView : public View +{ +public: + osg::ref_ptr mLoaded; + + virtual void reset(unsigned int frame) {} +}; + TerrainGrid::TerrainGrid(osg::Group* parent, osg::Group* compileRoot, Resource::ResourceSystem* resourceSystem, Storage* storage, int nodeMask, int preCompileMask) : Terrain::World(parent, compileRoot, resourceSystem, storage, nodeMask, preCompileMask) , mNumSplits(4) @@ -23,10 +31,10 @@ TerrainGrid::~TerrainGrid() } } -osg::ref_ptr TerrainGrid::cacheCell(int x, int y) +void TerrainGrid::cacheCell(View* view, int x, int y) { osg::Vec2f center(x+0.5f, y+0.5f); - return buildTerrain(NULL, 1.f, center); + static_cast(view)->mLoaded = buildTerrain(NULL, 1.f, center); } osg::ref_ptr TerrainGrid::buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter) @@ -84,4 +92,9 @@ void TerrainGrid::unloadCell(int x, int y) mGrid.erase(it); } +View *TerrainGrid::createView() +{ + return new MyView; +} + } diff --git a/components/terrain/terraingrid.hpp b/components/terrain/terraingrid.hpp index dc4523eef..189fe7f63 100644 --- a/components/terrain/terraingrid.hpp +++ b/components/terrain/terraingrid.hpp @@ -17,10 +17,7 @@ namespace Terrain TerrainGrid(osg::Group* parent, osg::Group* compileRoot, Resource::ResourceSystem* resourceSystem, Storage* storage, int nodeMask, int preCompileMask=~0); ~TerrainGrid(); - /// Load a terrain cell and store it in cache for later use. - /// @note The returned ref_ptr should be kept by the caller to ensure that the terrain stays in cache for as long as needed. - /// @note Thread safe. - virtual osg::ref_ptr cacheCell(int x, int y); + virtual void cacheCell(View* view, int x, int y); /// @note Not thread safe. virtual void loadCell(int x, int y); @@ -28,6 +25,8 @@ namespace Terrain /// @note Not thread safe. virtual void unloadCell(int x, int y); + View* createView(); + private: osg::ref_ptr buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter); diff --git a/components/terrain/world.cpp b/components/terrain/world.cpp index cbd4327c3..b7cc0ae01 100644 --- a/components/terrain/world.cpp +++ b/components/terrain/world.cpp @@ -70,11 +70,6 @@ float World::getHeightAt(const osg::Vec3f &worldPos) return mStorage->getHeightAt(worldPos); } -osg::ref_ptr World::cacheCell(int x, int y) -{ - return NULL; -} - void World::updateTextureFiltering() { mTextureManager->updateTextureFiltering(); diff --git a/components/terrain/world.hpp b/components/terrain/world.hpp index f7454c60a..6aac963ce 100644 --- a/components/terrain/world.hpp +++ b/components/terrain/world.hpp @@ -62,11 +62,9 @@ namespace Terrain float getHeightAt (const osg::Vec3f& worldPos); - /// Load a terrain cell and store it in cache for later use. - /// @note The returned ref_ptr should be kept by the caller to ensure that the terrain stays in cache for as long as needed. + /// Load a terrain cell at maximum LOD and store it in the View for later use. /// @note Thread safe. - /// @note May be ignored by derived implementations that don't organize the terrain into cells. - virtual osg::ref_ptr cacheCell(int x, int y); + virtual void cacheCell(View* view, int x, int y) {} /// Load the cell into the scene graph. /// @note Not thread safe.