openmw-tes3coop/apps/openmw/mwrender/globalmap.cpp

229 lines
9.4 KiB
C++
Raw Normal View History

2012-09-20 11:56:37 +00:00
#include "globalmap.hpp"
#include <boost/filesystem.hpp>
2012-09-20 11:56:37 +00:00
#include <OgreImage.h>
#include <OgreTextureManager.h>
#include <OgreColourValue.h>
#include <OgreHardwareVertexBuffer.h>
#include <OgreRoot.h>
2012-11-04 11:13:04 +00:00
#include <OgreHardwarePixelBuffer.h>
2012-09-20 11:56:37 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2012-10-01 15:17:04 +00:00
#include "../mwworld/esmstore.hpp"
2012-09-20 11:56:37 +00:00
namespace MWRender
{
GlobalMap::GlobalMap(const std::string &cacheDir)
: mCacheDir(cacheDir)
, mMinX(0), mMaxX(0)
, mMinY(0), mMaxY(0)
2012-09-20 11:56:37 +00:00
{
}
void GlobalMap::render ()
{
Ogre::TexturePtr tex;
2012-09-20 11:56:37 +00:00
const MWWorld::ESMStore &esmStore =
MWBase::Environment::get().getWorld()->getStore();
// get the size of the world
MWWorld::Store<ESM::Cell>::iterator it = esmStore.get<ESM::Cell>().extBegin();
for (; it != esmStore.get<ESM::Cell>().extEnd(); ++it)
2012-09-20 11:56:37 +00:00
{
if (it->getGridX() < mMinX)
mMinX = it->getGridX();
if (it->getGridX() > mMaxX)
mMaxX = it->getGridX();
if (it->getGridY() < mMinY)
mMinY = it->getGridY();
if (it->getGridY() > mMaxY)
mMaxY = it->getGridY();
}
2012-09-20 11:56:37 +00:00
int cellSize = 24;
mWidth = cellSize*(mMaxX-mMinX+1);
mHeight = cellSize*(mMaxY-mMinY+1);
2012-09-20 11:56:37 +00:00
2012-11-04 11:13:04 +00:00
mExploredBuffer.resize((mMaxX-mMinX+1) * (mMaxY-mMinY+1) * 4);
2012-09-20 11:56:37 +00:00
2012-09-21 18:35:50 +00:00
//if (!boost::filesystem::exists(mCacheDir + "/GlobalMap.png"))
if (1)
{
Ogre::Image image;
2012-09-20 11:56:37 +00:00
2012-10-09 15:10:25 +00:00
std::vector<Ogre::uchar> data (mWidth * mHeight * 3);
2012-09-20 11:56:37 +00:00
for (int x = mMinX; x <= mMaxX; ++x)
2012-09-20 11:56:37 +00:00
{
for (int y = mMinY; y <= mMaxY; ++y)
2012-09-20 11:56:37 +00:00
{
ESM::Land* land = esmStore.get<ESM::Land>().search (x,y);
2012-09-20 11:56:37 +00:00
if (land)
{
2012-09-21 08:49:34 +00:00
if (!land->isDataLoaded(ESM::Land::DATA_VHGT))
2012-09-20 11:56:37 +00:00
{
2012-09-21 08:49:34 +00:00
land->loadData(ESM::Land::DATA_VHGT);
2012-09-20 11:56:37 +00:00
}
}
for (int cellY=0; cellY<cellSize; ++cellY)
{
for (int cellX=0; cellX<cellSize; ++cellX)
{
int vertexX = float(cellX)/float(cellSize) * ESM::Land::LAND_SIZE;
int vertexY = float(cellY)/float(cellSize) * ESM::Land::LAND_SIZE;
int texelX = (x-mMinX) * cellSize + cellX;
int texelY = (mHeight-1) - ((y-mMinY) * cellSize + cellY);
2012-09-20 11:56:37 +00:00
Ogre::ColourValue waterShallowColour(0.15, 0.2, 0.19);
Ogre::ColourValue waterDeepColour(0.1, 0.14, 0.13);
Ogre::ColourValue groundColour(0.254, 0.19, 0.13);
Ogre::ColourValue mountainColour(0.05, 0.05, 0.05);
Ogre::ColourValue hillColour(0.16, 0.12, 0.08);
float mountainHeight = 15000.f;
float hillHeight = 2500.f;
unsigned char r,g,b;
if (land)
{
2012-09-21 08:49:34 +00:00
float landHeight = land->mLandData->mHeights[vertexY * ESM::Land::LAND_SIZE + vertexX];
2012-09-20 11:56:37 +00:00
if (landHeight >= 0)
{
if (landHeight >= hillHeight)
{
float factor = std::min(1.f, float(landHeight-hillHeight)/mountainHeight);
r = (hillColour.r * (1-factor) + mountainColour.r * factor) * 255;
g = (hillColour.g * (1-factor) + mountainColour.g * factor) * 255;
b = (hillColour.b * (1-factor) + mountainColour.b * factor) * 255;
}
else
{
float factor = std::min(1.f, float(landHeight)/hillHeight);
r = (groundColour.r * (1-factor) + hillColour.r * factor) * 255;
g = (groundColour.g * (1-factor) + hillColour.g * factor) * 255;
b = (groundColour.b * (1-factor) + hillColour.b * factor) * 255;
}
}
else
{
if (landHeight >= -100)
{
float factor = std::min(1.f, -1*landHeight/100.f);
r = (((waterShallowColour+groundColour)/2).r * (1-factor) + waterShallowColour.r * factor) * 255;
g = (((waterShallowColour+groundColour)/2).g * (1-factor) + waterShallowColour.g * factor) * 255;
b = (((waterShallowColour+groundColour)/2).b * (1-factor) + waterShallowColour.b * factor) * 255;
}
else
{
float factor = std::min(1.f, -1*(landHeight-100)/1000.f);
r = (waterShallowColour.r * (1-factor) + waterDeepColour.r * factor) * 255;
g = (waterShallowColour.g * (1-factor) + waterDeepColour.g * factor) * 255;
b = (waterShallowColour.b * (1-factor) + waterDeepColour.b * factor) * 255;
}
}
}
else
{
r = waterDeepColour.r * 255;
g = waterDeepColour.g * 255;
b = waterDeepColour.b * 255;
}
// uncomment this line to outline cell borders
//if (cellX == 0 || cellX == cellSize-1 || cellY == 0|| cellY == cellSize-1) r = 255;
data[texelY * mWidth * 3 + texelX * 3] = r;
data[texelY * mWidth * 3 + texelX * 3+1] = g;
data[texelY * mWidth * 3 + texelX * 3+2] = b;
2012-09-20 11:56:37 +00:00
}
}
}
}
2012-10-09 15:10:25 +00:00
image.loadDynamicImage (&data[0], mWidth, mHeight, Ogre::PF_B8G8R8);
2012-09-20 11:56:37 +00:00
2012-09-21 18:35:50 +00:00
//image.save (mCacheDir + "/GlobalMap.png");
2012-09-20 11:56:37 +00:00
tex = Ogre::TextureManager::getSingleton ().createManual ("GlobalMap.png", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, Ogre::PF_B8G8R8, Ogre::TU_DEFAULT);
2012-09-20 11:56:37 +00:00
tex->loadImage(image);
}
else
tex = Ogre::TextureManager::getSingleton ().getByName ("GlobalMap.png");
2012-09-20 11:56:37 +00:00
tex->load();
}
void GlobalMap::worldPosToImageSpace(float x, float z, float& imageX, float& imageY)
{
imageX = float(x / 8192.f - mMinX) / (mMaxX - mMinX + 1);
imageY = 1.f-float(-z / 8192.f - mMinY) / (mMaxY - mMinY + 1);
}
void GlobalMap::cellTopLeftCornerToImageSpace(int x, int y, float& imageX, float& imageY)
{
imageX = float(x - mMinX) / (mMaxX - mMinX + 1);
// NB y + 1, because we want the top left corner, not bottom left where the origin of the cell is
imageY = 1.f-float(y - mMinY + 1) / (mMaxY - mMinY + 1);
}
2012-11-04 11:13:04 +00:00
void GlobalMap::exploreCell(int cellX, int cellY)
{
mExploredCells.push_back(std::make_pair(cellX, cellY));
int width = mMaxX-mMinX+1;
int height = mMaxY-mMinY+1;
if (mOverlayTexture.isNull())
{
mOverlayTexture = Ogre::TextureManager::getSingleton().createManual("GlobalMapOverlay", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Ogre::TEX_TYPE_2D, width, height, 0, Ogre::PF_A8B8G8R8, Ogre::TU_DYNAMIC_WRITE_ONLY);
}
2012-11-04 11:13:04 +00:00
for (int x = mMinX; x <= mMaxX; ++x)
{
for (int y = mMinY; y <= mMaxY; ++y)
{
unsigned char r,g,b,a;
r = 0;
g = 0;
b = 0;
if (std::find(mExploredCells.begin(), mExploredCells.end(), std::make_pair(x, y)) != mExploredCells.end())
a = 0;
else
a = 128;
int texelX = (x-mMinX);
int texelY = (height-1) - (y-mMinY);
assert( static_cast<unsigned int>(texelY * width * 4 + texelX * 4+3) < mExploredBuffer.size());
mExploredBuffer[texelY * width * 4 + texelX * 4] = r;
mExploredBuffer[texelY * width * 4 + texelX * 4+1] = g;
mExploredBuffer[texelY * width * 4 + texelX * 4+2] = b;
mExploredBuffer[texelY * width * 4 + texelX * 4+3] = a;
}
}
Ogre::Image image;
image.loadDynamicImage(&mExploredBuffer[0], width, height, Ogre::PF_A8B8G8R8);
memcpy(mOverlayTexture->getBuffer()->lock(Ogre::HardwareBuffer::HBL_DISCARD),
&mExploredBuffer[0], width*height*4);
mOverlayTexture->getBuffer()->unlock();
}
2012-09-20 11:56:37 +00:00
}