From c998a5c4575dff0744db3ece88fa1ce1e19fa3f4 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 2 Jan 2013 21:51:03 +0100 Subject: [PATCH] Added KeyframeMesh::Get/Set vertex components Former-commit-id: ff51b02dfd5f0675c33c01f39418312834c5d8a9 --- include/Nazara/Utility/KeyframeMesh.hpp | 11 +- src/Nazara/Utility/KeyframeMesh.cpp | 180 +++++++++++++++++++++- src/Nazara/Utility/Loaders/MD2/Loader.cpp | 13 +- 3 files changed, 191 insertions(+), 13 deletions(-) diff --git a/include/Nazara/Utility/KeyframeMesh.hpp b/include/Nazara/Utility/KeyframeMesh.hpp index 0576653ac..13df9eee1 100644 --- a/include/Nazara/Utility/KeyframeMesh.hpp +++ b/include/Nazara/Utility/KeyframeMesh.hpp @@ -30,7 +30,12 @@ class NAZARA_API NzKeyframeMesh final : public NzSubMesh nzAnimationType GetAnimationType() const override; unsigned int GetFrameCount() const; const NzIndexBuffer* GetIndexBuffer() const override; + NzVector3f GetNormal(unsigned int frameIndex, unsigned int vertexIndex) const; + NzVector3f GetPosition(unsigned int frameIndex, unsigned int vertexIndex) const; + NzVector3f GetTangent(unsigned int frameIndex, unsigned int vertexIndex) const; + NzVector2f GetTexCoords(unsigned int vertexIndex) const; void GetVertex(unsigned int frameIndex, unsigned int vertexIndex, NzMeshVertex* dest) const; + NzVertexBuffer* GetVertexBuffer() override; const NzVertexBuffer* GetVertexBuffer() const override; @@ -41,8 +46,10 @@ class NAZARA_API NzKeyframeMesh final : public NzSubMesh void SetAABB(unsigned int frameIndex, const NzAxisAlignedBox& aabb); void SetIndexBuffer(const NzIndexBuffer* indexBuffer); - void SetVertex(unsigned int frameIndex, unsigned int vertexIndex, const NzMeshVertex& source); - void SetTexCoords(unsigned int vertexIndex, const NzVector2f& uv); + void SetNormal(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& normal); + void SetPosition(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& position); + void SetTangent(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& tangent); + void SetTexCoords(unsigned int vertexIndex, const NzVector2f& texCoords); private: void InterpolateImpl(unsigned int frameA, unsigned int frameB, float interpolation) const; diff --git a/src/Nazara/Utility/KeyframeMesh.cpp b/src/Nazara/Utility/KeyframeMesh.cpp index a2aa0d8ac..802d8c016 100644 --- a/src/Nazara/Utility/KeyframeMesh.cpp +++ b/src/Nazara/Utility/KeyframeMesh.cpp @@ -153,6 +153,118 @@ const NzIndexBuffer* NzKeyframeMesh::GetIndexBuffer() const return m_impl->indexBuffer; } +NzVector3f NzKeyframeMesh::GetNormal(unsigned int frameIndex, unsigned int vertexIndex) const +{ + #if NAZARA_UTILITY_SAFE + if (!m_impl) + { + NazaraError("Keyframe mesh not created"); + return NzVector3f(); + } + + if (frameIndex >= m_impl->frameCount) + { + NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); + return NzVector3f(); + } + #endif + + unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); + + #if NAZARA_UTILITY_SAFE + if (vertexIndex >= vertexCount) + { + NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')'); + return NzVector3f(); + } + #endif + + unsigned int index = frameIndex*vertexCount + vertexIndex; + + return m_impl->normals[index]; +} + +NzVector3f NzKeyframeMesh::GetPosition(unsigned int frameIndex, unsigned int vertexIndex) const +{ + #if NAZARA_UTILITY_SAFE + if (!m_impl) + { + NazaraError("Keyframe mesh not created"); + return NzVector3f(); + } + + if (frameIndex >= m_impl->frameCount) + { + NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); + return NzVector3f(); + } + #endif + + unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); + + #if NAZARA_UTILITY_SAFE + if (vertexIndex >= vertexCount) + { + NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')'); + return NzVector3f(); + } + #endif + + unsigned int index = frameIndex*vertexCount + vertexIndex; + + return m_impl->positions[index]; +} + +NzVector3f NzKeyframeMesh::GetTangent(unsigned int frameIndex, unsigned int vertexIndex) const +{ + #if NAZARA_UTILITY_SAFE + if (!m_impl) + { + NazaraError("Keyframe mesh not created"); + return NzVector3f(); + } + + if (frameIndex >= m_impl->frameCount) + { + NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); + return NzVector3f(); + } + #endif + + unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); + + #if NAZARA_UTILITY_SAFE + if (vertexIndex >= vertexCount) + { + NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')'); + return NzVector3f(); + } + #endif + + unsigned int index = frameIndex*vertexCount + vertexIndex; + + return m_impl->tangents[index]; +} + +NzVector2f NzKeyframeMesh::GetTexCoords(unsigned int vertexIndex) const +{ + #if NAZARA_UTILITY_SAFE + if (!m_impl) + { + NazaraError("Keyframe mesh not created"); + return NzVector2f(); + } + + if (vertexIndex >= m_impl->vertexBuffer->GetVertexCount()) + { + NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(m_impl->vertexBuffer->GetVertexCount()) + ')'); + return NzVector2f(); + } + #endif + + return m_impl->uv[vertexIndex]; +} + void NzKeyframeMesh::GetVertex(unsigned int frameIndex, unsigned int vertexIndex, NzMeshVertex* dest) const { #if NAZARA_UTILITY_SAFE @@ -303,7 +415,7 @@ void NzKeyframeMesh::SetIndexBuffer(const NzIndexBuffer* indexBuffer) m_impl->indexBuffer = indexBuffer; } -void NzKeyframeMesh::SetVertex(unsigned int frameIndex, unsigned int vertexIndex, const NzMeshVertex& source) +void NzKeyframeMesh::SetNormal(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& normal) { #if NAZARA_UTILITY_SAFE if (!m_impl) @@ -331,9 +443,69 @@ void NzKeyframeMesh::SetVertex(unsigned int frameIndex, unsigned int vertexIndex unsigned int index = frameIndex*vertexCount + vertexIndex; - m_impl->normals[index] = source.normal; - m_impl->positions[index] = source.position; - m_impl->tangents[index] = source.tangent; + m_impl->normals[index] = normal; +} + +void NzKeyframeMesh::SetPosition(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& position) +{ + #if NAZARA_UTILITY_SAFE + if (!m_impl) + { + NazaraError("Keyframe mesh not created"); + return; + } + + if (frameIndex >= m_impl->frameCount) + { + NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); + return; + } + #endif + + unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); + + #if NAZARA_UTILITY_SAFE + if (vertexIndex >= vertexCount) + { + NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')'); + return; + } + #endif + + unsigned int index = frameIndex*vertexCount + vertexIndex; + + m_impl->positions[index] = position; +} + +void NzKeyframeMesh::SetTangent(unsigned int frameIndex, unsigned int vertexIndex, const NzVector3f& tangent) +{ + #if NAZARA_UTILITY_SAFE + if (!m_impl) + { + NazaraError("Keyframe mesh not created"); + return; + } + + if (frameIndex >= m_impl->frameCount) + { + NazaraError("Frame index out of bounds (" + NzString::Number(frameIndex) + " >= " + NzString::Number(m_impl->frameCount) + ')'); + return; + } + #endif + + unsigned int vertexCount = m_impl->vertexBuffer->GetVertexCount(); + + #if NAZARA_UTILITY_SAFE + if (vertexIndex >= vertexCount) + { + NazaraError("Vertex index out of bounds (" + NzString::Number(vertexIndex) + " >= " + NzString::Number(vertexCount) + ')'); + return; + } + #endif + + unsigned int index = frameIndex*vertexCount + vertexIndex; + + m_impl->tangents[index] = tangent; } void NzKeyframeMesh::SetTexCoords(unsigned int vertexIndex, const NzVector2f& uv) diff --git a/src/Nazara/Utility/Loaders/MD2/Loader.cpp b/src/Nazara/Utility/Loaders/MD2/Loader.cpp index e2bac8ee5..9abdf9d5e 100644 --- a/src/Nazara/Utility/Loaders/MD2/Loader.cpp +++ b/src/Nazara/Utility/Loaders/MD2/Loader.cpp @@ -189,20 +189,19 @@ namespace // On fait en sorte d'étendre l'AABB pour qu'il contienne ce sommet aabb.ExtendTo(position); - // Et on finit par copier les éléments dans le buffer - NzMeshVertex vertex; - vertex.normal = md2Normals[vert.n]; - vertex.position = position; - + // On calcule l'indice (On affecte dans le sens inverse) unsigned int vertexIndex = vertexCount - (t*3 + v) - 1; + + // Et on finit par copier les éléments dans le buffer + subMesh->SetNormal(f, vertexIndex, md2Normals[vert.n]); + subMesh->SetPosition(f, vertexIndex, position); + if (f == 0) { // On ne définit les coordonnées de texture que lors de la première frame const md2_texCoord& texC = texCoords[triangles[t].texCoords[v]]; subMesh->SetTexCoords(vertexIndex, NzVector2f(texC.u / static_cast(header.skinwidth), 1.f - texC.v / static_cast(header.skinheight))); } - - subMesh->SetVertex(f, vertexIndex, vertex); } }