Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
SceneAnimatorFileIO.cpp
Go to the documentation of this file.
1
2
4
7
8namespace Divide {
12
13void AnimEvaluator::save(const AnimEvaluator& evaluator, ByteBuffer& dataOut) {
15
16 // The animation name;
17 dataOut << evaluator._name;
18 // the duration
19 dataOut << evaluator._duration;
20 // the number of ticks per second
21 dataOut << evaluator._ticksPerSecond;
22 // number of animation channels,
23 dataOut << static_cast<uint32_t>(evaluator._channels.size());
24 // for each channel
25 for (const auto& channel : evaluator._channels) {
26 // the channel name
27 dataOut << channel._name;
28 dataOut << channel._nameKey;
29 // the number of position keys
30 uint32_t nsize = static_cast<uint32_t>(channel._positionKeys.size());
31 dataOut << nsize;
32 // for each position key;
33 for (size_t i = 0; i < nsize; i++) {
34 // position key
35 dataOut << channel._positionKeys[i].mTime;
36 // position key
37 dataOut << channel._positionKeys[i].mValue.x;
38 dataOut << channel._positionKeys[i].mValue.y;
39 dataOut << channel._positionKeys[i].mValue.z;
40 }
41
42 nsize = static_cast<uint32_t>(channel._rotationKeys.size());
43 // the number of rotation keys
44 dataOut << nsize;
45 // for each channel
46 for (size_t i = 0; i < nsize; i++) {
47 // rotation key
48 dataOut << channel._rotationKeys[i].mTime;
49 // rotation key
50 dataOut << channel._rotationKeys[i].mValue.x;
51 dataOut << channel._rotationKeys[i].mValue.y;
52 dataOut << channel._rotationKeys[i].mValue.z;
53 dataOut << channel._rotationKeys[i].mValue.w;
54 }
55
56 nsize = static_cast<uint32_t>(channel._scalingKeys.size());
57 // the number of scaling keys
58 dataOut << nsize;
59 // for each channel
60 for (size_t i = 0; i < nsize; i++) {
61 // scale key
62 dataOut << channel._scalingKeys[i].mTime;
63 // scale key
64 dataOut << channel._scalingKeys[i].mValue.x;
65 dataOut << channel._scalingKeys[i].mValue.y;
66 dataOut << channel._scalingKeys[i].mValue.z;
67 }
68 }
69}
70
71void AnimEvaluator::load(AnimEvaluator& evaluator, ByteBuffer& dataIn) {
72 Console::d_printfn(LOCALE_STR("CREATE_ANIMATION_BEGIN"), evaluator._name.c_str());
73
74 auto tempVer = decltype(BYTE_BUFFER_VERSION_EVALUATOR){0};
75 dataIn >> tempVer;
76 if (tempVer == BYTE_BUFFER_VERSION_EVALUATOR) {
77 // the animation name
78 dataIn >> evaluator._name;
79 // the duration
80 dataIn >> evaluator._duration;
81 // the number of ticks per second
82 dataIn >> evaluator._ticksPerSecond;
83 // the number animation channels
84 uint32_t nsize = 0;
85 dataIn >> nsize;
86 evaluator._channels.resize(nsize);
87 evaluator._lastPositions.resize(nsize, vec3<U32>());
88 // for each channel
89 for (AnimationChannel& channel : evaluator._channels) {
90 //the channel name
91 dataIn >> channel._name;
92 dataIn >> channel._nameKey;
93 // the number of position keys
94 dataIn >> nsize;
95 channel._positionKeys.resize(nsize);
96 // for each position key
97 for (size_t i = 0; i < nsize; i++) {
98 aiVectorKey& pos = channel._positionKeys[i];
99 // position key
100 dataIn >> pos.mTime;
101 // position key
102 dataIn >> pos.mValue.x;
103 dataIn >> pos.mValue.y;
104 dataIn >> pos.mValue.z;
105 }
106
107 // the number of rotation keys
108 dataIn >> nsize;
109 channel._rotationKeys.resize(nsize);
110 // for each rotation key
111 for (size_t i = 0; i < nsize; i++) {
112 aiQuatKey& rot = channel._rotationKeys[i];
113 // rotation key
114 dataIn >> rot.mTime;
115 // rotation key
116 dataIn >> rot.mValue.x;
117 dataIn >> rot.mValue.y;
118 dataIn >> rot.mValue.z;
119 dataIn >> rot.mValue.w;
120 }
121
122 // the number of scaling keys
123 dataIn >> nsize;
124 channel._scalingKeys.resize(nsize);
125 // for each scaling key
126 for (size_t i = 0; i < nsize; i++) {
127 aiVectorKey& scale = channel._scalingKeys[i];
128 // scale key
129 dataIn >> scale.mTime;
130 // scale key
131 dataIn >> scale.mValue.x;
132 dataIn >> scale.mValue.y;
133 dataIn >> scale.mValue.z;
134 }
135 }
136 evaluator._lastPositions.resize(evaluator._channels.size(), vec3<U32>());
137 } else {
139 }
140}
141
142void SceneAnimator::save([[maybe_unused]] PlatformContext& context, ByteBuffer& dataOut) const {
143 // first recursively save the skeleton
144 if (_skeleton) {
145 saveSkeleton(dataOut, _skeleton);
146 }
147
149 dataOut << to_U32(_bones.size());
150 for (Bone* bone : _bones) {
151 dataOut << bone->name();
152 }
153
154 // the number of animations
155 const uint32_t nsize = static_cast<uint32_t>(_animations.size());
156 dataOut << nsize;
157
158 for (uint32_t i(0); i < nsize; i++) {
159 AnimEvaluator::save(*_animations[i], dataOut);
160 }
161}
162
164 // make sure to clear this before writing new data
165 release(true);
166 assert(_animations.empty());
167
168 _skeleton = loadSkeleton(dataIn, nullptr);
169
170 auto tempVer = decltype(BYTE_BUFFER_VERSION_ANIMATOR){0};
171 dataIn >> tempVer;
172 if (tempVer == BYTE_BUFFER_VERSION_ANIMATOR) {
173
174 string boneName;
175 uint32_t nsize = 0;
176 dataIn >> nsize;
177 _bones.resize(nsize);
178 for (uint32_t i(0); i < nsize; i++) {
179 dataIn >> boneName;
180 _bones[i] = _skeleton->find(boneName);
181 }
183 // the number of animations
184 dataIn >> nsize;
185 _animations.resize(nsize);
186
187 for (uint32_t idx = 0; idx < nsize; ++idx)
188 {
189 _animations[idx] = std::make_unique<AnimEvaluator>();
190 AnimEvaluator::load(*_animations[idx], dataIn);
191 // get all the animation names so I can reference them by name and get the correct id
192 insert(_animationNameToID, _ID(_animations[idx]->name().c_str()), idx);
193 }
194
195 init(context);
196 } else {
198 }
199}
200
203
204 // the name of the bone
205 dataOut << parent->name();
206 // the bone offsets
207 dataOut << parent->offsetMatrix();
208 // original bind pose
209 dataOut << parent->originalLocalTransform();
210
211 // number of children
212 const uint32_t nsize = static_cast<uint32_t>(parent->_children.size());
213 dataOut << nsize;
214 // continue for all children
215 for (Bone* child : parent->_children) {
216 saveSkeleton(dataOut, child);
217 }
218}
219
221
222 auto tempVer = decltype(BYTE_BUFFER_VERSION_SKELETON){0};
223 dataIn >> tempVer;
224 if (tempVer == BYTE_BUFFER_VERSION_SKELETON) {
225
226 string tempString;
227 // the name of the bone
228 dataIn >> tempString;
229 // create a node
230 Bone* internalNode = new Bone(tempString);
231 // set the parent, in the case this is the root node, it will be null
232 internalNode->_parent = parent;
233 mat4<F32> temp;
234 // the bone offsets
235 dataIn >> temp; internalNode->offsetMatrix(temp);
236 // original bind pose
237 dataIn >> temp; internalNode->originalLocalTransform(temp);
238
239 // a copy saved
240 internalNode->localTransform(internalNode->originalLocalTransform());
241 CalculateBoneToWorldTransform(internalNode);
242 // the number of children
243 U32 nsize = 0u;
244 dataIn >> nsize;
245
246 // recursively call this function on all children
247 // continue for all child nodes and assign the created internal nodes as our children
248 for (U32 a = 0u; a < nsize; a++) {
249 internalNode->_children.push_back(loadSkeleton(dataIn, internalNode));
250 }
251 return internalNode;
252 } else {
254 }
255
256 return nullptr;
257}
258
259} //namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
#define DIVIDE_UNEXPECTED_CALL()
static void save(const AnimEvaluator &evaluator, ByteBuffer &dataOut)
vector< AnimationChannel > _channels
vector that holds all bone channels
static void load(AnimEvaluator &evaluator, ByteBuffer &dataIn)
vector< vec3< U32 > > _lastPositions
Bone * _parent
Definition: Bone.h:51
Bone * find(const string &name)
Definition: Bone.h:77
vector< Bone * > _children
Definition: Bone.h:52
vector< std::unique_ptr< AnimEvaluator > > _animations
A vector that holds each animation.
bool init(PlatformContext &context, Bone *skeleton, const vector< Bone * > &bones)
This will build the skeleton based on the scene passed to it and CLEAR EVERYTHING.
void save(PlatformContext &context, ByteBuffer &dataOut) const
Bone * loadSkeleton(ByteBuffer &dataIn, Bone *parent)
void release(bool releaseAnimations)
Frees all memory and initializes everything to a default state.
vector< Bone * > _bones
hashMap< U64, U32 > _animationNameToID
find animations quickly
void saveSkeleton(ByteBuffer &dataOut, Bone *parent) const
I/O operations.
void load(PlatformContext &context, ByteBuffer &dataIn)
Bone * _skeleton
Root node of the internal scene structure.
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U16 BYTE_BUFFER_VERSION_EVALUATOR
constexpr U32 to_U32(const T value)
void insert(eastl::vector< T, A1 > &target, const eastl::vector< T, A2 > &source)
Definition: Vector.h:97
constexpr U16 BYTE_BUFFER_VERSION_ANIMATOR
void CalculateBoneToWorldTransform(Bone *pInternalNode) noexcept
Calculates the global transformation matrix for the given internal node.
Project & parent
Definition: DefaultScene.h:41
uint16_t U16
constexpr U64 _ID(const char *const str, const U64 value=val_64_const) noexcept
constexpr I16 to_I16(const T value)
uint32_t U32
constexpr U16 BYTE_BUFFER_VERSION_SKELETON
static NO_INLINE void d_printfn(const char *format, T &&... args)