2014-06-29 00:42:36 +00:00
|
|
|
#include "terraingrid.hpp"
|
|
|
|
|
2015-06-02 23:18:36 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-02-19 00:45:28 +00:00
|
|
|
#include <osg/Material>
|
2017-03-06 19:41:02 +00:00
|
|
|
#include <osg/Group>
|
2017-03-03 17:26:40 +00:00
|
|
|
|
2016-02-09 14:30:53 +00:00
|
|
|
#include <components/sceneutil/unrefqueue.hpp>
|
2015-06-02 23:18:36 +00:00
|
|
|
|
2017-03-06 15:32:56 +00:00
|
|
|
#include "texturemanager.hpp"
|
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
|
|
|
{
|
|
|
|
|
2016-02-19 14:00:50 +00:00
|
|
|
TerrainGrid::TerrainGrid(osg::Group* parent, Resource::ResourceSystem* resourceSystem, osgUtil::IncrementalCompileOperation* ico, Storage* storage, int nodeMask, Shader::ShaderManager* shaderManager, SceneUtil::UnrefQueue* unrefQueue)
|
2015-06-02 23:18:36 +00:00
|
|
|
: Terrain::World(parent, resourceSystem, ico, storage, nodeMask)
|
2015-11-06 19:14:57 +00:00
|
|
|
, mNumSplits(4)
|
2016-02-09 14:30:53 +00:00
|
|
|
, mUnrefQueue(unrefQueue)
|
2016-02-19 14:00:50 +00:00
|
|
|
, mShaderManager(shaderManager)
|
2014-06-29 00:42:36 +00:00
|
|
|
{
|
2016-02-19 00:45:28 +00:00
|
|
|
osg::ref_ptr<osg::Material> material (new osg::Material);
|
|
|
|
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
|
|
|
|
mTerrainRoot->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON);
|
2017-03-06 19:41:02 +00:00
|
|
|
|
|
|
|
mChunkManager->setShaderManager(mShaderManager);
|
2014-06-29 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TerrainGrid::~TerrainGrid()
|
|
|
|
{
|
|
|
|
while (!mGrid.empty())
|
|
|
|
{
|
|
|
|
unloadCell(mGrid.begin()->first.first, mGrid.begin()->first.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 19:57:30 +00:00
|
|
|
osg::ref_ptr<osg::Node> TerrainGrid::cacheCell(int x, int y)
|
|
|
|
{
|
2017-03-06 19:41:02 +00:00
|
|
|
osg::Vec2f center(x+0.5f, y+0.5f);
|
|
|
|
return 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-06 19:41:02 +00:00
|
|
|
osg::ref_ptr<osg::Node> node = mChunkManager->getChunk(chunkSize, chunkCenter);
|
|
|
|
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
|
|
|
|
|
|
|
if (mUnrefQueue.get())
|
2016-02-09 19:23:53 +00:00
|
|
|
mUnrefQueue->push(terrainNode);
|
2014-06-29 00:42:36 +00:00
|
|
|
|
2015-06-02 23:18:36 +00:00
|
|
|
mGrid.erase(it);
|
2014-06-29 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 22:14:52 +00:00
|
|
|
void TerrainGrid::updateTextureFiltering()
|
|
|
|
{
|
2017-03-06 15:32:56 +00:00
|
|
|
mTextureManager->updateTextureFiltering();
|
2016-02-14 22:14:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 00:42:36 +00:00
|
|
|
}
|