nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "anim.h" nuclear@0: #include "dynarr.h" nuclear@0: nuclear@0: #define ROT_USE_SLERP nuclear@0: nuclear@0: static void invalidate_cache(struct anm_node *node); nuclear@0: nuclear@0: int anm_init_node(struct anm_node *node) nuclear@0: { nuclear@0: int i, j; nuclear@0: static const float defaults[] = { nuclear@0: 0.0f, 0.0f, 0.0f, /* default position */ nuclear@0: 0.0f, 0.0f, 0.0f, 1.0f, /* default rotation quat */ nuclear@0: 1.0f, 1.0f, 1.0f /* default scale factor */ nuclear@0: }; nuclear@0: nuclear@0: memset(node, 0, sizeof *node); nuclear@0: nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: /* initialize thread-local matrix cache */ nuclear@0: pthread_key_create(&node->cache_key, 0); nuclear@0: pthread_mutex_init(&node->cache_list_lock, 0); nuclear@1: #endif nuclear@0: nuclear@0: for(i=0; itracks + i) == -1) { nuclear@0: for(j=0; jtracks + i); nuclear@0: } nuclear@0: } nuclear@0: anm_set_track_default(node->tracks + i, defaults[i]); nuclear@0: } nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: void anm_destroy_node(struct anm_node *node) nuclear@0: { nuclear@0: int i; nuclear@0: free(node->name); nuclear@0: nuclear@0: for(i=0; itracks + i); nuclear@0: } nuclear@0: nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: /* destroy thread-specific cache */ nuclear@0: pthread_key_delete(node->cache_key); nuclear@1: #endif nuclear@0: nuclear@0: while(node->cache_list) { nuclear@0: struct mat_cache *tmp = node->cache_list; nuclear@0: node->cache_list = tmp->next; nuclear@0: free(tmp); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void anm_destroy_node_tree(struct anm_node *tree) nuclear@0: { nuclear@0: struct anm_node *c, *tmp; nuclear@0: nuclear@0: if(!tree) return; nuclear@0: nuclear@0: c = tree->child; nuclear@0: while(c) { nuclear@0: tmp = c; nuclear@0: c = c->next; nuclear@0: nuclear@0: anm_destroy_node_tree(tmp); nuclear@0: } nuclear@0: anm_destroy_node(tree); nuclear@0: } nuclear@0: nuclear@0: struct anm_node *anm_create_node(void) nuclear@0: { nuclear@0: struct anm_node *n; nuclear@0: nuclear@0: if((n = malloc(sizeof *n))) { nuclear@0: if(anm_init_node(n) == -1) { nuclear@0: free(n); nuclear@0: return 0; nuclear@0: } nuclear@0: } nuclear@0: return n; nuclear@0: } nuclear@0: nuclear@0: void anm_free_node(struct anm_node *node) nuclear@0: { nuclear@0: anm_destroy_node(node); nuclear@0: free(node); nuclear@0: } nuclear@0: nuclear@0: void anm_free_node_tree(struct anm_node *tree) nuclear@0: { nuclear@0: struct anm_node *c, *tmp; nuclear@0: nuclear@0: if(!tree) return; nuclear@0: nuclear@0: c = tree->child; nuclear@0: while(c) { nuclear@0: tmp = c; nuclear@0: c = c->next; nuclear@0: nuclear@0: anm_free_node_tree(tmp); nuclear@0: } nuclear@0: nuclear@0: anm_free_node(tree); nuclear@0: } nuclear@0: nuclear@0: int anm_set_node_name(struct anm_node *node, const char *name) nuclear@0: { nuclear@0: char *str; nuclear@0: nuclear@0: if(!(str = malloc(strlen(name) + 1))) { nuclear@0: return -1; nuclear@0: } nuclear@0: strcpy(str, name); nuclear@0: free(node->name); nuclear@0: node->name = str; nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: const char *anm_get_node_name(struct anm_node *node) nuclear@0: { nuclear@0: return node->name ? node->name : ""; nuclear@0: } nuclear@0: nuclear@0: void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in) nuclear@0: { nuclear@0: int i; nuclear@0: nuclear@0: for(i=0; itracks + i, in); nuclear@0: } nuclear@0: invalidate_cache(node); nuclear@0: } nuclear@0: nuclear@0: void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex) nuclear@0: { nuclear@0: int i; nuclear@0: nuclear@0: for(i=0; itracks + i, ex); nuclear@0: } nuclear@0: invalidate_cache(node); nuclear@0: } nuclear@0: nuclear@0: void anm_link_node(struct anm_node *p, struct anm_node *c) nuclear@0: { nuclear@0: c->next = p->child; nuclear@0: p->child = c; nuclear@0: nuclear@0: c->parent = p; nuclear@0: invalidate_cache(c); nuclear@0: } nuclear@0: nuclear@0: int anm_unlink_node(struct anm_node *p, struct anm_node *c) nuclear@0: { nuclear@0: struct anm_node *iter; nuclear@0: nuclear@0: if(p->child == c) { nuclear@0: p->child = c->next; nuclear@0: c->next = 0; nuclear@0: invalidate_cache(c); nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: iter = p->child; nuclear@0: while(iter->next) { nuclear@0: if(iter->next == c) { nuclear@0: iter->next = c->next; nuclear@0: c->next = 0; nuclear@0: invalidate_cache(c); nuclear@0: return 0; nuclear@0: } nuclear@0: } nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm) nuclear@0: { nuclear@0: anm_set_value(node->tracks + ANM_TRACK_POS_X, tm, pos.x); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_POS_Y, tm, pos.y); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_POS_Z, tm, pos.z); nuclear@0: invalidate_cache(node); nuclear@0: } nuclear@0: nuclear@0: vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm) nuclear@0: { nuclear@0: vec3_t v; nuclear@0: v.x = anm_get_value(node->tracks + ANM_TRACK_POS_X, tm); nuclear@0: v.y = anm_get_value(node->tracks + ANM_TRACK_POS_Y, tm); nuclear@0: v.z = anm_get_value(node->tracks + ANM_TRACK_POS_Z, tm); nuclear@0: return v; nuclear@0: } nuclear@0: nuclear@0: void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm) nuclear@0: { nuclear@0: anm_set_value(node->tracks + ANM_TRACK_ROT_X, tm, rot.x); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_ROT_Y, tm, rot.y); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_ROT_Z, tm, rot.z); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_ROT_W, tm, rot.w); nuclear@0: invalidate_cache(node); nuclear@0: } nuclear@0: nuclear@0: quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm) nuclear@0: { nuclear@0: #ifndef ROT_USE_SLERP nuclear@0: quat_t q; nuclear@0: q.x = anm_get_value(node->tracks + ANM_TRACK_ROT_X, tm); nuclear@0: q.y = anm_get_value(node->tracks + ANM_TRACK_ROT_Y, tm); nuclear@0: q.z = anm_get_value(node->tracks + ANM_TRACK_ROT_Z, tm); nuclear@0: q.w = anm_get_value(node->tracks + ANM_TRACK_ROT_W, tm); nuclear@0: return q; nuclear@0: #else nuclear@0: int idx0, idx1, last_idx; nuclear@0: anm_time_t tstart, tend; nuclear@0: float t, dt; nuclear@0: struct anm_track *track_x, *track_y, *track_z, *track_w; nuclear@0: quat_t q, q1, q2; nuclear@0: nuclear@0: track_x = node->tracks + ANM_TRACK_ROT_X; nuclear@0: track_y = node->tracks + ANM_TRACK_ROT_Y; nuclear@0: track_z = node->tracks + ANM_TRACK_ROT_Z; nuclear@0: track_w = node->tracks + ANM_TRACK_ROT_W; nuclear@0: nuclear@0: if(!track_x->count) { nuclear@0: q.x = track_x->def_val; nuclear@0: q.y = track_y->def_val; nuclear@0: q.z = track_z->def_val; nuclear@0: q.w = track_w->def_val; nuclear@0: return q; nuclear@0: } nuclear@0: nuclear@0: last_idx = track_x->count - 1; nuclear@0: nuclear@0: tstart = track_x->keys[0].time; nuclear@0: tend = track_x->keys[last_idx].time; nuclear@0: nuclear@0: if(tstart == tend) { nuclear@0: q.x = track_x->keys[0].val; nuclear@0: q.y = track_y->keys[0].val; nuclear@0: q.z = track_z->keys[0].val; nuclear@0: q.w = track_w->keys[0].val; nuclear@0: return q; nuclear@0: } nuclear@0: nuclear@0: tm = anm_remap_time(track_x, tm, tstart, tend); nuclear@0: nuclear@0: idx0 = anm_get_key_interval(track_x, tm); nuclear@0: assert(idx0 >= 0 && idx0 < track_x->count); nuclear@0: idx1 = idx0 + 1; nuclear@0: nuclear@0: if(idx0 == last_idx) { nuclear@0: q.x = track_x->keys[idx0].val; nuclear@0: q.y = track_y->keys[idx0].val; nuclear@0: q.z = track_z->keys[idx0].val; nuclear@0: q.w = track_w->keys[idx0].val; nuclear@0: return q; nuclear@0: } nuclear@0: nuclear@0: dt = (float)(track_x->keys[idx1].time - track_x->keys[idx0].time); nuclear@0: t = (float)(tm - track_x->keys[idx0].time) / dt; nuclear@0: nuclear@0: q1.x = track_x->keys[idx0].val; nuclear@0: q1.y = track_y->keys[idx0].val; nuclear@0: q1.z = track_z->keys[idx0].val; nuclear@0: q1.w = track_w->keys[idx0].val; nuclear@0: nuclear@0: q2.x = track_x->keys[idx1].val; nuclear@0: q2.y = track_y->keys[idx1].val; nuclear@0: q2.z = track_z->keys[idx1].val; nuclear@0: q2.w = track_w->keys[idx1].val; nuclear@0: nuclear@0: /*q1 = quat_normalize(q1); nuclear@0: q2 = quat_normalize(q2);*/ nuclear@0: nuclear@0: return quat_slerp(q1, q2, t); nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm) nuclear@0: { nuclear@0: anm_set_value(node->tracks + ANM_TRACK_SCL_X, tm, scl.x); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_SCL_Y, tm, scl.y); nuclear@0: anm_set_value(node->tracks + ANM_TRACK_SCL_Z, tm, scl.z); nuclear@0: invalidate_cache(node); nuclear@0: } nuclear@0: nuclear@0: vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm) nuclear@0: { nuclear@0: vec3_t v; nuclear@0: v.x = anm_get_value(node->tracks + ANM_TRACK_SCL_X, tm); nuclear@0: v.y = anm_get_value(node->tracks + ANM_TRACK_SCL_Y, tm); nuclear@0: v.z = anm_get_value(node->tracks + ANM_TRACK_SCL_Z, tm); nuclear@0: return v; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: vec3_t anm_get_position(struct anm_node *node, anm_time_t tm) nuclear@0: { nuclear@0: mat4_t xform; nuclear@0: vec3_t pos = {0.0, 0.0, 0.0}; nuclear@0: nuclear@0: if(!node->parent) { nuclear@0: return anm_get_node_position(node, tm); nuclear@0: } nuclear@0: nuclear@0: anm_get_matrix(node, xform, tm); nuclear@0: return v3_transform(pos, xform); nuclear@0: } nuclear@0: nuclear@0: quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm) nuclear@0: { nuclear@0: quat_t rot, prot; nuclear@0: rot = anm_get_node_rotation(node, tm); nuclear@0: nuclear@0: if(!node->parent) { nuclear@0: return rot; nuclear@0: } nuclear@0: nuclear@0: prot = anm_get_rotation(node->parent, tm); nuclear@0: return quat_mul(prot, rot); nuclear@0: } nuclear@0: nuclear@0: vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm) nuclear@0: { nuclear@0: vec3_t s, ps; nuclear@0: s = anm_get_node_scaling(node, tm); nuclear@0: nuclear@0: if(!node->parent) { nuclear@0: return s; nuclear@0: } nuclear@0: nuclear@0: ps = anm_get_scaling(node->parent, tm); nuclear@0: return v3_mul(s, ps); nuclear@0: } nuclear@0: nuclear@0: void anm_set_pivot(struct anm_node *node, vec3_t piv) nuclear@0: { nuclear@0: node->pivot = piv; nuclear@0: } nuclear@0: nuclear@0: vec3_t anm_get_pivot(struct anm_node *node) nuclear@0: { nuclear@0: return node->pivot; nuclear@0: } nuclear@0: nuclear@0: void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm) nuclear@0: { nuclear@0: int i; nuclear@0: mat4_t rmat; nuclear@0: vec3_t pos, scale; nuclear@0: quat_t rot; nuclear@0: nuclear@0: pos = anm_get_node_position(node, tm); nuclear@0: rot = anm_get_node_rotation(node, tm); nuclear@0: scale = anm_get_node_scaling(node, tm); nuclear@0: nuclear@0: m4_set_translation(mat, node->pivot.x, node->pivot.y, node->pivot.z); nuclear@0: nuclear@0: quat_to_mat4(rmat, rot); nuclear@0: for(i=0; i<3; i++) { nuclear@0: mat[i][0] = rmat[i][0]; nuclear@0: mat[i][1] = rmat[i][1]; nuclear@0: mat[i][2] = rmat[i][2]; nuclear@0: } nuclear@0: /* this loop is equivalent to: m4_mult(mat, mat, rmat); */ nuclear@0: nuclear@0: mat[0][0] *= scale.x; mat[0][1] *= scale.y; mat[0][2] *= scale.z; mat[0][3] += pos.x; nuclear@0: mat[1][0] *= scale.x; mat[1][1] *= scale.y; mat[1][2] *= scale.z; mat[1][3] += pos.y; nuclear@0: mat[2][0] *= scale.x; mat[2][1] *= scale.y; mat[2][2] *= scale.z; mat[2][3] += pos.z; nuclear@0: nuclear@0: m4_translate(mat, -node->pivot.x, -node->pivot.y, -node->pivot.z); nuclear@0: nuclear@0: /* that's basically: pivot * rotation * translation * scaling * -pivot */ nuclear@0: } nuclear@0: nuclear@0: void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm) nuclear@0: { nuclear@0: mat4_t tmp; nuclear@0: anm_get_node_matrix(node, tmp, tm); nuclear@0: m4_inverse(mat, tmp); nuclear@0: } nuclear@0: nuclear@0: void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm) nuclear@0: { nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: struct mat_cache *cache = pthread_getspecific(node->cache_key); nuclear@1: #else nuclear@1: struct mat_cache *cache = node->cache_list; nuclear@1: #endif nuclear@0: if(!cache) { nuclear@0: cache = malloc(sizeof *cache); nuclear@0: assert(cache); nuclear@1: cache->time = ANM_TIME_INVAL; nuclear@1: cache->inv_time = ANM_TIME_INVAL; nuclear@0: nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: pthread_mutex_lock(&node->cache_list_lock); nuclear@0: cache->next = node->cache_list; nuclear@0: node->cache_list = cache; nuclear@0: pthread_mutex_unlock(&node->cache_list_lock); nuclear@0: pthread_setspecific(node->cache_key, cache); nuclear@1: #else nuclear@1: cache->next = node->cache_list; nuclear@1: node->cache_list = cache; nuclear@1: #endif nuclear@0: } nuclear@0: nuclear@0: if(cache->time != tm) { nuclear@0: anm_get_node_matrix(node, cache->matrix, tm); nuclear@0: nuclear@0: if(node->parent) { nuclear@0: mat4_t parent_mat; nuclear@0: nuclear@0: anm_get_matrix(node->parent, parent_mat, tm); nuclear@0: m4_mult(cache->matrix, parent_mat, cache->matrix); nuclear@0: } nuclear@0: cache->time = tm; nuclear@0: } nuclear@0: m4_copy(mat, cache->matrix); nuclear@0: } nuclear@0: nuclear@0: void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm) nuclear@0: { nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: struct mat_cache *cache = pthread_getspecific(node->cache_key); nuclear@1: #else nuclear@1: struct mat_cache *cache = node->cache_list; nuclear@1: #endif nuclear@0: if(!cache) { nuclear@0: cache = malloc(sizeof *cache); nuclear@0: assert(cache); nuclear@1: cache->time = ANM_TIME_INVAL; nuclear@1: cache->inv_time = ANM_TIME_INVAL; nuclear@0: nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: pthread_mutex_lock(&node->cache_list_lock); nuclear@0: cache->next = node->cache_list; nuclear@0: node->cache_list = cache; nuclear@0: pthread_mutex_unlock(&node->cache_list_lock); nuclear@0: pthread_setspecific(node->cache_key, cache); nuclear@1: #else nuclear@1: cache->next = node->cache_list; nuclear@1: node->cache_list = cache; nuclear@1: #endif nuclear@0: } nuclear@0: nuclear@0: if(cache->inv_time != tm) { nuclear@0: anm_get_matrix(node, mat, tm); nuclear@0: m4_inverse(cache->inv_matrix, mat); nuclear@0: cache->inv_time = tm; nuclear@0: } nuclear@0: m4_copy(mat, cache->inv_matrix); nuclear@0: } nuclear@0: nuclear@0: anm_time_t anm_get_start_time(struct anm_node *node) nuclear@0: { nuclear@0: int i; nuclear@0: struct anm_node *c; nuclear@0: anm_time_t res = LONG_MAX; nuclear@0: nuclear@0: for(i=0; itracks[i].count) { nuclear@0: anm_time_t tm = node->tracks[i].keys[0].time; nuclear@0: if(tm < res) { nuclear@0: res = tm; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: c = node->child; nuclear@0: while(c) { nuclear@0: anm_time_t tm = anm_get_start_time(c); nuclear@0: if(tm < res) { nuclear@0: res = tm; nuclear@0: } nuclear@0: c = c->next; nuclear@0: } nuclear@0: return res; nuclear@0: } nuclear@0: nuclear@0: anm_time_t anm_get_end_time(struct anm_node *node) nuclear@0: { nuclear@0: int i; nuclear@0: struct anm_node *c; nuclear@0: anm_time_t res = LONG_MIN; nuclear@0: nuclear@0: for(i=0; itracks[i].count) { nuclear@0: anm_time_t tm = node->tracks[i].keys[node->tracks[i].count - 1].time; nuclear@0: if(tm > res) { nuclear@0: res = tm; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: c = node->child; nuclear@0: while(c) { nuclear@0: anm_time_t tm = anm_get_end_time(c); nuclear@0: if(tm > res) { nuclear@0: res = tm; nuclear@0: } nuclear@0: c = c->next; nuclear@0: } nuclear@0: return res; nuclear@0: } nuclear@0: nuclear@0: static void invalidate_cache(struct anm_node *node) nuclear@0: { nuclear@1: #ifdef ANIM_THREAD_SAFE nuclear@0: struct mat_cache *cache = pthread_getspecific(node->cache_key); nuclear@1: #else nuclear@1: struct mat_cache *cache = node->cache_list; nuclear@1: #endif nuclear@0: if(cache) { nuclear@0: cache->time = cache->inv_time = ANM_TIME_INVAL; nuclear@0: } nuclear@0: }