mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 14:16:48 +01:00
c8068d8f3d
Stage 8 of the EU07M model format. A model can carry animation clips; each track of a clip sets the translation, the rotation or the scale of a node at a time of the clip, and the parts of a node without a track keep its TRS. - Format, five optional sections: ACLP (clips: name, duration, flags 0x1 loop and 0x2 autoplay, range of tracks), ATRK (tracks: node, part, interpolation step / linear / cubic, ranges of keys), ATIM (key times), AVEC (vectors; with cubic the in-tangent, value and out-tangent of glTF) and AROT (rotations in 8 bytes, "smallest three": the three smaller components as snorm16 scaled by sqrt(2) and the index of the largest; eu07m::encode_rotation / decode_rotation). The validator refuses a node out of range, keys out of their arrays, times which aren't numbers from 0 up, a cubic rotation and tracks out of the range of the clip. -eu07m dump lists the clips and their tracks. The current release doesn't read these sections; a file without them loads as before. - Sampling (model/clip.cpp): step, lerp / slerp the shorter way, the Hermite spline of glTF with tangents per second; the first and the last key held outside of them; a looped clip goes around its duration, another stops at its end. - In the game: the clips play by the simulation time, from TAnimModel::play_clip(name, speed) or, flagged autoplay, from the first time the instance is drawn. RaPrepare() samples them for the instance drawn and sets the transforms of the nodes (TSubModel::SetClipTransform(), the new animation type at_Clip, gated by the instance like the others), so a clip moving the joints of a skin bends the skinned parts. The nodes a clip moves are flagged animated at load, which keeps the model out of the instancing. stop_clips() stops them. - The editor self-test gets "clip <node> <name> [speed]". Not yet: a scenery event starting a clip, and the CHNL channels bound to the time of a clip. Checked: a skinned tube with an autoplay clip (the tip to 60 degrees and back in 2 s, looped) beside a still one, six screenshots in Vulkan and in GL 4.6 go from straight to fully bent while the other stays (time let run in the editor for the test only). Unit tests 1200/1200, 6 new ones for the packing, the interpolations, the time and the file. Not built with GCC here.
320 lines
12 KiB
C++
320 lines
12 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/eu07m/eu07m_build.h"
|
|
|
|
#include <glm/gtc/quaternion.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <numbers>
|
|
#include <unordered_map>
|
|
|
|
namespace eu07m {
|
|
|
|
namespace {
|
|
|
|
constexpr float snorm16_scale{32767.f};
|
|
constexpr float minimum_length{1e-12f};
|
|
|
|
glm::vec3 perpendicular(glm::vec3 const &Normal) {
|
|
// the axis least along the normal gives the steadiest cross product
|
|
auto const absolute{glm::abs(Normal)};
|
|
glm::vec3 axis{1.f, 0.f, 0.f};
|
|
if (absolute.y < absolute.x && absolute.y <= absolute.z)
|
|
axis = {0.f, 1.f, 0.f};
|
|
else if (absolute.z < absolute.x && absolute.z < absolute.y)
|
|
axis = {0.f, 0.f, 1.f};
|
|
return glm::normalize(glm::cross(axis, Normal));
|
|
}
|
|
|
|
std::int16_t pack_snorm16(float const Value) {
|
|
return static_cast<std::int16_t>(std::lround(std::clamp(Value, -1.f, 1.f) * snorm16_scale));
|
|
}
|
|
|
|
glm::mat4 compose(node_record const &Node) {
|
|
glm::quat const rotation{Node.rotation[3], Node.rotation[0], Node.rotation[1], Node.rotation[2]};
|
|
auto transform{glm::mat4_cast(glm::normalize(rotation))};
|
|
transform[0] *= Node.scale[0];
|
|
transform[1] *= Node.scale[1];
|
|
transform[2] *= Node.scale[2];
|
|
transform[3] = glm::vec4{Node.translation[0], Node.translation[1], Node.translation[2], 1.f};
|
|
return transform;
|
|
}
|
|
|
|
std::unordered_map<std::uint32_t, std::size_t> matrix_lookup(model const &Model) {
|
|
std::unordered_map<std::uint32_t, std::size_t> lookup;
|
|
for (std::size_t idx = 0; idx < Model.transforms.size(); ++idx)
|
|
lookup.try_emplace(Model.transforms[idx].node, idx);
|
|
return lookup;
|
|
}
|
|
|
|
glm::mat4 node_transform(model const &Model, std::uint32_t const Node, std::unordered_map<std::uint32_t, std::size_t> const &Matrices) {
|
|
auto const &node{Model.nodes[Node]};
|
|
if ((node.flags & node_flags::matrix) != 0) {
|
|
if (auto const lookup{Matrices.find(Node)}; lookup != Matrices.end()) {
|
|
glm::mat4 matrix;
|
|
std::memcpy(&matrix[0][0], Model.transforms[lookup->second].matrix.data(), sizeof(float) * 16);
|
|
return matrix;
|
|
}
|
|
}
|
|
return compose(node);
|
|
}
|
|
|
|
std::string lowercase(std::string_view const Text) {
|
|
std::string result{Text};
|
|
for (auto &character : result) {
|
|
if (character >= 'A' && character <= 'Z')
|
|
character = static_cast<char>(character - 'A' + 'a');
|
|
}
|
|
return result;
|
|
}
|
|
|
|
struct box {
|
|
glm::vec3 min{std::numeric_limits<float>::max()};
|
|
glm::vec3 max{std::numeric_limits<float>::lowest()};
|
|
bool empty() const {
|
|
return min.x > max.x;
|
|
}
|
|
void add(glm::vec3 const &Point) {
|
|
min = glm::min(min, Point);
|
|
max = glm::max(max, Point);
|
|
}
|
|
glm::vec3 centre() const {
|
|
return empty() ? glm::vec3{0.f} : (min + max) * 0.5f;
|
|
}
|
|
};
|
|
|
|
glm::vec3 to_vec3(position const &Position) {
|
|
return {Position[0], Position[1], Position[2]};
|
|
}
|
|
|
|
// the sphere around the centre of the box of the vertices, reaching the farthest of them
|
|
std::array<float, 4> sphere_of(model const &Model, std::size_t const First, std::size_t const Count, box &Box) {
|
|
for (auto idx = First; idx < First + Count; ++idx)
|
|
Box.add(to_vec3(Model.positions[idx]));
|
|
auto const centre{Box.centre()};
|
|
float radius{0.f};
|
|
for (auto idx = First; idx < First + Count; ++idx)
|
|
radius = std::max(radius, glm::length(to_vec3(Model.positions[idx]) - centre));
|
|
return {centre.x, centre.y, centre.z, radius};
|
|
}
|
|
|
|
bounds_record to_bounds(box const &Box, float const Radius) {
|
|
bounds_record bounds;
|
|
if (Box.empty())
|
|
return bounds;
|
|
auto const centre{Box.centre()};
|
|
bounds.min = {Box.min.x, Box.min.y, Box.min.z};
|
|
bounds.max = {Box.max.x, Box.max.y, Box.max.z};
|
|
bounds.sphere = {centre.x, centre.y, centre.z, Radius};
|
|
return bounds;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::array<std::int16_t, 4> encode_qtangent(tangent_frame const &Frame) {
|
|
auto const normal{glm::dot(Frame.normal, Frame.normal) > minimum_length ? glm::normalize(Frame.normal) : glm::vec3{0.f, 0.f, 1.f}};
|
|
auto tangent{Frame.tangent - normal * glm::dot(normal, Frame.tangent)};
|
|
tangent = glm::dot(tangent, tangent) > minimum_length ? glm::normalize(tangent) : perpendicular(normal);
|
|
auto const bitangent{glm::cross(normal, tangent)};
|
|
auto rotation{glm::normalize(glm::quat_cast(glm::mat3{tangent, bitangent, normal}))};
|
|
if (rotation.w < 0.f)
|
|
rotation = -rotation;
|
|
// w keeps its sign through the packing, the sign of the bitangent rides on it
|
|
constexpr float bias{1.f / snorm16_scale};
|
|
if (rotation.w < bias) {
|
|
auto const scale{std::sqrt(1.f - bias * bias) / std::max(glm::length(glm::vec3{rotation.x, rotation.y, rotation.z}), minimum_length)};
|
|
rotation = glm::quat{bias, rotation.x * scale, rotation.y * scale, rotation.z * scale};
|
|
}
|
|
if (Frame.handedness < 0.f)
|
|
rotation = -rotation;
|
|
return {pack_snorm16(rotation.x), pack_snorm16(rotation.y), pack_snorm16(rotation.z), pack_snorm16(rotation.w)};
|
|
}
|
|
|
|
packed_rotation encode_rotation(std::array<float, 4> const &Quaternion) {
|
|
auto quaternion{Quaternion};
|
|
auto const length{std::sqrt(quaternion[0] * quaternion[0] + quaternion[1] * quaternion[1] + quaternion[2] * quaternion[2] + quaternion[3] * quaternion[3])};
|
|
if (false == std::isfinite(length) || length <= 0.f)
|
|
return {};
|
|
std::size_t largest{0};
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
quaternion[idx] /= length;
|
|
if (std::abs(quaternion[idx]) > std::abs(quaternion[largest]))
|
|
largest = idx;
|
|
}
|
|
auto const sign{quaternion[largest] < 0.f ? -1.f : 1.f};
|
|
packed_rotation packed;
|
|
packed.largest = static_cast<std::uint16_t>(largest);
|
|
std::size_t slot{0};
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
if (idx == largest)
|
|
continue;
|
|
// the other components lie within +-1/sqrt(2)
|
|
auto const scaled{std::clamp(sign * quaternion[idx] * std::numbers::sqrt2_v<float>, -1.f, 1.f)};
|
|
packed.components[slot] = static_cast<std::int16_t>(std::lround(scaled * 32767.f));
|
|
++slot;
|
|
}
|
|
return packed;
|
|
}
|
|
|
|
std::array<float, 4> decode_rotation(packed_rotation const &Packed) {
|
|
std::array<float, 4> quaternion{0.f, 0.f, 0.f, 1.f};
|
|
auto const largest{static_cast<std::size_t>(std::min<std::uint16_t>(Packed.largest, 3))};
|
|
auto sum{0.f};
|
|
std::size_t slot{0};
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
if (idx == largest)
|
|
continue;
|
|
quaternion[idx] = static_cast<float>(Packed.components[slot]) / 32767.f / std::numbers::sqrt2_v<float>;
|
|
++slot;
|
|
sum += quaternion[idx] * quaternion[idx];
|
|
}
|
|
quaternion[largest] = std::sqrt(std::max(0.f, 1.f - sum));
|
|
return quaternion;
|
|
}
|
|
|
|
tangent_frame decode_qtangent(std::array<std::int16_t, 4> const &Packed) {
|
|
// the first and the third column of the rotation matrix of the quaternion, which 2 / |q|^2 makes unit without a square root:
|
|
// what glm::mat3_cast(glm::normalize(q)) gives, for the two columns needed, at a part of the cost (this runs for every vertex)
|
|
auto const x{Packed[0] / snorm16_scale};
|
|
auto const y{Packed[1] / snorm16_scale};
|
|
auto const z{Packed[2] / snorm16_scale};
|
|
auto const w{Packed[3] / snorm16_scale};
|
|
tangent_frame frame;
|
|
frame.handedness = w < 0.f ? -1.f : 1.f;
|
|
auto const norm{x * x + y * y + z * z + w * w};
|
|
if (norm < 1e-12f)
|
|
return frame;
|
|
auto const s{2.f / norm};
|
|
frame.tangent = {1.f - s * (y * y + z * z), s * (x * y + w * z), s * (x * z - w * y)};
|
|
frame.normal = {s * (x * z + w * y), s * (y * z - w * x), 1.f - s * (x * x + y * y)};
|
|
return frame;
|
|
}
|
|
|
|
glm::mat4 local_transform(model const &Model, std::uint32_t const Node) {
|
|
return node_transform(Model, Node, matrix_lookup(Model));
|
|
}
|
|
|
|
std::vector<glm::mat4> local_transforms(model const &Model) {
|
|
auto const matrices{matrix_lookup(Model)};
|
|
std::vector<glm::mat4> transforms(Model.nodes.size());
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx)
|
|
transforms[idx] = node_transform(Model, idx, matrices);
|
|
return transforms;
|
|
}
|
|
|
|
std::vector<glm::mat4> model_transforms(model const &Model) {
|
|
auto const matrices{matrix_lookup(Model)};
|
|
std::vector<glm::mat4> transforms(Model.nodes.size());
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
auto const local{node_transform(Model, idx, matrices)};
|
|
auto const parent{Model.nodes[idx].parent};
|
|
transforms[idx] = (parent >= 0 && parent < static_cast<std::int64_t>(idx)) ? transforms[parent] * local : local;
|
|
}
|
|
return transforms;
|
|
}
|
|
|
|
bool set_transform(node_record &Node, glm::mat4 const &Matrix, float const Tolerance) {
|
|
glm::vec3 scale{glm::length(glm::vec3{Matrix[0]}), glm::length(glm::vec3{Matrix[1]}), glm::length(glm::vec3{Matrix[2]})};
|
|
glm::mat3 rotation{1.f};
|
|
if (scale.x > minimum_length && scale.y > minimum_length && scale.z > minimum_length) {
|
|
rotation = glm::mat3{glm::vec3{Matrix[0]} / scale.x, glm::vec3{Matrix[1]} / scale.y, glm::vec3{Matrix[2]} / scale.z};
|
|
// a mirror goes into the scale, the rotation stays proper
|
|
if (glm::determinant(rotation) < 0.f) {
|
|
scale.x = -scale.x;
|
|
rotation[0] = -rotation[0];
|
|
}
|
|
}
|
|
auto const quaternion{glm::normalize(glm::quat_cast(rotation))};
|
|
Node.translation = {Matrix[3].x, Matrix[3].y, Matrix[3].z};
|
|
Node.rotation = {quaternion.x, quaternion.y, quaternion.z, quaternion.w};
|
|
Node.scale = {scale.x, scale.y, scale.z};
|
|
auto const composed{compose(Node)};
|
|
for (int column = 0; column < 4; ++column) {
|
|
for (int row = 0; row < 4; ++row) {
|
|
auto const original{Matrix[column][row]};
|
|
if (std::abs(composed[column][row] - original) > Tolerance * std::max(1.f, std::abs(original)))
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void build_name_index(model &Model) {
|
|
Model.name_index.clear();
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
if (auto const name{Model.nodes[idx].name}; name != no_index && name < Model.strings.size())
|
|
Model.name_index.push_back({hash_name(Model.strings[name]), idx, 0});
|
|
}
|
|
std::ranges::sort(Model.name_index, [](name_index_record const &Left, name_index_record const &Right) { return Left.hash != Right.hash ? Left.hash < Right.hash : Left.node < Right.node; });
|
|
}
|
|
|
|
std::vector<std::uint32_t> find_nodes(model const &Model, std::string_view const Name) {
|
|
auto const key{lowercase(Name)};
|
|
std::vector<std::uint32_t> found;
|
|
auto const matches{[&Model, &key](std::uint32_t const Node) { return lowercase(Model.string(Model.nodes[Node].name)) == key; }};
|
|
if (Model.name_index.empty()) {
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
if (Model.nodes[idx].name != no_index && matches(idx))
|
|
found.push_back(idx);
|
|
}
|
|
return found;
|
|
}
|
|
auto const hash{hash_name(Name)};
|
|
auto const range{std::ranges::equal_range(Model.name_index, hash, {}, &name_index_record::hash)};
|
|
for (auto const &entry : range) {
|
|
if (matches(entry.node))
|
|
found.push_back(entry.node);
|
|
}
|
|
return found;
|
|
}
|
|
|
|
void compute_bounds(model &Model) {
|
|
std::vector<box> boxes(Model.nodes.size());
|
|
std::vector<float> radii(Model.nodes.size(), 0.f);
|
|
for (auto &primitive : Model.primitives) {
|
|
if (primitive.node >= Model.nodes.size() || primitive.vertex_count == 0)
|
|
continue;
|
|
box own;
|
|
primitive.sphere = sphere_of(Model, primitive.base_vertex, primitive.vertex_count, own);
|
|
auto &nodebox{boxes[primitive.node]};
|
|
nodebox.add(own.min);
|
|
nodebox.add(own.max);
|
|
}
|
|
// the radius of the node around the centre of its box, from all its vertices
|
|
for (auto const &primitive : Model.primitives) {
|
|
if (primitive.node >= Model.nodes.size() || primitive.vertex_count == 0)
|
|
continue;
|
|
auto const centre{boxes[primitive.node].centre()};
|
|
auto &radius{radii[primitive.node]};
|
|
for (auto idx = std::size_t{primitive.base_vertex}; idx < std::size_t{primitive.base_vertex} + primitive.vertex_count; ++idx)
|
|
radius = std::max(radius, glm::length(to_vec3(Model.positions[idx]) - centre));
|
|
}
|
|
Model.bounds.clear();
|
|
Model.bounds.reserve(Model.nodes.size() + 1);
|
|
auto const transforms{model_transforms(Model)};
|
|
box whole;
|
|
for (std::size_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
Model.bounds.push_back(to_bounds(boxes[idx], radii[idx]));
|
|
if (boxes[idx].empty())
|
|
continue;
|
|
for (int corner = 0; corner < 8; ++corner) {
|
|
glm::vec3 const point{(corner & 1) ? boxes[idx].max.x : boxes[idx].min.x, (corner & 2) ? boxes[idx].max.y : boxes[idx].min.y, (corner & 4) ? boxes[idx].max.z : boxes[idx].min.z};
|
|
whole.add(glm::vec3{transforms[idx] * glm::vec4{point, 1.f}});
|
|
}
|
|
}
|
|
Model.bounds.push_back(to_bounds(whole, whole.empty() ? 0.f : glm::length(whole.max - whole.centre())));
|
|
}
|
|
|
|
} // namespace eu07m
|