From b86332a51b57f414b9ada59c339286943c730913 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 29 Mar 2013 14:42:55 +0100 Subject: [PATCH] Refactored TriangleIterator/VertexMapper Former-commit-id: b8d4fc79142b36519ebe1879276415fe30970d3e --- include/Nazara/Utility/TriangleIterator.hpp | 4 +- include/Nazara/Utility/VertexMapper.hpp | 12 +- src/Nazara/Utility/TriangleIterator.cpp | 8 +- src/Nazara/Utility/VertexMapper.cpp | 231 +++----------------- 4 files changed, 46 insertions(+), 209 deletions(-) diff --git a/include/Nazara/Utility/TriangleIterator.hpp b/include/Nazara/Utility/TriangleIterator.hpp index 65cda25cb..e6739102e 100644 --- a/include/Nazara/Utility/TriangleIterator.hpp +++ b/include/Nazara/Utility/TriangleIterator.hpp @@ -27,12 +27,12 @@ class NAZARA_API NzTriangleIterator NzVector3f GetNormal(unsigned int i) const; NzVector3f GetPosition(unsigned int i) const; NzVector3f GetTangent(unsigned int i) const; - NzVector2f GetTexCoords(unsigned int i) const; + NzVector2f GetTexCoord(unsigned int i) const; void SetNormal(unsigned int i, const NzVector3f& normal); void SetPosition(unsigned int i, const NzVector3f& position); void SetTangent(unsigned int i, const NzVector3f& tangent); - void SetTexCoords(unsigned int i, const NzVector2f& texCoords); + void SetTexCoord(unsigned int i, const NzVector2f& texCoords); void Unmap(); diff --git a/include/Nazara/Utility/VertexMapper.hpp b/include/Nazara/Utility/VertexMapper.hpp index 49fb0f19b..0a19af2c4 100644 --- a/include/Nazara/Utility/VertexMapper.hpp +++ b/include/Nazara/Utility/VertexMapper.hpp @@ -10,12 +10,12 @@ #include #include #include +#include #include +#include class NzSubMesh; -class NzVertexMapperImpl; - class NAZARA_API NzVertexMapper { public: @@ -25,18 +25,20 @@ class NAZARA_API NzVertexMapper NzVector3f GetNormal(unsigned int i) const; NzVector3f GetPosition(unsigned int i) const; NzVector3f GetTangent(unsigned int i) const; - NzVector2f GetTexCoords(unsigned int i) const; + NzVector2f GetTexCoord(unsigned int i) const; unsigned int GetVertexCount(); void SetNormal(unsigned int i, const NzVector3f& normal); void SetPosition(unsigned int i, const NzVector3f& position); void SetTangent(unsigned int i, const NzVector3f& tangent); - void SetTexCoords(unsigned int i, const NzVector2f& texCoords); + void SetTexCoord(unsigned int i, const NzVector2f& texCoord); void Unmap(); private: - NzVertexMapperImpl* m_impl; + NzBufferMapper m_mapper; + NzMeshVertex* m_vertices; + unsigned int m_vertexCount; }; #endif // NAZARA_VERTEXMAPPER_HPP diff --git a/src/Nazara/Utility/TriangleIterator.cpp b/src/Nazara/Utility/TriangleIterator.cpp index 2471ab2f2..3a113e711 100644 --- a/src/Nazara/Utility/TriangleIterator.cpp +++ b/src/Nazara/Utility/TriangleIterator.cpp @@ -98,7 +98,7 @@ NzVector3f NzTriangleIterator::GetTangent(unsigned int i) const return m_vertexMapper.GetTangent(m_triangleIndices[i]); } -NzVector2f NzTriangleIterator::GetTexCoords(unsigned int i) const +NzVector2f NzTriangleIterator::GetTexCoord(unsigned int i) const { #if NAZARA_UTILITY_SAFE if (i > 2) @@ -108,7 +108,7 @@ NzVector2f NzTriangleIterator::GetTexCoords(unsigned int i) const } #endif - return m_vertexMapper.GetTexCoords(m_triangleIndices[i]); + return m_vertexMapper.GetTexCoord(m_triangleIndices[i]); } void NzTriangleIterator::SetNormal(unsigned int i, const NzVector3f& normal) @@ -150,7 +150,7 @@ void NzTriangleIterator::SetTangent(unsigned int i, const NzVector3f& tangent) m_vertexMapper.SetTangent(m_triangleIndices[i], tangent); } -void NzTriangleIterator::SetTexCoords(unsigned int i, const NzVector2f& texCoords) +void NzTriangleIterator::SetTexCoord(unsigned int i, const NzVector2f& texCoords) { #if NAZARA_UTILITY_SAFE if (i > 2) @@ -160,7 +160,7 @@ void NzTriangleIterator::SetTexCoords(unsigned int i, const NzVector2f& texCoord } #endif - m_vertexMapper.SetTexCoords(m_triangleIndices[i], texCoords); + m_vertexMapper.SetTexCoord(m_triangleIndices[i], texCoords); } void NzTriangleIterator::Unmap() diff --git a/src/Nazara/Utility/VertexMapper.cpp b/src/Nazara/Utility/VertexMapper.cpp index e4462079a..655c18fb7 100644 --- a/src/Nazara/Utility/VertexMapper.cpp +++ b/src/Nazara/Utility/VertexMapper.cpp @@ -5,259 +5,94 @@ #include #include #include -#include // NzMeshVertex #include #include #include #include -class NzVertexMapperImpl -{ - public: - NzVertexMapperImpl() = default; - virtual ~NzVertexMapperImpl() = default; - - virtual NzVector3f GetNormal(unsigned int i) const = 0; - virtual NzVector3f GetPosition(unsigned int i) const = 0; - virtual NzVector3f GetTangent(unsigned int i) const = 0; - virtual NzVector2f GetTexCoords(unsigned int i) const = 0; - virtual unsigned int GetVertexCount() const = 0; - - virtual void SetNormal(unsigned int i, const NzVector3f& normal) = 0; - virtual void SetPosition(unsigned int i, const NzVector3f& position) = 0; - virtual void SetTangent(unsigned int i, const NzVector3f& tangent) = 0; - virtual void SetTexCoords(unsigned int i, const NzVector2f& texCoords) = 0; -}; - -namespace -{ - class SubMeshVertexMapper : public NzVertexMapperImpl - { - public: - SubMeshVertexMapper(NzSubMesh* subMesh) : - m_subMesh(subMesh) - { - #if NAZARA_UTILITY_SAFE - if (!m_subMesh) - { - NazaraError("Invalid subMesh"); ///TODO: Unexpected - return; - } - #endif - } - - virtual ~SubMeshVertexMapper() noexcept - { - m_subMesh->Finish(); - } - - protected: - NzSubMeshRef m_subMesh; - }; - - class SkeletalMeshVertexMapper : public SubMeshVertexMapper - { - public: - SkeletalMeshVertexMapper(NzSkeletalMesh* subMesh) : - SubMeshVertexMapper(subMesh), - m_mesh(subMesh) - { - m_vertices = reinterpret_cast(m_mesh->GetBindPoseBuffer()); - } - - virtual ~SkeletalMeshVertexMapper() noexcept - { - } - - NzVector3f GetNormal(unsigned int i) const - { - return m_vertices[i].normal; - } - - NzVector3f GetPosition(unsigned int i) const - { - return m_vertices[i].position; - } - - NzVector3f GetTangent(unsigned int i) const - { - return m_vertices[i].tangent; - } - - NzVector2f GetTexCoords(unsigned int i) const - { - return m_vertices[i].uv; - } - - unsigned int GetVertexCount() const - { - return m_mesh->GetVertexCount(); - } - - void SetNormal(unsigned int i, const NzVector3f& normal) - { - m_vertices[i].normal = normal; - } - - void SetPosition(unsigned int i, const NzVector3f& position) - { - m_vertices[i].position = position; - } - - void SetTangent(unsigned int i, const NzVector3f& tangent) - { - m_vertices[i].tangent = tangent; - } - - void SetTexCoords(unsigned int i, const NzVector2f& texCoords) - { - m_vertices[i].uv = texCoords; - } - - private: - NzMeshVertex* m_vertices; - NzSkeletalMesh* m_mesh; - }; - - class StaticMeshVertexMapper : public SubMeshVertexMapper - { - public: - StaticMeshVertexMapper(NzStaticMesh* subMesh) : - SubMeshVertexMapper(subMesh), - m_vertexMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadWrite) - { - m_vertices = reinterpret_cast(m_vertexMapper.GetPointer()); - } - - virtual ~StaticMeshVertexMapper() noexcept - { - } - - NzVector3f GetNormal(unsigned int i) const - { - return m_vertices[i].normal; - } - - NzVector3f GetPosition(unsigned int i) const - { - return m_vertices[i].position; - } - - NzVector3f GetTangent(unsigned int i) const - { - return m_vertices[i].tangent; - } - - NzVector2f GetTexCoords(unsigned int i) const - { - return m_vertices[i].uv; - } - - unsigned int GetVertexCount() const - { - return m_vertexMapper.GetBuffer()->GetVertexCount(); - } - - void SetNormal(unsigned int i, const NzVector3f& normal) - { - m_vertices[i].normal = normal; - } - - void SetPosition(unsigned int i, const NzVector3f& position) - { - m_vertices[i].position = position; - } - - void SetTangent(unsigned int i, const NzVector3f& tangent) - { - m_vertices[i].tangent = tangent; - } - - void SetTexCoords(unsigned int i, const NzVector2f& texCoords) - { - m_vertices[i].uv = texCoords; - } - - private: - NzBufferMapper m_vertexMapper; - NzMeshVertex* m_vertices; - }; -} - NzVertexMapper::NzVertexMapper(NzSubMesh* subMesh) { + #ifdef NAZARA_DEBUG + m_vertices = nullptr; // Pour détecter les erreurs + #endif + switch (subMesh->GetAnimationType()) { case nzAnimationType_Skeletal: - m_impl = new SkeletalMeshVertexMapper(static_cast(subMesh)); + m_vertices = reinterpret_cast(static_cast(subMesh)->GetBindPoseBuffer()); break; case nzAnimationType_Static: - m_impl = new StaticMeshVertexMapper(static_cast(subMesh)); + if (!m_mapper.Map(static_cast(subMesh)->GetVertexBuffer(), nzBufferAccess_ReadWrite)) + NazaraError("Failed to map buffer"); ///TODO: Unexpected + + m_vertices = reinterpret_cast(m_mapper.GetPointer()); break; } #ifdef NAZARA_DEBUG - if (!m_impl) - NazaraInternalError("No impl"); ///TODO: Internal, Unexpected + if (!m_vertices) + NazaraInternalError("No vertices"); ///TODO: Internal, Unexpected #endif + + m_vertexCount = subMesh->GetVertexCount(); } -NzVertexMapper::~NzVertexMapper() -{ - delete m_impl; -} +NzVertexMapper::~NzVertexMapper() = default; NzVector3f NzVertexMapper::GetNormal(unsigned int i) const { - return m_impl->GetNormal(i); + #if NAZARA_UTILITY_SAFE + if (i >= m_vertexCount) + { + + } + #endif + + return m_vertices[i].normal; } NzVector3f NzVertexMapper::GetPosition(unsigned int i) const { - return m_impl->GetPosition(i); + return m_vertices[i].position; } NzVector3f NzVertexMapper::GetTangent(unsigned int i) const { - return m_impl->GetTangent(i); + return m_vertices[i].tangent; } -NzVector2f NzVertexMapper::GetTexCoords(unsigned int i) const +NzVector2f NzVertexMapper::GetTexCoord(unsigned int i) const { - return m_impl->GetTexCoords(i); + return m_vertices[i].uv; } unsigned int NzVertexMapper::GetVertexCount() { - return m_impl->GetVertexCount(); + return m_vertexCount; } void NzVertexMapper::SetNormal(unsigned int i, const NzVector3f& normal) { - m_impl->SetNormal(i, normal); + m_vertices[i].normal = normal; } void NzVertexMapper::SetPosition(unsigned int i, const NzVector3f& position) { - m_impl->SetPosition(i, position); + m_vertices[i].position = position; } void NzVertexMapper::SetTangent(unsigned int i, const NzVector3f& tangent) { - m_impl->SetTangent(i, tangent); + m_vertices[i].tangent = tangent; } -void NzVertexMapper::SetTexCoords(unsigned int i, const NzVector2f& texCoords) +void NzVertexMapper::SetTexCoord(unsigned int i, const NzVector2f& texCoord) { - m_impl->SetTexCoords(i, texCoords); + m_vertices[i].uv = texCoord; } void NzVertexMapper::Unmap() { - if (m_impl) - { - delete m_impl; - m_impl = nullptr; - } + m_mapper.Unmap(); }