Added index iterators

Former-commit-id: de3ed77ba9e3c48aa68020e23ded679066b9878f
This commit is contained in:
Lynix
2013-06-05 15:33:43 +02:00
parent fbc0d7404e
commit 146ca80a63
7 changed files with 297 additions and 67 deletions

View File

@@ -17,7 +17,7 @@ namespace
{
}
void Generate(float size, unsigned int recursionLevel, NzMeshVertex* vertices, nzUInt32* indices, NzBoxf* aabb, unsigned int indexOffset)
void Generate(float size, unsigned int recursionLevel, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
// Grandement inspiré de http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
const float t = (1.f + 2.236067f)/2.f;
@@ -202,7 +202,7 @@ void NzComputeUvSphereIndexVertexCount(unsigned int sliceCount, unsigned int sta
*vertexCount = sliceCount * stackCount;
}
void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, NzMeshVertex* vertices, nzUInt32* indices, NzBoxf* aabb, unsigned int indexOffset)
void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
unsigned int xIndexCount, yIndexCount, zIndexCount;
unsigned int xVertexCount, yVertexCount, zVertexCount;
@@ -258,7 +258,7 @@ void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, co
}
}
void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, NzMeshVertex* vertices, nzUInt32* indices, NzBoxf* aabb, unsigned int indexOffset)
void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
unsigned int vertexCount;
NzComputeBoxIndexVertexCount(NzVector3ui(subdivision), nullptr, &vertexCount);
@@ -281,13 +281,13 @@ void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4
}
}
void NzGenerateIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix, NzMeshVertex* vertices, nzUInt32* indices, NzBoxf* aabb, unsigned int indexOffset)
void NzGenerateIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
IcoSphereBuilder builder(matrix);
builder.Generate(size, recursionLevel, vertices, indices, aabb, indexOffset);
}
void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector3f& position, const NzVector3f& normal, const NzVector2f& size, NzMeshVertex* vertices, nzUInt32* indices, NzBoxf* aabb, unsigned int indexOffset)
void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector3f& position, const NzVector3f& normal, const NzVector2f& size, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
// Le nombre de faces appartenant à un axe est équivalent à 2 exposant la subdivision (1,2,4,8,16,32,...)
unsigned int horizontalFaceCount = (1 << subdivision.x);
@@ -344,7 +344,7 @@ void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector3f& position,
aabb->Set(rotation * NzVector3f(-halfSizeX, 0.f, -halfSizeY), rotation * NzVector3f(halfSizeX, 0.f, halfSizeY));
}
void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const NzMatrix4f& matrix, NzMeshVertex* vertices, nzUInt32* indices, NzBoxf* aabb, unsigned int indexOffset)
void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const NzMatrix4f& matrix, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
// http://stackoverflow.com/questions/14080932/implementing-opengl-sphere-example-code
float invSliceCount = 1.f / (sliceCount-1);

View File

