diff --git a/docs/eu07m-format.md b/docs/eu07m-format.md index b4d52aaf5..998f9e16c 100644 --- a/docs/eu07m-format.md +++ b/docs/eu07m-format.md @@ -261,8 +261,13 @@ Części węzła bez ścieżki zostają takie, jak w jego TRS. - **Czas.** Klip idzie po czasie symulacji od chwili startu, z tempem `speed`. Zapętlony zaczyna od nowa po swojej długości, inny zatrzymuje się na końcu. Klip z flagą 0x2 startuje sam, gdy egzemplarz jest rysowany pierwszy raz. - **W grze.** `TAnimModel::play_clip(nazwa, tempo)` startuje klip (komenda self-testu `clip [tempo]`), - a `stop_clips()` zatrzymuje wszystkie. Przy każdym rysowaniu egzemplarza `RaPrepare()` próbkuje grające klipy i - ustawia transformy węzłów (`TSubModel::SetClipTransform()`, animacja `at_Clip`), tak jak inne animacje egzemplarza. + a `stop_clips()` zatrzymuje wszystkie: każdy zostaje wstrzymany w chwili zatrzymania i dalej ustawia swoje węzły, bo + poza tym, co ustawia egzemplarz przy rysowaniu, węzły trzymają wspólne submodele wszystkich egzemplarzy. Przy każdym + rysowaniu egzemplarza `RaPrepare()` próbkuje klipy i ustawia transformy węzłów (`TSubModel::SetClipTransform()`, + animacja `at_Clip`), przed animacjami eventów: event scenerii ruszający węzeł (`rotate`, `translate`…) bierze go od + klipu, także zakończonego albo zatrzymanego. +- Węzeł z macierzą (`XFRM`) ma pola TRS tylko w przybliżeniu: części, których ścieżki klipu nie ruszają, są brane z + rozkładu samej macierzy, więc węzeł nie skacze, gdy klip zaczyna ruszać inną jego część. Węzły, które klipy przesuwają, są przy wczytaniu oznaczane jako animowane, więc model z klipami nie jest instancjonowany. Klip ruszający jointami skóry zgina skinowane części (sekcja „Skinning”). - **Jeszcze nie:** event scenerii, który startuje klip, oraz powiązanie kanałów `CHNL` z czasem klipu. Obecne wydanie diff --git a/model/AnimModel.cpp b/model/AnimModel.cpp index d4abeb0b3..a0357af24 100644 --- a/model/AnimModel.cpp +++ b/model/AnimModel.cpp @@ -755,10 +755,12 @@ void TAnimModel::RaPrepare() TSubModel::iVariantSeed = 0; TSubModel::pasText = &asText; // przekazanie tekstu do wyświetlacza (!!!! do przemyślenia) + // the clips first: an animation event of the scenery on a node (rotate, translate...) takes over from a clip on the same node, + // one which ended or was stopped included, as the later one set on the submodel counts + apply_clips(); for (auto entry : m_animlist) { entry->PrepareModel(); } - apply_clips(); } // the state of the lit and the dark part of a numbered light, as RaPrepare() shows them @@ -813,7 +815,15 @@ bool TAnimModel::play_clip( std::string_view const Name, float const Speed ) { void TAnimModel::stop_clips() { - m_clips.clear(); + // the clips are held where they are, not dropped: the pose lives in the submodels the instances of the model share, so the + // nodes of this instance are set again for each of its draws (apply_clips()), or another instance playing a clip would leave + // them at rest + auto const now { Timer::GetTime() }; + for( auto &playing : m_clips ) { + if( false == playing.held.has_value() ) { + playing.held = ( now - playing.start ) * playing.speed; + } + } m_autoplaystarted = true; } @@ -835,7 +845,7 @@ void TAnimModel::apply_clips() { auto const count { pModel->submodel_count() }; for( auto const &playing : m_clips ) { auto const &clip { clips[ playing.clip ] }; - auto const time { animation::clip_time( clip, ( now - playing.start ) * playing.speed ) }; + auto const time { animation::clip_time( clip, playing.held.value_or( ( now - playing.start ) * playing.speed ) ) }; for( auto const &node : clip.nodes ) { if( ( node.node < 0 ) || ( node.node >= count ) ) { continue; } root[ node.node ].SetClipTransform( animation::local_transform( clip, node, time ) ); diff --git a/model/AnimModel.h b/model/AnimModel.h index 986103591..3faee6144 100644 --- a/model/AnimModel.h +++ b/model/AnimModel.h @@ -20,6 +20,8 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/DynObj.h" #include "scene/scenenode.h" +#include + const int iMaxNumLights = 8; float const DefaultDarkThresholdLevel { 0.325f }; @@ -192,7 +194,8 @@ public: // starts the animation clip of the model (EU07M) with the name from its beginning, at Speed times its pace; a clip already playing // starts over. false when the model has no clip of the name bool play_clip( std::string_view Name, float Speed = 1.f ); - // stops the clips playing, the nodes they moved stay where they were last put till the next clip moves them + // stops the clips playing: each is held at the moment it was stopped, so the nodes stay where they were put, till a clip + // started on them or an animation event of the scenery moves them void stop_clips(); // radius() subclass details, calculates node's bounding radius @@ -245,6 +248,7 @@ public: std::size_t clip { 0 }; double start { 0.0 }; float speed { 1.f }; + std::optional held; // stopped (stop_clips()): the time into the clip it stays at }; std::vector m_clips; bool m_autoplaystarted { false }; // the clips of the model to play on their own were started diff --git a/model/eu07m/eu07m_load.cpp b/model/eu07m/eu07m_load.cpp index 9f58c3bec..06bd45c7b 100644 --- a/model/eu07m/eu07m_load.cpp +++ b/model/eu07m/eu07m_load.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/utilities.h" #include "utilities/wholefile.h" +#include #include #include @@ -398,6 +399,20 @@ animation::node_tracks &node_entry(animation::clip &Clip, eu07m::model const &Mo entry.rest_translation = {record.translation[0], record.translation[1], record.translation[2]}; entry.rest_rotation = glm::quat{record.rotation[3], record.rotation[0], record.rotation[1], record.rotation[2]}; entry.rest_scale = {record.scale[0], record.scale[1], record.scale[2]}; + if ((record.flags & eu07m::node_flags::matrix) != 0) { + // the TRS fields of a node with a matrix (XFRM) are only an approximation of it: the parts the tracks leave are taken from + // the matrix itself, so the node doesn't jump when a clip starts moving one of its parts + auto const matrix{eu07m::local_transform(Model, Node)}; + glm::vec3 scale{glm::length(glm::vec3{matrix[0]}), glm::length(glm::vec3{matrix[1]}), glm::length(glm::vec3{matrix[2]})}; + if (glm::determinant(glm::mat3{matrix}) < 0.f) + scale.x = -scale.x; // a mirroring matrix: the rotation stays a proper one + if ((scale.x != 0.f) && (scale.y != 0.f) && (scale.z != 0.f)) { + glm::mat3 const rotation{glm::vec3{matrix[0]} / scale.x, glm::vec3{matrix[1]} / scale.y, glm::vec3{matrix[2]} / scale.z}; + entry.rest_translation = glm::vec3{matrix[3]}; + entry.rest_rotation = glm::normalize(glm::quat_cast(rotation)); + entry.rest_scale = scale; + } + } return Clip.nodes.emplace_back(entry); }