libanim

view src/anim.c @ 4:1ce7e250bae3

really stupid mistake in hierarchical transformations... ignored the parent matrix and instead multiplied an uninitialized one...
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Feb 2013 20:54:19 +0200
parents f561282b13e8
children 2cf7284d2bbb
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_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
316 {
317 struct mat_cache *cache = pthread_getspecific(node->cache_key);
318 if(!cache) {
319 cache = malloc(sizeof *cache);
320 assert(cache);
322 pthread_mutex_lock(&node->cache_list_lock);
323 cache->next = node->cache_list;
324 node->cache_list = cache;
325 pthread_mutex_unlock(&node->cache_list_lock);
327 cache->time = ANM_TIME_INVAL;
328 cache->inv_time = ANM_TIME_INVAL;
329 pthread_setspecific(node->cache_key, cache);
330 }
332 if(cache->time != tm) {
333 mat4_t tmat, rmat, smat, pivmat, neg_pivmat;
334 vec3_t pos, scale;
335 quat_t rot;
337 m4_identity(tmat);
338 /*no need to m4_identity(rmat); quat_to_mat4 sets this properly */
339 m4_identity(smat);
340 m4_identity(pivmat);
341 m4_identity(neg_pivmat);
343 pos = anm_get_node_position(node, tm);
344 rot = anm_get_node_rotation(node, tm);
345 scale = anm_get_node_scaling(node, tm);
347 m4_translate(pivmat, node->pivot.x, node->pivot.y, node->pivot.z);
348 m4_translate(neg_pivmat, -node->pivot.x, -node->pivot.y, -node->pivot.z);
350 m4_translate(tmat, pos.x, pos.y, pos.z);
351 quat_to_mat4(rmat, rot);
352 m4_scale(smat, scale.x, scale.y, scale.z);
354 /* ok this would look nicer in C++ */
355 m4_mult(cache->matrix, pivmat, tmat);
356 m4_mult(cache->matrix, cache->matrix, rmat);
357 m4_mult(cache->matrix, cache->matrix, smat);
358 m4_mult(cache->matrix, cache->matrix, neg_pivmat);
360 if(node->parent) {
361 mat4_t parent_mat;
363 anm_get_matrix(node->parent, parent_mat, tm);
364 m4_mult(cache->matrix, parent_mat, cache->matrix);
365 }
366 cache->time = tm;
367 }
368 m4_copy(mat, cache->matrix);
369 }
371 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
372 {
373 struct mat_cache *cache = pthread_getspecific(node->cache_key);
374 if(!cache) {
375 cache = malloc(sizeof *cache);
376 assert(cache);
378 pthread_mutex_lock(&node->cache_list_lock);
379 cache->next = node->cache_list;
380 node->cache_list = cache;
381 pthread_mutex_unlock(&node->cache_list_lock);
383 cache->inv_time = ANM_TIME_INVAL;
384 cache->inv_time = ANM_TIME_INVAL;
385 pthread_setspecific(node->cache_key, cache);
386 }
388 if(cache->inv_time != tm) {
389 anm_get_matrix(node, mat, tm);
390 m4_inverse(cache->inv_matrix, mat);
391 cache->inv_time = tm;
392 }
393 m4_copy(mat, cache->inv_matrix);
394 }
396 anm_time_t anm_get_start_time(struct anm_node *node)
397 {
398 int i;
399 struct anm_node *c;
400 anm_time_t res = LONG_MAX;
402 for(i=0; i<ANM_NUM_TRACKS; i++) {
403 if(node->tracks[i].count) {
404 anm_time_t tm = node->tracks[i].keys[0].time;
405 if(tm < res) {
406 res = tm;
407 }
408 }
409 }
411 c = node->child;
412 while(c) {
413 anm_time_t tm = anm_get_start_time(c);
414 if(tm < res) {
415 res = tm;
416 }
417 c = c->next;
418 }
419 return res;
420 }
422 anm_time_t anm_get_end_time(struct anm_node *node)
423 {
424 int i;
425 struct anm_node *c;
426 anm_time_t res = LONG_MIN;
428 for(i=0; i<ANM_NUM_TRACKS; i++) {
429 if(node->tracks[i].count) {
430 anm_time_t tm = node->tracks[i].keys[node->tracks[i].count - 1].time;
431 if(tm > res) {
432 res = tm;
433 }
434 }
435 }
437 c = node->child;
438 while(c) {
439 anm_time_t tm = anm_get_end_time(c);
440 if(tm > res) {
441 res = tm;
442 }
443 c = c->next;
444 }
445 return res;
446 }
448 static void invalidate_cache(struct anm_node *node)
449 {
450 struct mat_cache *cache = pthread_getspecific(node->cache_key);
451 if(cache) {
452 cache->time = ANM_TIME_INVAL;
453 }
454 }