Refactored TriangleIterator/VertexMapper

Former-commit-id: b8d4fc79142b36519ebe1879276415fe30970d3e
This commit is contained in:
Lynix 2013-03-29 14:42:55 +01:00
parent 5048705702
commit b86332a51b
4 changed files with 46 additions and 209 deletions

View File

@ -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();

View File

@ -10,12 +10,12 @@
#include <Nazara/Prerequesites.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>
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<NzVertexBuffer> m_mapper;
NzMeshVertex* m_vertices;
unsigned int m_vertexCount;
};
#endif // NAZARA_VERTEXMAPPER_HPP

View File

@ -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()

View File

@ -5,259 +5,94 @@
#include <Nazara/Utility/VertexMapper.hpp>
#include <Nazara/Utility/BufferMapper.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Mesh.hpp> // NzMeshVertex
#include <Nazara/Utility/SkeletalMesh.hpp>
#include <Nazara/Utility/StaticMesh.hpp>
#include <Nazara/Utility/SubMesh.hpp>
#include <Nazara/Utility/Debug.hpp>
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<NzMeshVertex*>(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<NzMeshVertex*>(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<NzVertexBuffer> 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<NzSkeletalMesh*>(subMesh));
m_vertices = reinterpret_cast<NzMeshVertex*>(static_cast<NzSkeletalMesh*>(subMesh)->GetBindPoseBuffer());
break;
case nzAnimationType_Static:
m_impl = new StaticMeshVertexMapper(static_cast<NzStaticMesh*>(subMesh));
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());
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();
}