2014-06-29 00:42:36 +00:00
|
|
|
#include "terraingrid.hpp"
|
|
|
|
|
2015-06-02 23:18:36 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2017-03-06 19:41:02 +00:00
|
|
|
#include <osg/Group>
|
2017-03-03 17:26:40 +00:00
|
|
|
|
2017-03-06 19:41:02 +00:00
|
|
|
#include "chunkmanager.hpp"
|
2014-06-29 00:42:36 +00:00
|
|
|
|
2014-08-07 18:43:33 +00:00
|
|
|
namespace Terrain
|
2014-06-29 00:42:36 +00:00
|
|
|
{
|
|
|
|
|
2017-03-09 19:52:50 +00:00
|
|
|
class MyView : public View
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
osg::ref_ptr<osg::Node> mLoaded;
|
|
|
|
|
|
|
|
virtual void reset(unsigned int frame) {}
|
|
|
|
};
|
|
|
|
|
2017-03-07 16:25:23 +00:00
|
|
|
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)
|
2015-11-06 19:14:57 +00:00
|
|
|
, mNumSplits(4)
|
2014-06-29 00:42:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TerrainGrid::~TerrainGrid()
|
|
|
|
{
|
|
|
|
while (!mGrid.empty())
|
|
|
|
{
|
|
|
|
unloadCell(mGrid.begin()->first.first, mGrid.begin()->first.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:52:50 +00:00
|
|
|
void TerrainGrid::cacheCell(View* view, int x, int y)
|
2016-02-09 19:57:30 +00:00
|
|
|
{
|
2017-03-06 19:41:02 +00:00
|
|
|
osg::Vec2f center(x+0.5f, y+0.5f);
|
2017-03-09 19:52:50 +00:00
|
|
|
static_cast<MyView*>(view)->mLoaded = buildTerrain(NULL, 1.f, center);
|
2016-02-09 19:57:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 19:14:57 +00:00
|
|
|
osg::ref_ptr<osg::Node> TerrainGrid::buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter)
|
2014-06-29 00:42:36 +00:00
|
|
|
{
|
2015-11-06 19:14:57 +00:00
|
|
|
if (chunkSize * mNumSplits > 1.f)
|
|
|
|
{
|
|
|
|
// keep splitting
|
|
|
|
osg::ref_ptr<osg::Group> group (new osg::Group);
|
|
|
|
if (parent)
|
|
|
|
parent->addChild(group);
|
2015-11-06 19:21:39 +00:00
|
|
|
|
2015-11-06 19:14:57 +00:00
|
|
|
float newChunkSize = chunkSize/2.f;
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(newChunkSize/2.f, newChunkSize/2.f));
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(newChunkSize/2.f, -newChunkSize/2.f));
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(-newChunkSize/2.f, newChunkSize/2.f));
|
|
|
|
buildTerrain(group, newChunkSize, chunkCenter + osg::Vec2f(-newChunkSize/2.f, -newChunkSize/2.f));
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-08 23:45:31 +00:00
|
|
|
osg::ref_ptr<osg::Node> node = mChunkManager->getChunk(chunkSize, chunkCenter, 0, 0);
|
2017-03-06 19:41:02 +00:00
|
|
|
if (!node)
|
|
|
|
return NULL;
|
2015-11-06 19:14:57 +00:00
|
|
|
if (parent)
|
2017-03-06 19:41:02 +00:00
|
|
|
parent->addChild(node);
|
2017-03-06 15:32:56 +00:00
|
|
|
|
2017-03-06 19:41:02 +00:00
|
|
|
return node;
|
2015-06-02 23:18:36 +00:00
|
|
|
}
|
2015-11-06 19:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TerrainGrid::loadCell(int x, int y)
|
|
|
|
{
|
|
|
|
if (mGrid.find(std::make_pair(x, y)) != mGrid.end())
|
|
|
|
return; // already loaded
|
2014-06-29 00:42:36 +00:00
|
|
|
|
2017-03-06 19:41:02 +00:00
|
|
|
osg::Vec2f center(x+0.5f, y+0.5f);
|
|
|
|
osg::ref_ptr<osg::Node> terrainNode = buildTerrain(NULL, 1.f, center);
|
2015-11-06 19:14:57 +00:00
|
|
|
if (!terrainNode)
|
2017-03-06 19:41:02 +00:00
|
|
|
return; // no terrain defined
|
2015-11-06 19:14:57 +00:00
|
|
|
|
2016-02-09 19:23:53 +00:00
|
|
|
mTerrainRoot->addChild(terrainNode);
|
2014-06-29 00:42:36 +00:00
|
|
|
|
2016-02-09 19:23:53 +00:00
|
|
|
mGrid[std::make_pair(x,y)] = terrainNode;
|
2014-06-29 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 23:18:36 +00:00
|
|
|
void TerrainGrid::unloadCell(int x, int y)
|
2014-06-29 00:42:36 +00:00
|
|
|
{
|
2015-06-02 23:18:36 +00:00
|
|
|
Grid::iterator it = mGrid.find(std::make_pair(x,y));
|
2014-06-29 00:42:36 +00:00
|
|
|
if (it == mGrid.end())
|
2015-06-02 23:18:36 +00:00
|
|
|
return;
|
2014-06-29 00:42:36 +00:00
|
|
|
|
2016-02-09 19:57:30 +00:00
|
|
|
osg::ref_ptr<osg::Node> terrainNode = it->second;
|
2016-02-09 19:23:53 +00:00
|
|
|
mTerrainRoot->removeChild(terrainNode);
|
2016-02-09 14:30:53 +00:00
|
|
|
|
2015-06-02 23:18:36 +00:00
|
|
|
mGrid.erase(it);
|
2014-06-29 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 19:52:50 +00:00
|
|
|
View *TerrainGrid::createView()
|
|
|
|
{
|
|
|
|
return new MyView;
|
|
|
|
}
|
|
|
|
|
2014-06-29 00:42:36 +00:00
|
|
|
}
|