diff --git a/model/skinning.cpp b/model/skinning.cpp index 4e346b8f4..3333072a7 100644 --- a/model/skinning.cpp +++ b/model/skinning.cpp @@ -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 const Rest, std::span 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 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") {