libanim

view src/anim.c @ 61:29946a9423a4

implemented high-level animation blending interface
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 30 Dec 2013 15:20:31 +0200
parents 203c11299586
children ebead35c9eb1
line source
1 #include <stdlib.h>
2 #include <limits.h>
3 #include <assert.h>
4 #include "anim.h"
5 #include "dynarr.h"
7 #define ROT_USE_SLERP
9 static void invalidate_cache(struct anm_node *node);
11 int anm_init_animation(struct anm_animation *anim)
12 {
13 int i, j;
14 static const float defaults[] = {
15 0.0f, 0.0f, 0.0f, /* default position */
16 0.0f, 0.0f, 0.0f, 1.0f, /* default rotation quat */
17 1.0f, 1.0f, 1.0f /* default scale factor */
18 };
20 anim->name = 0;
22 for(i=0; i<ANM_NUM_TRACKS; i++) {
23 if(anm_init_track(anim->tracks + i) == -1) {
24 for(j=0; j<i; j++) {
25 anm_destroy_track(anim->tracks + i);
26 }
27 }
28 anm_set_track_default(anim->tracks + i, defaults[i]);
29 }
30 return 0;
31 }
33 void anm_destroy_animation(struct anm_animation *anim)
34 {
35 int i;
36 for(i=0; i<ANM_NUM_TRACKS; i++) {
37 anm_destroy_track(anim->tracks + i);
38 }
39 free(anim->name);
40 }
42 void anm_set_animation_name(struct anm_animation *anim, const char *name)
43 {
44 char *newname = malloc(strlen(name) + 1);
45 if(!newname) return;
47 strcpy(newname, name);
49 free(anim->name);
50 anim->name = newname;
51 }
53 /* ---- node implementation ----- */
55 int anm_init_node(struct anm_node *node)
56 {
57 memset(node, 0, sizeof *node);
59 node->cur_anim[1] = -1;
61 if(!(node->animations = dynarr_alloc(1, sizeof *node->animations))) {
62 return -1;
63 }
64 if(anm_init_animation(node->animations) == -1) {
65 dynarr_free(node->animations);
66 return -1;
67 }
69 /* initialize thread-local matrix cache */
70 pthread_key_create(&node->cache_key, 0);
71 pthread_mutex_init(&node->cache_list_lock, 0);
73 return 0;
74 }
76 void anm_destroy_node(struct anm_node *node)
77 {
78 int i;
79 free(node->name);
81 for(i=0; i<ANM_NUM_TRACKS; i++) {
82 anm_destroy_animation(node->animations + i);
83 }
84 dynarr_free(node->animations);
86 /* destroy thread-specific cache */
87 pthread_key_delete(node->cache_key);
89 while(node->cache_list) {
90 struct mat_cache *tmp = node->cache_list;
91 node->cache_list = tmp->next;
92 free(tmp);
93 }
94 }
96 void anm_destroy_node_tree(struct anm_node *tree)
97 {
98 struct anm_node *c, *tmp;
100 if(!tree) return;
102 c = tree->child;
103 while(c) {
104 tmp = c;
105 c = c->next;
107 anm_destroy_node_tree(tmp);
108 }
109 anm_destroy_node(tree);
110 }
112 struct anm_node *anm_create_node(void)
113 {
114 struct anm_node *n;
116 if((n = malloc(sizeof *n))) {
117 if(anm_init_node(n) == -1) {
118 free(n);
119 return 0;
120 }
121 }
122 return n;
123 }
125 void anm_free_node(struct anm_node *node)
126 {
127 anm_destroy_node(node);
128 free(node);
129 }
131 void anm_free_node_tree(struct anm_node *tree)
132 {
133 struct anm_node *c, *tmp;
135 if(!tree) return;
137 c = tree->child;
138 while(c) {
139 tmp = c;
140 c = c->next;
142 anm_free_node_tree(tmp);
143 }
145 anm_free_node(tree);
146 }
148 int anm_set_node_name(struct anm_node *node, const char *name)
149 {
150 char *str;
152 if(!(str = malloc(strlen(name) + 1))) {
153 return -1;
154 }
155 strcpy(str, name);
156 free(node->name);
157 node->name = str;
158 return 0;
159 }
161 const char *anm_get_node_name(struct anm_node *node)
162 {
163 return node->name ? node->name : "";
164 }
166 void anm_link_node(struct anm_node *p, struct anm_node *c)
167 {
168 c->next = p->child;
169 p->child = c;
171 c->parent = p;
172 invalidate_cache(c);
173 }
175 int anm_unlink_node(struct anm_node *p, struct anm_node *c)
176 {
177 struct anm_node *iter;
179 if(p->child == c) {
180 p->child = c->next;
181 c->next = 0;
182 invalidate_cache(c);
183 return 0;
184 }
186 iter = p->child;
187 while(iter->next) {
188 if(iter->next == c) {
189 iter->next = c->next;
190 c->next = 0;
191 invalidate_cache(c);
192 return 0;
193 }
194 }
195 return -1;
196 }
198 void anm_set_pivot(struct anm_node *node, vec3_t piv)
199 {
200 node->pivot = piv;
201 }
203 vec3_t anm_get_pivot(struct anm_node *node)
204 {
205 return node->pivot;
206 }
209 /* animation management */
211 int anm_use_node_animation(struct anm_node *node, int aidx)
212 {
213 if(aidx == node->cur_anim[0] && node->cur_anim[1] == -1) {
214 return 0; /* no change, no invalidation */
215 }
217 if(aidx < 0 || aidx >= anm_get_animation_count(node)) {
218 return -1;
219 }
221 node->cur_anim[0] = aidx;
222 node->cur_anim[1] = -1;
223 node->cur_mix = 0;
224 node->blend_dur = -1;
226 invalidate_cache(node);
227 return 0;
228 }
230 int anm_use_node_animations(struct anm_node *node, int aidx, int bidx, float t)
231 {
232 int num_anim;
234 if(node->cur_anim[0] == aidx && node->cur_anim[1] == bidx &&
235 fabs(t - node->cur_mix) < 1e-6) {
236 return 0; /* no change, no invalidation */
237 }
239 num_anim = anm_get_animation_count(node);
240 if(aidx < 0 || aidx >= num_anim) {
241 return anm_use_animation(node, bidx);
242 }
243 if(bidx < 0 || bidx >= num_anim) {
244 return anm_use_animation(node, aidx);
245 }
246 node->cur_anim[0] = aidx;
247 node->cur_anim[1] = bidx;
248 node->cur_mix = t;
250 invalidate_cache(node);
251 return 0;
252 }
254 int anm_use_animation(struct anm_node *node, int aidx)
255 {
256 struct anm_node *child;
258 if(anm_use_node_animation(node, aidx) == -1) {
259 return -1;
260 }
262 child = node->child;
263 while(child) {
264 if(anm_use_animation(child, aidx) == -1) {
265 return -1;
266 }
267 child = child->next;
268 }
269 return 0;
270 }
272 int anm_use_animations(struct anm_node *node, int aidx, int bidx, float t)
273 {
274 struct anm_node *child;
276 if(anm_use_node_animations(node, aidx, bidx, t) == -1) {
277 return -1;
278 }
280 child = node->child;
281 while(child) {
282 if(anm_use_animations(child, aidx, bidx, t) == -1) {
283 return -1;
284 }
285 child = child->next;
286 }
287 return 0;
289 }
291 void anm_set_node_animation_offset(struct anm_node *node, anm_time_t offs, int which)
292 {
293 if(which < 0 || which >= 2) {
294 return;
295 }
296 node->cur_anim_offset[which] = offs;
297 }
299 anm_time_t anm_get_animation_offset(const struct anm_node *node, int which)
300 {
301 if(which < 0 || which >= 2) {
302 return 0;
303 }
304 return node->cur_anim_offset[which];
305 }
307 void anm_set_animation_offset(struct anm_node *node, anm_time_t offs, int which)
308 {
309 struct anm_node *c = node->child;
310 while(c) {
311 anm_set_animation_offset(c, offs, which);
312 c = c->next;
313 }
315 anm_set_node_animation_offset(node, offs, which);
316 }
318 int anm_get_active_animation_index(const struct anm_node *node, int which)
319 {
320 if(which < 0 || which >= 2) return -1;
321 return node->cur_anim[which];
322 }
324 struct anm_animation *anm_get_active_animation(const struct anm_node *node, int which)
325 {
326 int idx = anm_get_active_animation_index(node, which);
327 if(idx < 0 || idx >= anm_get_animation_count(node)) {
328 return 0;
329 }
330 return node->animations + idx;
331 }
333 float anm_get_active_animation_mix(const struct anm_node *node)
334 {
335 return node->cur_mix;
336 }
338 int anm_get_animation_count(const struct anm_node *node)
339 {
340 return dynarr_size(node->animations);
341 }
343 int anm_add_node_animation(struct anm_node *node)
344 {
345 struct anm_animation newanim;
346 anm_init_animation(&newanim);
348 node->animations = dynarr_push(node->animations, &newanim);
349 return 0;
350 }
352 int anm_remove_node_animation(struct anm_node *node, int idx)
353 {
354 fprintf(stderr, "anm_remove_animation: unimplemented!");
355 abort();
356 return 0;
357 }
359 int anm_add_animation(struct anm_node *node)
360 {
361 struct anm_node *child;
363 if(anm_add_node_animation(node) == -1) {
364 return -1;
365 }
367 child = node->child;
368 while(child) {
369 if(anm_add_animation(child)) {
370 return -1;
371 }
372 child = child->next;
373 }
374 return 0;
375 }
377 int anm_remove_animation(struct anm_node *node, int idx)
378 {
379 struct anm_node *child;
381 if(anm_remove_node_animation(node, idx) == -1) {
382 return -1;
383 }
385 child = node->child;
386 while(child) {
387 if(anm_remove_animation(child, idx) == -1) {
388 return -1;
389 }
390 child = child->next;
391 }
392 return 0;
393 }
395 struct anm_animation *anm_get_animation(struct anm_node *node, int idx)
396 {
397 if(idx < 0 || idx > anm_get_animation_count(node)) {
398 return 0;
399 }
400 return node->animations + idx;
401 }
403 struct anm_animation *anm_get_animation_by_name(struct anm_node *node, const char *name)
404 {
405 return anm_get_animation(node, anm_find_animation(node, name));
406 }
408 int anm_find_animation(struct anm_node *node, const char *name)
409 {
410 int i, count = anm_get_animation_count(node);
411 for(i=0; i<count; i++) {
412 if(strcmp(node->animations[i].name, name) == 0) {
413 return i;
414 }
415 }
416 return -1;
417 }
419 /* all the rest act on the current animation(s) */
421 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in)
422 {
423 int i;
424 struct anm_animation *anim = anm_get_active_animation(node, 0);
425 if(!anim) return;
427 for(i=0; i<ANM_NUM_TRACKS; i++) {
428 anm_set_track_interpolator(anim->tracks + i, in);
429 }
430 invalidate_cache(node);
431 }
433 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex)
434 {
435 int i;
436 struct anm_animation *anim = anm_get_active_animation(node, 0);
437 if(!anim) return;
439 for(i=0; i<ANM_NUM_TRACKS; i++) {
440 anm_set_track_extrapolator(anim->tracks + i, ex);
441 }
442 invalidate_cache(node);
443 }
445 void anm_set_node_active_animation_name(struct anm_node *node, const char *name)
446 {
447 struct anm_animation *anim = anm_get_active_animation(node, 0);
448 if(!anim) return;
450 anm_set_animation_name(anim, name);
451 }
453 void anm_set_active_animation_name(struct anm_node *node, const char *name)
454 {
455 struct anm_node *child;
457 anm_set_node_active_animation_name(node, name);
459 child = node->child;
460 while(child) {
461 anm_set_active_animation_name(child, name);
462 child = child->next;
463 }
464 }
466 const char *anm_get_active_animation_name(struct anm_node *node)
467 {
468 struct anm_animation *anim = anm_get_active_animation(node, 0);
469 if(anim) {
470 return anim->name;
471 }
472 return 0;
473 }
475 /* ---- high level animation blending ---- */
476 void anm_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur)
477 {
478 struct anm_node *c = node->child;
479 while(c) {
480 anm_transition(c, anmidx, start, dur);
481 c = c->next;
482 }
484 anm_node_transition(node, anmidx, start, dur);
485 }
487 void anm_node_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur)
488 {
489 node->cur_anim[1] = anmidx;
490 node->cur_anim_offset[1] = start;
491 node->blend_dur = dur;
492 }
495 #define BLEND_START_TM node->cur_anim_offset[1]
497 static anm_time_t animation_time(struct anm_node *node, anm_time_t tm, int which)
498 {
499 float t;
501 if(node->blend_dur >= 0) {
502 /* we're in transition... */
503 t = (float)(tm - BLEND_START_TM) / (float)node->blend_dur;
504 if(t < 0.0) t = 0.0;
506 node->cur_mix = t;
508 if(t > 1.0) {
509 /* switch completely over to the target animation and stop blending */
510 anm_use_node_animation(node, node->cur_anim[1]);
511 node->cur_anim_offset[0] = node->cur_anim_offset[1];
512 }
513 }
515 return tm - node->cur_anim_offset[which];
516 }
519 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm)
520 {
521 struct anm_animation *anim = anm_get_active_animation(node, 0);
522 if(!anim) return;
524 anm_set_value(anim->tracks + ANM_TRACK_POS_X, tm, pos.x);
525 anm_set_value(anim->tracks + ANM_TRACK_POS_Y, tm, pos.y);
526 anm_set_value(anim->tracks + ANM_TRACK_POS_Z, tm, pos.z);
527 invalidate_cache(node);
528 }
531 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm)
532 {
533 vec3_t v;
534 anm_time_t tm0 = animation_time(node, tm, 0);
535 struct anm_animation *anim0 = anm_get_active_animation(node, 0);
536 struct anm_animation *anim1 = anm_get_active_animation(node, 1);
538 if(!anim0) {
539 return v3_cons(0, 0, 0);
540 }
542 v.x = anm_get_value(anim0->tracks + ANM_TRACK_POS_X, tm0);
543 v.y = anm_get_value(anim0->tracks + ANM_TRACK_POS_Y, tm0);
544 v.z = anm_get_value(anim0->tracks + ANM_TRACK_POS_Z, tm0);
546 if(anim1) {
547 vec3_t v1;
548 anm_time_t tm1 = animation_time(node, tm, 1);
549 v1.x = anm_get_value(anim1->tracks + ANM_TRACK_POS_X, tm1);
550 v1.y = anm_get_value(anim1->tracks + ANM_TRACK_POS_Y, tm1);
551 v1.z = anm_get_value(anim1->tracks + ANM_TRACK_POS_Z, tm1);
553 v.x = v.x + (v1.x - v.x) * node->cur_mix;
554 v.y = v.y + (v1.y - v.y) * node->cur_mix;
555 v.z = v.z + (v1.z - v.z) * node->cur_mix;
556 }
558 return v;
559 }
561 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm)
562 {
563 struct anm_animation *anim = anm_get_active_animation(node, 0);
564 if(!anim) return;
566 anm_set_value(anim->tracks + ANM_TRACK_ROT_X, tm, rot.x);
567 anm_set_value(anim->tracks + ANM_TRACK_ROT_Y, tm, rot.y);
568 anm_set_value(anim->tracks + ANM_TRACK_ROT_Z, tm, rot.z);
569 anm_set_value(anim->tracks + ANM_TRACK_ROT_W, tm, rot.w);
570 invalidate_cache(node);
571 }
573 static quat_t get_node_rotation(struct anm_node *node, anm_time_t tm, struct anm_animation *anim)
574 {
575 #ifndef ROT_USE_SLERP
576 quat_t q;
577 q.x = anm_get_value(anim->tracks + ANM_TRACK_ROT_X, tm);
578 q.y = anm_get_value(anim->tracks + ANM_TRACK_ROT_Y, tm);
579 q.z = anm_get_value(anim->tracks + ANM_TRACK_ROT_Z, tm);
580 q.w = anm_get_value(anim->tracks + ANM_TRACK_ROT_W, tm);
581 return q;
582 #else
583 int idx0, idx1, last_idx;
584 anm_time_t tstart, tend;
585 float t, dt;
586 struct anm_track *track_x, *track_y, *track_z, *track_w;
587 quat_t q, q1, q2;
589 track_x = anim->tracks + ANM_TRACK_ROT_X;
590 track_y = anim->tracks + ANM_TRACK_ROT_Y;
591 track_z = anim->tracks + ANM_TRACK_ROT_Z;
592 track_w = anim->tracks + ANM_TRACK_ROT_W;
594 if(!track_x->count) {
595 q.x = track_x->def_val;
596 q.y = track_y->def_val;
597 q.z = track_z->def_val;
598 q.w = track_w->def_val;
599 return q;
600 }
602 last_idx = track_x->count - 1;
604 tstart = track_x->keys[0].time;
605 tend = track_x->keys[last_idx].time;
607 if(tstart == tend) {
608 q.x = track_x->keys[0].val;
609 q.y = track_y->keys[0].val;
610 q.z = track_z->keys[0].val;
611 q.w = track_w->keys[0].val;
612 return q;
613 }
615 tm = anm_remap_time(track_x, tm, tstart, tend);
617 idx0 = anm_get_key_interval(track_x, tm);
618 assert(idx0 >= 0 && idx0 < track_x->count);
619 idx1 = idx0 + 1;
621 if(idx0 == last_idx) {
622 q.x = track_x->keys[idx0].val;
623 q.y = track_y->keys[idx0].val;
624 q.z = track_z->keys[idx0].val;
625 q.w = track_w->keys[idx0].val;
626 return q;
627 }
629 dt = (float)(track_x->keys[idx1].time - track_x->keys[idx0].time);
630 t = (float)(tm - track_x->keys[idx0].time) / dt;
632 q1.x = track_x->keys[idx0].val;
633 q1.y = track_y->keys[idx0].val;
634 q1.z = track_z->keys[idx0].val;
635 q1.w = track_w->keys[idx0].val;
637 q2.x = track_x->keys[idx1].val;
638 q2.y = track_y->keys[idx1].val;
639 q2.z = track_z->keys[idx1].val;
640 q2.w = track_w->keys[idx1].val;
642 /*q1 = quat_normalize(q1);
643 q2 = quat_normalize(q2);*/
645 return quat_slerp(q1, q2, t);
646 #endif
647 }
649 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm)
650 {
651 quat_t q;
652 anm_time_t tm0 = animation_time(node, tm, 0);
653 struct anm_animation *anim0 = anm_get_active_animation(node, 0);
654 struct anm_animation *anim1 = anm_get_active_animation(node, 1);
656 if(!anim0) {
657 return quat_identity();
658 }
660 q = get_node_rotation(node, tm0, anim0);
662 if(anim1) {
663 anm_time_t tm1 = animation_time(node, tm, 1);
664 quat_t q1 = get_node_rotation(node, tm1, anim1);
666 q = quat_slerp(q, q1, node->cur_mix);
667 }
668 return q;
669 }
671 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm)
672 {
673 struct anm_animation *anim = anm_get_active_animation(node, 0);
674 if(!anim) return;
676 anm_set_value(anim->tracks + ANM_TRACK_SCL_X, tm, scl.x);
677 anm_set_value(anim->tracks + ANM_TRACK_SCL_Y, tm, scl.y);
678 anm_set_value(anim->tracks + ANM_TRACK_SCL_Z, tm, scl.z);
679 invalidate_cache(node);
680 }
682 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm)
683 {
684 vec3_t v;
685 anm_time_t tm0 = animation_time(node, tm, 0);
686 struct anm_animation *anim0 = anm_get_active_animation(node, 0);
687 struct anm_animation *anim1 = anm_get_active_animation(node, 1);
689 if(!anim0) {
690 return v3_cons(1, 1, 1);
691 }
693 v.x = anm_get_value(anim0->tracks + ANM_TRACK_SCL_X, tm0);
694 v.y = anm_get_value(anim0->tracks + ANM_TRACK_SCL_Y, tm0);
695 v.z = anm_get_value(anim0->tracks + ANM_TRACK_SCL_Z, tm0);
697 if(anim1) {
698 vec3_t v1;
699 anm_time_t tm1 = animation_time(node, tm, 1);
700 v1.x = anm_get_value(anim1->tracks + ANM_TRACK_SCL_X, tm1);
701 v1.y = anm_get_value(anim1->tracks + ANM_TRACK_SCL_Y, tm1);
702 v1.z = anm_get_value(anim1->tracks + ANM_TRACK_SCL_Z, tm1);
704 v.x = v.x + (v1.x - v.x) * node->cur_mix;
705 v.y = v.y + (v1.y - v.y) * node->cur_mix;
706 v.z = v.z + (v1.z - v.z) * node->cur_mix;
707 }
709 return v;
710 }
713 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm)
714 {
715 mat4_t xform;
716 vec3_t pos = {0.0, 0.0, 0.0};
718 if(!node->parent) {
719 return anm_get_node_position(node, tm);
720 }
722 anm_get_matrix(node, xform, tm);
723 return v3_transform(pos, xform);
724 }
726 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm)
727 {
728 quat_t rot, prot;
729 rot = anm_get_node_rotation(node, tm);
731 if(!node->parent) {
732 return rot;
733 }
735 prot = anm_get_rotation(node->parent, tm);
736 return quat_mul(prot, rot);
737 }
739 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm)
740 {
741 vec3_t s, ps;
742 s = anm_get_node_scaling(node, tm);
744 if(!node->parent) {
745 return s;
746 }
748 ps = anm_get_scaling(node->parent, tm);
749 return v3_mul(s, ps);
750 }
752 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
753 {
754 int i;
755 mat4_t rmat;
756 vec3_t pos, scale;
757 quat_t rot;
759 pos = anm_get_node_position(node, tm);
760 rot = anm_get_node_rotation(node, tm);
761 scale = anm_get_node_scaling(node, tm);
763 m4_set_translation(mat, node->pivot.x, node->pivot.y, node->pivot.z);
765 quat_to_mat4(rmat, rot);
766 for(i=0; i<3; i++) {
767 mat[i][0] = rmat[i][0];
768 mat[i][1] = rmat[i][1];
769 mat[i][2] = rmat[i][2];
770 }
771 /* this loop is equivalent to: m4_mult(mat, mat, rmat); */
773 mat[0][0] *= scale.x; mat[0][1] *= scale.y; mat[0][2] *= scale.z; mat[0][3] += pos.x;
774 mat[1][0] *= scale.x; mat[1][1] *= scale.y; mat[1][2] *= scale.z; mat[1][3] += pos.y;
775 mat[2][0] *= scale.x; mat[2][1] *= scale.y; mat[2][2] *= scale.z; mat[2][3] += pos.z;
777 m4_translate(mat, -node->pivot.x, -node->pivot.y, -node->pivot.z);
779 /* that's basically: pivot * rotation * translation * scaling * -pivot */
780 }
782 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
783 {
784 mat4_t tmp;
785 anm_get_node_matrix(node, tmp, tm);
786 m4_inverse(mat, tmp);
787 }
789 void anm_eval_node(struct anm_node *node, anm_time_t tm)
790 {
791 anm_get_node_matrix(node, node->matrix, tm);
792 }
794 void anm_eval(struct anm_node *node, anm_time_t tm)
795 {
796 struct anm_node *c;
798 anm_eval_node(node, tm);
800 if(node->parent) {
801 /* due to post-order traversal, the parent matrix is already evaluated */
802 m4_mult(node->matrix, node->parent->matrix, node->matrix);
803 }
805 /* recersively evaluate all children */
806 c = node->child;
807 while(c) {
808 anm_eval(c, tm);
809 c = c->next;
810 }
811 }
813 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
814 {
815 struct mat_cache *cache = pthread_getspecific(node->cache_key);
816 if(!cache) {
817 cache = malloc(sizeof *cache);
818 assert(cache);
820 pthread_mutex_lock(&node->cache_list_lock);
821 cache->next = node->cache_list;
822 node->cache_list = cache;
823 pthread_mutex_unlock(&node->cache_list_lock);
825 cache->time = ANM_TIME_INVAL;
826 cache->inv_time = ANM_TIME_INVAL;
827 pthread_setspecific(node->cache_key, cache);
828 }
830 if(cache->time != tm) {
831 anm_get_node_matrix(node, cache->matrix, tm);
833 if(node->parent) {
834 mat4_t parent_mat;
836 anm_get_matrix(node->parent, parent_mat, tm);
837 m4_mult(cache->matrix, parent_mat, cache->matrix);
838 }
839 cache->time = tm;
840 }
841 m4_copy(mat, cache->matrix);
842 }
844 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
845 {
846 struct mat_cache *cache = pthread_getspecific(node->cache_key);
847 if(!cache) {
848 cache = malloc(sizeof *cache);
849 assert(cache);
851 pthread_mutex_lock(&node->cache_list_lock);
852 cache->next = node->cache_list;
853 node->cache_list = cache;
854 pthread_mutex_unlock(&node->cache_list_lock);
856 cache->inv_time = ANM_TIME_INVAL;
857 cache->inv_time = ANM_TIME_INVAL;
858 pthread_setspecific(node->cache_key, cache);
859 }
861 if(cache->inv_time != tm) {
862 anm_get_matrix(node, mat, tm);
863 m4_inverse(cache->inv_matrix, mat);
864 cache->inv_time = tm;
865 }
866 m4_copy(mat, cache->inv_matrix);
867 }
869 anm_time_t anm_get_start_time(struct anm_node *node)
870 {
871 int i, j;
872 struct anm_node *c;
873 anm_time_t res = LONG_MAX;
875 for(j=0; j<2; j++) {
876 struct anm_animation *anim = anm_get_active_animation(node, j);
877 if(!anim) break;
879 for(i=0; i<ANM_NUM_TRACKS; i++) {
880 if(anim->tracks[i].count) {
881 anm_time_t tm = anim->tracks[i].keys[0].time;
882 if(tm < res) {
883 res = tm;
884 }
885 }
886 }
887 }
889 c = node->child;
890 while(c) {
891 anm_time_t tm = anm_get_start_time(c);
892 if(tm < res) {
893 res = tm;
894 }
895 c = c->next;
896 }
897 return res;
898 }
900 anm_time_t anm_get_end_time(struct anm_node *node)
901 {
902 int i, j;
903 struct anm_node *c;
904 anm_time_t res = LONG_MIN;
906 for(j=0; j<2; j++) {
907 struct anm_animation *anim = anm_get_active_animation(node, j);
908 if(!anim) break;
910 for(i=0; i<ANM_NUM_TRACKS; i++) {
911 if(anim->tracks[i].count) {
912 anm_time_t tm = anim->tracks[i].keys[anim->tracks[i].count - 1].time;
913 if(tm > res) {
914 res = tm;
915 }
916 }
917 }
918 }
920 c = node->child;
921 while(c) {
922 anm_time_t tm = anm_get_end_time(c);
923 if(tm > res) {
924 res = tm;
925 }
926 c = c->next;
927 }
928 return res;
929 }
931 static void invalidate_cache(struct anm_node *node)
932 {
933 struct mat_cache *cache = pthread_getspecific(node->cache_key);
934 if(cache) {
935 cache->time = cache->inv_time = ANM_TIME_INVAL;
936 }
937 }