1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 06:23:52 +00:00

local map rendering (nothing to see yet, as it is not displayed in GUI)

This commit is contained in:
scrawl 2012-03-10 15:28:18 +01:00
parent 3d1cbcd4e7
commit 5b38b17baf
8 changed files with 245 additions and 2 deletions

View file

@ -14,7 +14,8 @@ set(GAME_HEADER
source_group(game FILES ${GAME} ${GAME_HEADER})
add_openmw_dir (mwrender
renderingmanager debugging sky player animation npcanimation creatureanimation actors objects renderinginterface
renderingmanager debugging sky player animation npcanimation creatureanimation actors objects
renderinginterface localmap
)
add_openmw_dir (mwinput

View file

@ -0,0 +1,160 @@
#include "localmap.hpp"
#include "renderingmanager.hpp"
#include <OgreOverlayManager.h>
#include <OgreMaterialManager.h>
#include <boost/filesystem.hpp>
using namespace MWRender;
using namespace Ogre;
#define CACHE_EXTENSION ".jpg"
#define MAP_RESOLUTION 1024 // 1024*1024 pixels for a 8192*8192 area in world units
LocalMap::LocalMap(OEngine::Render::OgreRenderer* rend)
{
mRendering = rend;
mCellCamera = mRendering->getScene()->createCamera("CellCamera");
mCellCamera->setProjectionType(PT_ORTHOGRAPHIC);
// look down -y
const float sqrt0pt5 = 0.707106781;
mCellCamera->setOrientation(Quaternion(sqrt0pt5, -sqrt0pt5, 0, 0));
// Debug overlay to view the maps
/*
render(0, 0, 10000, 10000, 8192, 8192, "Cell_0_0");
MaterialPtr mat = MaterialManager::getSingleton().create("testMaterial", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mat->getTechnique(0)->getPass(0)->createTextureUnitState("Cell_0_0");
OverlayManager& ovm = OverlayManager::getSingleton();
Overlay* mOverlay = ovm.create( "testOverlay" );
OverlayContainer* overlay_panel;
overlay_panel = (OverlayContainer*)ovm.createOverlayElement("Panel", "testPanel");
overlay_panel->_setPosition(0, 0);
overlay_panel->_setDimensions(0.5, 0.5);
overlay_panel->setMaterialName( "testMaterial" );
overlay_panel->show();
mOverlay->add2D(overlay_panel);
mOverlay->show();
*/
}
void LocalMap::requestMap(MWWorld::Ptr::CellStore* cell)
{
std::string name = "Cell_" + StringConverter::toString(cell->cell->data.gridX)
+ "_" + StringConverter::toString(cell->cell->data.gridY);
const int x = cell->cell->data.gridX;
const int y = cell->cell->data.gridY;
render((x+0.5)*8192, (-y-0.5)*8192, -10000, 10000, 8192, 8192, name);
}
void LocalMap::requestMap(MWWorld::Ptr::CellStore* cell,
AxisAlignedBox bounds)
{
Vector2 z(bounds.getMaximum().y, bounds.getMinimum().y);
Vector2 min(bounds.getMinimum().x, bounds.getMinimum().z);
Vector2 max(bounds.getMaximum().x, bounds.getMaximum().z);
/// \todo why is this workaround needed?
min *= 1.3;
max *= 1.3;
Vector2 length = max-min;
Vector2 center(bounds.getCenter().x, bounds.getCenter().z);
// divide into 8192*8192 segments
const int segsX = std::ceil( length.x / 8192 );
const int segsY = std::ceil( length.y / 8192 );
for (int x=0; x<segsX; ++x)
{
for (int y=0; y<segsY; ++y)
{
Vector2 start = min + Vector2(8192*x,8192*y);
Vector2 newcenter = start + 4096;
render(newcenter.x, newcenter.y, z.y, z.x, 8192, 8192,
cell->cell->name + "_" + StringConverter::toString(x) + "_" + StringConverter::toString(y));
}
}
}
void LocalMap::render(const float x, const float y,
const float zlow, const float zhigh,
const float xw, const float yw, const std::string& texture)
{
// disable fog
// changing FOG_MODE is not a solution when using shaders, thus we have to push linear start/end
const float fStart = mRendering->getScene()->getFogStart();
const float fEnd = mRendering->getScene()->getFogEnd();
const ColourValue& clr = mRendering->getScene()->getFogColour();
mRendering->getScene()->setFog(FOG_LINEAR, clr, 0, 1000000, 10000000);
// make everything visible
mRendering->getScene()->setAmbientLight(ColourValue(1,1,1));
mCellCamera->setPosition(Vector3(x, zhigh, y));
mCellCamera->setFarClipDistance( (zhigh-zlow) * 1.1 );
mCellCamera->setOrthoWindow(xw, yw);
TexturePtr tex;
// try loading from memory
tex = TextureManager::getSingleton().getByName(texture);
if (tex.isNull())
{
// try loading from disk
//if (boost::filesystem::exists(texture+CACHE_EXTENSION))
//{
/// \todo
//}
//else
{
// render
tex = TextureManager::getSingleton().createManual(
texture,
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
TEX_TYPE_2D,
xw*MAP_RESOLUTION/8192, yw*MAP_RESOLUTION/8192,
0,
PF_R8G8B8,
TU_RENDERTARGET);
RenderTarget* rtt = tex->getBuffer()->getRenderTarget();
rtt->setAutoUpdated(false);
Viewport* vp = rtt->addViewport(mCellCamera);
vp->setOverlaysEnabled(false);
vp->setShadowsEnabled(false);
vp->setBackgroundColour(ColourValue(0, 0, 0));
//vp->setVisibilityMask( ... );
rtt->update();
/// \todo
// save to cache for next time
//rtt->writeContentsToFile("./" + texture + CACHE_EXTENSION);
}
}
/*
if (!MaterialManager::getSingleton().getByName("testMaterial").isNull())
{
MaterialPtr mat = MaterialManager::getSingleton().getByName("testMaterial");
mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(texture);
}
*/
// re-enable fog
mRendering->getScene()->setFog(FOG_LINEAR, clr, 0, fStart, fEnd);
}

View file

@ -0,0 +1,48 @@
#ifndef _GAME_RENDER_LOCALMAP_H
#define _GAME_RENDER_LOCALMAP_H
#include "../mwworld/ptr.hpp"
#include <openengine/ogre/renderer.hpp>
namespace MWRender
{
///
/// \brief Local map rendering
///
class LocalMap
{
public:
LocalMap(OEngine::Render::OgreRenderer*);
/**
* Request the local map for an exterior cell.
* It will either be loaded from a disk cache,
* or rendered if it is not already cached.
* @param exterior cell
*/
void requestMap (MWWorld::Ptr::CellStore* cell);
/**
* Request the local map for an interior cell.
* It will either be loaded from a disk cache,
* or rendered if it is not already cached.
* @param interior cell
* @param bounding box of the cell
*/
void requestMap (MWWorld::Ptr::CellStore* cell,
Ogre::AxisAlignedBox bounds);
private:
OEngine::Render::OgreRenderer* mRendering;
Ogre::Camera* mCellCamera;
void render(const float x, const float y,
const float zlow, const float zhigh,
const float xw, const float yw,
const std::string& texture);
};
}
#endif

View file

@ -109,6 +109,9 @@ void Objects::insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh)
// If it is set too low:
// - there will be too many batches.
sg->setRegionDimensions(Ogre::Vector3(2500,2500,2500));
mBounds[ptr.getCell()] = Ogre::AxisAlignedBox::BOX_NULL;
mBounds[ptr.getCell()].merge(ent->getBoundingBox());
}
else
{
@ -116,6 +119,7 @@ void Objects::insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh)
}
sg->addEntity(ent,insert->_getDerivedPosition(),insert->_getDerivedOrientation(),insert->_getDerivedScale());
mBounds[ptr.getCell()].merge(insert->_getDerivedPosition());
mRenderer.getScene()->destroyEntity(ent);
}
@ -202,6 +206,9 @@ void Objects::removeCell(MWWorld::Ptr::CellStore* store)
mRenderer.getScene()->destroyStaticGeometry (sg);
sg = 0;
}
if(mBounds.find(store) != mBounds.end())
mBounds.erase(store);
}
void Objects::buildStaticGeometry(ESMS::CellStore<MWWorld::RefData>& cell)
@ -212,3 +219,8 @@ void Objects::buildStaticGeometry(ESMS::CellStore<MWWorld::RefData>& cell)
sg->build();
}
}
Ogre::AxisAlignedBox Objects::getDimensions(MWWorld::Ptr::CellStore* cell)
{
return mBounds[cell];
}

