Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Animation.cpp
1/*
2Copyright (C) 2017-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include <exception>
18#include <vector>
19#include <cassert>
20#include <cstring>
21#include <cmath>
22#include <mutex>
23#include "aeongames/AeonEngine.hpp"
24#include "aeongames/ProtoBufHelpers.hpp"
25#include "aeongames/ProtoBufUtils.hpp"
26#include "aeongames/Utilities.hpp"
27#include "aeongames/Animation.hpp"
29#ifdef _MSC_VER
30#pragma warning( push )
31#pragma warning( disable : PROTOBUF_WARNINGS )
32#endif
33#include "animation.pb.h"
34#ifdef _MSC_VER
35#pragma warning( pop )
36#endif
37
38namespace AeonGames
39{
40 Animation::Animation()
41 = default;
42 void Animation::LoadFromMemory ( const void* aBuffer, size_t aBufferSize )
43 {
44 LoadFromProtoBufObject<Animation, AnimationMsg, "AEONANM"_mgk> ( *this, aBuffer, aBufferSize );
45 }
46
47 void Animation::LoadFromPBMsg ( const AnimationMsg& aAnimationMsg )
48 {
49 mVersion = aAnimationMsg.version();
50 mFrameRate = aAnimationMsg.framerate();
51 mDuration = aAnimationMsg.duration();
52 mFrames.reserve ( aAnimationMsg.frame_size() );
53 for ( auto& frame : aAnimationMsg.frame() )
54 {
55 mFrames.emplace_back();
56 mFrames.back().reserve ( frame.bone_size() );
57 for ( auto& joint : frame.bone() )
58 {
59 mFrames.back().emplace_back (
61 {
63 {
64 joint.scale().x(),
65 joint.scale().y(),
66 joint.scale().z()
67 },
69 {
70 joint.rotation().w(),
71 joint.rotation().x(),
72 joint.rotation().y(),
73 joint.rotation().z()
74 },
76 {
77 joint.translation().x(),
78 joint.translation().y(),
79 joint.translation().z()
80 } }
81 );
82 }
83 }
84 }
85
87 {
88 mVersion = 0;
89 mFrameRate = 0;
90 mDuration = 0;
91 mFrames.clear();
92 }
93
94 Animation::~Animation() = default;
95
96 uint32_t Animation::GetFrameRate() const
97 {
98 return mFrameRate;
99 }
100
102 {
103 return mDuration;
104 }
105
106 double Animation::GetSample ( double aTime ) const
107 {
113 return fmod ( mFrameRate * fmod ( aTime, mDuration ), mFrames.size() );
114 }
115
116 double Animation::AddTimeToSample ( double aSample, double aTime ) const
117 {
118 return fmod ( aSample + ( mFrameRate * fmod ( aTime, mDuration ) ), mFrames.size() );
119 }
120
121 const Transform Animation::GetTransform ( size_t aBoneIndex, double aSample ) const
122 {
123 double frame;
124 double interpolation = modf ( aSample, &frame );
125 auto frame1 = static_cast<size_t> ( frame );
126 size_t frame2 = ( ( frame1 + 1 ) % mFrames.size() );
127 size_t frame0 = frame1 == 0 ? mFrames.size() - 1 : ( ( frame1 - 1 ) % mFrames.size() );
128 size_t frame3 = ( ( frame1 + 2 ) % mFrames.size() );
130 assert ( ( interpolation >= 0.0 ) && ( interpolation < 1.0 ) && "Interpolation out of range [0.0,1.0)." );
131 return Interpolate ( mFrames[frame0][aBoneIndex], mFrames[frame1][aBoneIndex], mFrames[frame2][aBoneIndex], mFrames[frame3][aBoneIndex], interpolation );
132 }
133}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
DLL double AddTimeToSample(double aSample, double aTime) const
Advance a sample position by a time delta.
DLL double GetDuration() const
Get the total duration of the animation.
DLL double GetSample(double aTime) const
Get a normalized sample position for a given time.
DLL void Unload() final
Unload animation data and free resources.
Definition Animation.cpp:86
DLL void LoadFromMemory(const void *aBuffer, size_t aBufferSize) final
Load animation data from a memory buffer.
Definition Animation.cpp:42
DLL void LoadFromPBMsg(const AnimationMsg &aAnimationMsg)
Load animation data from a protobuf message.
Definition Animation.cpp:47
DLL const Transform GetTransform(size_t aBoneIndex, double aSample) const
Get the transform for a specific bone at a given sample.
DLL uint32_t GetFrameRate() const
Get the frame rate of the animation.
Definition Animation.cpp:96
Quaternion class.
Component class for any object that requires space transformations.
Definition Transform.hpp:34
3D vector class.
Definition Vector3.hpp:32
<- This is here just for the literals
Definition AABB.hpp:31
DLL const Transform Interpolate(const Transform &aTransform0, const Transform &aTransform1, const Transform &aTransform2, const Transform &aTransform3, double aInterpolation)
Interpolate transforms using spline and mlerp methods.
void LoadFromProtoBufObject(T &aTarget, const void *aBuffer, size_t aBufferSize)
Loads a Protocol Buffer message from a buffer and populates a target object.