Utility/MeshLoader: Fix pre-transformation matrix not affecting normal and tangents in some cases (Fix #131)

This commit is contained in:
Lynix
2017-08-30 15:58:19 +02:00
parent 4df9c94eb0
commit c48d752ad4
3 changed files with 19 additions and 4 deletions

View File

@@ -202,6 +202,12 @@ bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters)
indexMapper.Unmap();
// Vertex buffer
// Make sure the normal/tangent matrix won't rescale our vectors
Nz::Matrix4f normalTangentMatrix = parameters.matrix;
if (normalTangentMatrix.HasScale())
normalTangentMatrix.ApplyScale(1.f / normalTangentMatrix.GetScale());
VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), vertexCount, parameters.storage, 0);
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_WriteOnly);
@@ -214,8 +220,8 @@ bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters)
aiVector3D uv = (iMesh->HasTextureCoords(0)) ? iMesh->mTextureCoords[0][j] : aiVector3D(0.f);
vertex->position = parameters.matrix * Vector3f(position.x, position.y, position.z);
vertex->normal.Set(normal.x, normal.y, normal.z);
vertex->tangent.Set(tangent.x, tangent.y, tangent.z);
vertex->normal = normalTangentMatrix.Transform({normal.x, normal.y, normal.z}, 0.f);
vertex->tangent = normalTangentMatrix.Transform({tangent.x, tangent.y, tangent.z}, 0.f);
vertex->uv.Set(parameters.texCoordOffset + Vector2f(uv.x, uv.y) * parameters.texCoordScale);
vertex++;
}