Updated Generate* functions

They take now multiples SparsePtr (as VertexPointers struct) instead of
a MeshVertex pointer


Former-commit-id: c9393015dfd426ee4a28d71bfdd9851c92d072b7
This commit is contained in:
Lynix 2015-01-26 17:17:16 +01:00
parent 5e3fdbca89
commit 8f101812c3
3 changed files with 187 additions and 71 deletions

View File

@ -8,6 +8,7 @@
#define NAZARA_ALGORITHM_UTILITY_HPP #define NAZARA_ALGORITHM_UTILITY_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Box.hpp> #include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Matrix4.hpp> #include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Vector2.hpp> #include <Nazara/Math/Vector2.hpp>
@ -23,6 +24,14 @@ struct NzSkinningData
NzMeshVertex* outputVertex; NzMeshVertex* outputVertex;
}; };
struct NzVertexPointers
{
NzSparsePtr<NzVector3f> normalPtr;
NzSparsePtr<NzVector3f> positionPtr;
NzSparsePtr<NzVector3f> tangentPtr;
NzSparsePtr<NzVector2f> uvPtr;
};
NAZARA_API void NzComputeBoxIndexVertexCount(const NzVector3ui& subdivision, unsigned int* indexCount, unsigned int* vertexCount); NAZARA_API void NzComputeBoxIndexVertexCount(const NzVector3ui& subdivision, unsigned int* indexCount, unsigned int* vertexCount);
NAZARA_API unsigned int NzComputeCacheMissCount(NzIndexIterator indices, unsigned int indexCount); NAZARA_API unsigned int NzComputeCacheMissCount(NzIndexIterator indices, unsigned int indexCount);
NAZARA_API void NzComputeConeIndexVertexCount(unsigned int subdivision, unsigned int* indexCount, unsigned int* vertexCount); NAZARA_API void NzComputeConeIndexVertexCount(unsigned int subdivision, unsigned int* indexCount, unsigned int* vertexCount);
@ -33,12 +42,12 @@ NAZARA_API void NzComputeUvSphereIndexVertexCount(unsigned int sliceCount, unsig
template<typename T> NzBoxf NzComputeVerticesAABB(const T* vertices, unsigned int vertexCount); template<typename T> NzBoxf NzComputeVerticesAABB(const T* vertices, unsigned int vertexCount);
///TODO: Remplacer le pointeur vertices par une structure composée de plusieurs SparsePtr ///TODO: Remplacer le pointeur vertices par une structure composée de plusieurs SparsePtr
NAZARA_API void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0);
NAZARA_API void NzGenerateCone(float length, float radius, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGenerateCone(float length, float radius, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0);
NAZARA_API void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0);
NAZARA_API void NzGenerateIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGenerateIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0);
NAZARA_API void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector2f& size, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector2f& size, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0);
NAZARA_API void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0);
NAZARA_API void NzOptimizeIndices(NzIndexIterator indices, unsigned int indexCount); NAZARA_API void NzOptimizeIndices(NzIndexIterator indices, unsigned int indexCount);

View File

