mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 05:57:08 +01:00
fix(model): a mirroring joint turns the handedness of the tangent frame too, so the bitangent mirrors with the surface
This commit is contained in:
+12
-4
@@ -119,10 +119,17 @@ glm::vec3 direction(glm::mat3 const &Rotation, glm::vec3 const &Vector)
|
||||
// what moves the normals: the inverse transpose of the rotation, here as its cofactors (the same direction, without the
|
||||
// division), with the sign of the determinant so a mirroring matrix keeps the normals facing out. the rotation itself would
|
||||
// tilt the normals of a part squashed by a scale
|
||||
glm::mat3 normal_matrix(glm::mat3 const &Rotation)
|
||||
glm::mat3 normal_matrix(glm::mat3 const &Rotation, bool const Mirrors)
|
||||
{
|
||||
glm::mat3 const cofactors{glm::cross(Rotation[1], Rotation[2]), glm::cross(Rotation[2], Rotation[0]), glm::cross(Rotation[0], Rotation[1])};
|
||||
return glm::dot(Rotation[0], cofactors[0]) < 0.f ? -cofactors : cofactors;
|
||||
return Mirrors ? -cofactors : cofactors;
|
||||
}
|
||||
|
||||
// the rotation turns the space inside out (a negative determinant): the normals take the sign, and so does the handedness of
|
||||
// the tangent frame, or the bitangent rebuilt from the normal and the tangent points the other way
|
||||
bool mirrors(glm::mat3 const &Rotation)
|
||||
{
|
||||
return glm::dot(Rotation[0], glm::cross(Rotation[1], Rotation[2])) < 0.f;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -142,8 +149,9 @@ void skin_vertices(std::span<gfx::basic_vertex const> const Rest, std::span<vert
|
||||
auto const matrix{blended(Weights[idx], Palette)};
|
||||
glm::mat3 const rotation{matrix};
|
||||
out.position = glm::vec3{matrix * glm::vec4{rest.position, 1.f}};
|
||||
out.normal = direction(normal_matrix(rotation), rest.normal);
|
||||
out.tangent = glm::vec4{direction(rotation, glm::vec3{rest.tangent}), rest.tangent.w};
|
||||
auto const mirrored{mirrors(rotation)};
|
||||
out.normal = direction(normal_matrix(rotation, mirrored), rest.normal);
|
||||
out.tangent = glm::vec4{direction(rotation, glm::vec3{rest.tangent}), mirrored ? -rest.tangent.w : rest.tangent.w};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -158,10 +158,13 @@ TEST_CASE("skinning: the normals of a part scaled unevenly stay square to its su
|
||||
std::vector<gfx::basic_vertex> out(1);
|
||||
skinning::skin_vertices(rest, weights, palette, out);
|
||||
CHECK(close_to(out[0].normal, glm::normalize(glm::vec3{0.5f, 1.f, 0.f})));
|
||||
// a mirroring joint keeps the normal facing out
|
||||
CHECK(out[0].tangent.w == doctest::Approx(vertex.tangent.w));
|
||||
// a mirroring joint keeps the normal facing out, and turns the handedness of the tangent frame, so the bitangent rebuilt
|
||||
// from the normal and the tangent mirrors with the surface
|
||||
std::vector<glm::mat4> const mirror{glm::scale(glm::mat4{1.f}, glm::vec3{-1.f, 1.f, 1.f})};
|
||||
skinning::skin_vertices(rest, weights, mirror, out);
|
||||
CHECK(close_to(out[0].normal, glm::normalize(glm::vec3{-1.f, 1.f, 0.f})));
|
||||
CHECK(out[0].tangent.w == doctest::Approx(-vertex.tangent.w));
|
||||
}
|
||||
|
||||
TEST_CASE("skinning: a joint turned moves its vertices, blended by the weights") {
|
||||
|
||||
Reference in New Issue
Block a user