rev |
line source |
nuclear@0
|
1 #include <stdlib.h>
|
nuclear@0
|
2 #include <limits.h>
|
nuclear@0
|
3 #include <assert.h>
|
nuclear@0
|
4 #include "anim.h"
|
nuclear@0
|
5 #include "dynarr.h"
|
nuclear@0
|
6
|
nuclear@0
|
7 #define ROT_USE_SLERP
|
nuclear@0
|
8
|
nuclear@0
|
9 static void invalidate_cache(struct anm_node *node);
|
nuclear@0
|
10
|
nuclear@0
|
11 int anm_init_node(struct anm_node *node)
|
nuclear@0
|
12 {
|
nuclear@0
|
13 int i, j;
|
nuclear@0
|
14 static const float defaults[] = {
|
nuclear@0
|
15 0.0f, 0.0f, 0.0f, /* default position */
|
nuclear@0
|
16 0.0f, 0.0f, 0.0f, 1.0f, /* default rotation quat */
|
nuclear@0
|
17 1.0f, 1.0f, 1.0f /* default scale factor */
|
nuclear@0
|
18 };
|
nuclear@0
|
19
|
nuclear@0
|
20 memset(node, 0, sizeof *node);
|
nuclear@0
|
21
|
nuclear@1
|
22 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
23 /* initialize thread-local matrix cache */
|
nuclear@0
|
24 pthread_key_create(&node->cache_key, 0);
|
nuclear@0
|
25 pthread_mutex_init(&node->cache_list_lock, 0);
|
nuclear@1
|
26 #endif
|
nuclear@0
|
27
|
nuclear@0
|
28 for(i=0; i<ANM_NUM_TRACKS; i++) {
|
nuclear@0
|
29 if(anm_init_track(node->tracks + i) == -1) {
|
nuclear@0
|
30 for(j=0; j<i; j++) {
|
nuclear@0
|
31 anm_destroy_track(node->tracks + i);
|
nuclear@0
|
32 }
|
nuclear@0
|
33 }
|
nuclear@0
|
34 anm_set_track_default(node->tracks + i, defaults[i]);
|
nuclear@0
|
35 }
|
nuclear@0
|
36 return 0;
|
nuclear@0
|
37 }
|
nuclear@0
|
38
|
nuclear@0
|
39 void anm_destroy_node(struct anm_node *node)
|
nuclear@0
|
40 {
|
nuclear@0
|
41 int i;
|
nuclear@0
|
42 free(node->name);
|
nuclear@0
|
43
|
nuclear@0
|
44 for(i=0; i<ANM_NUM_TRACKS; i++) {
|
nuclear@0
|
45 anm_destroy_track(node->tracks + i);
|
nuclear@0
|
46 }
|
nuclear@0
|
47
|
nuclear@1
|
48 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
49 /* destroy thread-specific cache */
|
nuclear@0
|
50 pthread_key_delete(node->cache_key);
|
nuclear@1
|
51 #endif
|
nuclear@0
|
52
|
nuclear@0
|
53 while(node->cache_list) {
|
nuclear@0
|
54 struct mat_cache *tmp = node->cache_list;
|
nuclear@0
|
55 node->cache_list = tmp->next;
|
nuclear@0
|
56 free(tmp);
|
nuclear@0
|
57 }
|
nuclear@0
|
58 }
|
nuclear@0
|
59
|
nuclear@0
|
60 void anm_destroy_node_tree(struct anm_node *tree)
|
nuclear@0
|
61 {
|
nuclear@0
|
62 struct anm_node *c, *tmp;
|
nuclear@0
|
63
|
nuclear@0
|
64 if(!tree) return;
|
nuclear@0
|
65
|
nuclear@0
|
66 c = tree->child;
|
nuclear@0
|
67 while(c) {
|
nuclear@0
|
68 tmp = c;
|
nuclear@0
|
69 c = c->next;
|
nuclear@0
|
70
|
nuclear@0
|
71 anm_destroy_node_tree(tmp);
|
nuclear@0
|
72 }
|
nuclear@0
|
73 anm_destroy_node(tree);
|
nuclear@0
|
74 }
|
nuclear@0
|
75
|
nuclear@0
|
76 struct anm_node *anm_create_node(void)
|
nuclear@0
|
77 {
|
nuclear@0
|
78 struct anm_node *n;
|
nuclear@0
|
79
|
nuclear@0
|
80 if((n = malloc(sizeof *n))) {
|
nuclear@0
|
81 if(anm_init_node(n) == -1) {
|
nuclear@0
|
82 free(n);
|
nuclear@0
|
83 return 0;
|
nuclear@0
|
84 }
|
nuclear@0
|
85 }
|
nuclear@0
|
86 return n;
|
nuclear@0
|
87 }
|
nuclear@0
|
88
|
nuclear@0
|
89 void anm_free_node(struct anm_node *node)
|
nuclear@0
|
90 {
|
nuclear@0
|
91 anm_destroy_node(node);
|
nuclear@0
|
92 free(node);
|
nuclear@0
|
93 }
|
nuclear@0
|
94
|
nuclear@0
|
95 void anm_free_node_tree(struct anm_node *tree)
|
nuclear@0
|
96 {
|
nuclear@0
|
97 struct anm_node *c, *tmp;
|
nuclear@0
|
98
|
nuclear@0
|
99 if(!tree) return;
|
nuclear@0
|
100
|
nuclear@0
|
101 c = tree->child;
|
nuclear@0
|
102 while(c) {
|
nuclear@0
|
103 tmp = c;
|
nuclear@0
|
104 c = c->next;
|
nuclear@0
|
105
|
nuclear@0
|
106 anm_free_node_tree(tmp);
|
nuclear@0
|
107 }
|
nuclear@0
|
108
|
nuclear@0
|
109 anm_free_node(tree);
|
nuclear@0
|
110 }
|
nuclear@0
|
111
|
nuclear@0
|
112 int anm_set_node_name(struct anm_node *node, const char *name)
|
nuclear@0
|
113 {
|
nuclear@0
|
114 char *str;
|
nuclear@0
|
115
|
nuclear@0
|
116 if(!(str = malloc(strlen(name) + 1))) {
|
nuclear@0
|
117 return -1;
|
nuclear@0
|
118 }
|
nuclear@0
|
119 strcpy(str, name);
|
nuclear@0
|
120 free(node->name);
|
nuclear@0
|
121 node->name = str;
|
nuclear@0
|
122 return 0;
|
nuclear@0
|
123 }
|
nuclear@0
|
124
|
nuclear@0
|
125 const char *anm_get_node_name(struct anm_node *node)
|
nuclear@0
|
126 {
|
nuclear@0
|
127 return node->name ? node->name : "";
|
nuclear@0
|
128 }
|
nuclear@0
|
129
|
nuclear@0
|
130 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in)
|
nuclear@0
|
131 {
|
nuclear@0
|
132 int i;
|
nuclear@0
|
133
|
nuclear@0
|
134 for(i=0; i<ANM_NUM_TRACKS; i++) {
|
nuclear@0
|
135 anm_set_track_interpolator(node->tracks + i, in);
|
nuclear@0
|
136 }
|
nuclear@0
|
137 invalidate_cache(node);
|
nuclear@0
|
138 }
|
nuclear@0
|
139
|
nuclear@0
|
140 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex)
|
nuclear@0
|
141 {
|
nuclear@0
|
142 int i;
|
nuclear@0
|
143
|
nuclear@0
|
144 for(i=0; i<ANM_NUM_TRACKS; i++) {
|
nuclear@0
|
145 anm_set_track_extrapolator(node->tracks + i, ex);
|
nuclear@0
|
146 }
|
nuclear@0
|
147 invalidate_cache(node);
|
nuclear@0
|
148 }
|
nuclear@0
|
149
|
nuclear@0
|
150 void anm_link_node(struct anm_node *p, struct anm_node *c)
|
nuclear@0
|
151 {
|
nuclear@0
|
152 c->next = p->child;
|
nuclear@0
|
153 p->child = c;
|
nuclear@0
|
154
|
nuclear@0
|
155 c->parent = p;
|
nuclear@0
|
156 invalidate_cache(c);
|
nuclear@0
|
157 }
|
nuclear@0
|
158
|
nuclear@0
|
159 int anm_unlink_node(struct anm_node *p, struct anm_node *c)
|
nuclear@0
|
160 {
|
nuclear@0
|
161 struct anm_node *iter;
|
nuclear@0
|
162
|
nuclear@0
|
163 if(p->child == c) {
|
nuclear@0
|
164 p->child = c->next;
|
nuclear@0
|
165 c->next = 0;
|
nuclear@0
|
166 invalidate_cache(c);
|
nuclear@0
|
167 return 0;
|
nuclear@0
|
168 }
|
nuclear@0
|
169
|
nuclear@0
|
170 iter = p->child;
|
nuclear@0
|
171 while(iter->next) {
|
nuclear@0
|
172 if(iter->next == c) {
|
nuclear@0
|
173 iter->next = c->next;
|
nuclear@0
|
174 c->next = 0;
|
nuclear@0
|
175 invalidate_cache(c);
|
nuclear@0
|
176 return 0;
|
nuclear@0
|
177 }
|
nuclear@0
|
178 }
|
nuclear@0
|
179 return -1;
|
nuclear@0
|
180 }
|
nuclear@0
|
181
|
nuclear@0
|
182 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm)
|
nuclear@0
|
183 {
|
nuclear@0
|
184 anm_set_value(node->tracks + ANM_TRACK_POS_X, tm, pos.x);
|
nuclear@0
|
185 anm_set_value(node->tracks + ANM_TRACK_POS_Y, tm, pos.y);
|
nuclear@0
|
186 anm_set_value(node->tracks + ANM_TRACK_POS_Z, tm, pos.z);
|
nuclear@0
|
187 invalidate_cache(node);
|
nuclear@0
|
188 }
|
nuclear@0
|
189
|
nuclear@0
|
190 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm)
|
nuclear@0
|
191 {
|
nuclear@0
|
192 vec3_t v;
|
nuclear@0
|
193 v.x = anm_get_value(node->tracks + ANM_TRACK_POS_X, tm);
|
nuclear@0
|
194 v.y = anm_get_value(node->tracks + ANM_TRACK_POS_Y, tm);
|
nuclear@0
|
195 v.z = anm_get_value(node->tracks + ANM_TRACK_POS_Z, tm);
|
nuclear@0
|
196 return v;
|
nuclear@0
|
197 }
|
nuclear@0
|
198
|
nuclear@0
|
199 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm)
|
nuclear@0
|
200 {
|
nuclear@0
|
201 anm_set_value(node->tracks + ANM_TRACK_ROT_X, tm, rot.x);
|
nuclear@0
|
202 anm_set_value(node->tracks + ANM_TRACK_ROT_Y, tm, rot.y);
|
nuclear@0
|
203 anm_set_value(node->tracks + ANM_TRACK_ROT_Z, tm, rot.z);
|
nuclear@0
|
204 anm_set_value(node->tracks + ANM_TRACK_ROT_W, tm, rot.w);
|
nuclear@0
|
205 invalidate_cache(node);
|
nuclear@0
|
206 }
|
nuclear@0
|
207
|
nuclear@0
|
208 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm)
|
nuclear@0
|
209 {
|
nuclear@0
|
210 #ifndef ROT_USE_SLERP
|
nuclear@0
|
211 quat_t q;
|
nuclear@0
|
212 q.x = anm_get_value(node->tracks + ANM_TRACK_ROT_X, tm);
|
nuclear@0
|
213 q.y = anm_get_value(node->tracks + ANM_TRACK_ROT_Y, tm);
|
nuclear@0
|
214 q.z = anm_get_value(node->tracks + ANM_TRACK_ROT_Z, tm);
|
nuclear@0
|
215 q.w = anm_get_value(node->tracks + ANM_TRACK_ROT_W, tm);
|
nuclear@0
|
216 return q;
|
nuclear@0
|
217 #else
|
nuclear@0
|
218 int idx0, idx1, last_idx;
|
nuclear@0
|
219 anm_time_t tstart, tend;
|
nuclear@0
|
220 float t, dt;
|
nuclear@0
|
221 struct anm_track *track_x, *track_y, *track_z, *track_w;
|
nuclear@0
|
222 quat_t q, q1, q2;
|
nuclear@0
|
223
|
nuclear@0
|
224 track_x = node->tracks + ANM_TRACK_ROT_X;
|
nuclear@0
|
225 track_y = node->tracks + ANM_TRACK_ROT_Y;
|
nuclear@0
|
226 track_z = node->tracks + ANM_TRACK_ROT_Z;
|
nuclear@0
|
227 track_w = node->tracks + ANM_TRACK_ROT_W;
|
nuclear@0
|
228
|
nuclear@0
|
229 if(!track_x->count) {
|
nuclear@0
|
230 q.x = track_x->def_val;
|
nuclear@0
|
231 q.y = track_y->def_val;
|
nuclear@0
|
232 q.z = track_z->def_val;
|
nuclear@0
|
233 q.w = track_w->def_val;
|
nuclear@0
|
234 return q;
|
nuclear@0
|
235 }
|
nuclear@0
|
236
|
nuclear@0
|
237 last_idx = track_x->count - 1;
|
nuclear@0
|
238
|
nuclear@0
|
239 tstart = track_x->keys[0].time;
|
nuclear@0
|
240 tend = track_x->keys[last_idx].time;
|
nuclear@0
|
241
|
nuclear@0
|
242 if(tstart == tend) {
|
nuclear@0
|
243 q.x = track_x->keys[0].val;
|
nuclear@0
|
244 q.y = track_y->keys[0].val;
|
nuclear@0
|
245 q.z = track_z->keys[0].val;
|
nuclear@0
|
246 q.w = track_w->keys[0].val;
|
nuclear@0
|
247 return q;
|
nuclear@0
|
248 }
|
nuclear@0
|
249
|
nuclear@0
|
250 tm = anm_remap_time(track_x, tm, tstart, tend);
|
nuclear@0
|
251
|
nuclear@0
|
252 idx0 = anm_get_key_interval(track_x, tm);
|
nuclear@0
|
253 assert(idx0 >= 0 && idx0 < track_x->count);
|
nuclear@0
|
254 idx1 = idx0 + 1;
|
nuclear@0
|
255
|
nuclear@0
|
256 if(idx0 == last_idx) {
|
nuclear@0
|
257 q.x = track_x->keys[idx0].val;
|
nuclear@0
|
258 q.y = track_y->keys[idx0].val;
|
nuclear@0
|
259 q.z = track_z->keys[idx0].val;
|
nuclear@0
|
260 q.w = track_w->keys[idx0].val;
|
nuclear@0
|
261 return q;
|
nuclear@0
|
262 }
|
nuclear@0
|
263
|
nuclear@0
|
264 dt = (float)(track_x->keys[idx1].time - track_x->keys[idx0].time);
|
nuclear@0
|
265 t = (float)(tm - track_x->keys[idx0].time) / dt;
|
nuclear@0
|
266
|
nuclear@0
|
267 q1.x = track_x->keys[idx0].val;
|
nuclear@0
|
268 q1.y = track_y->keys[idx0].val;
|
nuclear@0
|
269 q1.z = track_z->keys[idx0].val;
|
nuclear@0
|
270 q1.w = track_w->keys[idx0].val;
|
nuclear@0
|
271
|
nuclear@0
|
272 q2.x = track_x->keys[idx1].val;
|
nuclear@0
|
273 q2.y = track_y->keys[idx1].val;
|
nuclear@0
|
274 q2.z = track_z->keys[idx1].val;
|
nuclear@0
|
275 q2.w = track_w->keys[idx1].val;
|
nuclear@0
|
276
|
nuclear@0
|
277 /*q1 = quat_normalize(q1);
|
nuclear@0
|
278 q2 = quat_normalize(q2);*/
|
nuclear@0
|
279
|
nuclear@0
|
280 return quat_slerp(q1, q2, t);
|
nuclear@0
|
281 #endif
|
nuclear@0
|
282 }
|
nuclear@0
|
283
|
nuclear@0
|
284 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm)
|
nuclear@0
|
285 {
|
nuclear@0
|
286 anm_set_value(node->tracks + ANM_TRACK_SCL_X, tm, scl.x);
|
nuclear@0
|
287 anm_set_value(node->tracks + ANM_TRACK_SCL_Y, tm, scl.y);
|
nuclear@0
|
288 anm_set_value(node->tracks + ANM_TRACK_SCL_Z, tm, scl.z);
|
nuclear@0
|
289 invalidate_cache(node);
|
nuclear@0
|
290 }
|
nuclear@0
|
291
|
nuclear@0
|
292 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm)
|
nuclear@0
|
293 {
|
nuclear@0
|
294 vec3_t v;
|
nuclear@0
|
295 v.x = anm_get_value(node->tracks + ANM_TRACK_SCL_X, tm);
|
nuclear@0
|
296 v.y = anm_get_value(node->tracks + ANM_TRACK_SCL_Y, tm);
|
nuclear@0
|
297 v.z = anm_get_value(node->tracks + ANM_TRACK_SCL_Z, tm);
|
nuclear@0
|
298 return v;
|
nuclear@0
|
299 }
|
nuclear@0
|
300
|
nuclear@0
|
301
|
nuclear@0
|
302 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm)
|
nuclear@0
|
303 {
|
nuclear@0
|
304 mat4_t xform;
|
nuclear@0
|
305 vec3_t pos = {0.0, 0.0, 0.0};
|
nuclear@0
|
306
|
nuclear@0
|
307 if(!node->parent) {
|
nuclear@0
|
308 return anm_get_node_position(node, tm);
|
nuclear@0
|
309 }
|
nuclear@0
|
310
|
nuclear@0
|
311 anm_get_matrix(node, xform, tm);
|
nuclear@0
|
312 return v3_transform(pos, xform);
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm)
|
nuclear@0
|
316 {
|
nuclear@0
|
317 quat_t rot, prot;
|
nuclear@0
|
318 rot = anm_get_node_rotation(node, tm);
|
nuclear@0
|
319
|
nuclear@0
|
320 if(!node->parent) {
|
nuclear@0
|
321 return rot;
|
nuclear@0
|
322 }
|
nuclear@0
|
323
|
nuclear@0
|
324 prot = anm_get_rotation(node->parent, tm);
|
nuclear@0
|
325 return quat_mul(prot, rot);
|
nuclear@0
|
326 }
|
nuclear@0
|
327
|
nuclear@0
|
328 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm)
|
nuclear@0
|
329 {
|
nuclear@0
|
330 vec3_t s, ps;
|
nuclear@0
|
331 s = anm_get_node_scaling(node, tm);
|
nuclear@0
|
332
|
nuclear@0
|
333 if(!node->parent) {
|
nuclear@0
|
334 return s;
|
nuclear@0
|
335 }
|
nuclear@0
|
336
|
nuclear@0
|
337 ps = anm_get_scaling(node->parent, tm);
|
nuclear@0
|
338 return v3_mul(s, ps);
|
nuclear@0
|
339 }
|
nuclear@0
|
340
|
nuclear@0
|
341 void anm_set_pivot(struct anm_node *node, vec3_t piv)
|
nuclear@0
|
342 {
|
nuclear@0
|
343 node->pivot = piv;
|
nuclear@0
|
344 }
|
nuclear@0
|
345
|
nuclear@0
|
346 vec3_t anm_get_pivot(struct anm_node *node)
|
nuclear@0
|
347 {
|
nuclear@0
|
348 return node->pivot;
|
nuclear@0
|
349 }
|
nuclear@0
|
350
|
nuclear@0
|
351 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
|
nuclear@0
|
352 {
|
nuclear@0
|
353 int i;
|
nuclear@0
|
354 mat4_t rmat;
|
nuclear@0
|
355 vec3_t pos, scale;
|
nuclear@0
|
356 quat_t rot;
|
nuclear@0
|
357
|
nuclear@0
|
358 pos = anm_get_node_position(node, tm);
|
nuclear@0
|
359 rot = anm_get_node_rotation(node, tm);
|
nuclear@0
|
360 scale = anm_get_node_scaling(node, tm);
|
nuclear@0
|
361
|
nuclear@0
|
362 m4_set_translation(mat, node->pivot.x, node->pivot.y, node->pivot.z);
|
nuclear@0
|
363
|
nuclear@0
|
364 quat_to_mat4(rmat, rot);
|
nuclear@0
|
365 for(i=0; i<3; i++) {
|
nuclear@0
|
366 mat[i][0] = rmat[i][0];
|
nuclear@0
|
367 mat[i][1] = rmat[i][1];
|
nuclear@0
|
368 mat[i][2] = rmat[i][2];
|
nuclear@0
|
369 }
|
nuclear@0
|
370 /* this loop is equivalent to: m4_mult(mat, mat, rmat); */
|
nuclear@0
|
371
|
nuclear@0
|
372 mat[0][0] *= scale.x; mat[0][1] *= scale.y; mat[0][2] *= scale.z; mat[0][3] += pos.x;
|
nuclear@0
|
373 mat[1][0] *= scale.x; mat[1][1] *= scale.y; mat[1][2] *= scale.z; mat[1][3] += pos.y;
|
nuclear@0
|
374 mat[2][0] *= scale.x; mat[2][1] *= scale.y; mat[2][2] *= scale.z; mat[2][3] += pos.z;
|
nuclear@0
|
375
|
nuclear@0
|
376 m4_translate(mat, -node->pivot.x, -node->pivot.y, -node->pivot.z);
|
nuclear@0
|
377
|
nuclear@0
|
378 /* that's basically: pivot * rotation * translation * scaling * -pivot */
|
nuclear@0
|
379 }
|
nuclear@0
|
380
|
nuclear@0
|
381 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
|
nuclear@0
|
382 {
|
nuclear@0
|
383 mat4_t tmp;
|
nuclear@0
|
384 anm_get_node_matrix(node, tmp, tm);
|
nuclear@0
|
385 m4_inverse(mat, tmp);
|
nuclear@0
|
386 }
|
nuclear@0
|
387
|
nuclear@0
|
388 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
|
nuclear@0
|
389 {
|
nuclear@1
|
390 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
391 struct mat_cache *cache = pthread_getspecific(node->cache_key);
|
nuclear@1
|
392 #else
|
nuclear@1
|
393 struct mat_cache *cache = node->cache_list;
|
nuclear@1
|
394 #endif
|
nuclear@0
|
395 if(!cache) {
|
nuclear@0
|
396 cache = malloc(sizeof *cache);
|
nuclear@0
|
397 assert(cache);
|
nuclear@1
|
398 cache->time = ANM_TIME_INVAL;
|
nuclear@1
|
399 cache->inv_time = ANM_TIME_INVAL;
|
nuclear@0
|
400
|
nuclear@1
|
401 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
402 pthread_mutex_lock(&node->cache_list_lock);
|
nuclear@0
|
403 cache->next = node->cache_list;
|
nuclear@0
|
404 node->cache_list = cache;
|
nuclear@0
|
405 pthread_mutex_unlock(&node->cache_list_lock);
|
nuclear@0
|
406 pthread_setspecific(node->cache_key, cache);
|
nuclear@1
|
407 #else
|
nuclear@1
|
408 cache->next = node->cache_list;
|
nuclear@1
|
409 node->cache_list = cache;
|
nuclear@1
|
410 #endif
|
nuclear@0
|
411 }
|
nuclear@0
|
412
|
nuclear@0
|
413 if(cache->time != tm) {
|
nuclear@0
|
414 anm_get_node_matrix(node, cache->matrix, tm);
|
nuclear@0
|
415
|
nuclear@0
|
416 if(node->parent) {
|
nuclear@0
|
417 mat4_t parent_mat;
|
nuclear@0
|
418
|
nuclear@0
|
419 anm_get_matrix(node->parent, parent_mat, tm);
|
nuclear@0
|
420 m4_mult(cache->matrix, parent_mat, cache->matrix);
|
nuclear@0
|
421 }
|
nuclear@0
|
422 cache->time = tm;
|
nuclear@0
|
423 }
|
nuclear@0
|
424 m4_copy(mat, cache->matrix);
|
nuclear@0
|
425 }
|
nuclear@0
|
426
|
nuclear@0
|
427 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm)
|
nuclear@0
|
428 {
|
nuclear@1
|
429 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
430 struct mat_cache *cache = pthread_getspecific(node->cache_key);
|
nuclear@1
|
431 #else
|
nuclear@1
|
432 struct mat_cache *cache = node->cache_list;
|
nuclear@1
|
433 #endif
|
nuclear@0
|
434 if(!cache) {
|
nuclear@0
|
435 cache = malloc(sizeof *cache);
|
nuclear@0
|
436 assert(cache);
|
nuclear@1
|
437 cache->time = ANM_TIME_INVAL;
|
nuclear@1
|
438 cache->inv_time = ANM_TIME_INVAL;
|
nuclear@0
|
439
|
nuclear@1
|
440 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
441 pthread_mutex_lock(&node->cache_list_lock);
|
nuclear@0
|
442 cache->next = node->cache_list;
|
nuclear@0
|
443 node->cache_list = cache;
|
nuclear@0
|
444 pthread_mutex_unlock(&node->cache_list_lock);
|
nuclear@0
|
445 pthread_setspecific(node->cache_key, cache);
|
nuclear@1
|
446 #else
|
nuclear@1
|
447 cache->next = node->cache_list;
|
nuclear@1
|
448 node->cache_list = cache;
|
nuclear@1
|
449 #endif
|
nuclear@0
|
450 }
|
nuclear@0
|
451
|
nuclear@0
|
452 if(cache->inv_time != tm) {
|
nuclear@0
|
453 anm_get_matrix(node, mat, tm);
|
nuclear@0
|
454 m4_inverse(cache->inv_matrix, mat);
|
nuclear@0
|
455 cache->inv_time = tm;
|
nuclear@0
|
456 }
|
nuclear@0
|
457 m4_copy(mat, cache->inv_matrix);
|
nuclear@0
|
458 }
|
nuclear@0
|
459
|
nuclear@0
|
460 anm_time_t anm_get_start_time(struct anm_node *node)
|
nuclear@0
|
461 {
|
nuclear@0
|
462 int i;
|
nuclear@0
|
463 struct anm_node *c;
|
nuclear@0
|
464 anm_time_t res = LONG_MAX;
|
nuclear@0
|
465
|
nuclear@0
|
466 for(i=0; i<ANM_NUM_TRACKS; i++) {
|
nuclear@0
|
467 if(node->tracks[i].count) {
|
nuclear@0
|
468 anm_time_t tm = node->tracks[i].keys[0].time;
|
nuclear@0
|
469 if(tm < res) {
|
nuclear@0
|
470 res = tm;
|
nuclear@0
|
471 }
|
nuclear@0
|
472 }
|
nuclear@0
|
473 }
|
nuclear@0
|
474
|
nuclear@0
|
475 c = node->child;
|
nuclear@0
|
476 while(c) {
|
nuclear@0
|
477 anm_time_t tm = anm_get_start_time(c);
|
nuclear@0
|
478 if(tm < res) {
|
nuclear@0
|
479 res = tm;
|
nuclear@0
|
480 }
|
nuclear@0
|
481 c = c->next;
|
nuclear@0
|
482 }
|
nuclear@0
|
483 return res;
|
nuclear@0
|
484 }
|
nuclear@0
|
485
|
nuclear@0
|
486 anm_time_t anm_get_end_time(struct anm_node *node)
|
nuclear@0
|
487 {
|
nuclear@0
|
488 int i;
|
nuclear@0
|
489 struct anm_node *c;
|
nuclear@0
|
490 anm_time_t res = LONG_MIN;
|
nuclear@0
|
491
|
nuclear@0
|
492 for(i=0; i<ANM_NUM_TRACKS; i++) {
|
nuclear@0
|
493 if(node->tracks[i].count) {
|
nuclear@0
|
494 anm_time_t tm = node->tracks[i].keys[node->tracks[i].count - 1].time;
|
nuclear@0
|
495 if(tm > res) {
|
nuclear@0
|
496 res = tm;
|
nuclear@0
|
497 }
|
nuclear@0
|
498 }
|
nuclear@0
|
499 }
|
nuclear@0
|
500
|
nuclear@0
|
501 c = node->child;
|
nuclear@0
|
502 while(c) {
|
nuclear@0
|
503 anm_time_t tm = anm_get_end_time(c);
|
nuclear@0
|
504 if(tm > res) {
|
nuclear@0
|
505 res = tm;
|
nuclear@0
|
506 }
|
nuclear@0
|
507 c = c->next;
|
nuclear@0
|
508 }
|
nuclear@0
|
509 return res;
|
nuclear@0
|
510 }
|
nuclear@0
|
511
|
nuclear@0
|
512 static void invalidate_cache(struct anm_node *node)
|
nuclear@0
|
513 {
|
nuclear@1
|
514 #ifdef ANIM_THREAD_SAFE
|
nuclear@0
|
515 struct mat_cache *cache = pthread_getspecific(node->cache_key);
|
nuclear@1
|
516 #else
|
nuclear@1
|
517 struct mat_cache *cache = node->cache_list;
|
nuclear@1
|
518 #endif
|
nuclear@0
|
519 if(cache) {
|
nuclear@0
|
520 cache->time = cache->inv_time = ANM_TIME_INVAL;
|
nuclear@0
|
521 }
|
nuclear@0
|
522 }
|