mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 10:46:45 +01:00
ce40e11b94
- The channel event reads the name of the channel as written (getTokens lowercased it), and set_channel() finds it regardless of case: a channel named doorLeft couldn't be set from a scenery. - JOIN.rest is read: the node is modelled at the rest value of its joint, which moves it by the value less rest (the limits stay values of the joint). A part modelled half open no longer jumps when its channel is set. Files without it (rest 0) move as before. - The spec says so, and that no vehicle sets channels yet. Test: a slider modelled half out.
134 lines
3.9 KiB
C++
134 lines
3.9 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/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <span>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/quaternion.hpp>
|
|
|
|
// the animation clips of an EU07M model (ACLP, ATRK and their keys), as the game samples them: each track 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 own TRS
|
|
namespace animation
|
|
{
|
|
|
|
enum class part : std::uint8_t
|
|
{
|
|
translation,
|
|
rotation,
|
|
scale
|
|
};
|
|
|
|
enum class curve : std::uint8_t
|
|
{
|
|
step,
|
|
linear, // lerp of vectors, slerp of rotations
|
|
cubic // the Hermite spline of glTF: in-tangent, value and out-tangent for each key, vectors only
|
|
};
|
|
|
|
struct track
|
|
{
|
|
part target{part::translation};
|
|
curve interpolation{curve::linear};
|
|
std::vector<float> times; // seconds, not decreasing
|
|
std::vector<glm::vec3> vectors; // translation, scale; three for each key with cubic
|
|
std::vector<glm::quat> rotations;
|
|
};
|
|
|
|
// the tracks of one node and the TRS the node has of its own
|
|
struct node_tracks
|
|
{
|
|
int node{-1};
|
|
int translation{-1}; // into the tracks of the clip, -1 without one
|
|
int rotation{-1};
|
|
int scale{-1};
|
|
glm::vec3 rest_translation{0.f};
|
|
glm::quat rest_rotation{1.f, 0.f, 0.f, 0.f};
|
|
glm::vec3 rest_scale{1.f};
|
|
};
|
|
|
|
struct clip
|
|
{
|
|
std::string name;
|
|
float duration{0.f};
|
|
bool loop{false};
|
|
bool autoplay{false};
|
|
std::vector<track> tracks;
|
|
std::vector<node_tracks> nodes; // the nodes the clip moves, each once
|
|
};
|
|
|
|
// the time of the clip after Elapsed seconds of playing: around the duration for a looped clip, else held at its end
|
|
float clip_time(clip const &Clip, double Elapsed);
|
|
|
|
// the value of the track at Time, held at the first and the last key outside of them
|
|
glm::vec3 sample_vector(track const &Track, float Time);
|
|
glm::quat sample_rotation(track const &Track, float Time);
|
|
|
|
// the transform of the node within its parent at Time: T * R * S, each part from its track or the rest of the node
|
|
glm::mat4 local_transform(clip const &Clip, node_tracks const &Node, float Time);
|
|
|
|
// the channels of an EU07M model (CHNL): a named value which moves joints (JOIN) and sets the time of clips
|
|
enum class joint_type : std::uint8_t
|
|
{
|
|
fixed,
|
|
hinge, // turns about the axis, radians
|
|
ball, // not driven by a channel
|
|
slider, // moves along the axis, metres
|
|
screw // turns about the axis, radians, and moves along it by the pitch for each turn
|
|
};
|
|
|
|
struct joint_binding
|
|
{
|
|
int node{-1};
|
|
joint_type type{joint_type::hinge};
|
|
glm::vec3 axis{1.f, 0.f, 0.f}; // in the space of the node
|
|
bool limited{false};
|
|
float minimum{0.f};
|
|
float maximum{0.f};
|
|
float pitch{0.f}; // metres for each turn of a screw
|
|
glm::mat4 rest{1.f}; // the transform of the node within its parent, which the joint moves from
|
|
float rest_value{0.f}; // the value of the joint the node is modelled at (JOIN.rest): the joint moves by the value less it
|
|
};
|
|
|
|
enum class binding_target : std::uint8_t
|
|
{
|
|
joint,
|
|
clip
|
|
};
|
|
|
|
struct channel_binding
|
|
{
|
|
binding_target target{binding_target::joint};
|
|
joint_binding joint; // of a joint
|
|
std::size_t clip{0}; // of a clip: its index; the value is the share of its duration, 0 to 1
|
|
float scale{1.f}; // the value of the target = the value of the channel * scale + offset
|
|
float offset{0.f};
|
|
};
|
|
|
|
struct channel
|
|
{
|
|
std::string name;
|
|
float minimum{0.f};
|
|
float maximum{1.f};
|
|
float initial{0.f};
|
|
std::vector<channel_binding> bindings;
|
|
};
|
|
|
|
// the value of the channel kept within its range
|
|
float channel_value(channel const &Channel, float Value);
|
|
// the transform of the node of the joint within its parent with the joint at Value (kept within its limits when it has them): the rest
|
|
// of the node, then the turn or the move along the axis. a fixed or a ball joint leaves the node at rest
|
|
glm::mat4 joint_transform(joint_binding const &Joint, float Value);
|
|
|
|
} // namespace animation
|