Reworked VertexMapper to make it way more generic

Will work with any attribute and vertex declaration


Former-commit-id: ca99734bd8a9c3e57c99b1cc338f03e79dda55f6
This commit is contained in:
Lynix 2014-07-08 11:21:41 +02:00
parent af1d2d9146
commit 70e1327071
3 changed files with 53 additions and 129 deletions

View File

@ -8,11 +8,13 @@
#define NAZARA_VERTEXMAPPER_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utility/BufferMapper.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/Mesh.hpp>
#include <Nazara/Utility/VertexDeclaration.hpp>
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<typename T> NzSparsePtr<T> GetAttributePtr(nzAttributeUsage attribute);
unsigned int GetVertexCount() const;
void Unmap();
private:
NzBufferMapper<NzVertexBuffer> m_mapper;
NzMeshVertex* m_vertices;
NzVertexDeclarationConstRef m_declaration;
unsigned int m_vertexCount;
};
#include <Nazara/Utility/VertexMapper.inl>
#endif // NAZARA_VERTEXMAPPER_HPP

View File

@ -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 <Nazara/Utility/Debug.hpp>
template <typename T>
NzSparsePtr<T> 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<T>(static_cast<nzUInt8*>(m_mapper.GetPointer()) + offset, m_declaration->GetStride());
}
else
{
NazaraError("Attribute 0x" + NzString::Number(attribute, 16) + " is not enabled");
return NzSparsePtr<T>();
}
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/VertexMapper.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Utility/BufferMapper.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/SkeletalMesh.hpp>
@ -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<NzMeshVertex*>(static_cast<NzSkeletalMesh*>(subMesh)->GetBindPoseBuffer());
{
NzSkeletalMesh* skeletalMesh = static_cast<NzSkeletalMesh*>(subMesh);
buffer = skeletalMesh->GetVertexBuffer();
break;
}
case nzAnimationType_Static:
if (!m_mapper.Map(static_cast<NzStaticMesh*>(subMesh)->GetVertexBuffer(), nzBufferAccess_ReadWrite))
NazaraError("Failed to map buffer"); ///TODO: Unexpected
m_vertices = reinterpret_cast<NzMeshVertex*>(m_mapper.GetPointer());
{
NzStaticMesh* staticMesh = static_cast<NzStaticMesh*>(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();