goat3d

diff libs/anim/anim.h @ 49:0ecb788a87f7

merged changes from libanim
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 16 Jan 2014 19:13:45 +0200
parents 4deb0b12fe14
children
line diff
     1.1 --- a/libs/anim/anim.h	Sun Dec 29 06:01:59 2013 +0200
     1.2 +++ b/libs/anim/anim.h	Thu Jan 16 19:13:45 2014 +0200
     1.3 @@ -3,6 +3,8 @@
     1.4  
     1.5  #include "config.h"
     1.6  
     1.7 +#include <pthread.h>
     1.8 +
     1.9  #include <vmath/vector.h>
    1.10  #include <vmath/quat.h>
    1.11  #include <vmath/matrix.h>
    1.12 @@ -25,17 +27,35 @@
    1.13  	ANM_NUM_TRACKS
    1.14  };
    1.15  
    1.16 +struct anm_animation {
    1.17 +	char *name;
    1.18 +	struct anm_track tracks[ANM_NUM_TRACKS];
    1.19 +};
    1.20 +
    1.21  struct anm_node {
    1.22  	char *name;
    1.23  
    1.24 -	struct anm_track tracks[ANM_NUM_TRACKS];
    1.25 +	int cur_anim[2];
    1.26 +	anm_time_t cur_anim_offset[2];
    1.27 +	float cur_mix;
    1.28 +
    1.29 +	/* high-level animation blending transition duration */
    1.30 +	anm_time_t blend_dur;
    1.31 +
    1.32 +	struct anm_animation *animations;
    1.33  	vec3_t pivot;
    1.34  
    1.35  	/* matrix cache */
    1.36  	struct mat_cache {
    1.37  		mat4_t matrix, inv_matrix;
    1.38  		anm_time_t time, inv_time;
    1.39 -	} cache;
    1.40 +		struct mat_cache *next;
    1.41 +	} *cache_list;
    1.42 +	pthread_key_t cache_key;
    1.43 +	pthread_mutex_t cache_list_lock;
    1.44 +
    1.45 +	/* matrix calculated by anm_eval functions (no locking, meant as a pre-pass) */
    1.46 +	mat4_t matrix;
    1.47  
    1.48  	struct anm_node *parent;
    1.49  	struct anm_node *child;
    1.50 @@ -46,6 +66,14 @@
    1.51  extern "C" {
    1.52  #endif
    1.53  
    1.54 +int anm_init_animation(struct anm_animation *anim);
    1.55 +void anm_destroy_animation(struct anm_animation *anim);
    1.56 +
    1.57 +void anm_set_animation_name(struct anm_animation *anim, const char *name);
    1.58 +
    1.59 +
    1.60 +/* ---- node/hierarchy management ---- */
    1.61 +
    1.62  /* node constructor and destructor */
    1.63  int anm_init_node(struct anm_node *node);
    1.64  void anm_destroy_node(struct anm_node *node);
    1.65 @@ -66,13 +94,82 @@
    1.66  int anm_set_node_name(struct anm_node *node, const char *name);
    1.67  const char *anm_get_node_name(struct anm_node *node);
    1.68  
    1.69 -void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in);
    1.70 -void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex);
    1.71 -
    1.72  /* link and unlink nodes with parent/child relations */
    1.73  void anm_link_node(struct anm_node *parent, struct anm_node *child);
    1.74  int anm_unlink_node(struct anm_node *parent, struct anm_node *child);
    1.75  
    1.76 +void anm_set_pivot(struct anm_node *node, vec3_t pivot);
    1.77 +vec3_t anm_get_pivot(struct anm_node *node);
    1.78 +
    1.79 +/* ---- multiple animations and animation blending ---- */
    1.80 +
    1.81 +/* set active animation(s) */
    1.82 +int anm_use_node_animation(struct anm_node *node, int aidx);
    1.83 +int anm_use_node_animations(struct anm_node *node, int aidx, int bidx, float t);
    1.84 +/* recursive variants */
    1.85 +int anm_use_animation(struct anm_node *node, int aidx);
    1.86 +int anm_use_animations(struct anm_node *node, int aidx, int bidx, float t);
    1.87 +
    1.88 +/* set/get current animation offset(s) */
    1.89 +void anm_set_node_animation_offset(struct anm_node *node, anm_time_t offs, int which);
    1.90 +anm_time_t anm_get_animation_offset(const struct anm_node *node, int which);
    1.91 +/* recursive variant */
    1.92 +void anm_set_animation_offset(struct anm_node *node, anm_time_t offs, int which);
    1.93 +
    1.94 +/* returns the requested current animation index, which can be 0 or 1 */
    1.95 +int anm_get_active_animation_index(const struct anm_node *node, int which);
    1.96 +/* returns the requested current animation, which can be 0 or 1 */
    1.97 +struct anm_animation *anm_get_active_animation(const struct anm_node *node, int which);
    1.98 +float anm_get_active_animation_mix(const struct anm_node *node);
    1.99 +
   1.100 +int anm_get_animation_count(const struct anm_node *node);
   1.101 +
   1.102 +/* add/remove an animation to the specified node */
   1.103 +int anm_add_node_animation(struct anm_node *node);
   1.104 +int anm_remove_node_animation(struct anm_node *node, int idx);
   1.105 +
   1.106 +/* add/remove an animation to the specified node and all it's descendants */
   1.107 +int anm_add_animation(struct anm_node *node);
   1.108 +int anm_remove_animation(struct anm_node *node, int idx);
   1.109 +
   1.110 +struct anm_animation *anm_get_animation(struct anm_node *node, int idx);
   1.111 +struct anm_animation *anm_get_animation_by_name(struct anm_node *node, const char *name);
   1.112 +
   1.113 +int anm_find_animation(struct anm_node *node, const char *name);
   1.114 +
   1.115 +/* set the interpolator for the (first) currently active animation */
   1.116 +void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in);
   1.117 +/* set the extrapolator for the (first) currently active animation */
   1.118 +void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex);
   1.119 +
   1.120 +/* set the name of the currently active animation of this node only */
   1.121 +void anm_set_node_active_animation_name(struct anm_node *node, const char *name);
   1.122 +/* recursively set the name of the currently active animation for this node
   1.123 + * and all it's descendants */
   1.124 +void anm_set_active_animation_name(struct anm_node *node, const char *name);
   1.125 +/* get the name of the currently active animation of this node */
   1.126 +const char *anm_get_active_animation_name(struct anm_node *node);
   1.127 +
   1.128 +
   1.129 +/* ---- high level animation blending interface ---- */
   1.130 +/* XXX this convenience interface assumes monotonically increasing time values
   1.131 + *     in all subsequent calls to anm_get_* and anm_eval_* functions.
   1.132 + *
   1.133 + * anmidx: index of the animation to transition to
   1.134 + * start: when to start the transition
   1.135 + * dur: transition duration
   1.136 + *
   1.137 + * sets up a transition from the current animation (cur_anim[0]) to another animation.
   1.138 + * at time start + dur, the transition will be completed, cur_anim[0] will be the new
   1.139 + * animation and cur_anim_offset[0] will be equal to start.
   1.140 + */
   1.141 +void anm_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur);
   1.142 +/* non-recursive variant, acts on a single node (you probably DON'T want to use this) */
   1.143 +void anm_node_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur);
   1.144 +
   1.145 +
   1.146 +/* ---- keyframes / PRS interpolation ---- */
   1.147 +
   1.148  void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm);
   1.149  vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm);
   1.150  
   1.151 @@ -87,13 +184,27 @@
   1.152  quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm);
   1.153  vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm);
   1.154  
   1.155 -void anm_set_pivot(struct anm_node *node, vec3_t pivot);
   1.156 -vec3_t anm_get_pivot(struct anm_node *node);
   1.157 +/* those return the start and end times of the whole tree */
   1.158 +anm_time_t anm_get_start_time(struct anm_node *node);
   1.159 +anm_time_t anm_get_end_time(struct anm_node *node);
   1.160 +
   1.161 +
   1.162 +/* ---- transformation matrices ---- */
   1.163  
   1.164  /* these calculate the matrix and inverse matrix of this node alone */
   1.165  void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
   1.166  void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
   1.167  
   1.168 +/* ---- top-down matrix calculation interface ---- */
   1.169 +
   1.170 +/* calculate and set the matrix of this node */
   1.171 +void anm_eval_node(struct anm_node *node, anm_time_t tm);
   1.172 +/* calculate and set the matrix of this node and all its children recursively */
   1.173 +void anm_eval(struct anm_node *node, anm_time_t tm);
   1.174 +
   1.175 +
   1.176 +/* ---- bottom-up lazy matrix calculation interface ---- */
   1.177 +
   1.178  /* These calculate the matrix and inverse matrix of this node taking hierarchy
   1.179   * into account. The results are cached in thread-specific storage and returned
   1.180   * if there's no change in time or tracks from the last query...
   1.181 @@ -101,10 +212,6 @@
   1.182  void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
   1.183  void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
   1.184  
   1.185 -/* those return the start and end times of the whole tree */
   1.186 -anm_time_t anm_get_start_time(struct anm_node *node);
   1.187 -anm_time_t anm_get_end_time(struct anm_node *node);
   1.188 -
   1.189  #ifdef __cplusplus
   1.190  }
   1.191  #endif