1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 00:35:23 +00:00

148 lines
4.2 KiB
C++
Raw Normal View History

#include "world.hpp"
2015-06-03 01:18:36 +02:00
#include <osg/Group>
#include <osg/Camera>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include "storage.hpp"
#include "texturemanager.hpp"
#include "chunkmanager.hpp"
#include "compositemaprenderer.hpp"
namespace Terrain
{
World::World(osg::Group* parent, osg::Group* compileRoot, Resource::ResourceSystem* resourceSystem, Storage* storage, unsigned int nodeMask, unsigned int preCompileMask, unsigned int borderMask)
2015-06-03 01:18:36 +02:00
: mStorage(storage)
, mParent(parent)
, mResourceSystem(resourceSystem)
2018-06-14 12:27:22 +02:00
, mBorderVisible(false)
2019-06-13 13:37:00 +00:00
, mHeightCullCallback(new HeightCullCallback)
{
2015-06-03 01:18:36 +02:00
mTerrainRoot = new osg::Group;
mTerrainRoot->setNodeMask(nodeMask);
mTerrainRoot->setName("Terrain Root");
2015-06-03 01:18:36 +02:00
osg::ref_ptr<osg::Camera> compositeCam = new osg::Camera;
compositeCam->setRenderOrder(osg::Camera::PRE_RENDER, -1);
compositeCam->setProjectionMatrix(osg::Matrix::identity());
compositeCam->setViewMatrix(osg::Matrix::identity());
compositeCam->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
compositeCam->setClearMask(0);
compositeCam->setNodeMask(preCompileMask);
mCompositeMapCamera = compositeCam;
compileRoot->addChild(compositeCam);
mCompositeMapRenderer = new CompositeMapRenderer;
compositeCam->addChild(mCompositeMapRenderer);
2015-06-03 01:18:36 +02:00
mParent->addChild(mTerrainRoot);
mTextureManager = std::make_unique<TextureManager>(mResourceSystem->getSceneManager());
mChunkManager = std::make_unique<ChunkManager>(mStorage, mResourceSystem->getSceneManager(), mTextureManager.get(), mCompositeMapRenderer);
mChunkManager->setNodeMask(nodeMask);
mCellBorder = std::make_unique<CellBorder>(this,mTerrainRoot.get(),borderMask,mResourceSystem->getSceneManager());
mResourceSystem->addResourceManager(mChunkManager.get());
mResourceSystem->addResourceManager(mTextureManager.get());
}
World::World(osg::Group* parent, Storage* storage, unsigned int nodeMask)
2020-01-12 11:42:47 +04:00
: mStorage(storage)
, mParent(parent)
, mCompositeMapCamera(nullptr)
, mCompositeMapRenderer(nullptr)
, mResourceSystem(nullptr)
, mTextureManager(nullptr)
, mChunkManager(nullptr)
, mCellBorder(nullptr)
, mBorderVisible(false)
, mHeightCullCallback(nullptr)
{
mTerrainRoot = new osg::Group;
mTerrainRoot->setNodeMask(nodeMask);
mParent->addChild(mTerrainRoot);
}
World::~World()
{
2020-01-12 11:42:47 +04:00
if (mResourceSystem && mChunkManager)
mResourceSystem->removeResourceManager(mChunkManager.get());
if (mResourceSystem && mTextureManager)
mResourceSystem->removeResourceManager(mTextureManager.get());
2015-06-03 01:18:36 +02:00
mParent->removeChild(mTerrainRoot);
2020-01-12 11:42:47 +04:00
if (mCompositeMapCamera && mCompositeMapRenderer)
{
mCompositeMapCamera->removeChild(mCompositeMapRenderer);
mCompositeMapCamera->getParent(0)->removeChild(mCompositeMapCamera);
}
}
2018-06-14 12:27:22 +02:00
void World::setBordersVisible(bool visible)
{
mBorderVisible = visible;
2018-06-14 13:14:38 +02:00
if (visible)
{
for (std::set<std::pair<int,int>>::iterator it = mLoadedCells.begin(); it != mLoadedCells.end(); ++it)
mCellBorder->createCellBorderGeometry(it->first,it->second);
}
else
2018-06-14 12:27:22 +02:00
mCellBorder->destroyCellBorderGeometry();
}
void World::loadCell(int x, int y)
{
if (mBorderVisible)
mCellBorder->createCellBorderGeometry(x,y);
2018-06-14 13:14:38 +02:00
mLoadedCells.insert(std::pair<int,int>(x,y));
2018-06-14 12:27:22 +02:00
}
void World::unloadCell(int x, int y)
{
if (mBorderVisible)
mCellBorder->destroyCellBorderGeometry(x,y);
2018-06-14 13:14:38 +02:00
mLoadedCells.erase(std::pair<int,int>(x,y));
2018-06-14 12:27:22 +02:00
}
void World::setTargetFrameRate(float rate)
{
mCompositeMapRenderer->setTargetFrameRate(rate);
}
2015-06-03 01:18:36 +02:00
float World::getHeightAt(const osg::Vec3f &worldPos)
{
return mStorage->getHeightAt(worldPos);
}
void World::updateTextureFiltering()
{
2020-01-12 11:42:47 +04:00
if (mTextureManager)
mTextureManager->updateTextureFiltering();
}
void World::clearAssociatedCaches()
{
2020-01-12 11:42:47 +04:00
if (mChunkManager)
mChunkManager->clearCache();
}
2019-06-13 13:37:00 +00:00
osg::Callback* World::getHeightCullCallback(float highz, unsigned int mask)
{
2020-01-12 11:42:47 +04:00
if (!mHeightCullCallback) return nullptr;
2019-06-13 13:37:00 +00:00
mHeightCullCallback->setHighZ(highz);
mHeightCullCallback->setCullMask(mask);
return mHeightCullCallback;
}
}