goat3d
view src/xform_node.cc @ 66:3751aabbc5b3
igame animation api is weird...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 19 Apr 2014 07:56:43 +0300 |
parents | 9ef9de80649c |
children | 8970ca3d55e0 |
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 if(!child || child == this) return;
80 children.push_back(child);
81 anm_link_node(anm, child->anm);
82 child->parent = this;
83 }
85 void XFormNode::remove_child(XFormNode *child)
86 {
87 std::vector<XFormNode*>::iterator it;
88 it = std::find(children.begin(), children.end(), child);
89 if(it != children.end()) {
90 children.erase(it);
91 anm_unlink_node(anm, child->anm);
93 if(child->parent == this) {
94 child->parent = 0;
95 }
96 }
97 }
99 int XFormNode::get_children_count() const
100 {
101 return (int)children.size();
102 }
104 XFormNode *XFormNode::get_child(int idx)
105 {
106 if(idx >= 0 && idx < get_children_count()) {
107 return children[idx];
108 }
109 return 0;
110 }
112 const XFormNode *XFormNode::get_child(int idx) const
113 {
114 if(idx >= 0 && idx < get_children_count()) {
115 return children[idx];
116 }
117 return 0;
118 }
121 void XFormNode::use_animation(int idx)
122 {
123 if(idx >= 0) {
124 anm_use_animation(anm, idx);
125 }
126 }
128 void XFormNode::use_animation(const char *name)
129 {
130 anm_use_animation(anm, anm_find_animation(anm, name));
131 }
133 void XFormNode::use_animation(int aidx, int bidx, float t)
134 {
135 anm_use_animations(anm, aidx, bidx, t);
136 }
138 void XFormNode::use_animation(const char *aname, const char *bname, float t)
139 {
140 int aidx = anm_find_animation(anm, aname);
141 int bidx = anm_find_animation(anm, bname);
143 if(aidx == -1) {
144 use_animation(bidx);
145 }
146 if(bidx == -1) {
147 use_animation(aidx);
148 }
149 anm_use_animations(anm, aidx, bidx, t);
150 }
152 int XFormNode::get_active_animation_index(int which) const
153 {
154 return anm_get_active_animation_index(anm, which);
155 }
157 float XFormNode::get_active_animation_mix() const
158 {
159 return anm_get_active_animation_mix(anm);
160 }
162 int XFormNode::get_animation_count() const
163 {
164 return anm_get_animation_count(anm);
165 }
167 void XFormNode::add_animation(const char *name)
168 {
169 int idx = get_animation_count();
171 anm_add_animation(anm);
172 use_animation(idx);
174 if(name) {
175 set_animation_name(name);
176 }
177 }
179 void XFormNode::set_animation_name(const char *name)
180 {
181 anm_set_active_animation_name(anm, name);
182 }
184 const char *XFormNode::get_animation_name() const
185 {
186 return anm_get_active_animation_name(anm);
187 }
189 static const int track_type_base[] = {ANM_TRACK_POS_X, ANM_TRACK_ROT_X, ANM_TRACK_SCL_X};
190 static const int track_type_nelem[] = {3, 4, 3};
192 int XFormNode::get_key_count(int trackid) const
193 {
194 struct anm_animation *anim = anm_get_active_animation(anm, 0);
195 return anim->tracks[track_type_base[trackid]].count;
196 }
198 int XFormNode::get_position_key_count() const
199 {
200 return get_key_count(POSITION_TRACK);
201 }
203 int XFormNode::get_rotation_key_count() const
204 {
205 return get_key_count(ROTATION_TRACK);
206 }
208 int XFormNode::get_scaling_key_count() const
209 {
210 return get_key_count(SCALING_TRACK);
211 }
213 long XFormNode::get_key_time(int trackid, int idx) const
214 {
215 struct anm_animation *anim = anm_get_active_animation(anm, 0);
216 struct anm_keyframe *key = anm_get_keyframe(anim->tracks + track_type_base[trackid], idx);
217 return ANM_TM2MSEC(key->time);
218 }
220 long XFormNode::get_position_key_time(int idx) const
221 {
222 return get_key_time(POSITION_TRACK, idx);
223 }
225 long XFormNode::get_rotation_key_time(int idx) const
226 {
227 return get_key_time(ROTATION_TRACK, idx);
228 }
230 long XFormNode::get_scaling_key_time(int idx) const
231 {
232 return get_key_time(SCALING_TRACK, idx);
233 }
235 int XFormNode::get_key_value(int trackid, int idx, float *val) const
236 {
237 struct anm_animation *anim = anm_get_active_animation(anm, 0);
239 int nelem = track_type_nelem[trackid];
240 for(int i=0; i<nelem; i++) {
241 struct anm_keyframe *key = anm_get_keyframe(anim->tracks + track_type_base[trackid] + i, idx);
242 val[i] = key->val;
243 }
244 return nelem;
245 }
247 Vector3 XFormNode::get_position_key_value(int idx) const
248 {
249 float val[3];
250 get_key_value(POSITION_TRACK, idx, val);
251 return Vector3(val[0], val[1], val[2]);
252 }
254 Quaternion XFormNode::get_rotation_key_value(int idx) const
255 {
256 float val[4];
257 get_key_value(ROTATION_TRACK, idx, val);
258 return Quaternion(val[3], val[0], val[1], val[2]);
259 }
261 Vector3 XFormNode::get_scaling_key_value(int idx) const
262 {
263 float val[3];
264 get_key_value(SCALING_TRACK, idx, val);
265 return Vector3(val[0], val[1], val[2]);
266 }
268 void XFormNode::set_position(const Vector3 &pos, long tmsec)
269 {
270 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
271 }
273 Vector3 XFormNode::get_node_position(long tmsec) const
274 {
275 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
276 return Vector3(p.x, p.y, p.z);
277 }
279 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
280 {
281 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
282 }
284 Quaternion XFormNode::get_node_rotation(long tmsec) const
285 {
286 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
287 return Quaternion(q.w, q.x, q.y, q.z);
288 }
290 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
291 {
292 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
293 }
295 Vector3 XFormNode::get_node_scaling(long tmsec) const
296 {
297 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
298 return Vector3(s.x, s.y, s.z);
299 }
301 // these take hierarchy into account
302 Vector3 XFormNode::get_position(long tmsec) const
303 {
304 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
305 return Vector3(v.x, v.y, v.z);
306 }
308 Quaternion XFormNode::get_rotation(long tmsec) const
309 {
310 quat_t q = anm_get_rotation(anm, tmsec);
311 return Quaternion(q.w, q.x, q.y, q.z);
312 }
314 Vector3 XFormNode::get_scaling(long tmsec) const
315 {
316 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
317 return Vector3(v.x, v.y, v.z);
318 }
320 void XFormNode::set_pivot(const Vector3 &pivot)
321 {
322 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
323 }
325 Vector3 XFormNode::get_pivot() const
326 {
327 vec3_t p = anm_get_pivot(anm);
328 return Vector3(p.x, p.y, p.z);
329 }
331 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
332 {
333 local_matrix = mat;
334 }
336 const Matrix4x4 &XFormNode::get_local_matrix() const
337 {
338 return local_matrix;
339 }
341 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
342 {
343 bone_matrix = bmat;
344 }
346 const Matrix4x4 &XFormNode::get_bone_matrix() const
347 {
348 return bone_matrix;
349 }
351 #define FOO
353 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
354 {
355 anm_time_t tm = ANM_MSEC2TM(tmsec);
357 if(mat) {
358 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
359 #ifdef FOO
360 *mat = local_matrix * *mat;
361 #else
362 *mat = *mat * local_matrix;
363 #endif
364 }
365 if(inv_mat) {
366 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
367 }
368 }
370 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
371 {
372 anm_time_t tm = ANM_MSEC2TM(tmsec);
374 if(mat) {
375 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
376 #ifdef FOO
377 *mat = local_matrix * *mat;
378 #else
379 *mat = *mat * local_matrix;
380 #endif
381 }
382 if(inv_mat) {
383 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
384 }
385 }
388 // ---- Track ----
390 Track::Track()
391 {
392 trk = new anm_track;
393 anm_init_track(trk);
394 }
396 Track::~Track()
397 {
398 anm_destroy_track(trk);
399 delete trk;
400 }
402 Track::Track(const Track &rhs)
403 {
404 trk = new anm_track;
405 anm_init_track(trk);
406 anm_copy_track(trk, rhs.trk);
407 interp = rhs.interp;
408 extrap = rhs.extrap;
409 }
411 Track &Track::operator =(const Track &rhs)
412 {
413 if(&rhs == this) {
414 return *this;
415 }
417 anm_copy_track(trk, rhs.trk);
418 interp = rhs.interp;
419 extrap = rhs.extrap;
420 return *this;
421 }
424 void Track::set_interpolator(Interp in)
425 {
426 anm_set_track_interpolator(trk, track_interpolator(in));
427 interp = in;
428 }
430 Interp Track::get_interpolator() const
431 {
432 return interp;
433 }
435 void Track::set_extrapolator(Extrap ex)
436 {
437 anm_set_track_extrapolator(trk, track_extrapolator(ex));
438 extrap = ex;
439 }
441 Extrap Track::get_extrapolator() const
442 {
443 return extrap;
444 }
446 void Track::set_default(double def)
447 {
448 anm_set_track_default(trk, def);
449 }
451 void Track::set_value(float val, long tmsec)
452 {
453 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
454 }
456 float Track::get_value(long tmsec) const
457 {
458 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
459 }
461 float Track::operator ()(long tmsec) const
462 {
463 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
464 }
467 // ---- Track3 ----
469 void Track3::set_interpolator(Interp in)
470 {
471 for(int i=0; i<3; i++) {
472 track[i].set_interpolator(in);
473 }
474 }
476 Interp Track3::get_interpolator() const
477 {
478 return track[0].get_interpolator();
479 }
481 void Track3::set_extrapolator(Extrap ex)
482 {
483 for(int i=0; i<3; i++) {
484 track[i].set_extrapolator(ex);
485 }
486 }
488 Extrap Track3::get_extrapolator() const
489 {
490 return track[0].get_extrapolator();
491 }
493 void Track3::set_default(const Vector3 &def)
494 {
495 for(int i=0; i<3; i++) {
496 track[i].set_default(def[i]);
497 }
498 }
500 void Track3::set_value(const Vector3 &val, long tmsec)
501 {
502 for(int i=0; i<3; i++) {
503 track[i].set_value(val[i], tmsec);
504 }
505 }
507 Vector3 Track3::get_value(long tmsec) const
508 {
509 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
510 }
512 Vector3 Track3::operator ()(long tmsec) const
513 {
514 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
515 }
518 static inline anm_interpolator track_interpolator(Interp in)
519 {
520 switch(in) {
521 case INTERP_STEP:
522 return ANM_INTERP_STEP;
523 case INTERP_LINEAR:
524 return ANM_INTERP_LINEAR;
525 case INTERP_CUBIC:
526 return ANM_INTERP_CUBIC;
527 }
529 assert(0);
530 return ANM_INTERP_STEP;
531 }
533 static inline anm_extrapolator track_extrapolator(Extrap ex)
534 {
535 switch(ex) {
536 case EXTRAP_EXTEND:
537 return ANM_EXTRAP_EXTEND;
538 case EXTRAP_CLAMP:
539 return ANM_EXTRAP_CLAMP;
540 case EXTRAP_REPEAT:
541 return ANM_EXTRAP_REPEAT;
542 }
544 assert(0);
545 return ANM_EXTRAP_EXTEND;
546 }