libanim

annotate src/anim.c @ 43:a720e8d7023d

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