Utility: Make mesh loader use the parameters vertex declaration

This commit is contained in:
Lynix
2017-10-21 04:51:05 +02:00
parent 2951b7811e
commit 9daadb73bc
9 changed files with 181 additions and 71 deletions

View File

@@ -7,11 +7,12 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Stream.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Utility/BufferMapper.hpp>
#include <Nazara/Utility/MaterialData.hpp>
#include <Nazara/Utility/Mesh.hpp>
#include <Nazara/Utility/StaticMesh.hpp>
#include <Nazara/Utility/VertexMapper.hpp>
#include <Nazara/Utility/Formats/MD2Constants.hpp>
#include <cassert>
#include <memory>
#include <Nazara/Utility/Debug.hpp>
@@ -156,7 +157,7 @@ namespace Nz
}
#endif
VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), header.num_vertices, parameters.storage, parameters.vertexBufferFlags);
VertexBufferRef vertexBuffer = VertexBuffer::New(parameters.vertexDeclaration, header.num_vertices, parameters.storage, parameters.vertexBufferFlags);
StaticMeshRef subMesh = StaticMesh::New(mesh);
if (!subMesh->Create(vertexBuffer))
{
@@ -189,23 +190,26 @@ namespace Nz
scale *= ScaleAdjust;
translate *= ScaleAdjust;
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_DiscardAndWrite);
MeshVertex* vertex = static_cast<MeshVertex*>(vertexMapper.GetPointer());
VertexMapper vertexMapper(vertexBuffer, BufferAccess_DiscardAndWrite);
// Loading texture coordinates
const unsigned int indexFix[3] = {0, 2, 1};
Vector2f invSkinSize(1.f / header.skinwidth, 1.f / header.skinheight);
for (unsigned int i = 0; i < header.num_tris; ++i)
if (auto uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord))
{
for (unsigned int j = 0; j < 3; ++j)
const unsigned int indexFix[3] = {0, 2, 1};
Vector2f invSkinSize(1.f / header.skinwidth, 1.f / header.skinheight);
for (unsigned int i = 0; i < header.num_tris; ++i)
{
const unsigned int fixedIndex = indexFix[j]; //< Reverse winding order
for (unsigned int j = 0; j < 3; ++j)
{
const unsigned int fixedIndex = indexFix[j]; //< Reverse winding order
const MD2_TexCoord& texC = texCoords[triangles[i].texCoords[fixedIndex]];
Vector2f uv(texC.u, texC.v);
uv *= invSkinSize;
const MD2_TexCoord& texC = texCoords[triangles[i].texCoords[fixedIndex]];
Vector2f uv(texC.u, texC.v);
uv *= invSkinSize;
vertex[triangles[i].vertices[fixedIndex]].uv.Set(parameters.texCoordOffset + uv * parameters.texCoordScale);
uvPtr[triangles[i].vertices[fixedIndex]].Set(parameters.texCoordOffset + uv * parameters.texCoordScale);
}
}
}
@@ -216,18 +220,27 @@ namespace Nz
Nz::Matrix4f matrix = Matrix4f::Transform(translate, rotationQuat, scale);
matrix *= parameters.matrix;
Nz::Matrix4f normalMatrix = Matrix4f::Rotate(rotationQuat);
normalMatrix *= parameters.matrix;
if (auto normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal))
{
Nz::Matrix4f normalMatrix = Matrix4f::Rotate(rotationQuat);
normalMatrix *= parameters.matrix;
for (unsigned int v = 0; v < header.num_vertices; ++v)
{
const MD2_Vertex& vert = vertices[v];
*normalPtr++ = normalMatrix.Transform(md2Normals[vert.n], 0.f);
}
}
auto posPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
assert(posPtr);
for (unsigned int v = 0; v < header.num_vertices; ++v)
{
const MD2_Vertex& vert = vertices[v];
Vector3f position = matrix * Vector3f(vert.x, vert.y, vert.z);
vertex->position = position;
vertex->normal = normalMatrix.Transform(md2Normals[vert.n], 0.f);
vertex++;
*posPtr++ = matrix * Vector3f(vert.x, vert.y, vert.z);
}
vertexMapper.Unmap();
@@ -236,7 +249,9 @@ namespace Nz
subMesh->SetMaterialIndex(0);
subMesh->GenerateAABB();
subMesh->GenerateTangents();
if (parameters.vertexDeclaration->HasComponentOfType<Vector3f>(VertexComponent_Tangent))
subMesh->GenerateTangents();
mesh->AddSubMesh(subMesh);