1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 15:29:55 +00:00
openmw/components/terrain/world.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
4.7 KiB
C++
Raw Normal View History

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