Terrain: use 32-bit indices if necessary

openmw-35
scrawl 10 years ago
parent f7bac58b39
commit 261da8dd0a

@ -4,50 +4,12 @@
#include "defs.hpp" #include "defs.hpp"
namespace Terrain namespace
{
Ogre::HardwareVertexBufferSharedPtr BufferCache::getUVBuffer()
{
if (mUvBufferMap.find(mNumVerts) != mUvBufferMap.end())
{
return mUvBufferMap[mNumVerts];
}
int vertexCount = mNumVerts * mNumVerts;
std::vector<float> uvs;
uvs.reserve(vertexCount*2);
for (unsigned int col = 0; col < mNumVerts; ++col)
{
for (unsigned int row = 0; row < mNumVerts; ++row)
{
uvs.push_back(col / static_cast<float>(mNumVerts-1)); // U
uvs.push_back(row / static_cast<float>(mNumVerts-1)); // V
}
}
Ogre::HardwareBufferManager* mgr = Ogre::HardwareBufferManager::getSingletonPtr();
Ogre::HardwareVertexBufferSharedPtr buffer = mgr->createVertexBuffer(
Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2),
vertexCount, Ogre::HardwareBuffer::HBU_STATIC);
buffer->writeData(0, buffer->getSizeInBytes(), &uvs[0], true);
mUvBufferMap[mNumVerts] = buffer;
return buffer;
}
Ogre::HardwareIndexBufferSharedPtr BufferCache::getIndexBuffer(unsigned int flags)
{ {
unsigned int verts = mNumVerts;
if (mIndexBufferMap.find(flags) != mIndexBufferMap.end()) template <typename IndexType>
Ogre::HardwareIndexBufferSharedPtr createIndexBuffer(unsigned int flags, unsigned int verts, Ogre::HardwareIndexBuffer::IndexType type)
{ {
return mIndexBufferMap[flags];
}
// LOD level n means every 2^n-th vertex is kept // LOD level n means every 2^n-th vertex is kept
size_t lodLevel = (flags >> (4*4)); size_t lodLevel = (flags >> (4*4));
@ -55,11 +17,11 @@ namespace Terrain
for (int i=0; i<4; ++i) for (int i=0; i<4; ++i)
lodDeltas[i] = (flags >> (4*i)) & (0xf); lodDeltas[i] = (flags >> (4*i)) & (0xf);
bool anyDeltas = (lodDeltas[North] || lodDeltas[South] || lodDeltas[West] || lodDeltas[East]); bool anyDeltas = (lodDeltas[Terrain::North] || lodDeltas[Terrain::South] || lodDeltas[Terrain::West] || lodDeltas[Terrain::East]);
size_t increment = 1 << lodLevel; size_t increment = 1 << lodLevel;
assert(increment < verts); assert(increment < verts);
std::vector<short> indices; std::vector<IndexType> indices;
indices.reserve((verts-1)*(verts-1)*2*3 / increment); indices.reserve((verts-1)*(verts-1)*2*3 / increment);
size_t rowStart = 0, colStart = 0, rowEnd = verts-1, colEnd = verts-1; size_t rowStart = 0, colStart = 0, rowEnd = verts-1, colEnd = verts-1;
@ -94,7 +56,7 @@ namespace Terrain
// South // South
size_t row = 0; size_t row = 0;
size_t outerStep = 1 << (lodDeltas[South] + lodLevel); size_t outerStep = 1 << (lodDeltas[Terrain::South] + lodLevel);
for (size_t col = 0; col < verts-1; col += outerStep) for (size_t col = 0; col < verts-1; col += outerStep)
{ {
indices.push_back(verts*col+row); indices.push_back(verts*col+row);
@ -118,7 +80,7 @@ namespace Terrain
// North // North
row = verts-1; row = verts-1;
outerStep = size_t(1) << (lodDeltas[North] + lodLevel); outerStep = size_t(1) << (lodDeltas[Terrain::North] + lodLevel);
for (size_t col = 0; col < verts-1; col += outerStep) for (size_t col = 0; col < verts-1; col += outerStep)
{ {
indices.push_back(verts*(col+outerStep)+row); indices.push_back(verts*(col+outerStep)+row);
@ -142,7 +104,7 @@ namespace Terrain
// West // West
size_t col = 0; size_t col = 0;
outerStep = size_t(1) << (lodDeltas[West] + lodLevel); outerStep = size_t(1) << (lodDeltas[Terrain::West] + lodLevel);
for (size_t row = 0; row < verts-1; row += outerStep) for (size_t row = 0; row < verts-1; row += outerStep)
{ {
indices.push_back(verts*col+row+outerStep); indices.push_back(verts*col+row+outerStep);
@ -166,7 +128,7 @@ namespace Terrain
// East // East
col = verts-1; col = verts-1;
outerStep = size_t(1) << (lodDeltas[East] + lodLevel); outerStep = size_t(1) << (lodDeltas[Terrain::East] + lodLevel);
for (size_t row = 0; row < verts-1; row += outerStep) for (size_t row = 0; row < verts-1; row += outerStep)
{ {
indices.push_back(verts*col+row); indices.push_back(verts*col+row);
@ -190,9 +152,64 @@ namespace Terrain
} }
Ogre::HardwareBufferManager* mgr = Ogre::HardwareBufferManager::getSingletonPtr(); Ogre::HardwareBufferManager* mgr = Ogre::HardwareBufferManager::getSingletonPtr();
Ogre::HardwareIndexBufferSharedPtr buffer = mgr->createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, Ogre::HardwareIndexBufferSharedPtr buffer = mgr->createIndexBuffer(type,
indices.size(), Ogre::HardwareBuffer::HBU_STATIC); indices.size(), Ogre::HardwareBuffer::HBU_STATIC);
buffer->writeData(0, buffer->getSizeInBytes(), &indices[0], true); buffer->writeData(0, buffer->getSizeInBytes(), &indices[0], true);
return buffer;
}
}
namespace Terrain
{
Ogre::HardwareVertexBufferSharedPtr BufferCache::getUVBuffer()
{
if (mUvBufferMap.find(mNumVerts) != mUvBufferMap.end())
{
return mUvBufferMap[mNumVerts];
}
int vertexCount = mNumVerts * mNumVerts;
std::vector<float> uvs;
uvs.reserve(vertexCount*2);
for (unsigned int col = 0; col < mNumVerts; ++col)
{
for (unsigned int row = 0; row < mNumVerts; ++row)
{
uvs.push_back(col / static_cast<float>(mNumVerts-1)); // U
uvs.push_back(row / static_cast<float>(mNumVerts-1)); // V
}
}
Ogre::HardwareBufferManager* mgr = Ogre::HardwareBufferManager::getSingletonPtr();
Ogre::HardwareVertexBufferSharedPtr buffer = mgr->createVertexBuffer(
Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2),
vertexCount, Ogre::HardwareBuffer::HBU_STATIC);
buffer->writeData(0, buffer->getSizeInBytes(), &uvs[0], true);
mUvBufferMap[mNumVerts] = buffer;
return buffer;
}
Ogre::HardwareIndexBufferSharedPtr BufferCache::getIndexBuffer(unsigned int flags)
{
unsigned int verts = mNumVerts;
if (mIndexBufferMap.find(flags) != mIndexBufferMap.end())
{
return mIndexBufferMap[flags];
}
Ogre::HardwareIndexBufferSharedPtr buffer;
if (verts*verts > (0xffffu))
buffer = createIndexBuffer<unsigned int>(flags, verts, Ogre::HardwareIndexBuffer::IT_32BIT);
else
buffer = createIndexBuffer<unsigned short>(flags, verts, Ogre::HardwareIndexBuffer::IT_16BIT);
mIndexBufferMap[flags] = buffer; mIndexBufferMap[flags] = buffer;
return buffer; return buffer;
} }

Loading…
Cancel
Save