Skip to content

Commit

Permalink
Use the View-based preloading for TerrainGrid as well
Browse files Browse the repository at this point in the history
  • Loading branch information
scrawl committed Mar 14, 2017
1 parent 3c29e2d commit 6ccb600
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
6 changes: 5 additions & 1 deletion apps/openmw/mwworld/cellpreloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace MWWorld
, mPreloadInstances(preloadInstances)
, mAbort(false)
{
mTerrainView = mTerrain->createView();

ListModelsVisitor visitor (mMeshes);
if (cell->getState() == MWWorld::CellStore::State_Loaded)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -160,6 +162,8 @@ namespace MWWorld

volatile bool mAbort;

osg::ref_ptr<Terrain::View> 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<osg::ref_ptr<const osg::Object> > mPreloadedObjects;
};
Expand Down
17 changes: 15 additions & 2 deletions components/terrain/terraingrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
namespace Terrain
{

class MyView : public View
{
public:
osg::ref_ptr<osg::Node> 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)
Expand All @@ -23,10 +31,10 @@ TerrainGrid::~TerrainGrid()
}
}

osg::ref_ptr<osg::Node> 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<MyView*>(view)->mLoaded = buildTerrain(NULL, 1.f, center);
}

osg::ref_ptr<osg::Node> TerrainGrid::buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter)
Expand Down Expand Up @@ -84,4 +92,9 @@ void TerrainGrid::unloadCell(int x, int y)
mGrid.erase(it);
}

View *TerrainGrid::createView()
{
return new MyView;
}

}
7 changes: 3 additions & 4 deletions components/terrain/terraingrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ 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<osg::Node> 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);

/// @note Not thread safe.
virtual void unloadCell(int x, int y);

View* createView();

private:
osg::ref_ptr<osg::Node> buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter);

Expand Down
5 changes: 0 additions & 5 deletions components/terrain/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ float World::getHeightAt(const osg::Vec3f &worldPos)
return mStorage->getHeightAt(worldPos);
}

osg::ref_ptr<osg::Node> World::cacheCell(int x, int y)
{
return NULL;
}

void World::updateTextureFiltering()
{
mTextureManager->updateTextureFiltering();
Expand Down
6 changes: 2 additions & 4 deletions components/terrain/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<osg::Node> 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.
Expand Down

0 comments on commit 6ccb600

Please sign in to comment.