libanim

view src/anim.c @ 1:69654793abc3

removed vmath.h from anim.h
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 23 Sep 2012 08:23:15 +0300
parents fad4701f484e
children f561282b13e8
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 pthread_setspecific(node->cache_key, cache);
329 }
331 if(cache->time != tm) {
332 mat4_t tmat, rmat, smat, pivmat, neg_pivmat;
333 vec3_t pos, scale;
334 quat_t rot;
336 m4_identity(tmat);
337 /*no need to m4_identity(rmat); quat_to_mat4 sets this properly */
338 m4_identity(smat);
339 m4_identity(pivmat);
340 m4_identity(neg_pivmat);
342 pos = anm_get_node_position(node, tm);
343 rot = anm_get_node_rotation(node, tm);
344 scale = anm_get_node_scaling(node, tm);
346 m4_translate(pivmat, node->pivot.x, node->pivot.y, node->pivot.z);
347 m4_translate(neg_pivmat, -node->pivot.x, -node->pivot.y, -node->pivot.z);
349 m4_translate(tmat, pos.x, pos.y, pos.z);
350 quat_to_mat4(rmat, rot);
351 m4_translate(smat, scale.x, scale.y, scale.z);
353 /* ok this would look nicer in C++ */
354 m4_mult(cache->matrix, pivmat, tmat);
355 m4_mult(cache->matrix, cache->matrix, rmat);
356 m4_mult(cache->matrix, cache->matrix, smat);
357 m4_mult(cache->matrix, cache->matrix, neg_pivmat);
359 if(node->parent) {
360 mat4_t parent_mat;
362 anm_get_matrix(node->parent, mat, tm);
363 m4_mult(cache->matrix, parent_mat, cache->matrix);
364 }
365 cache->time = tm;
366 }
367 m4_copy(mat, cache->matrix);
368 }
370 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
371 {
372 struct mat_cache *cache = pthread_getspecific(node->cache_key);
373 if(!cache) {
374 cache = malloc(sizeof *cache);
375 assert(cache);
377 pthread_mutex_lock(&node->cache_list_lock);
378 cache->next = node->cache_list;
379 node->cache_list = cache;
380 pthread_mutex_unlock(&node->cache_list_lock);
382 cache->inv_time = ANM_TIME_INVAL;
383 pthread_setspecific(node->cache_key, cache);
384 }
386 if(cache->inv_time != tm) {
387 anm_get_matrix(node, mat, tm);
388 m4_inverse(cache->inv_matrix, mat);
389 cache->inv_time = tm;
390 }
391 m4_copy(mat, cache->inv_matrix);
392 }
394 anm_time_t anm_get_start_time(struct anm_node *node)
395 {
396 int i;
397 struct anm_node *c;
398 anm_time_t res = LONG_MAX;
400 for(i=0; i<ANM_NUM_TRACKS; i++) {
401 if(node->tracks[i].count) {
402 anm_time_t tm = node->tracks[i].keys[0].time;
403 if(tm < res) {
404 res = tm;
405 }
406 }
407 }
409 c = node->child;
410 while(c) {
411 anm_time_t tm = anm_get_start_time(c);
412 if(tm < res) {
413 res = tm;
414 }
415 c = c->next;
416 }
417 return res;
418 }
420 anm_time_t anm_get_end_time(struct anm_node *node)
421 {
422 int i;
423 struct anm_node *c;
424 anm_time_t res = LONG_MIN;
426 for(i=0; i<ANM_NUM_TRACKS; i++) {
427 if(node->tracks[i].count) {
428 anm_time_t tm = node->tracks[i].keys[node->tracks[i].count - 1].time;
429 if(tm > res) {
430 res = tm;
431 }
432 }
433 }
435 c = node->child;
436 while(c) {
437 anm_time_t tm = anm_get_end_time(c);
438 if(tm > res) {
439 res = tm;
440 }
441 c = c->next;
442 }
443 return res;
444 }
446 static void invalidate_cache(struct anm_node *node)
447 {
448 struct mat_cache *cache = pthread_getspecific(node->cache_key);
449 if(cache) {
450 cache->time = ANM_TIME_INVAL;
451 }
452 }