18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 21:07:06 +01:00
Files
maszyna/model/skinning.cpp
T

159 lines
5.0 KiB
C++

/*
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 <algorithm>
#include <cmath>
#include <limits>
namespace skinning
{
template <typename Weight> vertex_weights unpack(std::array<std::uint16_t, 4> const &Joints, std::array<Weight, 4> 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<float>(Weights[idx]) / static_cast<float>(std::numeric_limits<Weight>::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::uint8_t>(std::array<std::uint16_t, 4> const &, std::array<std::uint8_t, 4> const &);
template vertex_weights unpack<std::uint16_t>(std::array<std::uint16_t, 4> const &, std::array<std::uint16_t, 4> const &);
std::vector<glm::mat4> palette(glm::mat4 const &Mesh, std::span<glm::mat4 const> const Joints, std::span<glm::mat4 const> const InverseBind)
{
auto const tomesh{glm::inverse(Mesh)};
std::vector<glm::mat4> 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<glm::mat4 const> 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<glm::mat4 const> 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<glm::mat4 const> 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<std::size_t>(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<gfx::basic_vertex const> const Rest, std::span<vertex_weights const> const Weights, std::span<glm::mat4 const> const Palette, std::span<gfx::basic_vertex> 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