1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00
OpenMW/apps/openmw/mwrender/globalmap.cpp

365 lines
14 KiB
C++
Raw Normal View History

2012-09-20 13:56:37 +02:00
#include "globalmap.hpp"
2015-05-16 14:48:20 +02:00
#include <climits>
2015-05-16 14:48:20 +02:00
#include <osg/Image>
#include <osg/Texture2D>
2012-09-20 13:56:37 +02:00
#include <osgDB/WriteFile>
#include <components/loadinglistener/loadinglistener.hpp>
2015-01-31 23:27:34 +01:00
#include <components/settings/settings.hpp>
#include <components/files/memorystream.hpp>
2014-01-25 18:20:17 +01:00
#include <components/esm/globalmap.hpp>
2012-09-20 13:56:37 +02:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2012-10-01 19:17:04 +04:00
#include "../mwworld/esmstore.hpp"
2012-09-20 13:56:37 +02:00
namespace MWRender
{
2015-05-16 14:48:20 +02:00
GlobalMap::GlobalMap()
: mWidth(0)
2013-07-31 18:46:32 +02:00
, mHeight(0)
, mMinX(0), mMaxX(0)
, mMinY(0), mMaxY(0)
2012-09-20 13:56:37 +02:00
{
mCellSize = Settings::Manager::getInt("global map cell size", "Map");
2012-09-20 13:56:37 +02:00
}
GlobalMap::~GlobalMap()
{
}
2012-09-20 13:56:37 +02:00
void GlobalMap::render (Loading::Listener* loadingListener)
2012-09-20 13:56:37 +02: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 13:56:37 +02: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 13:56:37 +02:00
mWidth = mCellSize*(mMaxX-mMinX+1);
mHeight = mCellSize*(mMaxY-mMinY+1);
2012-09-20 13:56:37 +02:00
loadingListener->loadingOn();
loadingListener->setLabel("Creating map");
loadingListener->setProgressRange((mMaxX-mMinX+1) * (mMaxY-mMinY+1));
loadingListener->setProgress(0);
2015-05-16 14:48:20 +02:00
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(mWidth, mHeight, 1, GL_RGB, GL_UNSIGNED_BYTE);
unsigned char* data = image->data();
2012-09-20 13:56:37 +02:00
for (int x = mMinX; x <= mMaxX; ++x)
{
for (int y = mMinY; y <= mMaxY; ++y)
2012-09-20 13:56:37 +02:00
{
ESM::Land* land = esmStore.get<ESM::Land>().search (x,y);
2012-09-20 13:56:37 +02:00
if (land)
{
int mask = ESM::Land::DATA_WNAM;
if (!land->isDataLoaded(mask))
land->loadData(mask);
}
2012-09-20 13:56:37 +02:00
for (int cellY=0; cellY<mCellSize; ++cellY)
{
for (int cellX=0; cellX<mCellSize; ++cellX)
2012-09-20 13:56:37 +02:00
{
int vertexX = static_cast<int>(float(cellX)/float(mCellSize) * 9);
int vertexY = static_cast<int>(float(cellY) / float(mCellSize) * 9);
2012-09-20 13:56:37 +02:00
int texelX = (x-mMinX) * mCellSize + cellX;
int texelY = (mHeight-1) - ((y-mMinY) * mCellSize + cellY);
2012-09-20 13:56:37 +02:00
unsigned char r,g,b;
2012-09-20 13:56:37 +02:00
float y = 0;
if (land && land->mDataTypes & ESM::Land::DATA_WNAM)
y = (land->mLandData->mWnam[vertexY * 9 + vertexX] << 4) / 2048.f;
else
y = (SCHAR_MIN << 4) / 2048.f;
if (y < 0)
{
r = static_cast<unsigned char>(14 * y + 38);
g = static_cast<unsigned char>(20 * y + 56);
b = static_cast<unsigned char>(18 * y + 51);
}
else if (y < 0.3f)
{
if (y < 0.1f)
y *= 8.f;
2012-09-20 13:56:37 +02:00
else
{
y -= 0.1f;
y += 0.8f;
2012-09-20 13:56:37 +02:00
}
r = static_cast<unsigned char>(66 - 32 * y);
g = static_cast<unsigned char>(48 - 23 * y);
b = static_cast<unsigned char>(33 - 16 * y);
2012-09-20 13:56:37 +02:00
}
else
{
y -= 0.3f;
y *= 1.428f;
r = static_cast<unsigned char>(34 - 29 * y);
g = static_cast<unsigned char>(25 - 20 * y);
b = static_cast<unsigned char>(17 - 12 * y);
}
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 13:56:37 +02:00
}
}
loadingListener->increaseProgress();
if (land)
land->unloadData();
2012-09-20 13:56:37 +02:00
}
}
2012-09-20 13:56:37 +02:00
2015-05-16 14:48:20 +02:00
mBaseTexture = new osg::Texture2D;
mBaseTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
mBaseTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
mBaseTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
mBaseTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
2015-05-16 14:48:20 +02:00
mBaseTexture->setImage(image);
mBaseTexture->setResizeNonPowerOfTwoHint(false);
2012-11-16 19:34:09 +01:00
clear();
loadingListener->loadingOff();
2012-09-20 13:56:37 +02:00
}
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 12:13:04 +01:00
void GlobalMap::exploreCell(int cellX, int cellY)
{
2015-05-16 14:48:20 +02:00
//float originX = static_cast<float>((cellX - mMinX) * mCellSize);
2012-11-16 19:34:09 +01:00
// NB y + 1, because we want the top left corner, not bottom left where the origin of the cell is
2015-05-16 14:48:20 +02:00
//float originY = static_cast<float>(mHeight - (cellY + 1 - mMinY) * mCellSize);
2012-11-04 12:13:04 +01:00
2012-11-16 19:34:09 +01:00
if (cellX > mMaxX || cellX < mMinX || cellY > mMaxY || cellY < mMinY)
return;
2015-05-16 14:48:20 +02:00
/*
2012-11-16 19:34:09 +01:00
Ogre::TexturePtr localMapTexture = Ogre::TextureManager::getSingleton().getByName("Cell_"
+ boost::lexical_cast<std::string>(cellX) + "_" + boost::lexical_cast<std::string>(cellY));
2012-11-04 12:13:04 +01:00
2012-11-16 19:34:09 +01:00
if (!localMapTexture.isNull())
2012-11-16 22:26:00 +01:00
{
int mapWidth = localMapTexture->getWidth();
int mapHeight = localMapTexture->getHeight();
mOverlayTexture->load();
mOverlayTexture->getBuffer()->blit(localMapTexture->getBuffer(), Ogre::Image::Box(0,0,mapWidth,mapHeight),
Ogre::Image::Box(static_cast<Ogre::uint32>(originX), static_cast<Ogre::uint32>(originY),
static_cast<Ogre::uint32>(originX + mCellSize), static_cast<Ogre::uint32>(originY + mCellSize)));
Ogre::Image backup;
std::vector<Ogre::uchar> data;
data.resize(mCellSize*mCellSize*4, 0);
backup.loadDynamicImage(&data[0], mCellSize, mCellSize, Ogre::PF_A8B8G8R8);
localMapTexture->getBuffer()->blitToMemory(Ogre::Image::Box(0,0,mapWidth,mapHeight), backup.getPixelBox());
for (int x=0; x<mCellSize; ++x)
for (int y=0; y<mCellSize; ++y)
{
assert (originX+x < mOverlayImage.getWidth());
assert (originY+y < mOverlayImage.getHeight());
assert (x < int(backup.getWidth()));
assert (y < int(backup.getHeight()));
mOverlayImage.setColourAt(backup.getColourAt(x, y, 0), static_cast<size_t>(originX + x), static_cast<size_t>(originY + y), 0);
}
2012-11-16 22:26:00 +01:00
}
2015-05-16 14:48:20 +02:00
*/
2012-11-04 12:13:04 +01:00
}
void GlobalMap::clear()
{
if (!mOverlayImage)
{
mOverlayImage = new osg::Image;
mOverlayImage->allocateImage(mWidth, mHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
assert(mOverlayImage->isDataContiguous());
}
memset(mOverlayImage->data(), 0, mOverlayImage->getTotalSizeInBytes());
mOverlayImage->dirty();
if (!mOverlayTexture)
{
mOverlayTexture = new osg::Texture2D;
mOverlayTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
mOverlayTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
mOverlayTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
mOverlayTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
mOverlayTexture->setImage(mOverlayImage);
mOverlayTexture->setResizeNonPowerOfTwoHint(false);
}
}
2014-01-25 18:20:17 +01:00
void GlobalMap::write(ESM::GlobalMap& map)
2014-01-25 18:20:17 +01:00
{
map.mBounds.mMinX = mMinX;
map.mBounds.mMaxX = mMaxX;
map.mBounds.mMinY = mMinY;
map.mBounds.mMaxY = mMaxY;
std::ostringstream ostream;
osgDB::ReaderWriter* readerwriter = osgDB::Registry::instance()->getReaderWriterForExtension("png");
if (!readerwriter)
{
std::cerr << "Can't write map overlay: no png readerwriter found" << std::endl;
return;
}
osgDB::ReaderWriter::WriteResult result = readerwriter->writeImage(*mOverlayImage, ostream);
if (!result.success())
{
std::cerr << "Can't write map overlay: " << result.message() << std::endl;
return;
}
std::string data = ostream.str();
map.mImageData = std::vector<char>(data.begin(), data.end());
2014-01-25 18:20:17 +01:00
}
struct Box
{
int mLeft, mRight, mTop, mBottom;
Box(int left, int right, int top, int bottom)
: mLeft(left), mRight(right), mTop(top), mBottom(bottom)
{
}
};
void GlobalMap::read(ESM::GlobalMap& map)
2014-01-25 18:20:17 +01:00
{
const ESM::GlobalMap::Bounds& bounds = map.mBounds;
if (bounds.mMaxX-bounds.mMinX < 0)
return;
if (bounds.mMaxY-bounds.mMinY < 0)
return;
2014-01-25 18:20:17 +01:00
if (bounds.mMinX > bounds.mMaxX
|| bounds.mMinY > bounds.mMaxY)
throw std::runtime_error("invalid map bounds");
2014-01-25 18:20:17 +01:00
if (!map.mImageData.size())
return;
Files::IMemStream istream(&map.mImageData[0], map.mImageData.size());
osgDB::ReaderWriter* readerwriter = osgDB::Registry::instance()->getReaderWriterForExtension("png");
if (!readerwriter)
{
std::cerr << "Can't read map overlay: no png readerwriter found" << std::endl;
return;
}
osgDB::ReaderWriter::ReadResult result = readerwriter->readImage(istream);
if (!result.success())
{
std::cerr << "Can't read map overlay: " << result.message() << std::endl;
return;
}
osg::ref_ptr<osg::Image> image = result.getImage();
int imageWidth = image->s();
int imageHeight = image->t();
int xLength = (bounds.mMaxX-bounds.mMinX+1);
int yLength = (bounds.mMaxY-bounds.mMinY+1);
// Size of one cell in image space
int cellImageSizeSrc = imageWidth / xLength;
if (int(imageHeight / yLength) != cellImageSizeSrc)
throw std::runtime_error("cell size must be quadratic");
// If cell bounds of the currently loaded content and the loaded savegame do not match,
// we need to resize source/dest boxes to accommodate
// This means nonexisting cells will be dropped silently
int cellImageSizeDst = mCellSize;
// Completely off-screen? -> no need to blit anything
if (bounds.mMaxX < mMinX
|| bounds.mMaxY < mMinY
|| bounds.mMinX > mMaxX
|| bounds.mMinY > mMaxY)
return;
2014-01-25 18:20:17 +01:00
int leftDiff = (mMinX - bounds.mMinX);
int topDiff = (bounds.mMaxY - mMaxY);
int rightDiff = (bounds.mMaxX - mMaxX);
int bottomDiff = (mMinY - bounds.mMinY);
Box srcBox ( std::max(0, leftDiff * cellImageSizeSrc),
std::max(0, topDiff * cellImageSizeSrc),
std::min(imageWidth, imageWidth - rightDiff * cellImageSizeSrc),
std::min(imageHeight, imageHeight - bottomDiff * cellImageSizeSrc));
Box destBox ( std::max(0, -leftDiff * cellImageSizeDst),
std::max(0, -topDiff * cellImageSizeDst),
std::min(mWidth, mWidth + rightDiff * cellImageSizeDst),
std::min(mHeight, mHeight + bottomDiff * cellImageSizeDst));
if (srcBox.mLeft == destBox.mLeft && srcBox.mRight == destBox.mRight
&& srcBox.mTop == destBox.mTop && srcBox.mBottom == destBox.mBottom
&& imageWidth == mWidth && imageHeight == mHeight)
{
mOverlayImage->copySubImage(0, 0, 0, image);
}
else
{
// TODO:
// Dimensions don't match. This could mean a changed map region, or a changed map resolution.
// In the latter case, we'll want to use filtering.
// Create a RTT Camera and draw the image onto mOverlayImage in the next frame?
}
mOverlayImage->dirty();
2015-05-16 14:48:20 +02:00
}
osg::ref_ptr<osg::Texture2D> GlobalMap::getBaseTexture()
{
return mBaseTexture;
2014-01-25 18:20:17 +01:00
}
osg::ref_ptr<osg::Texture2D> GlobalMap::getOverlayTexture()
{
return mOverlayTexture;
}
2012-09-20 13:56:37 +02:00
}