goat3d

view src/xform_node.cc @ 56:ca549434dc95

fixed a crashing bug in anm_destroy_node introduced when I added the multiple animations per node
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Jan 2014 02:50:54 +0200
parents 498ca7ac7047
children dad392c710df 3751aabbc5b3
line source
1 #include <assert.h>
2 #include <algorithm>
3 #include "xform_node.h"
4 #include "anim/anim.h"
5 #include "anim/track.h"
7 using namespace g3dimpl;
9 static inline anm_interpolator track_interpolator(Interp in);
10 static inline anm_extrapolator track_extrapolator(Extrap ex);
12 XFormNode::XFormNode()
13 {
14 anm = new anm_node;
15 anm_init_node(anm);
16 parent = 0;
18 // TODO read them from anm to get the correct initial values
19 interp = INTERP_LINEAR;
20 extrap = EXTRAP_EXTEND;
21 }
23 XFormNode::~XFormNode()
24 {
25 anm_destroy_node(anm);
26 delete anm;
27 }
29 struct anm_node *XFormNode::get_libanim_node() const
30 {
31 return anm;
32 }
34 void XFormNode::set_name(const char *name)
35 {
36 anm_set_node_name(anm, name);
37 }
39 const char *XFormNode::get_name() const
40 {
41 return anm_get_node_name(anm);
42 }
44 void XFormNode::set_interpolator(Interp in)
45 {
46 anm_set_interpolator(anm, track_interpolator(in));
47 interp = in;
48 }
50 Interp XFormNode::get_interpolator() const
51 {
52 return interp;
53 }
55 void XFormNode::set_extrapolator(Extrap ex)
56 {
57 anm_set_extrapolator(anm, track_extrapolator(ex));
58 extrap = ex;
59 }
61 Extrap XFormNode::get_extrapolator() const
62 {
63 return extrap;
64 }
66 XFormNode *XFormNode::get_parent()
67 {
68 return parent;
69 }
71 const XFormNode *XFormNode::get_parent() const
72 {
73 return parent;
74 }
76 void XFormNode::add_child(XFormNode *child)
77 {
78 children.push_back(child);
79 anm_link_node(anm, child->anm);
80 child->parent = this;
81 }
83 void XFormNode::remove_child(XFormNode *child)
84 {
85 std::vector<XFormNode*>::iterator it;
86 it = std::find(children.begin(), children.end(), child);
87 if(it != children.end()) {
88 children.erase(it);
89 anm_unlink_node(anm, child->anm);
91 if(child->parent == this) {
92 child->parent = 0;
93 }
94 }
95 }
97 int XFormNode::get_children_count() const
98 {
99 return (int)children.size();
100 }
102 XFormNode *XFormNode::get_child(int idx)
103 {
104 if(idx >= 0 && idx < get_children_count()) {
105 return children[idx];
106 }
107 return 0;
108 }
110 const XFormNode *XFormNode::get_child(int idx) const
111 {
112 if(idx >= 0 && idx < get_children_count()) {
113 return children[idx];
114 }
115 return 0;
116 }
119 void XFormNode::use_animation(int idx)
120 {
121 if(idx >= 0) {
122 anm_use_animation(anm, idx);
123 }
124 }
126 void XFormNode::use_animation(const char *name)
127 {
128 anm_use_animation(anm, anm_find_animation(anm, name));
129 }
131 void XFormNode::use_animation(int aidx, int bidx, float t)
132 {
133 anm_use_animations(anm, aidx, bidx, t);
134 }
136 void XFormNode::use_animation(const char *aname, const char *bname, float t)
137 {
138 int aidx = anm_find_animation(anm, aname);
139 int bidx = anm_find_animation(anm, bname);
141 if(aidx == -1) {
142 use_animation(bidx);
143 }
144 if(bidx == -1) {
145 use_animation(aidx);
146 }
147 anm_use_animations(anm, aidx, bidx, t);
148 }
150 int XFormNode::get_active_animation_index(int which) const
151 {
152 return anm_get_active_animation_index(anm, which);
153 }
155 float XFormNode::get_active_animation_mix() const
156 {
157 return anm_get_active_animation_mix(anm);
158 }
160 int XFormNode::get_animation_count() const
161 {
162 return anm_get_animation_count(anm);
163 }
165 void XFormNode::add_animation(const char *name)
166 {
167 int idx = get_animation_count();
169 anm_add_animation(anm);
170 use_animation(idx);
172 if(name) {
173 set_animation_name(name);
174 }
175 }
177 void XFormNode::set_animation_name(const char *name)
178 {
179 anm_set_active_animation_name(anm, name);
180 }
182 const char *XFormNode::get_animation_name() const
183 {
184 return anm_get_active_animation_name(anm);
185 }
187 static const int track_type_base[] = {ANM_TRACK_POS_X, ANM_TRACK_ROT_X, ANM_TRACK_SCL_X};
188 static const int track_type_nelem[] = {3, 4, 3};
190 int XFormNode::get_key_count(int trackid) const
191 {
192 struct anm_animation *anim = anm_get_active_animation(anm, 0);
193 return anim->tracks[track_type_base[trackid]].count;
194 }
196 int XFormNode::get_position_key_count() const
197 {
198 return get_key_count(POSITION_TRACK);
199 }
201 int XFormNode::get_rotation_key_count() const
202 {
203 return get_key_count(ROTATION_TRACK);
204 }
206 int XFormNode::get_scaling_key_count() const
207 {
208 return get_key_count(SCALING_TRACK);
209 }
211 long XFormNode::get_key_time(int trackid, int idx) const
212 {
213 struct anm_animation *anim = anm_get_active_animation(anm, 0);
214 struct anm_keyframe *key = anm_get_keyframe(anim->tracks + track_type_base[trackid], idx);
215 return ANM_TM2MSEC(key->time);
216 }
218 long XFormNode::get_position_key_time(int idx) const
219 {
220 return get_key_time(POSITION_TRACK, idx);
221 }
223 long XFormNode::get_rotation_key_time(int idx) const
224 {
225 return get_key_time(ROTATION_TRACK, idx);
226 }
228 long XFormNode::get_scaling_key_time(int idx) const
229 {
230 return get_key_time(SCALING_TRACK, idx);
231 }
233 int XFormNode::get_key_value(int trackid, int idx, float *val) const
234 {
235 struct anm_animation *anim = anm_get_active_animation(anm, 0);
237 int nelem = track_type_nelem[trackid];
238 for(int i=0; i<nelem; i++) {
239 struct anm_keyframe *key = anm_get_keyframe(anim->tracks + track_type_base[trackid] + i, idx);
240 val[i] = key->val;
241 }
242 return nelem;
243 }
245 Vector3 XFormNode::get_position_key_value(int idx) const
246 {
247 float val[3];
248 get_key_value(POSITION_TRACK, idx, val);
249 return Vector3(val[0], val[1], val[2]);
250 }
252 Quaternion XFormNode::get_rotation_key_value(int idx) const
253 {
254 float val[4];
255 get_key_value(ROTATION_TRACK, idx, val);
256 return Quaternion(val[3], val[0], val[1], val[2]);
257 }
259 Vector3 XFormNode::get_scaling_key_value(int idx) const
260 {
261 float val[3];
262 get_key_value(SCALING_TRACK, idx, val);
263 return Vector3(val[0], val[1], val[2]);
264 }
266 void XFormNode::set_position(const Vector3 &pos, long tmsec)
267 {
268 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
269 }
271 Vector3 XFormNode::get_node_position(long tmsec) const
272 {
273 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
274 return Vector3(p.x, p.y, p.z);
275 }
277 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
278 {
279 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
280 }
282 Quaternion XFormNode::get_node_rotation(long tmsec) const
283 {
284 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
285 return Quaternion(q.w, q.x, q.y, q.z);
286 }
288 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
289 {
290 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
291 }
293 Vector3 XFormNode::get_node_scaling(long tmsec) const
294 {
295 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
296 return Vector3(s.x, s.y, s.z);
297 }
299 // these take hierarchy into account
300 Vector3 XFormNode::get_position(long tmsec) const
301 {
302 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
303 return Vector3(v.x, v.y, v.z);
304 }
306 Quaternion XFormNode::get_rotation(long tmsec) const
307 {
308 quat_t q = anm_get_rotation(anm, tmsec);
309 return Quaternion(q.w, q.x, q.y, q.z);
310 }
312 Vector3 XFormNode::get_scaling(long tmsec) const
313 {
314 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
315 return Vector3(v.x, v.y, v.z);
316 }
318 void XFormNode::set_pivot(const Vector3 &pivot)
319 {
320 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
321 }
323 Vector3 XFormNode::get_pivot() const
324 {
325 vec3_t p = anm_get_pivot(anm);
326 return Vector3(p.x, p.y, p.z);
327 }
329 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
330 {
331 local_matrix = mat;
332 }
334 const Matrix4x4 &XFormNode::get_local_matrix() const
335 {
336 return local_matrix;
337 }
339 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
340 {
341 bone_matrix = bmat;
342 }
344 const Matrix4x4 &XFormNode::get_bone_matrix() const
345 {
346 return bone_matrix;
347 }
349 #define FOO
351 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
352 {
353 anm_time_t tm = ANM_MSEC2TM(tmsec);
355 if(mat) {
356 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
357 #ifdef FOO
358 *mat = local_matrix * *mat;
359 #else
360 *mat = *mat * local_matrix;
361 #endif
362 }
363 if(inv_mat) {
364 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
365 }
366 }
368 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
369 {
370 anm_time_t tm = ANM_MSEC2TM(tmsec);
372 if(mat) {
373 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
374 #ifdef FOO
375 *mat = local_matrix * *mat;
376 #else
377 *mat = *mat * local_matrix;
378 #endif
379 }
380 if(inv_mat) {
381 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
382 }
383 }
386 // ---- Track ----
388 Track::Track()
389 {
390 trk = new anm_track;
391 anm_init_track(trk);
392 }
394 Track::~Track()
395 {
396 anm_destroy_track(trk);
397 delete trk;
398 }
400 Track::Track(const Track &rhs)
401 {
402 trk = new anm_track;
403 anm_init_track(trk);
404 anm_copy_track(trk, rhs.trk);
405 interp = rhs.interp;
406 extrap = rhs.extrap;
407 }
409 Track &Track::operator =(const Track &rhs)
410 {
411 if(&rhs == this) {
412 return *this;
413 }
415 anm_copy_track(trk, rhs.trk);
416 interp = rhs.interp;
417 extrap = rhs.extrap;
418 return *this;
419 }
422 void Track::set_interpolator(Interp in)
423 {
424 anm_set_track_interpolator(trk, track_interpolator(in));
425 interp = in;
426 }
428 Interp Track::get_interpolator() const
429 {
430 return interp;
431 }
433 void Track::set_extrapolator(Extrap ex)
434 {
435 anm_set_track_extrapolator(trk, track_extrapolator(ex));
436 extrap = ex;
437 }
439 Extrap Track::get_extrapolator() const
440 {
441 return extrap;
442 }
444 void Track::set_default(double def)
445 {
446 anm_set_track_default(trk, def);
447 }
449 void Track::set_value(float val, long tmsec)
450 {
451 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
452 }
454 float Track::get_value(long tmsec) const
455 {
456 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
457 }
459 float Track::operator ()(long tmsec) const
460 {
461 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
462 }
465 // ---- Track3 ----
467 void Track3::set_interpolator(Interp in)
468 {
469 for(int i=0; i<3; i++) {
470 track[i].set_interpolator(in);
471 }
472 }
474 Interp Track3::get_interpolator() const
475 {
476 return track[0].get_interpolator();
477 }
479 void Track3::set_extrapolator(Extrap ex)
480 {
481 for(int i=0; i<3; i++) {
482 track[i].set_extrapolator(ex);
483 }
484 }
486 Extrap Track3::get_extrapolator() const
487 {
488 return track[0].get_extrapolator();
489 }
491 void Track3::set_default(const Vector3 &def)
492 {
493 for(int i=0; i<3; i++) {
494 track[i].set_default(def[i]);
495 }
496 }
498 void Track3::set_value(const Vector3 &val, long tmsec)
499 {
500 for(int i=0; i<3; i++) {
501 track[i].set_value(val[i], tmsec);
502 }
503 }
505 Vector3 Track3::get_value(long tmsec) const
506 {
507 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
508 }
510 Vector3 Track3::operator ()(long tmsec) const
511 {
512 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
513 }
516 static inline anm_interpolator track_interpolator(Interp in)
517 {
518 switch(in) {
519 case INTERP_STEP:
520 return ANM_INTERP_STEP;
521 case INTERP_LINEAR:
522 return ANM_INTERP_LINEAR;
523 case INTERP_CUBIC:
524 return ANM_INTERP_CUBIC;
525 }
527 assert(0);
528 return ANM_INTERP_STEP;
529 }
531 static inline anm_extrapolator track_extrapolator(Extrap ex)
532 {
533 switch(ex) {
534 case EXTRAP_EXTEND:
535 return ANM_EXTRAP_EXTEND;
536 case EXTRAP_CLAMP:
537 return ANM_EXTRAP_CLAMP;
538 case EXTRAP_REPEAT:
539 return ANM_EXTRAP_REPEAT;
540 }
542 assert(0);
543 return ANM_EXTRAP_EXTEND;
544 }