/* 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/clip.h" #include #include #include #include namespace animation { namespace { // the key at or before Time and the share of the way to the next one struct key_span { std::size_t key{0}; float share{0.f}; float length{0.f}; // seconds between the two keys bool last{true}; // Time at or past the last key, or a single key }; key_span find_span(std::span const Times, float const Time) { key_span result; if (Times.size() < 2 || Time <= Times.front()) { result.last = Times.size() < 2; return result; } if (Time >= Times.back()) { result.key = Times.size() - 1; return result; } auto const next{static_cast(std::distance(Times.begin(), std::ranges::upper_bound(Times, Time)))}; result.key = next - 1; result.length = Times[next] - Times[result.key]; result.share = result.length > 0.f ? (Time - Times[result.key]) / result.length : 0.f; result.last = false; return result; } glm::vec3 hermite(glm::vec3 const &From, glm::vec3 const &FromTangent, glm::vec3 const &To, glm::vec3 const &ToTangent, float const Share) { auto const t2{Share * Share}; auto const t3{t2 * Share}; return (2.f * t3 - 3.f * t2 + 1.f) * From + (t3 - 2.f * t2 + Share) * FromTangent + (-2.f * t3 + 3.f * t2) * To + (t3 - t2) * ToTangent; } glm::vec3 vector_value(track const &Track, std::size_t const Key) { // with cubic the keys come as in-tangent, value, out-tangent auto const index{Track.interpolation == curve::cubic ? Key * 3 + 1 : Key}; return index < Track.vectors.size() ? Track.vectors[index] : glm::vec3{0.f}; } } // namespace float clip_time(clip const &Clip, double const Elapsed) { if (Clip.duration <= 0.f || Elapsed <= 0.0) { return 0.f; } auto const duration{static_cast(Clip.duration)}; if (Clip.loop) { return static_cast(std::fmod(Elapsed, duration)); } return static_cast(std::min(Elapsed, duration)); } glm::vec3 sample_vector(track const &Track, float const Time) { auto const span{find_span(Track.times, Time)}; if (span.last || Track.interpolation == curve::step || span.share <= 0.f) { return vector_value(Track, span.key); } auto const from{vector_value(Track, span.key)}; auto const to{vector_value(Track, span.key + 1)}; if (Track.interpolation == curve::linear) { return glm::mix(from, to, span.share); } auto const outtangent{Track.vectors.size() > span.key * 3 + 2 ? Track.vectors[span.key * 3 + 2] : glm::vec3{0.f}}; auto const intangent{Track.vectors.size() > (span.key + 1) * 3 ? Track.vectors[(span.key + 1) * 3] : glm::vec3{0.f}}; return hermite(from, outtangent * span.length, to, intangent * span.length, span.share); } glm::quat sample_rotation(track const &Track, float const Time) { if (Track.rotations.empty()) { return glm::quat{1.f, 0.f, 0.f, 0.f}; } auto const span{find_span(Track.times, Time)}; auto const from{Track.rotations[std::min(span.key, Track.rotations.size() - 1)]}; if (span.last || Track.interpolation == curve::step || span.share <= 0.f || span.key + 1 >= Track.rotations.size()) { return from; } // the shorter way round return glm::normalize(glm::slerp(from, Track.rotations[span.key + 1], span.share)); } glm::mat4 local_transform(clip const &Clip, node_tracks const &Node, float const Time) { auto const tracked = [&Clip](int const Track) { return (Track >= 0) && (static_cast(Track) < Clip.tracks.size()); }; auto const translation{tracked(Node.translation) ? sample_vector(Clip.tracks[Node.translation], Time) : Node.rest_translation}; auto const rotation{tracked(Node.rotation) ? sample_rotation(Clip.tracks[Node.rotation], Time) : Node.rest_rotation}; auto const scale{tracked(Node.scale) ? sample_vector(Clip.tracks[Node.scale], Time) : Node.rest_scale}; return glm::translate(glm::mat4{1.f}, translation) * glm::mat4_cast(rotation) * glm::scale(glm::mat4{1.f}, scale); } float channel_value(channel const &Channel, float const Value) { auto const low{std::min(Channel.minimum, Channel.maximum)}; auto const high{std::max(Channel.minimum, Channel.maximum)}; return std::clamp(Value, low, high); } glm::mat4 joint_transform(joint_binding const &Joint, float const Value) { auto value{Value}; if (Joint.limited) { value = std::clamp(value, std::min(Joint.minimum, Joint.maximum), std::max(Joint.minimum, Joint.maximum)); } // the node is modelled at the rest value of the joint: at that value it stays as modelled value -= Joint.rest_value; auto const length{glm::length(Joint.axis)}; if (length <= 0.f) { return Joint.rest; } auto const axis{Joint.axis / length}; switch (Joint.type) { case joint_type::hinge: return Joint.rest * glm::rotate(glm::mat4{1.f}, value, axis); case joint_type::slider: return Joint.rest * glm::translate(glm::mat4{1.f}, axis * value); case joint_type::screw: return Joint.rest * glm::translate(glm::mat4{1.f}, axis * (value / (2.f * std::numbers::pi_v) * Joint.pitch)) * glm::rotate(glm::mat4{1.f}, value, axis); default: return Joint.rest; } } } // namespace animation