1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-09 21:42:13 +00:00
OpenMW/components/terrain/terraingrid.cpp

107 lines
3.0 KiB
C++
Raw Normal View History

#include "terraingrid.hpp"
2015-06-02 23:18:36 +00:00
#include <memory>
#include <osg/Material>
#include <osg/Group>
#include <components/sceneutil/unrefqueue.hpp>
2015-06-02 23:18:36 +00:00
#include "texturemanager.hpp"
#include "chunkmanager.hpp"
namespace Terrain
{
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)
, mNumSplits(4)
, mUnrefQueue(unrefQueue)
2016-02-19 14:00:50 +00:00
, mShaderManager(shaderManager)
{
osg::ref_ptr<osg::Material> material (new osg::Material);
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
mTerrainRoot->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON);
mChunkManager->setShaderManager(mShaderManager);
}
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)
{
osg::Vec2f center(x+0.5f, y+0.5f);
return buildTerrain(NULL, 1.f, center);
2016-02-09 19:57:30 +00:00
}
osg::ref_ptr<osg::Node> TerrainGrid::buildTerrain (osg::Group* parent, float chunkSize, const osg::Vec2f& chunkCenter)
{
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
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
{
osg::ref_ptr<osg::Node> node = mChunkManager->getChunk(chunkSize, chunkCenter);
if (!node)
return NULL;
if (parent)
parent->addChild(node);
return node;
2015-06-02 23:18:36 +00:00
}
}
void TerrainGrid::loadCell(int x, int y)
{
if (mGrid.find(std::make_pair(x, y)) != mGrid.end())
return; // already loaded
osg::Vec2f center(x+0.5f, y+0.5f);
osg::ref_ptr<osg::Node> terrainNode = buildTerrain(NULL, 1.f, center);
if (!terrainNode)
return; // no terrain defined
2016-02-09 19:23:53 +00:00
mTerrainRoot->addChild(terrainNode);
2016-02-09 19:23:53 +00:00
mGrid[std::make_pair(x,y)] = terrainNode;
}
2015-06-02 23:18:36 +00:00
void TerrainGrid::unloadCell(int x, int y)
{
2015-06-02 23:18:36 +00:00
Grid::iterator it = mGrid.find(std::make_pair(x,y));
if (it == mGrid.end())
2015-06-02 23:18:36 +00:00
return;
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);
if (mUnrefQueue.get())
2016-02-09 19:23:53 +00:00
mUnrefQueue->push(terrainNode);
2015-06-02 23:18:36 +00:00
mGrid.erase(it);
}
void TerrainGrid::updateTextureFiltering()
{
mTextureManager->updateTextureFiltering();
}
}