Utility: Make mesh loader use the parameters vertex declaration
This commit is contained in:
@@ -1164,6 +1164,6 @@ namespace Nz
|
||||
|
||||
if (vertexPointers.tangentPtr)
|
||||
*vertexPointers.tangentPtr++ = matrix.Transform(*vertexPointers.tangentPtr, 0.f) / scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <Nazara/Utility/VertexMapper.hpp>
|
||||
#include <Nazara/Utility/Formats/MD5MeshParser.hpp>
|
||||
#include <memory>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
@@ -128,6 +129,7 @@ namespace Nz
|
||||
|
||||
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_WriteOnly);
|
||||
SkeletalMeshVertex* vertices = static_cast<SkeletalMeshVertex*>(vertexMapper.GetPointer());
|
||||
|
||||
for (const MD5MeshParser::Vertex& vertex : md5Mesh.vertices)
|
||||
{
|
||||
// Skinning MD5 (Formule d'Id Tech)
|
||||
@@ -254,13 +256,15 @@ namespace Nz
|
||||
indexMapper.Unmap();
|
||||
|
||||
// Vertex buffer
|
||||
VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), UInt32(vertexCount), parameters.storage, parameters.vertexBufferFlags);
|
||||
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_WriteOnly);
|
||||
VertexBufferRef vertexBuffer = VertexBuffer::New(parameters.vertexDeclaration, UInt32(vertexCount), parameters.storage, parameters.vertexBufferFlags);
|
||||
|
||||
VertexMapper vertexMapper(vertexBuffer, BufferAccess_DiscardAndWrite);
|
||||
|
||||
auto posPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
|
||||
|
||||
MeshVertex* vertices = static_cast<MeshVertex*>(vertexMapper.GetPointer());
|
||||
for (const MD5MeshParser::Vertex& md5Vertex : md5Mesh.vertices)
|
||||
{
|
||||
// Skinning MD5 (Formule d'Id Tech)
|
||||
// Id Tech MD5 skinning
|
||||
Vector3f finalPos(Vector3f::Zero());
|
||||
for (unsigned int j = 0; j < md5Vertex.weightCount; ++j)
|
||||
{
|
||||
@@ -271,9 +275,13 @@ namespace Nz
|
||||
}
|
||||
|
||||
// On retourne le modèle dans le bon sens
|
||||
vertices->position = matrix * finalPos;
|
||||
vertices->uv.Set(parameters.texCoordOffset + md5Vertex.uv * parameters.texCoordScale);
|
||||
vertices++;
|
||||
*posPtr++ = matrix * finalPos;
|
||||
}
|
||||
|
||||
if (auto uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord))
|
||||
{
|
||||
for (const MD5MeshParser::Vertex& md5Vertex : md5Mesh.vertices)
|
||||
*uvPtr++ = parameters.texCoordOffset + md5Vertex.uv * parameters.texCoordScale;
|
||||
}
|
||||
|
||||
vertexMapper.Unmap();
|
||||
@@ -287,9 +295,16 @@ namespace Nz
|
||||
|
||||
subMesh->SetIndexBuffer(indexBuffer);
|
||||
subMesh->GenerateAABB();
|
||||
subMesh->GenerateNormalsAndTangents();
|
||||
subMesh->SetMaterialIndex(i);
|
||||
|
||||
if (parameters.vertexDeclaration->HasComponentOfType<Vector3f>(VertexComponent_Normal))
|
||||
{
|
||||
if (parameters.vertexDeclaration->HasComponentOfType<Vector3f>(VertexComponent_Tangent))
|
||||
subMesh->GenerateNormalsAndTangents();
|
||||
else
|
||||
subMesh->GenerateNormals();
|
||||
}
|
||||
|
||||
mesh->AddSubMesh(subMesh);
|
||||
|
||||
// Material
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
#include <Nazara/Utility/Formats/OBJLoader.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Utility/BufferMapper.hpp>
|
||||
#include <Nazara/Utility/IndexMapper.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/MTLParser.hpp>
|
||||
#include <Nazara/Utility/Formats/OBJParser.hpp>
|
||||
#include <limits>
|
||||
@@ -232,7 +232,7 @@ namespace Nz
|
||||
|
||||
// Création des buffers
|
||||
IndexBufferRef indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), UInt32(indices.size()), parameters.storage, parameters.indexBufferFlags);
|
||||
VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), UInt32(vertexCount), parameters.storage, parameters.vertexBufferFlags);
|
||||
VertexBufferRef vertexBuffer = VertexBuffer::New(parameters.vertexDeclaration, UInt32(vertexCount), parameters.storage, parameters.vertexBufferFlags);
|
||||
|
||||
// Remplissage des indices
|
||||
IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly);
|
||||
@@ -250,30 +250,45 @@ namespace Nz
|
||||
|
||||
bool hasNormals = true;
|
||||
bool hasTexCoords = true;
|
||||
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_WriteOnly);
|
||||
MeshVertex* meshVertices = static_cast<MeshVertex*>(vertexMapper.GetPointer());
|
||||
|
||||
VertexMapper vertexMapper(vertexBuffer, BufferAccess_DiscardAndWrite);
|
||||
|
||||
auto normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal);
|
||||
auto posPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
|
||||
auto uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord);
|
||||
|
||||
if (!normalPtr)
|
||||
hasNormals = false;
|
||||
|
||||
if (!uvPtr)
|
||||
hasTexCoords = false;
|
||||
|
||||
for (auto& vertexPair : vertices)
|
||||
{
|
||||
const OBJParser::FaceVertex& vertexIndices = vertexPair.first;
|
||||
unsigned int index = vertexPair.second;
|
||||
|
||||
MeshVertex& vertex = meshVertices[index];
|
||||
|
||||
const Vector4f& vec = positions[vertexIndices.position-1];
|
||||
vertex.position = Vector3f(parameters.matrix * vec);
|
||||
posPtr[index] = Vector3f(parameters.matrix * vec);
|
||||
|
||||
if (vertexIndices.normal > 0)
|
||||
vertex.normal = normalMatrix.Transform(normals[vertexIndices.normal - 1], 0.f);
|
||||
else
|
||||
hasNormals = false;
|
||||
|
||||
if (vertexIndices.texCoord > 0)
|
||||
if (hasNormals)
|
||||
{
|
||||
Vector2f uv = Vector2f(texCoords[vertexIndices.texCoord - 1]);
|
||||
vertex.uv.Set(parameters.texCoordOffset + uv * parameters.texCoordScale);
|
||||
if (vertexIndices.normal > 0)
|
||||
normalPtr[index] = normalMatrix.Transform(normals[vertexIndices.normal - 1], 0.f);
|
||||
else
|
||||
hasNormals = false;
|
||||
}
|
||||
|
||||
if (hasTexCoords)
|
||||
{
|
||||
if (vertexIndices.texCoord > 0)
|
||||
{
|
||||
Vector2f uv = Vector2f(texCoords[vertexIndices.texCoord - 1]);
|
||||
uvPtr[index] = Vector2f(parameters.texCoordOffset + uv * parameters.texCoordScale);
|
||||
}
|
||||
else
|
||||
hasTexCoords = false;
|
||||
}
|
||||
else
|
||||
hasTexCoords = false;
|
||||
}
|
||||
|
||||
vertexMapper.Unmap();
|
||||
@@ -298,7 +313,7 @@ namespace Nz
|
||||
subMesh->GenerateTangents();
|
||||
else if (hasTexCoords)
|
||||
subMesh->GenerateNormalsAndTangents();
|
||||
else
|
||||
else if (normalPtr)
|
||||
subMesh->GenerateNormals();
|
||||
|
||||
mesh->AddSubMesh(meshes[i].name + '_' + materials[meshes[i].material], subMesh);
|
||||
|
||||
@@ -47,6 +47,12 @@ namespace Nz
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!vertexDeclaration->HasComponent(VertexComponent_Position))
|
||||
{
|
||||
NazaraError("Vertex declaration must contains a vertex position");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -54,7 +60,7 @@ namespace Nz
|
||||
{
|
||||
MeshImpl()
|
||||
{
|
||||
materialData.resize(1); // Un matériau par défaut
|
||||
materialData.resize(1); // One material by default
|
||||
}
|
||||
|
||||
std::unordered_map<String, UInt32> subMeshMap;
|
||||
@@ -62,10 +68,10 @@ namespace Nz
|
||||
std::vector<SubMeshRef> subMeshes;
|
||||
AnimationType animationType;
|
||||
Boxf aabb;
|
||||
Skeleton skeleton; // Uniquement pour les meshs squelettiques
|
||||
Skeleton skeleton; // Only used by skeletal meshes
|
||||
String animationPath;
|
||||
bool aabbUpdated = false;
|
||||
UInt32 jointCount; // Uniquement pour les meshs squelettiques
|
||||
UInt32 jointCount; // Only used by skeletal meshes
|
||||
};
|
||||
|
||||
Mesh::~Mesh()
|
||||
|
||||
@@ -28,12 +28,14 @@ namespace Nz
|
||||
void SubMesh::GenerateNormals()
|
||||
{
|
||||
VertexMapper mapper(this);
|
||||
unsigned int vertexCount = GetVertexCount();
|
||||
UInt32 vertexCount = mapper.GetVertexCount();
|
||||
|
||||
SparsePtr<Vector3f> normals = mapper.GetComponentPtr<Vector3f>(VertexComponent_Normal);
|
||||
SparsePtr<Vector3f> positions = mapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
|
||||
if (!normals || !positions)
|
||||
return;
|
||||
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
for (UInt32 i = 0; i < vertexCount; ++i)
|
||||
normals[i].MakeZero();
|
||||
|
||||
TriangleIterator iterator(this);
|
||||
@@ -52,21 +54,23 @@ namespace Nz
|
||||
}
|
||||
while (iterator.Advance());
|
||||
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
for (UInt32 i = 0; i < vertexCount; ++i)
|
||||
normals[i].Normalize();
|
||||
}
|
||||
|
||||
void SubMesh::GenerateNormalsAndTangents()
|
||||
{
|
||||
VertexMapper mapper(this);
|
||||
unsigned int vertexCount = GetVertexCount();
|
||||
UInt32 vertexCount = mapper.GetVertexCount();
|
||||
|
||||
SparsePtr<Vector3f> normals = mapper.GetComponentPtr<Vector3f>(VertexComponent_Normal);
|
||||
SparsePtr<Vector3f> positions = mapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
|
||||
SparsePtr<Vector3f> tangents = mapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent);
|
||||
SparsePtr<Vector2f> texCoords = mapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord);
|
||||
if (!normals || !positions || !tangents || !texCoords)
|
||||
return;
|
||||
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
for (UInt32 i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
normals[i].MakeZero();
|
||||
tangents[i].MakeZero();
|
||||
@@ -105,7 +109,7 @@ namespace Nz
|
||||
}
|
||||
while (iterator.Advance());
|
||||
|
||||
for (unsigned int i = 0; i < vertexCount; ++i)
|
||||
for (UInt32 i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
normals[i].Normalize();
|
||||
tangents[i].Normalize();
|
||||
@@ -120,6 +124,8 @@ namespace Nz
|
||||
SparsePtr<Vector3f> positions = mapper.GetComponentPtr<Vector3f>(VertexComponent_Position);
|
||||
SparsePtr<Vector3f> tangents = mapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent);
|
||||
SparsePtr<Vector2f> texCoords = mapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord);
|
||||
if (!normals || !positions || !tangents || !texCoords)
|
||||
return;
|
||||
|
||||
TriangleIterator iterator(this);
|
||||
do
|
||||
|
||||
Reference in New Issue
Block a user