From 70e13270711fe4d2f5b5071689e5dd74354135fa Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 8 Jul 2014 11:21:41 +0200 Subject: [PATCH] Reworked VertexMapper to make it way more generic Will work with any attribute and vertex declaration Former-commit-id: ca99734bd8a9c3e57c99b1cc338f03e79dda55f6 --- include/Nazara/Utility/VertexMapper.hpp | 18 ++-- include/Nazara/Utility/VertexMapper.inl | 27 +++++ src/Nazara/Utility/VertexMapper.cpp | 137 ++++-------------------- 3 files changed, 53 insertions(+), 129 deletions(-) create mode 100644 include/Nazara/Utility/VertexMapper.inl diff --git a/include/Nazara/Utility/VertexMapper.hpp b/include/Nazara/Utility/VertexMapper.hpp index 99df2576d..d6a3845c0 100644 --- a/include/Nazara/Utility/VertexMapper.hpp +++ b/include/Nazara/Utility/VertexMapper.hpp @@ -8,11 +8,13 @@ #define NAZARA_VERTEXMAPPER_HPP #include +#include #include #include #include #include #include +#include class NzSubMesh; @@ -22,23 +24,17 @@ class NAZARA_API NzVertexMapper NzVertexMapper(NzSubMesh* subMesh); ~NzVertexMapper(); - NzVector3f GetNormal(unsigned int i) const; - NzVector3f GetPosition(unsigned int i) const; - NzVector3f GetTangent(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 SetTexCoord(unsigned int i, const NzVector2f& texCoord); + template NzSparsePtr GetAttributePtr(nzAttributeUsage attribute); + unsigned int GetVertexCount() const; void Unmap(); private: NzBufferMapper m_mapper; - NzMeshVertex* m_vertices; + NzVertexDeclarationConstRef m_declaration; unsigned int m_vertexCount; }; +#include + #endif // NAZARA_VERTEXMAPPER_HPP diff --git a/include/Nazara/Utility/VertexMapper.inl b/include/Nazara/Utility/VertexMapper.inl new file mode 100644 index 000000000..d66f5bd93 --- /dev/null +++ b/include/Nazara/Utility/VertexMapper.inl @@ -0,0 +1,27 @@ +// Copyright (C) 2014 Jérôme Leclercq +// This file is part of the "Nazara Engine - Utility module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include + +template +NzSparsePtr NzVertexMapper::GetAttributePtr(nzAttributeUsage attribute) +{ + bool enabled; + nzAttributeType type; + unsigned int offset; + m_declaration->GetAttribute(attribute, &enabled, &type, &offset); + + if (enabled) + { + ///TODO: Vérifier le rapport entre le type de l'attribut et le type template ? + return NzSparsePtr(static_cast(m_mapper.GetPointer()) + offset, m_declaration->GetStride()); + } + else + { + NazaraError("Attribute 0x" + NzString::Number(attribute, 16) + " is not enabled"); + return NzSparsePtr(); + } +} + +#include diff --git a/src/Nazara/Utility/VertexMapper.cpp b/src/Nazara/Utility/VertexMapper.cpp index 1824e13ba..08f257f2c 100644 --- a/src/Nazara/Utility/VertexMapper.cpp +++ b/src/Nazara/Utility/VertexMapper.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include @@ -12,143 +13,43 @@ NzVertexMapper::NzVertexMapper(NzSubMesh* subMesh) { - #ifdef NAZARA_DEBUG - m_vertices = nullptr; // Pour détecter les erreurs - #endif - + NzErrorFlags flags(nzErrorFlag_ThrowException); + NzVertexBuffer* buffer = nullptr; switch (subMesh->GetAnimationType()) { case nzAnimationType_Skeletal: - m_vertices = reinterpret_cast(static_cast(subMesh)->GetBindPoseBuffer()); + { + NzSkeletalMesh* skeletalMesh = static_cast(subMesh); + buffer = skeletalMesh->GetVertexBuffer(); break; + } case nzAnimationType_Static: - if (!m_mapper.Map(static_cast(subMesh)->GetVertexBuffer(), nzBufferAccess_ReadWrite)) - NazaraError("Failed to map buffer"); ///TODO: Unexpected - - m_vertices = reinterpret_cast(m_mapper.GetPointer()); + { + NzStaticMesh* staticMesh = static_cast(subMesh); + buffer = staticMesh->GetVertexBuffer(); break; + } } - #ifdef NAZARA_DEBUG - if (!m_vertices) - NazaraInternalError("No vertices"); ///TODO: Internal, Unexpected - #endif + if (!buffer) + { + NazaraInternalError("Animation type not handled (0x" + NzString::Number(subMesh->GetAnimationType(), 16) + ')'); + } + m_declaration = buffer->GetVertexDeclaration(); m_vertexCount = subMesh->GetVertexCount(); + + m_mapper.Map(buffer, nzBufferAccess_ReadWrite); } NzVertexMapper::~NzVertexMapper() = default; -NzVector3f NzVertexMapper::GetNormal(unsigned int i) const -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return NzVector3f(); - } - #endif - - return m_vertices[i].normal; -} - -NzVector3f NzVertexMapper::GetPosition(unsigned int i) const -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return NzVector3f(); - } - #endif - - return m_vertices[i].position; -} - -NzVector3f NzVertexMapper::GetTangent(unsigned int i) const -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return NzVector3f(); - } - #endif - - return m_vertices[i].tangent; -} - -NzVector2f NzVertexMapper::GetTexCoord(unsigned int i) const -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return NzVector2f(); - } - #endif - - return m_vertices[i].uv; -} - -unsigned int NzVertexMapper::GetVertexCount() +unsigned int NzVertexMapper::GetVertexCount() const { return m_vertexCount; } -void NzVertexMapper::SetNormal(unsigned int i, const NzVector3f& normal) -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return; - } - #endif - - m_vertices[i].normal = normal; -} - -void NzVertexMapper::SetPosition(unsigned int i, const NzVector3f& position) -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return; - } - #endif - - m_vertices[i].position = position; -} - -void NzVertexMapper::SetTangent(unsigned int i, const NzVector3f& tangent) -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return; - } - #endif - - m_vertices[i].tangent = tangent; -} - -void NzVertexMapper::SetTexCoord(unsigned int i, const NzVector2f& texCoord) -{ - #if NAZARA_UTILITY_SAFE - if (i >= m_vertexCount) - { - NazaraError("Vertex index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_vertexCount) + ')'); - return; - } - #endif - - m_vertices[i].uv = texCoord; -} - void NzVertexMapper::Unmap() { m_mapper.Unmap();