View file

@ -14,6 +14,7 @@ class Objects{
OEngine::Render::OgreRenderer &mRenderer;
std::map<MWWorld::Ptr::CellStore *, Ogre::SceneNode *> mCellSceneNodes;
std::map<MWWorld::Ptr::CellStore *, Ogre::StaticGeometry*> mStaticGeometry;
std::map<MWWorld::Ptr::CellStore *, Ogre::AxisAlignedBox> mBounds;
Ogre::SceneNode* mMwRoot;
bool mIsStatic;
static int uniqueID;
@ -42,6 +43,9 @@ public:
void insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh);
void insertLight (const MWWorld::Ptr& ptr, float r, float g, float b, float radius);
Ogre::AxisAlignedBox getDimensions(MWWorld::Ptr::CellStore*);
///< get a bounding box that encloses all objects in the specified cell
bool deleteObject (const MWWorld::Ptr& ptr);
///< \return found?

View file

@ -55,6 +55,8 @@ RenderingManager::RenderingManager (OEngine::Render::OgreRenderer& _rend, const
mPlayer = new MWRender::Player (mRendering.getCamera(), playerNode);
mSun = 0;
mLocalMap = new MWRender::LocalMap(&mRendering);
}
RenderingManager::~RenderingManager ()
@ -62,6 +64,7 @@ RenderingManager::~RenderingManager ()
//TODO: destroy mSun?
delete mPlayer;
delete mSkyManager;
delete mLocalMap;
}
MWRender::SkyManager* RenderingManager::getSkyManager()
@ -322,4 +325,12 @@ void RenderingManager::setGlare(bool glare)
mSkyManager->setGlare(glare);
}
void RenderingManager::requestMap(MWWorld::Ptr::CellStore* cell)
{
if (!(cell->cell->data.flags & ESM::Cell::Interior))
mLocalMap->requestMap(cell);
else
mLocalMap->requestMap(cell, mObjects.getDimensions(cell));
}
} // namespace

View file

@ -24,6 +24,7 @@
#include "objects.hpp"
#include "actors.hpp"
#include "player.hpp"
#include "localmap.hpp"
namespace Ogre
{
@ -102,6 +103,9 @@ class RenderingManager: private RenderingInterface {
int skyGetSecundaPhase() const;
void skySetMoonColour (bool red);
void configureAmbient(ESMS::CellStore<MWWorld::RefData> &mCell);
void requestMap (MWWorld::Ptr::CellStore* cell);
///< request the local map for a cell
/// configure fog according to cell
void configureFog(ESMS::CellStore<MWWorld::RefData> &mCell);
@ -148,6 +152,8 @@ class RenderingManager: private RenderingInterface {
MWRender::Player *mPlayer;
MWRender::Debugging mDebugging;
MWRender::LocalMap* mLocalMap;
};
}

View file

@ -101,7 +101,8 @@ namespace MWWorld
insertCell(*cell, mEnvironment);
mRendering.cellAdded (cell);
mRendering.configureAmbient(*cell);
mRendering.requestMap(cell);
mRendering.configureAmbient(*cell);
}