Skylicht Engine
Loading...
Searching...
No Matches
CAnimationTrack.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27namespace Skylicht
28{
31 template<class T>
32 class SKYLICHT_API CKeyFrameData
33 {
34 public:
36 f32 Frame; // Second
37
40 };
41
42 typedef CKeyFrameData<core::vector3df> CPositionKey;
43 typedef CKeyFrameData<core::quaternion> CRotationKey;
44 typedef CKeyFrameData<core::vector3df> CScaleKey;
45
48 template<class T>
49 class SKYLICHT_API CArrayKeyFrame
50 {
51 public:
53 core::array<CKeyFrameData<T>> Data;
54
57
59 int Hint;
60
61 CArrayKeyFrame() :
62 Hint(0)
63 {
64 }
65
69 inline void clearHint()
70 {
71 Hint = 0;
72 }
73
79 int getIndex(f32 frame);
80
85 inline u32 size()
86 {
87 return Data.size();
88 }
89
95 {
96 return Data.pointer();
97 }
98
103 inline f32 getLastFrame()
104 {
105 if (Data.size() == 0)
106 return 0.0f;
107
108 return Data.getLast().Frame;
109 }
110 };
111
112 template<class T>
114 {
115 int foundPositionIndex = -1;
116
117 int numKey = (int)Data.size();
118 CKeyFrameData<T>* pData = Data.pointer();
119
120 // Test the Hints...
121 if (Hint >= 0 && Hint < numKey)
122 {
123 if (Hint > 0 && pData[Hint].Frame >= frame && pData[Hint - 1].Frame < frame)
124 foundPositionIndex = Hint;
125 else if (Hint + 1 < numKey)
126 {
127 if (pData[Hint + 1].Frame >= frame && pData[Hint + 0].Frame < frame)
128 {
129 Hint++;
130 foundPositionIndex = Hint;
131 }
132 }
133 }
134
135 // The Hint test failed, do a full scan...
136 if (foundPositionIndex == -1)
137 {
138 for (s32 i = 0; i < numKey; ++i)
139 {
140 if (pData[i].Frame >= frame) // Keys should to be sorted by frame
141 {
142 foundPositionIndex = i;
143 Hint = i;
144 break;
145 }
146 }
147 }
148
149 return foundPositionIndex;
150 }
151
156 class SKYLICHT_API CAnimationData
157 {
158 public:
162
163 CAnimationData()
164 {
165 }
166 };
167
172 class SKYLICHT_API CAnimationTrack
173 {
174 protected:
177
178 public:
180 std::string Name;
181
184
185 public:
186 CAnimationTrack();
187
188 virtual ~CAnimationTrack();
189
197 static void quaternionSlerp(core::quaternion& result, core::quaternion q1, core::quaternion q2, float t);
198
206 void getFrameData(f32 frame,
207 core::vector3df& position,
208 core::vector3df& scale,
209 core::quaternion& rotation);
210
216
221 {
222 if (m_data)
223 {
224 m_data->Positions.clearHint();
225 m_data->Rotations.clearHint();
226 m_data->Scales.clearHint();
227 }
228
229 m_data = NULL;
230
231 Name = "";
232 HaveAnimation = false;
233 }
234
240 {
241 m_data = data;
242 }
243
249 {
250 return m_data;
251 }
252 };
253}
Container for PRS (Position, Rotation, Scale) animation data.
Definition CAnimationTrack.h:157
CAnimationData * m_data
Pointer to the shared animation data.
Definition CAnimationTrack.h:176
bool HaveAnimation
Whether this track currently has animation data.
Definition CAnimationTrack.h:183
CAnimationData * getAnimData()
Gets the underlying animation data.
void getFrameData(f32 frame, core::vector3df &position, core::vector3df &scale, core::quaternion &rotation)
Calculates the interpolated PRS data for a specific frame.
void setAnimationData(CAnimationData *data)
Sets the source animation data for this track.
Definition CAnimationTrack.h:239
void clearAllKeyFrame()
Clears all keyframe search hints and resets the track.
Definition CAnimationTrack.h:220
std::string Name
The name of the track.
Definition CAnimationTrack.h:180
CAnimationData * getFrameData()
Gets the underlying animation data.
Definition CAnimationTrack.h:248
static void quaternionSlerp(core::quaternion &result, core::quaternion q1, core::quaternion q2, float t)
Static utility for spherical linear interpolation between quaternions.
Container for an array of keyframes with interpolation support.
Definition CAnimationTrack.h:50
int Hint
Internal hint for optimizing keyframe searching.
Definition CAnimationTrack.h:59
CKeyFrameData< T > * pointer()
Returns the raw pointer to the keyframe data.
Definition CAnimationTrack.h:94
void clearHint()
Resets the search hint.
Definition CAnimationTrack.h:69
int getIndex(f32 frame)
Finds the index of the keyframe at or just before the specified time.
Definition CAnimationTrack.h:113
T Default
Default value used if no keyframes are present.
Definition CAnimationTrack.h:56
f32 getLastFrame()
Gets the time of the last keyframe.
Definition CAnimationTrack.h:103
u32 size()
Gets the number of keyframes.
Definition CAnimationTrack.h:85
core::array< CKeyFrameData< T > > Data
List of keyframes, sorted by time.
Definition CAnimationTrack.h:53
Template for keyframe data.
Definition CAnimationTrack.h:33
f32 Frame
Definition CAnimationTrack.h:36
core::vector3df Value
Definition CAnimationTrack.h:39
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29