/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" #include "model/skinning.h" #include #include #include namespace skinning { template vertex_weights unpack(std::array const &Joints, std::array const &Weights) { vertex_weights result; result.joints = Joints; auto total{0.f}; for (std::size_t idx = 0; idx < 4; ++idx) { result.weights[idx] = static_cast(Weights[idx]) / static_cast(std::numeric_limits::max()); total += result.weights[idx]; } if (total <= 0.f) { result.joints = {}; result.weights = {1.f, 0.f, 0.f, 0.f}; return result; } // the weights of the file add up to the largest value of the type; the rounding of the division is made up for here for (auto &weight : result.weights) { weight /= total; } return result; } template vertex_weights unpack(std::array const &, std::array const &); template vertex_weights unpack(std::array const &, std::array const &); std::vector palette(glm::mat4 const &Mesh, std::span const Joints, std::span const InverseBind) { auto const tomesh{glm::inverse(Mesh)}; std::vector result; result.reserve(Joints.size()); for (std::size_t idx = 0; idx < Joints.size(); ++idx) { auto const inversebind{idx < InverseBind.size() ? InverseBind[idx] : glm::mat4{1.f}}; result.emplace_back(tomesh * Joints[idx] * inversebind); } return result; } bool rest_pose(std::span const Palette, float const Tolerance, float const Scale) { glm::mat4 const identity{1.f}; // the translation is rounded in proportion to the distances the palette was made of auto const translation{Tolerance * std::max(1.f, Scale)}; return std::ranges::all_of(Palette, [&identity, Tolerance, translation](glm::mat4 const &Matrix) { for (int column = 0; column < 4; ++column) { auto const limit{column == 3 ? translation : Tolerance}; for (int row = 0; row < 4; ++row) { if (std::abs(Matrix[column][row] - identity[column][row]) > limit) { return false; } } } return true; }); } float palette_scale(glm::mat4 const &Mesh, std::span const Joints) { auto const reach{[](glm::mat4 const &Matrix) { return std::max({std::abs(Matrix[3][0]), std::abs(Matrix[3][1]), std::abs(Matrix[3][2])}); }}; auto scale{reach(Mesh)}; for (auto const &joint : Joints) { scale = std::max(scale, reach(joint)); } return scale; } namespace { glm::mat4 blended(vertex_weights const &Weights, std::span const Palette) { glm::mat4 result{0.f}; for (std::size_t idx = 0; idx < 4; ++idx) { auto const weight{Weights.weights[idx]}; if (weight <= 0.f) { continue; } auto const joint{static_cast(Weights.joints[idx])}; result += weight * (joint < Palette.size() ? Palette[joint] : glm::mat4{1.f}); } return result; } glm::vec3 direction(glm::mat3 const &Rotation, glm::vec3 const &Vector) { auto const moved{Rotation * Vector}; auto const length{glm::length(moved)}; return length > 0.f ? moved / length : 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, 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 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 void skin_vertices(std::span const Rest, std::span const Weights, std::span const Palette, std::span const Out) { auto const count{std::min(Rest.size(), Out.size())}; for (std::size_t idx = 0; idx < count; ++idx) { auto const &rest{Rest[idx]}; auto &out{Out[idx]}; out = rest; if (idx >= Weights.size()) { continue; } auto const matrix{blended(Weights[idx], Palette)}; glm::mat3 const rotation{matrix}; out.position = glm::vec3{matrix * glm::vec4{rest.position, 1.f}}; 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}; } } } // namespace skinning