libanim

view src/anim.c @ 46:eb665509682a

simplified the matrix calculation and removed two quaternion normalizations that were probably not needed
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 08 Mar 2013 04:54:05 +0200
parents ffe668a61bca
children b408f3f655e9
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_node(struct anm_node *node)
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 memset(node, 0, sizeof *node);
22 /* initialize thread-local matrix cache */
23 pthread_key_create(&node->cache_key, 0);
25 for(i=0; i<ANM_NUM_TRACKS; i++) {
26 if(anm_init_track(node->tracks + i) == -1) {
27 for(j=0; j<i; j++) {
28 anm_destroy_track(node->tracks + i);
29 }
30 }
31 anm_set_track_default(node->tracks + i, defaults[i]);
32 }
33 return 0;
34 }
36 void anm_destroy_node(struct anm_node *node)
37 {
38 int i;
39 free(node->name);
41 for(i=0; i<ANM_NUM_TRACKS; i++) {
42 anm_destroy_track(node->tracks + i);
43 }
45 /* destroy thread-specific cache */
46 pthread_key_delete(node->cache_key);
48 while(node->cache_list) {
49 struct mat_cache *tmp = node->cache_list;
50 node->cache_list = tmp->next;
51 free(tmp);
52 }
53 }
55 void anm_destroy_node_tree(struct anm_node *tree)
56 {
57 struct anm_node *c, *tmp;
59 if(!tree) return;
61 c = tree->child;
62 while(c) {
63 tmp = c;
64 c = c->next;
66 anm_destroy_node_tree(tmp);
67 }
68 anm_destroy_node(tree);
69 }
71 struct anm_node *anm_create_node(void)
72 {
73 struct anm_node *n;
75 if((n = malloc(sizeof *n))) {
76 if(anm_init_node(n) == -1) {
77 free(n);
78 return 0;
79 }
80 }
81 return n;
82 }
84 void anm_free_node(struct anm_node *node)
85 {
86 anm_destroy_node(node);
87 free(node);
88 }
90 void anm_free_node_tree(struct anm_node *tree)
91 {
92 struct anm_node *c, *tmp;
94 if(!tree) return;
96 c = tree->child;
97 while(c) {
98 tmp = c;
99 c = c->next;
101 anm_free_node_tree(tmp);
102 }
104 anm_free_node(tree);
105 }
107 int anm_set_node_name(struct anm_node *node, const char *name)
108 {
109 char *str;
111 if(!(str = malloc(strlen(name) + 1))) {
112 return -1;
113 }
114 strcpy(str, name);
115 free(node->name);
116 node->name = str;
117 return 0;
118 }
120 const char *anm_get_node_name(struct anm_node *node)
121 {
122 return node->name ? node->name : "";
123 }
125 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in)
126 {
127 int i;
129 for(i=0; i<ANM_NUM_TRACKS; i++) {
130 anm_set_track_interpolator(node->tracks + i, in);
131 }
132 invalidate_cache(node);
133 }
135 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex)
136 {
137 int i;
139 for(i=0; i<ANM_NUM_TRACKS; i++) {
140 anm_set_track_extrapolator(node->tracks + i, ex);
141 }
142 invalidate_cache(node);
143 }
145 void anm_link_node(struct anm_node *p, struct anm_node *c)
146 {
147 c->next = p->child;
148 p->child = c;
150 c->parent = p;
151 invalidate_cache(c);
152 }
154 int anm_unlink_node(struct anm_node *p, struct anm_node *c)
155 {
156 struct anm_node *iter;
158 if(p->child == c) {
159 p->child = c->next;
160 c->next = 0;
161 invalidate_cache(c);
162 return 0;
163 }
165 iter = p->child;
166 while(iter->next) {
167 if(iter->next == c) {
168 iter->next = c->next;
169 c->next = 0;
170 invalidate_cache(c);
171 return 0;
172 }
173 }
174 return -1;
175 }
177 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm)
178 {
179 anm_set_value(node->tracks + ANM_TRACK_POS_X, tm, pos.x);
180 anm_set_value(node->tracks + ANM_TRACK_POS_Y, tm, pos.y);
181 anm_set_value(node->tracks + ANM_TRACK_POS_Z, tm, pos.z);
182 invalidate_cache(node);
183 }
185 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm)
186 {
187 vec3_t v;
188 v.x = anm_get_value(node->tracks + ANM_TRACK_POS_X, tm);
189 v.y = anm_get_value(node->tracks + ANM_TRACK_POS_Y, tm);
190 v.z = anm_get_value(node->tracks + ANM_TRACK_POS_Z, tm);
191 return v;
192 }
194 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm)
195 {
196 anm_set_value(node->tracks + ANM_TRACK_ROT_X, tm, rot.x);
197 anm_set_value(node->tracks + ANM_TRACK_ROT_Y, tm, rot.y);
198 anm_set_value(node->tracks + ANM_TRACK_ROT_Z, tm, rot.z);
199 anm_set_value(node->tracks + ANM_TRACK_ROT_W, tm, rot.w);
200 invalidate_cache(node);
201 }
203 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm)
204 {
205 #ifndef ROT_USE_SLERP
206 quat_t q;
207 q.x = anm_get_value(node->tracks + ANM_TRACK_ROT_X, tm);
208 q.y = anm_get_value(node->tracks + ANM_TRACK_ROT_Y, tm);
209 q.z = anm_get_value(node->tracks + ANM_TRACK_ROT_Z, tm);
210 q.w = anm_get_value(node->tracks + ANM_TRACK_ROT_W, tm);
211 return q;
212 #else
213 int idx0, idx1, last_idx;
214 anm_time_t tstart, tend;
215 float t, dt;
216 struct anm_track *track_x, *track_y, *track_z, *track_w;
217 quat_t q, q1, q2;
219 track_x = node->tracks + ANM_TRACK_ROT_X;
220 track_y = node->tracks + ANM_TRACK_ROT_Y;
221 track_z = node->tracks + ANM_TRACK_ROT_Z;
222 track_w = node->tracks + ANM_TRACK_ROT_W;
224 if(!track_x->count) {
225 q.x = track_x->def_val;
226 q.y = track_y->def_val;
227 q.z = track_z->def_val;
228 q.w = track_w->def_val;
229 return q;
230 }
232 last_idx = track_x->count - 1;
234 tstart = track_x->keys[0].time;
235 tend = track_x->keys[last_idx].time;
237 if(tstart == tend) {
238 q.x = track_x->keys[0].val;
239 q.y = track_y->keys[0].val;
240 q.z = track_z->keys[0].val;
241 q.w = track_w->keys[0].val;
242 return q;
243 }
245 tm = anm_remap_time(track_x, tm, tstart, tend);
247 idx0 = anm_get_key_interval(track_x, tm);
248 assert(idx0 >= 0 && idx0 < track_x->count);
249 idx1 = idx0 + 1;
251 if(idx0 == last_idx) {
252 q.x = track_x->keys[idx0].val;
253 q.y = track_y->keys[idx0].val;
254 q.z = track_z->keys[idx0].val;
255 q.w = track_w->keys[idx0].val;
256 return q;
257 }
259 dt = (float)(track_x->keys[idx1].time - track_x->keys[idx0].time);
260 t = (float)(tm - track_x->keys[idx0].time) / dt;
262 q1.x = track_x->keys[idx0].val;
263 q1.y = track_y->keys[idx0].val;
264 q1.z = track_z->keys[idx0].val;
265 q1.w = track_w->keys[idx0].val;
267 q2.x = track_x->keys[idx1].val;
268 q2.y = track_y->keys[idx1].val;
269 q2.z = track_z->keys[idx1].val;
270 q2.w = track_w->keys[idx1].val;
272 /*q1 = quat_normalize(q1);
273 q2 = quat_normalize(q2);*/
275 return quat_slerp(q1, q2, t);
276 #endif
277 }
279 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm)
280 {
281 anm_set_value(node->tracks + ANM_TRACK_SCL_X, tm, scl.x);
282 anm_set_value(node->tracks + ANM_TRACK_SCL_Y, tm, scl.y);
283 anm_set_value(node->tracks + ANM_TRACK_SCL_Z, tm, scl.z);
284 invalidate_cache(node);
285 }
287 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm)
288 {
289 vec3_t v;
290 v.x = anm_get_value(node->tracks + ANM_TRACK_SCL_X, tm);
291 v.y = anm_get_value(node->tracks + ANM_TRACK_SCL_Y, tm);
292 v.z = anm_get_value(node->tracks + ANM_TRACK_SCL_Z, tm);
293 return v;
294 }
297 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm)
298 {
299 mat4_t xform;
300 vec3_t pos = {0.0, 0.0, 0.0};
302 if(!node->parent) {
303 return anm_get_node_position(node, tm);
304 }
306 anm_get_matrix(node, xform, tm);
307 return v3_transform(pos, xform);
308 }
310 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm)
311 {
312 quat_t rot, prot;
313 rot = anm_get_node_rotation(node, tm);
315 if(!node->parent) {
316 return rot;
317 }
319 prot = anm_get_rotation(node->parent, tm);
320 return quat_mul(prot, rot);
321 }
323 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm)
324 {
325 vec3_t s, ps;
326 s = anm_get_node_scaling(node, tm);
328 if(!node->parent) {
329 return s;
330 }
332 ps = anm_get_scaling(node->parent, tm);
333 return v3_mul(s, ps);
334 }
336 void anm_set_pivot(struct anm_node *node, vec3_t piv)
337 {
338 node->pivot = piv;
339 }
341 vec3_t anm_get_pivot(struct anm_node *node)
342 {
343 return node->pivot;
344 }
346 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
347 {
348 int i;
349 mat4_t rmat;
350 vec3_t pos, scale;
351 quat_t rot;
353 pos = anm_get_node_position(node, tm);
354 rot = anm_get_node_rotation(node, tm);
355 scale = anm_get_node_scaling(node, tm);
357 m4_set_translation(mat, node->pivot.x, node->pivot.y, node->pivot.z);
359 quat_to_mat4(rmat, rot);
360 for(i=0; i<3; i++) {
361 mat[i][0] = rmat[i][0];
362 mat[i][1] = rmat[i][1];
363 mat[i][2] = rmat[i][2];
364 }
365 /* this loop is equivalent to: m4_mult(mat, mat, rmat); */
367 mat[0][0] *= scale.x; mat[0][1] *= scale.y; mat[0][2] *= scale.z; mat[0][3] += pos.x;
368 mat[1][0] *= scale.x; mat[1][1] *= scale.y; mat[1][2] *= scale.z; mat[1][3] += pos.y;
369 mat[2][0] *= scale.x; mat[2][1] *= scale.y; mat[2][2] *= scale.z; mat[2][3] += pos.z;
371 m4_translate(mat, -node->pivot.x, -node->pivot.y, -node->pivot.z);
373 /* that's basically: pivot * rotation * translation * scaling * -pivot */
374 }
376 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
377 {
378 mat4_t tmp;
379 anm_get_node_matrix(node, tmp, tm);
380 m4_inverse(mat, tmp);
381 }
383 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
384 {
385 struct mat_cache *cache = pthread_getspecific(node->cache_key);
386 if(!cache) {
387 cache = malloc(sizeof *cache);
388 assert(cache);
390 pthread_mutex_lock(&node->cache_list_lock);
391 cache->next = node->cache_list;
392 node->cache_list = cache;
393 pthread_mutex_unlock(&node->cache_list_lock);
395 cache->time = ANM_TIME_INVAL;
396 cache->inv_time = ANM_TIME_INVAL;
397 pthread_setspecific(node->cache_key, cache);
398 }
400 if(cache->time != tm) {
401 anm_get_node_matrix(node, cache->matrix, tm);
403 if(node->parent) {
404 mat4_t parent_mat;
406 anm_get_matrix(node->parent, parent_mat, tm);
407 m4_mult(cache->matrix, parent_mat, cache->matrix);
408 }
409 cache->time = tm;
410 }
411 m4_copy(mat, cache->matrix);
412 }
414 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
415 {
416 struct mat_cache *cache = pthread_getspecific(node->cache_key);
417 if(!cache) {
418 cache = malloc(sizeof *cache);
419 assert(cache);
421 pthread_mutex_lock(&node->cache_list_lock);
422 cache->next = node->cache_list;
423 node->cache_list = cache;
424 pthread_mutex_unlock(&node->cache_list_lock);
426 cache->inv_time = ANM_TIME_INVAL;
427 cache->inv_time = ANM_TIME_INVAL;
428 pthread_setspecific(node->cache_key, cache);
429 }
431 if(cache->inv_time != tm) {
432 anm_get_matrix(node, mat, tm);
433 m4_inverse(cache->inv_matrix, mat);
434 cache->inv_time = tm;
435 }
436 m4_copy(mat, cache->inv_matrix);
437 }
439 anm_time_t anm_get_start_time(struct anm_node *node)
440 {
441 int i;
442 struct anm_node *c;
443 anm_time_t res = LONG_MAX;
445 for(i=0; i<ANM_NUM_TRACKS; i++) {
446 if(node->tracks[i].count) {
447 anm_time_t tm = node->tracks[i].keys[0].time;
448 if(tm < res) {
449 res = tm;
450 }
451 }
452 }
454 c = node->child;
455 while(c) {
456 anm_time_t tm = anm_get_start_time(c);
457 if(tm < res) {
458 res = tm;
459 }
460 c = c->next;
461 }
462 return res;
463 }
465 anm_time_t anm_get_end_time(struct anm_node *node)
466 {
467 int i;
468 struct anm_node *c;
469 anm_time_t res = LONG_MIN;
471 for(i=0; i<ANM_NUM_TRACKS; i++) {
472 if(node->tracks[i].count) {
473 anm_time_t tm = node->tracks[i].keys[node->tracks[i].count - 1].time;
474 if(tm > res) {
475 res = tm;
476 }
477 }
478 }
480 c = node->child;
481 while(c) {
482 anm_time_t tm = anm_get_end_time(c);
483 if(tm > res) {
484 res = tm;
485 }
486 c = c->next;
487 }
488 return res;
489 }
491 static void invalidate_cache(struct anm_node *node)
492 {
493 struct mat_cache *cache = pthread_getspecific(node->cache_key);
494 if(cache) {
495 cache->time = ANM_TIME_INVAL;
496 }
497 }