fixed the global colour map, apparently Ogre::Terrain was corrupting it

somehow, now we are just bypassing Ogre::Terrain for the colour map and
passing it directly to the material, which makes more sense
performance-wise anyway
This commit is contained in:
scrawl 2012-03-02 15:29:12 +01:00
parent 04c141ddd5
commit 2bea4c4724
3 changed files with 55 additions and 27 deletions

View File

@ -6,6 +6,8 @@
#include "components/esm/loadland.hpp" #include "components/esm/loadland.hpp"
#include <boost/lexical_cast.hpp>
using namespace Ogre; using namespace Ogre;
namespace MWRender namespace MWRender
@ -48,6 +50,7 @@ namespace MWRender
matProfile->setLayerNormalMappingEnabled(false); matProfile->setLayerNormalMappingEnabled(false);
matProfile->setLayerParallaxMappingEnabled(false); matProfile->setLayerParallaxMappingEnabled(false);
matProfile->setReceiveDynamicShadowsEnabled(false); matProfile->setReceiveDynamicShadowsEnabled(false);
matProfile->setGlobalColourMapEnabled(true);
mLandSize = ESM::Land::LAND_SIZE; mLandSize = ESM::Land::LAND_SIZE;
mRealSize = ESM::Land::REAL_SIZE; mRealSize = ESM::Land::REAL_SIZE;
@ -163,9 +166,12 @@ namespace MWRender
if ( store->land[1][1]->landData->usingColours ) if ( store->land[1][1]->landData->usingColours )
{ {
Ogre::Image vertex = getVertexColours(store, x*32, y*32, mLandSize); Ogre::TexturePtr vertex = getVertexColours(store, x*32, y*32, mLandSize);
terrain->setGlobalColourMapEnabled(true);
terrain->getGlobalColourMap()->loadImage(vertex); MaterialPtr mat = terrain->_getMaterial();
mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName( vertex->getName() );
mat = terrain->_getCompositeMapMaterial();
mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName( vertex->getName() );
} }
} }
} }
@ -200,9 +206,12 @@ namespace MWRender
if ( store->land[1][1]->landData->usingColours ) if ( store->land[1][1]->landData->usingColours )
{ {
Ogre::Image vertex = getVertexColours(store, 0, 0, mLandSize); Ogre::TexturePtr vertex = getVertexColours(store, 0, 0, mLandSize);
terrain->setGlobalColourMapEnabled(true);
terrain->getGlobalColourMap()->loadImage(vertex); MaterialPtr mat = terrain->_getMaterial();
mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName( vertex->getName() );
mat = terrain->_getCompositeMapMaterial();
mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName( vertex->getName() );
} }
} }
@ -495,14 +504,37 @@ namespace MWRender
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
Ogre::Image TerrainManager::getVertexColours(MWWorld::Ptr::CellStore* store, Ogre::TexturePtr TerrainManager::getVertexColours(MWWorld::Ptr::CellStore* store,
int fromX, int fromY, int size) int fromX, int fromY, int size)
{ {
Ogre::TextureManager* const texMgr = Ogre::TextureManager::getSingletonPtr();
const char* const colours = store->land[1][1]->landData->colours; const char* const colours = store->land[1][1]->landData->colours;
Ogre::uchar* imgData = OGRE_ALLOC_T(Ogre::uchar, const std::string colourTextureName = "VtexColours_" +
size*size*sizeof(Ogre::uchar)*3, boost::lexical_cast<std::string>(store->cell->getGridX()) +
Ogre::MEMCATEGORY_GENERAL); "_" +
boost::lexical_cast<std::string>(store->cell->getGridY()) +
"_" +
boost::lexical_cast<std::string>(fromX) +
"_" +
boost::lexical_cast<std::string>(fromY);
Ogre::TexturePtr tex = texMgr->getByName(colourTextureName);
if ( !tex.isNull() )
{
return tex;
}
tex = texMgr->createManual(colourTextureName,
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Ogre::TEX_TYPE_2D, size, size, 0, Ogre::PF_BYTE_BGRA);
Ogre::HardwarePixelBufferSharedPtr pixelBuffer = tex->getBuffer();
pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();
Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
for ( int y = 0; y < size; y++ ) for ( int y = 0; y < size; y++ )
{ {
@ -518,18 +550,16 @@ namespace MWRender
const unsigned char b = colours[colourOffset + 2]; const unsigned char b = colours[colourOffset + 2];
//as is the case elsewhere we need to flip the y //as is the case elsewhere we need to flip the y
const size_t imageOffset = (size - 1 - y)*size*3 + x*3; const size_t imageOffset = (size - 1 - y)*size*4 + x*4;
imgData[imageOffset + 0] = r; pDest[imageOffset + 0] = b;
imgData[imageOffset + 1] = g; pDest[imageOffset + 1] = g;
imgData[imageOffset + 2] = b; pDest[imageOffset + 2] = r;
} }
} }
Ogre::Image img; pixelBuffer->unlock();
img.loadDynamicImage(imgData, size, size, 1, Ogre::PF_R8G8B8, true);
return img; return tex;
} }
} }