@@ -4,6 +4,7 @@
#include <Nazara/Utility/IndexMapper.hpp>
#include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/IndexIterator.hpp>
#include <Nazara/Utility/SubMesh.hpp>
#include <Nazara/Utility/Debug.hpp>
@@ -101,6 +102,14 @@ NzIndexMapper(subMesh->GetIndexBuffer())
nzUInt32 NzIndexMapper::Get(unsigned int i) const
{
#if NAZARA_UTILITY_SAFE
if (i >= m_indexCount)
{
NazaraError("Index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_indexCount) + ')');
return 0;
}
#endif
return m_getter(m_mapper.GetPointer(), i);
}
@@ -109,8 +118,21 @@ const NzIndexBuffer* NzIndexMapper::GetBuffer() const
return m_mapper.GetBuffer();
}
unsigned int NzIndexMapper::GetIndexCount() const
{
return m_indexCount;
}
void NzIndexMapper::Set(unsigned int i, nzUInt32 value)
{
#if NAZARA_UTILITY_SAFE
if (i >= m_indexCount)
{
NazaraError("Index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_indexCount) + ')');
return;
}
#endif
m_setter(m_mapper.GetPointer(), i, value);
}
@@ -118,3 +140,13 @@ void NzIndexMapper::Unmap()
{
m_mapper.Unmap();
}
NzIndexIterator NzIndexMapper::begin()
{
return NzIndexIterator(this, 0);
}
NzIndexIterator NzIndexMapper::end()
{
return NzIndexIterator(this, m_indexCount); // Post-end
}

View File

@@ -188,20 +188,10 @@ void NzMesh::Build(const NzPrimitiveList& list, const NzMeshParams& params)
vertexBuffer.reset(new NzVertexBuffer(GetDeclaration(), vertexCount, params.storage, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
///TODO: Remplacer par un itérateur sur les indices
std::vector<nzUInt32> indices;
indices.resize(indexCount);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer.get(), nzBufferAccess_WriteOnly);
NzGenerateBox(primitive.box.lengths, primitive.box.subdivision, primitive.box.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), &indices[0], &aabb);
vertexMapper.Unmap();
NzIndexMapper indexMapper(indexBuffer.get(), nzBufferAccess_WriteOnly);
for (unsigned int i = 0; i < indexCount; ++i)
indexMapper.Set(i, indices[i]);
indexMapper.Unmap();
NzGenerateBox(primitive.box.lengths, primitive.box.subdivision, primitive.box.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb);
break;
}
@@ -217,20 +207,10 @@ void NzMesh::Build(const NzPrimitiveList& list, const NzMeshParams& params)
vertexBuffer.reset(new NzVertexBuffer(GetDeclaration(), vertexCount, params.storage, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
///TODO: Remplacer par un itérateur sur les indices
std::vector<nzUInt32> indices;
indices.resize(indexCount);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer.get(), nzBufferAccess_WriteOnly);
NzGeneratePlane(primitive.plane.subdivision, primitive.plane.position, primitive.plane.normal, primitive.plane.size, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), &indices[0], &aabb);
vertexMapper.Unmap();
NzIndexMapper indexMapper(indexBuffer.get(), nzBufferAccess_WriteOnly);
for (unsigned int i = 0; i < indexCount; ++i)
indexMapper.Set(i, indices[i]);
indexMapper.Unmap();
NzGeneratePlane(primitive.plane.subdivision, primitive.plane.position, primitive.plane.normal, primitive.plane.size, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb);
break;
}
@@ -250,20 +230,10 @@ void NzMesh::Build(const NzPrimitiveList& list, const NzMeshParams& params)
vertexBuffer.reset(new NzVertexBuffer(GetDeclaration(), vertexCount, params.storage, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
///TODO: Remplacer par un itérateur sur les indices
std::vector<nzUInt32> indices;
indices.resize(indexCount);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer.get(), nzBufferAccess_WriteOnly);
NzGenerateCubicSphere(primitive.sphere.size, primitive.sphere.cubic.subdivision, primitive.sphere.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), &indices[0], &aabb);
vertexMapper.Unmap();
NzIndexMapper indexMapper(indexBuffer.get(), nzBufferAccess_WriteOnly);
for (unsigned int i = 0; i < indexCount; ++i)
indexMapper.Set(i, indices[i]);
indexMapper.Unmap();
NzGenerateCubicSphere(primitive.sphere.size, primitive.sphere.cubic.subdivision, primitive.sphere.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb);
break;
}
@@ -279,20 +249,10 @@ void NzMesh::Build(const NzPrimitiveList& list, const NzMeshParams& params)
vertexBuffer.reset(new NzVertexBuffer(GetDeclaration(), vertexCount, params.storage, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
///TODO: Remplacer par un itérateur sur les indices
std::vector<nzUInt32> indices;
indices.resize(indexCount);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer.get(), nzBufferAccess_WriteOnly);
NzGenerateIcoSphere(primitive.sphere.size, primitive.sphere.ico.recursionLevel, primitive.sphere.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), &indices[0], &aabb);
vertexMapper.Unmap();
NzIndexMapper indexMapper(indexBuffer.get(), nzBufferAccess_WriteOnly);
for (unsigned int i = 0; i < indexCount; ++i)
indexMapper.Set(i, indices[i]);
indexMapper.Unmap();
NzGenerateIcoSphere(primitive.sphere.size, primitive.sphere.ico.recursionLevel, primitive.sphere.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb);
break;
}
@@ -308,20 +268,11 @@ void NzMesh::Build(const NzPrimitiveList& list, const NzMeshParams& params)
vertexBuffer.reset(new NzVertexBuffer(GetDeclaration(), vertexCount, params.storage, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
///TODO: Remplacer par un itérateur sur les indices
std::vector<nzUInt32> indices;
indices.resize(indexCount);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer.get(), nzBufferAccess_WriteOnly);
NzGenerateUvSphere(primitive.sphere.size, primitive.sphere.uv.slices, primitive.sphere.uv.stacks, primitive.sphere.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), &indices[0], &aabb);
vertexMapper.Unmap();
NzIndexMapper indexMapper(indexBuffer.get(), nzBufferAccess_WriteOnly);
for (unsigned int i = 0; i < indexCount; ++i)
indexMapper.Set(i, indices[i]);
indexMapper.Unmap();
NzGenerateUvSphere(primitive.sphere.size, primitive.sphere.uv.slices, primitive.sphere.uv.stacks, primitive.sphere.matrix, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb);
break;
}
}
break;