diff --git a/include/Nazara/Utility/Algorithm.hpp b/include/Nazara/Utility/Algorithm.hpp index 3b403f681..01e6f2960 100644 --- a/include/Nazara/Utility/Algorithm.hpp +++ b/include/Nazara/Utility/Algorithm.hpp @@ -21,6 +21,7 @@ NAZARA_API void NzComputeCubicSphereIndexVertexCount(unsigned int subdivision, u NAZARA_API void NzComputeIcoSphereIndexVertexCount(unsigned int recursionLevel, unsigned int* indexCount, unsigned int* vertexCount); NAZARA_API void NzComputePlaneIndexVertexCount(const NzVector2ui& subdivision, unsigned int* indexCount, unsigned int* vertexCount); NAZARA_API void NzComputeUvSphereIndexVertexCount(unsigned int sliceCount, unsigned int stackCount, unsigned int* indexCount, unsigned int* vertexCount); +template NzBoxf NzComputeVerticesAABB(const T* vertices, unsigned int vertexCount); NAZARA_API void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, const NzMatrix4f& matrix, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); NAZARA_API void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb = nullptr, unsigned int indexOffset = 0); @@ -30,8 +31,7 @@ NAZARA_API void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned NAZARA_API void NzOptimizeIndices(NzIndexIterator indices, unsigned int indexCount); -inline void NzTransformVertex(NzMeshVertex* vertex, const NzMatrix4f& matrix); -inline void NzTransformVertices(NzMeshVertex* vertices, unsigned int vertexCount, const NzMatrix4f& matrix); +template void NzTransformVertices(T* vertices, unsigned int vertexCount, const NzMatrix4f& matrix); #include diff --git a/include/Nazara/Utility/Algorithm.inl b/include/Nazara/Utility/Algorithm.inl index b08d09227..bbd5b0303 100644 --- a/include/Nazara/Utility/Algorithm.inl +++ b/include/Nazara/Utility/Algorithm.inl @@ -4,21 +4,42 @@ #include -void NzTransformVertex(NzMeshVertex* vertex, const NzMatrix4f& matrix) +template +NzBoxf NzComputeVerticesAABB(const T* vertices, unsigned int vertexCount) { - vertex->normal = matrix.Transform(vertex->normal, 0.f); - vertex->position = matrix.Transform(vertex->position); - vertex->tangent = matrix.Transform(vertex->tangent, 0.f); - vertex++; + NzBoxf aabb; + if (vertexCount > 0) + { + aabb.Set(vertices->position); + vertices++; + + for (unsigned int i = 1; i < vertexCount; ++i) + { + aabb.ExtendTo(vertices->position); + vertices++; + } + } + else + aabb.MakeZero(); + + return aabb; } -void NzTransformVertices(NzMeshVertex* vertices, unsigned int vertexCount, const NzMatrix4f& matrix) +template +void NzTransformVertices(T* vertices, unsigned int vertexCount, const NzMatrix4f& matrix) { if (matrix.IsIdentity()) return; + NzVector3f scale = matrix.GetScale(); + for (unsigned int i = 0; i < vertexCount; ++i) - NzTransformVertex(vertices++, matrix); + { + vertices->normal = matrix.Transform(vertices->normal, 0.f) / scale; + vertices->position = matrix.Transform(vertices->position); + vertices->tangent = matrix.Transform(vertices->tangent, 0.f) / scale; + vertices++; + } } #include