View File

@ -130,10 +130,8 @@ namespace MWRender{
* @param fromX the *vertex* index in the current cell to start making texture from * @param fromX the *vertex* index in the current cell to start making texture from
* @param fromY the *vertex* index in the current cell to start making the texture from * @param fromY the *vertex* index in the current cell to start making the texture from
* @param size the size (number of vertexes) to get * @param size the size (number of vertexes) to get
*
* @TODO FIXME the return of this function possibly copies the image data
*/ */
Ogre::Image getVertexColours(MWWorld::Ptr::CellStore* store, Ogre::TexturePtr getVertexColours(MWWorld::Ptr::CellStore* store,
int fromX, int fromY, int size); int fromX, int fromY, int size);
}; };

View File

@ -203,7 +203,7 @@ namespace Ogre
// normalmap // normalmap
--freeTextureUnits; --freeTextureUnits;
// colourmap // colourmap
if (terrain->getGlobalColourMapEnabled()) //if (terrain->getGlobalColourMapEnabled())
--freeTextureUnits; --freeTextureUnits;
if (isShadowingEnabled(HIGH_LOD, terrain)) if (isShadowingEnabled(HIGH_LOD, terrain))
{ {
@ -359,9 +359,9 @@ namespace Ogre
tu->setTextureAddressingMode(TextureUnitState::TAM_CLAMP); tu->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);
// global colour map // global colour map
if (terrain->getGlobalColourMapEnabled() && isGlobalColourMapEnabled()) //if (terrain->getGlobalColourMapEnabled() && isGlobalColourMapEnabled())
{ {
tu = pass->createTextureUnitState(terrain->getGlobalColourMap()->getName()); tu = pass->createTextureUnitState("");
tu->setTextureAddressingMode(TextureUnitState::TAM_CLAMP); tu->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);
} }
@ -1004,7 +1004,7 @@ namespace Ogre
"uniform sampler2D globalNormal : register(s" << currentSamplerIdx++ << ")\n"; "uniform sampler2D globalNormal : register(s" << currentSamplerIdx++ << ")\n";
if (terrain->getGlobalColourMapEnabled() && prof->isGlobalColourMapEnabled()) //if (terrain->getGlobalColourMapEnabled() && prof->isGlobalColourMapEnabled())
{ {
outStream << ", uniform sampler2D globalColourMap : register(s" outStream << ", uniform sampler2D globalColourMap : register(s"
<< currentSamplerIdx++ << ")\n"; << currentSamplerIdx++ << ")\n";
@ -1299,7 +1299,7 @@ namespace Ogre
} }
else else
{ {
if (terrain->getGlobalColourMapEnabled() && prof->isGlobalColourMapEnabled()) //if (terrain->getGlobalColourMapEnabled() && prof->isGlobalColourMapEnabled())
{ {
// sample colour map and apply to diffuse // sample colour map and apply to diffuse
outStream << " diffuse *= tex2D(globalColourMap, uv).rgb;\n"; outStream << " diffuse *= tex2D(globalColourMap, uv).rgb;\n";