@ -42,14 +42,14 @@ namespace
{ {
} }
void Generate(float size, unsigned int recursionLevel, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void Generate(float size, unsigned int recursionLevel, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
// Grandement inspiré de http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html // Grandement inspiré de http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
const float t = (1.f + 2.236067f)/2.f; const float t = (1.f + 2.236067f)/2.f;
m_cache.clear(); m_cache.clear();
m_size = size; m_size = size;
m_vertices = vertices; m_vertices = vertexPointers;
m_vertexIndex = 0; m_vertexIndex = 0;
// Sommets de base // Sommets de base
@ -132,10 +132,8 @@ namespace
unsigned int AddVertex(const NzVector3f& position) unsigned int AddVertex(const NzVector3f& position)
{ {
NzMeshVertex& vertex = m_vertices[m_vertexIndex]; m_vertices.normalPtr[m_vertexIndex] = NzVector3f::Normalize(m_matrix.Transform(position, 0.f));
m_vertices.positionPtr[m_vertexIndex] = m_matrix.Transform(m_size * position.GetNormal());
vertex.normal = NzVector3f::Normalize(m_matrix.Transform(position, 0.f));
vertex.position = m_matrix.Transform(m_size * position.GetNormal());
return m_vertexIndex++; return m_vertexIndex++;
} }
@ -147,7 +145,7 @@ namespace
if (it != m_cache.end()) if (it != m_cache.end())
return it->second; return it->second;
NzVector3f middle = NzVector3f::Lerp(m_vertices[index1].position, m_vertices[index2].position, 0.5f); NzVector3f middle = NzVector3f::Lerp(m_vertices.positionPtr[index1], m_vertices.positionPtr[index2], 0.5f);
unsigned int index = AddVertex(middle); unsigned int index = AddVertex(middle);
m_cache[key] = index; m_cache[key] = index;
@ -158,7 +156,7 @@ namespace
private: private:
std::unordered_map<nzUInt64, unsigned int> m_cache; std::unordered_map<nzUInt64, unsigned int> m_cache;
const NzMatrix4f& m_matrix; const NzMatrix4f& m_matrix;
NzMeshVertex* m_vertices; NzVertexPointers m_vertices;
float m_size; float m_size;
unsigned int m_vertexIndex; unsigned int m_vertexIndex;
}; };
@ -714,7 +712,7 @@ void NzComputeUvSphereIndexVertexCount(unsigned int sliceCount, unsigned int sta
/**********************************NzGenerate*********************************/ /**********************************NzGenerate*********************************/
void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
unsigned int xIndexCount, yIndexCount, zIndexCount; unsigned int xIndexCount, yIndexCount, zIndexCount;
unsigned int xVertexCount, yVertexCount, zVertexCount; unsigned int xVertexCount, yVertexCount, zVertexCount;
@ -728,45 +726,105 @@ void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, co
// Face +X // Face +X
transform.MakeTransform(NzVector3f::UnitX() * halfLengths.x, NzEulerAnglesf(-90.f, 0.f, -90.f)); transform.MakeTransform(NzVector3f::UnitX() * halfLengths.x, NzEulerAnglesf(-90.f, 0.f, -90.f));
NzGeneratePlane(NzVector2ui(subdivision.z, subdivision.y), NzVector2f(lengths.z, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertices, indices, nullptr, indexOffset); NzGeneratePlane(NzVector2ui(subdivision.z, subdivision.y), NzVector2f(lengths.z, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += xVertexCount; indexOffset += xVertexCount;
indices += xIndexCount; indices += xIndexCount;
vertices += xVertexCount;
vertexPointers.positionPtr += xVertexCount;
if (vertexPointers.normalPtr)
vertexPointers.normalPtr += xVertexCount;
if (vertexPointers.tangentPtr)
vertexPointers.tangentPtr += xVertexCount;
if (vertexPointers.uvPtr)
vertexPointers.uvPtr += xVertexCount;
// Face +Y // Face +Y
transform.MakeTransform(NzVector3f::UnitY() * halfLengths.y, NzEulerAnglesf(0.f, 0.f, 0.f)); transform.MakeTransform(NzVector3f::UnitY() * halfLengths.y, NzEulerAnglesf(0.f, 0.f, 0.f));
NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.z), NzVector2f(lengths.x, lengths.z), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertices, indices, nullptr, indexOffset); NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.z), NzVector2f(lengths.x, lengths.z), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += yVertexCount; indexOffset += yVertexCount;
indices += yIndexCount; indices += yIndexCount;
vertices += yVertexCount;
vertexPointers.positionPtr += yVertexCount;
if (vertexPointers.normalPtr)
vertexPointers.normalPtr += yVertexCount;
if (vertexPointers.tangentPtr)
vertexPointers.tangentPtr += yVertexCount;
if (vertexPointers.uvPtr)
vertexPointers.uvPtr += yVertexCount;
// Face +Z // Face +Z
transform.MakeTransform(NzVector3f::UnitZ() * halfLengths.z, NzEulerAnglesf(-90.f, 90.f, 90.f)); transform.MakeTransform(NzVector3f::UnitZ() * halfLengths.z, NzEulerAnglesf(-90.f, 90.f, 90.f));
NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.y), NzVector2f(lengths.x, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertices, indices, nullptr, indexOffset); NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.y), NzVector2f(lengths.x, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += zVertexCount; indexOffset += zVertexCount;
indices += zIndexCount; indices += zIndexCount;
vertices += zVertexCount;
vertexPointers.positionPtr += zVertexCount;
if (vertexPointers.normalPtr)
vertexPointers.normalPtr += zVertexCount;
if (vertexPointers.tangentPtr)
vertexPointers.tangentPtr += zVertexCount;
if (vertexPointers.uvPtr)
vertexPointers.uvPtr += zVertexCount;
// Face -X // Face -X
transform.MakeTransform(-NzVector3f::UnitX() * halfLengths.x, NzEulerAnglesf(-90.f, 0.f, 90.f)); transform.MakeTransform(-NzVector3f::UnitX() * halfLengths.x, NzEulerAnglesf(-90.f, 0.f, 90.f));
NzGeneratePlane(NzVector2ui(subdivision.z, subdivision.y), NzVector2f(lengths.z, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertices, indices, nullptr, indexOffset); NzGeneratePlane(NzVector2ui(subdivision.z, subdivision.y), NzVector2f(lengths.z, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += xVertexCount; indexOffset += xVertexCount;
indices += xIndexCount; indices += xIndexCount;
vertices += xVertexCount;
vertexPointers.positionPtr += xVertexCount;
if (vertexPointers.normalPtr)
vertexPointers.normalPtr += xVertexCount;
if (vertexPointers.tangentPtr)
vertexPointers.tangentPtr += xVertexCount;
if (vertexPointers.uvPtr)
vertexPointers.uvPtr += xVertexCount;
// Face -Y // Face -Y
transform.MakeTransform(-NzVector3f::UnitY() * halfLengths.y, NzEulerAnglesf(0.f, 0.f, 180.f)); transform.MakeTransform(-NzVector3f::UnitY() * halfLengths.y, NzEulerAnglesf(0.f, 0.f, 180.f));
NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.z), NzVector2f(lengths.x, lengths.z), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertices, indices, nullptr, indexOffset); NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.z), NzVector2f(lengths.x, lengths.z), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += yVertexCount; indexOffset += yVertexCount;
indices += yIndexCount; indices += yIndexCount;
vertices += yVertexCount;
vertexPointers.positionPtr += yVertexCount;
if (vertexPointers.normalPtr)
vertexPointers.normalPtr += yVertexCount;
if (vertexPointers.tangentPtr)
vertexPointers.tangentPtr += yVertexCount;
if (vertexPointers.uvPtr)
vertexPointers.uvPtr += yVertexCount;
// Face -Z // Face -Z
transform.MakeTransform(-NzVector3f::UnitZ() * halfLengths.z, NzEulerAnglesf(-90.f, -90.f, 90.f)); transform.MakeTransform(-NzVector3f::UnitZ() * halfLengths.z, NzEulerAnglesf(-90.f, -90.f, 90.f));
NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.y), NzVector2f(lengths.x, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertices, indices, nullptr, indexOffset); NzGeneratePlane(NzVector2ui(subdivision.x, subdivision.y), NzVector2f(lengths.x, lengths.y), NzMatrix4f::ConcatenateAffine(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += zVertexCount; indexOffset += zVertexCount;
indices += zIndexCount; indices += zIndexCount;
vertices += zVertexCount;
vertexPointers.positionPtr += zVertexCount;
if (vertexPointers.normalPtr)
vertexPointers.normalPtr += zVertexCount;
if (vertexPointers.tangentPtr)
vertexPointers.tangentPtr += zVertexCount;
if (vertexPointers.uvPtr)
vertexPointers.uvPtr += zVertexCount;
if (aabb) if (aabb)
{ {
@ -775,20 +833,20 @@ void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, co
} }
} }
void NzGenerateCone(float length, float radius, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void NzGenerateCone(float length, float radius, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
constexpr float round = 2.f*static_cast<float>(M_PI); constexpr float round = 2.f*static_cast<float>(M_PI);
float delta = round/subdivision; float delta = round/subdivision;
vertices->position = matrix.GetTranslation(); // matrix.Transform(NzVector3f(0.f)); *vertexPointers.positionPtr++ = matrix.GetTranslation(); // matrix.Transform(NzVector3f(0.f));
vertices->normal = matrix.Transform(NzVector3f::Up(), 0.f);
vertices++; if (vertexPointers.normalPtr)
*vertexPointers.normalPtr++ = matrix.Transform(NzVector3f::Up(), 0.f);
for (unsigned int i = 0; i < subdivision; ++i) for (unsigned int i = 0; i < subdivision; ++i)
{ {
float angle = delta*i; float angle = delta*i;
vertices->position = matrix.Transform(NzVector3f(radius*std::sin(angle), -length, radius*std::cos(angle))); *vertexPointers.positionPtr++ = matrix.Transform(NzVector3f(radius*std::sin(angle), -length, radius*std::cos(angle)));
vertices++;
*indices++ = indexOffset + 0; *indices++ = indexOffset + 0;
*indices++ = indexOffset + i+1; *indices++ = indexOffset + i+1;
@ -821,13 +879,14 @@ void NzGenerateCone(float length, float radius, unsigned int subdivision, const
} }
} }
void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
///DOC: Cette fonction va accéder aux pointeurs en écriture ET en lecture
unsigned int vertexCount; unsigned int vertexCount;
NzComputeBoxIndexVertexCount(NzVector3ui(subdivision), nullptr, &vertexCount); NzComputeBoxIndexVertexCount(NzVector3ui(subdivision), nullptr, &vertexCount);
// On envoie une matrice identité de sorte à ce que la boîte ne subisse aucune transformation (rendant plus facile l'étape suivante) // On envoie une matrice identité de sorte à ce que la boîte ne subisse aucune transformation (rendant plus facile l'étape suivante)
NzGenerateBox(NzVector3f(size, size, size), NzVector3ui(subdivision), NzMatrix4f::Identity(), textureCoords, vertices, indices, nullptr, indexOffset); NzGenerateBox(NzVector3f(size, size, size), NzVector3ui(subdivision), NzMatrix4f::Identity(), textureCoords, vertexPointers, indices, nullptr, indexOffset);
if (aabb) if (aabb)
{ {
@ -837,20 +896,24 @@ void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4
for (unsigned int i = 0; i < vertexCount; ++i) for (unsigned int i = 0; i < vertexCount; ++i)
{ {
vertices->position = matrix.Transform(size * vertices->position.GetNormal()); NzVector3f normal = vertexPointers.positionPtr->GetNormal();
vertices->normal = vertices->position.GetNormal();
//vertices->tangent = ??? *vertexPointers.positionPtr++ = matrix.Transform(size * normal);
vertices++;
if (vertexPointers.normalPtr)
*vertexPointers.normalPtr++ = normal;
///FIXME: *vertexPointers.tangentPtr++ = ???
} }
} }
void NzGenerateIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void NzGenerateIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
IcoSphereBuilder builder(matrix); IcoSphereBuilder builder(matrix);
builder.Generate(size, recursionLevel, textureCoords, vertices, indices, aabb, indexOffset); builder.Generate(size, recursionLevel, textureCoords, vertexPointers, indices, aabb, indexOffset);
} }
void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector2f& size, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector2f& size, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
// Pour plus de facilité, on va construire notre plan en considérant que la normale est de 0,1,0 // Pour plus de facilité, on va construire notre plan en considérant que la normale est de 0,1,0
// Et appliquer ensuite une matrice "finissant le travail" // Et appliquer ensuite une matrice "finissant le travail"
@ -881,11 +944,16 @@ void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector2f& size, con
for (unsigned int y = 0; y < verticalVertexCount; ++y) for (unsigned int y = 0; y < verticalVertexCount; ++y)
{ {
NzVector3f localPos((2.f*x*invHorizontalVertexCount - 1.f) * halfSizeX, 0.f, (2.f*y*invVerticalVertexCount - 1.f) * halfSizeY); NzVector3f localPos((2.f*x*invHorizontalVertexCount - 1.f) * halfSizeX, 0.f, (2.f*y*invVerticalVertexCount - 1.f) * halfSizeY);
vertices->position = matrix * localPos; *vertexPointers.positionPtr++ = matrix * localPos;
vertices->uv.Set(textureCoords.x + x*invHorizontalVertexCount*textureCoords.width, textureCoords.y + y*invVerticalVertexCount*textureCoords.height);
vertices->normal = normal; if (vertexPointers.normalPtr)
vertices->tangent = tangent; *vertexPointers.normalPtr++ = normal;
vertices++;
if (vertexPointers.tangentPtr)
*vertexPointers.tangentPtr++ = tangent;
if (vertexPointers.uvPtr)
*vertexPointers.uvPtr++ = NzVector2f(textureCoords.x + x*invHorizontalVertexCount*textureCoords.width, textureCoords.y + y*invVerticalVertexCount*textureCoords.height);
if (x != horizontalVertexCount-1 && y != verticalVertexCount-1) if (x != horizontalVertexCount-1 && y != verticalVertexCount-1)
{ {
@ -904,7 +972,7 @@ void NzGeneratePlane(const NzVector2ui& subdivision, const NzVector2f& size, con
aabb->Set(matrix.Transform(NzVector3f(-halfSizeX, 0.f, -halfSizeY), 0.f), matrix.Transform(NzVector3f(halfSizeX, 0.f, halfSizeY), 0.f)); aabb->Set(matrix.Transform(NzVector3f(-halfSizeX, 0.f, -halfSizeY), 0.f), matrix.Transform(NzVector3f(halfSizeX, 0.f, halfSizeY), 0.f));
} }
void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset) void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzVertexPointers vertexPointers, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{ {
// http://stackoverflow.com/questions/14080932/implementing-opengl-sphere-example-code // http://stackoverflow.com/questions/14080932/implementing-opengl-sphere-example-code
float invSliceCount = 1.f / (sliceCount-1); float invSliceCount = 1.f / (sliceCount-1);
@ -930,10 +998,13 @@ void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackC
normal.x = std::cos(sliceValPi2) * sinStackValPi; normal.x = std::cos(sliceValPi2) * sinStackValPi;
normal.z = std::sin(sliceValPi2) * sinStackValPi; normal.z = std::sin(sliceValPi2) * sinStackValPi;
vertices->position = matrix.Transform(size * normal); *vertexPointers.positionPtr++ = matrix.Transform(size * normal);
vertices->normal = matrix.Transform(normal, 0.f);
vertices->uv.Set(textureCoords.x + textureCoords.width*(1.f - sliceVal), textureCoords.y + textureCoords.height*stackVal); if (vertexPointers.normalPtr)
vertices++; *vertexPointers.normalPtr++ = matrix.Transform(normal, 0.f);
if (vertexPointers.uvPtr)
*vertexPointers.uvPtr++ = NzVector2f(textureCoords.x + textureCoords.width*(1.f - sliceVal), textureCoords.y + textureCoords.height*stackVal);
if (stack != stackCount-1 && slice != sliceCount-1) if (stack != stackCount-1 && slice != sliceCount-1)
{ {

View File

@ -10,13 +10,13 @@
#include <Nazara/Utility/Algorithm.hpp> #include <Nazara/Utility/Algorithm.hpp>
#include <Nazara/Utility/Animation.hpp> #include <Nazara/Utility/Animation.hpp>
#include <Nazara/Utility/Buffer.hpp> #include <Nazara/Utility/Buffer.hpp>
#include <Nazara/Utility/BufferMapper.hpp>
#include <Nazara/Utility/Config.hpp> #include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/IndexMapper.hpp> #include <Nazara/Utility/IndexMapper.hpp>
#include <Nazara/Utility/SkeletalMesh.hpp> #include <Nazara/Utility/SkeletalMesh.hpp>
#include <Nazara/Utility/Skeleton.hpp> #include <Nazara/Utility/Skeleton.hpp>
#include <Nazara/Utility/StaticMesh.hpp> #include <Nazara/Utility/StaticMesh.hpp>
#include <Nazara/Utility/SubMesh.hpp> #include <Nazara/Utility/SubMesh.hpp>
#include <Nazara/Utility/VertexMapper.hpp>
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <memory> #include <memory>
@ -179,10 +179,16 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static); indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static);
vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static); vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly); NzVertexMapper vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateBox(primitive.box.lengths, primitive.box.subdivision, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb); NzVertexPointers pointers;
pointers.normalPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Normal);
pointers.positionPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Position);
pointers.tangentPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Tangent);
pointers.uvPtr = vertexMapper.GetComponentPtr<NzVector2f>(nzVertexComponent_TexCoord);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateBox(primitive.box.lengths, primitive.box.subdivision, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb);
break; break;
} }
@ -195,10 +201,16 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static); indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static);
vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static); vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly); NzVertexMapper vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateCone(primitive.cone.length, primitive.cone.radius, primitive.cone.subdivision, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb); NzVertexPointers pointers;
pointers.normalPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Normal);
pointers.positionPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Position);
pointers.tangentPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Tangent);
pointers.uvPtr = vertexMapper.GetComponentPtr<NzVector2f>(nzVertexComponent_TexCoord);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateCone(primitive.cone.length, primitive.cone.radius, primitive.cone.subdivision, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb);
break; break;
} }
@ -211,10 +223,16 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static); indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static);
vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static); vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly); NzVertexMapper vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGeneratePlane(primitive.plane.subdivision, primitive.plane.size, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb); NzVertexPointers pointers;
pointers.normalPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Normal);
pointers.positionPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Position);
pointers.tangentPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Tangent);
pointers.uvPtr = vertexMapper.GetComponentPtr<NzVector2f>(nzVertexComponent_TexCoord);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGeneratePlane(primitive.plane.subdivision, primitive.plane.size, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb);
break; break;
} }
@ -231,10 +249,16 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static); indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static);
vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static); vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly); NzVertexMapper vertexMapper(vertexBuffer, nzBufferAccess_ReadWrite);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateCubicSphere(primitive.sphere.size, primitive.sphere.cubic.subdivision, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb); NzVertexPointers pointers;
pointers.normalPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Normal);
pointers.positionPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Position);
pointers.tangentPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Tangent);
pointers.uvPtr = vertexMapper.GetComponentPtr<NzVector2f>(nzVertexComponent_TexCoord);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateCubicSphere(primitive.sphere.size, primitive.sphere.cubic.subdivision, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb);
break; break;
} }
@ -247,10 +271,16 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static); indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static);
vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static); vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly); NzVertexMapper vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateIcoSphere(primitive.sphere.size, primitive.sphere.ico.recursionLevel, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb); NzVertexPointers pointers;
pointers.normalPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Normal);
pointers.positionPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Position);
pointers.tangentPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Tangent);
pointers.uvPtr = vertexMapper.GetComponentPtr<NzVector2f>(nzVertexComponent_TexCoord);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateIcoSphere(primitive.sphere.size, primitive.sphere.ico.recursionLevel, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb);
break; break;
} }
@ -263,10 +293,16 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static); indexBuffer = NzIndexBuffer::New(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static);
vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static); vertexBuffer = NzVertexBuffer::New(declaration, vertexCount, params.storage, nzBufferUsage_Static);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly); NzVertexMapper vertexMapper(vertexBuffer, nzBufferAccess_WriteOnly);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateUvSphere(primitive.sphere.size, primitive.sphere.uv.sliceCount, primitive.sphere.uv.stackCount, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb); NzVertexPointers pointers;
pointers.normalPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Normal);
pointers.positionPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Position);
pointers.tangentPtr = vertexMapper.GetComponentPtr<NzVector3f>(nzVertexComponent_Tangent);
pointers.uvPtr = vertexMapper.GetComponentPtr<NzVector2f>(nzVertexComponent_TexCoord);
NzIndexMapper indexMapper(indexBuffer, nzBufferAccess_WriteOnly);
NzGenerateUvSphere(primitive.sphere.size, primitive.sphere.uv.sliceCount, primitive.sphere.uv.stackCount, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb);
break; break;
} }
} }