libanim
view src/anim.c @ 42:d48408ab376f
added anm_get_node_matrix and anm_get_node_inv_matrix functions to calculate
node matrices without taking the hierarchy into account...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 27 Feb 2013 21:38:33 +0200 |
parents | 1ce7e250bae3 |
children | b3312cf87715 |
line source
1 #include <stdlib.h>
2 #include <limits.h>
3 #include <assert.h>
4 #include "anim.h"
5 #include "dynarr.h"
7 static void invalidate_cache(struct anm_node *node);
9 int anm_init_node(struct anm_node *node)
10 {
11 int i, j;
12 static const float defaults[] = {
13 0.0f, 0.0f, 0.0f, /* default position */
14 0.0f, 0.0f, 0.0f, 1.0f, /* default rotation quat */
15 1.0f, 1.0f, 1.0f /* default scale factor */
16 };
18 memset(node, 0, sizeof *node);
20 /* initialize thread-local matrix cache */
21 pthread_key_create(&node->cache_key, 0);
23 for(i=0; i<ANM_NUM_TRACKS; i++) {
24 if(anm_init_track(node->tracks + i) == -1) {
25 for(j=0; j<i; j++) {
26 anm_destroy_track(node->tracks + i);
27 }
28 }
29 anm_set_track_default(node->tracks + i, defaults[i]);
30 }
31 return 0;
32 }
34 void anm_destroy_node(struct anm_node *node)
35 {
36 int i;
37 free(node->name);
39 for(i=0; i<ANM_NUM_TRACKS; i++) {
40 anm_destroy_track(node->tracks + i);
41 }
43 /* destroy thread-specific cache */
44 pthread_key_delete(node->cache_key);
46 while(node->cache_list) {
47 struct mat_cache *tmp = node->cache_list;
48 node->cache_list = tmp->next;
49 free(tmp);
50 }
51 }
53 void anm_destroy_node_tree(struct anm_node *tree)
54 {
55 struct anm_node *c, *tmp;
57 if(!tree) return;
59 c = tree->child;
60 while(c) {
61 tmp = c;
62 c = c->next;
64 anm_destroy_node_tree(tmp);
65 }
66 anm_destroy_node(tree);
67 }
69 struct anm_node *anm_create_node(void)
70 {
71 struct anm_node *n;
73 if((n = malloc(sizeof *n))) {
74 if(anm_init_node(n) == -1) {
75 free(n);
76 return 0;
77 }
78 }
79 return n;
80 }
82 void anm_free_node(struct anm_node *node)
83 {
84 anm_destroy_node(node);
85 free(node);
86 }
88 void anm_free_node_tree(struct anm_node *tree)
89 {
90 struct anm_node *c, *tmp;
92 if(!tree) return;
94 c = tree->child;
95 while(c) {
96 tmp = c;
97 c = c->next;
99 anm_free_node_tree(tmp);
100 }
102 anm_free_node(tree);
103 }
105 int anm_set_node_name(struct anm_node *node, const char *name)
106 {
107 char *str;
109 if(!(str = malloc(strlen(name) + 1))) {
110 return -1;
111 }
112 strcpy(str, name);
113 free(node->name);
114 node->name = str;
115 return 0;
116 }
118 const char *anm_get_node_name(struct anm_node *node)
119 {
120 return node->name ? node->name : "";
121 }
123 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in)
124 {
125 int i;
127 for(i=0; i<ANM_NUM_TRACKS; i++) {
128 anm_set_track_interpolator(node->tracks + i, in);
129 }
130 invalidate_cache(node);
131 }
133 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex)
134 {
135 int i;
137 for(i=0; i<ANM_NUM_TRACKS; i++) {
138 anm_set_track_extrapolator(node->tracks + i, ex);
139 }
140 invalidate_cache(node);
141 }
143 void anm_link_node(struct anm_node *p, struct anm_node *c)
144 {
145 c->next = p->child;
146 p->child = c;
148 c->parent = p;
149 invalidate_cache(c);
150 }
152 int anm_unlink_node(struct anm_node *p, struct anm_node *c)
153 {
154 struct anm_node *iter;
156 if(p->child == c) {
157 p->child = c->next;
158 c->next = 0;
159 invalidate_cache(c);
160 return 0;
161 }
163 iter = p->child;
164 while(iter->next) {
165 if(iter->next == c) {
166 iter->next = c->next;
167 c->next = 0;
168 invalidate_cache(c);
169 return 0;
170 }
171 }
172 return -1;
173 }
175 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm)
176 {
177 anm_set_value(node->tracks + ANM_TRACK_POS_X, tm, pos.x);
178 anm_set_value(node->tracks + ANM_TRACK_POS_Y, tm, pos.y);
179 anm_set_value(node->tracks + ANM_TRACK_POS_Z, tm, pos.z);
180 invalidate_cache(node);
181 }
183 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm)
184 {
185 vec3_t v;
186 v.x = anm_get_value(node->tracks + ANM_TRACK_POS_X, tm);
187 v.y = anm_get_value(node->tracks + ANM_TRACK_POS_Y, tm);
188 v.z = anm_get_value(node->tracks + ANM_TRACK_POS_Z, tm);
189 return v;
190 }
192 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm)
193 {
194 anm_set_value(node->tracks + ANM_TRACK_ROT_X, tm, rot.x);
195 anm_set_value(node->tracks + ANM_TRACK_ROT_Y, tm, rot.y);
196 anm_set_value(node->tracks + ANM_TRACK_ROT_Z, tm, rot.z);
197 anm_set_value(node->tracks + ANM_TRACK_ROT_W, tm, rot.w);
198 invalidate_cache(node);
199 }
201 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm)
202 {
203 int idx0, idx1, last_idx;
204 anm_time_t tstart, tend;
205 float t, dt;
206 struct anm_track *track_x, *track_y, *track_z, *track_w;
207 quat_t q, q1, q2;
209 track_x = node->tracks + ANM_TRACK_ROT_X;
210 track_y = node->tracks + ANM_TRACK_ROT_Y;
211 track_z = node->tracks + ANM_TRACK_ROT_Z;
212 track_w = node->tracks + ANM_TRACK_ROT_W;
214 if(!track_x->count) {
215 q.x = track_x->def_val;
216 q.y = track_y->def_val;
217 q.z = track_z->def_val;
218 q.w = track_w->def_val;
219 return q;
220 }
222 last_idx = track_x->count - 1;
224 tstart = track_x->keys[0].time;
225 tend = track_x->keys[last_idx].time;
226 tm = anm_remap_time(track_x, tm, tstart, tend);
228 idx0 = anm_get_key_interval(track_x, tm);
229 assert(idx0 >= 0 && idx0 < track_x->count);
230 idx1 = idx0 + 1;
232 dt = (float)(track_x->keys[idx1].time - track_x->keys[idx0].time);
233 t = (float)(tm - track_x->keys[idx0].time) / dt;
235 q1.x = track_x->keys[idx0].val;
236 q1.y = track_y->keys[idx0].val;
237 q1.z = track_z->keys[idx0].val;
238 q1.w = track_w->keys[idx0].val;
240 q2.x = track_x->keys[idx1].val;
241 q2.y = track_y->keys[idx1].val;
242 q2.z = track_z->keys[idx1].val;
243 q2.w = track_w->keys[idx1].val;
245 return quat_slerp(q1, q2, t);
246 }
248 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm)
249 {
250 anm_set_value(node->tracks + ANM_TRACK_SCL_X, tm, scl.x);
251 anm_set_value(node->tracks + ANM_TRACK_SCL_Y, tm, scl.y);
252 anm_set_value(node->tracks + ANM_TRACK_SCL_Z, tm, scl.z);
253 invalidate_cache(node);
254 }
256 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm)
257 {
258 vec3_t v;
259 v.x = anm_get_value(node->tracks + ANM_TRACK_SCL_X, tm);
260 v.y = anm_get_value(node->tracks + ANM_TRACK_SCL_Y, tm);
261 v.z = anm_get_value(node->tracks + ANM_TRACK_SCL_Z, tm);
262 return v;
263 }
266 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm)
267 {
268 mat4_t xform;
269 vec3_t pos = {0.0, 0.0, 0.0};
271 if(!node->parent) {
272 return anm_get_node_position(node, tm);
273 }
275 anm_get_matrix(node, xform, tm);
276 return v3_transform(pos, xform);
277 }
279 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm)
280 {
281 quat_t rot, prot;
282 rot = anm_get_node_rotation(node, tm);
284 if(!node->parent) {
285 return rot;
286 }
288 prot = anm_get_rotation(node->parent, tm);
289 return quat_mul(prot, rot);
290 }
292 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm)
293 {
294 vec3_t s, ps;
295 s = anm_get_node_scaling(node, tm);
297 if(!node->parent) {
298 return s;
299 }
301 ps = anm_get_scaling(node->parent, tm);
302 return v3_mul(s, ps);
303 }
305 void anm_set_pivot(struct anm_node *node, vec3_t piv)
306 {
307 node->pivot = piv;
308 }
310 vec3_t anm_get_pivot(struct anm_node *node)
311 {
312 return node->pivot;
313 }
315 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
316 {
317 mat4_t tmat, rmat, smat, pivmat, neg_pivmat;
318 vec3_t pos, scale;
319 quat_t rot;
321 m4_identity(tmat);
322 /*no need to m4_identity(rmat); quat_to_mat4 sets this properly */
323 m4_identity(smat);
324 m4_identity(pivmat);
325 m4_identity(neg_pivmat);
327 pos = anm_get_node_position(node, tm);
328 rot = anm_get_node_rotation(node, tm);
329 scale = anm_get_node_scaling(node, tm);
331 m4_translate(pivmat, node->pivot.x, node->pivot.y, node->pivot.z);
332 m4_translate(neg_pivmat, -node->pivot.x, -node->pivot.y, -node->pivot.z);
334 m4_translate(tmat, pos.x, pos.y, pos.z);
335 quat_to_mat4(rmat, rot);
336 m4_scale(smat, scale.x, scale.y, scale.z);
338 /* ok this would look nicer in C++ */
339 m4_mult(mat, pivmat, tmat);
340 m4_mult(mat, mat, rmat);
341 m4_mult(mat, mat, smat);
342 m4_mult(mat, mat, neg_pivmat);
343 }
345 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
346 {
347 mat4_t tmp;
348 anm_get_node_matrix(node, tmp, tm);
349 m4_inverse(mat, tmp);
350 }
352 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
353 {
354 struct mat_cache *cache = pthread_getspecific(node->cache_key);
355 if(!cache) {
356 cache = malloc(sizeof *cache);
357 assert(cache);
359 pthread_mutex_lock(&node->cache_list_lock);
360 cache->next = node->cache_list;
361 node->cache_list = cache;
362 pthread_mutex_unlock(&node->cache_list_lock);
364 cache->time = ANM_TIME_INVAL;
365 cache->inv_time = ANM_TIME_INVAL;
366 pthread_setspecific(node->cache_key, cache);
367 }
369 if(cache->time != tm) {
370 anm_get_node_matrix(node, cache->matrix, tm);
372 if(node->parent) {
373 mat4_t parent_mat;
375 anm_get_matrix(node->parent, parent_mat, tm);
376 m4_mult(cache->matrix, parent_mat, cache->matrix);
377 }
378 cache->time = tm;
379 }
380 m4_copy(mat, cache->matrix);
381 }
383 void anm_get_inv_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->inv_time = ANM_TIME_INVAL;
396 cache->inv_time = ANM_TIME_INVAL;
397 pthread_setspecific(node->cache_key, cache);
398 }
400 if(cache->inv_time != tm) {
401 anm_get_matrix(node, mat, tm);
402 m4_inverse(cache->inv_matrix, mat);
403 cache->inv_time = tm;
404 }
405 m4_copy(mat, cache->inv_matrix);
406 }
408 anm_time_t anm_get_start_time(struct anm_node *node)
409 {
410 int i;
411 struct anm_node *c;
412 anm_time_t res = LONG_MAX;
414 for(i=0; i<ANM_NUM_TRACKS; i++) {
415 if(node->tracks[i].count) {
416 anm_time_t tm = node->tracks[i].keys[0].time;
417 if(tm < res) {
418 res = tm;
419 }
420 }
421 }
423 c = node->child;
424 while(c) {
425 anm_time_t tm = anm_get_start_time(c);
426 if(tm < res) {
427 res = tm;
428 }
429 c = c->next;
430 }
431 return res;
432 }
434 anm_time_t anm_get_end_time(struct anm_node *node)
435 {
436 int i;
437 struct anm_node *c;
438 anm_time_t res = LONG_MIN;
440 for(i=0; i<ANM_NUM_TRACKS; i++) {
441 if(node->tracks[i].count) {
442 anm_time_t tm = node->tracks[i].keys[node->tracks[i].count - 1].time;
443 if(tm > res) {
444 res = tm;
445 }
446 }
447 }
449 c = node->child;
450 while(c) {
451 anm_time_t tm = anm_get_end_time(c);
452 if(tm > res) {
453 res = tm;
454 }
455 c = c->next;
456 }
457 return res;
458 }
460 static void invalidate_cache(struct anm_node *node)
461 {
462 struct mat_cache *cache = pthread_getspecific(node->cache_key);
463 if(cache) {
464 cache->time = ANM_TIME_INVAL;
465 }
466 }