goat3d

annotate 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
rev   line source
nuclear@27 1 #ifndef LIBANIM_H_
nuclear@27 2 #define LIBANIM_H_
nuclear@27 3
nuclear@27 4 #include "config.h"
nuclear@27 5
nuclear@49 6 #include <pthread.h>
nuclear@49 7
nuclear@27 8 #include <vmath/vector.h>
nuclear@27 9 #include <vmath/quat.h>
nuclear@27 10 #include <vmath/matrix.h>
nuclear@27 11 #include "track.h"
nuclear@27 12
nuclear@27 13 enum {
nuclear@27 14 ANM_TRACK_POS_X,
nuclear@27 15 ANM_TRACK_POS_Y,
nuclear@27 16 ANM_TRACK_POS_Z,
nuclear@27 17
nuclear@27 18 ANM_TRACK_ROT_X,
nuclear@27 19 ANM_TRACK_ROT_Y,
nuclear@27 20 ANM_TRACK_ROT_Z,
nuclear@27 21 ANM_TRACK_ROT_W,
nuclear@27 22
nuclear@27 23 ANM_TRACK_SCL_X,
nuclear@27 24 ANM_TRACK_SCL_Y,
nuclear@27 25 ANM_TRACK_SCL_Z,
nuclear@27 26
nuclear@27 27 ANM_NUM_TRACKS
nuclear@27 28 };
nuclear@27 29
nuclear@49 30 struct anm_animation {
nuclear@49 31 char *name;
nuclear@49 32 struct anm_track tracks[ANM_NUM_TRACKS];
nuclear@49 33 };
nuclear@49 34
nuclear@27 35 struct anm_node {
nuclear@27 36 char *name;
nuclear@27 37
nuclear@49 38 int cur_anim[2];
nuclear@49 39 anm_time_t cur_anim_offset[2];
nuclear@49 40 float cur_mix;
nuclear@49 41
nuclear@49 42 /* high-level animation blending transition duration */
nuclear@49 43 anm_time_t blend_dur;
nuclear@49 44
nuclear@49 45 struct anm_animation *animations;
nuclear@27 46 vec3_t pivot;
nuclear@27 47
nuclear@27 48 /* matrix cache */
nuclear@27 49 struct mat_cache {
nuclear@27 50 mat4_t matrix, inv_matrix;
nuclear@27 51 anm_time_t time, inv_time;
nuclear@49 52 struct mat_cache *next;
nuclear@49 53 } *cache_list;
nuclear@49 54 pthread_key_t cache_key;
nuclear@49 55 pthread_mutex_t cache_list_lock;
nuclear@49 56
nuclear@49 57 /* matrix calculated by anm_eval functions (no locking, meant as a pre-pass) */
nuclear@49 58 mat4_t matrix;
nuclear@27 59
nuclear@27 60 struct anm_node *parent;
nuclear@27 61 struct anm_node *child;
nuclear@27 62 struct anm_node *next;
nuclear@27 63 };
nuclear@27 64
nuclear@27 65 #ifdef __cplusplus
nuclear@27 66 extern "C" {
nuclear@27 67 #endif
nuclear@27 68
nuclear@49 69 int anm_init_animation(struct anm_animation *anim);
nuclear@49 70 void anm_destroy_animation(struct anm_animation *anim);
nuclear@49 71
nuclear@49 72 void anm_set_animation_name(struct anm_animation *anim, const char *name);
nuclear@49 73
nuclear@49 74
nuclear@49 75 /* ---- node/hierarchy management ---- */
nuclear@49 76
nuclear@27 77 /* node constructor and destructor */
nuclear@27 78 int anm_init_node(struct anm_node *node);
nuclear@27 79 void anm_destroy_node(struct anm_node *node);
nuclear@27 80
nuclear@27 81 /* recursively destroy an animation node tree */
nuclear@27 82 void anm_destroy_node_tree(struct anm_node *tree);
nuclear@27 83
nuclear@27 84 /* helper functions to allocate/construct and destroy/free with
nuclear@27 85 * a single call. They call anm_init_node and anm_destroy_node
nuclear@27 86 * internally.
nuclear@27 87 */
nuclear@27 88 struct anm_node *anm_create_node(void);
nuclear@27 89 void anm_free_node(struct anm_node *node);
nuclear@27 90
nuclear@27 91 /* recursively destroy and free the nodes of a node tree */
nuclear@27 92 void anm_free_node_tree(struct anm_node *tree);
nuclear@27 93
nuclear@27 94 int anm_set_node_name(struct anm_node *node, const char *name);
nuclear@27 95 const char *anm_get_node_name(struct anm_node *node);
nuclear@27 96
nuclear@27 97 /* link and unlink nodes with parent/child relations */
nuclear@27 98 void anm_link_node(struct anm_node *parent, struct anm_node *child);
nuclear@27 99 int anm_unlink_node(struct anm_node *parent, struct anm_node *child);
nuclear@27 100
nuclear@49 101 void anm_set_pivot(struct anm_node *node, vec3_t pivot);
nuclear@49 102 vec3_t anm_get_pivot(struct anm_node *node);
nuclear@49 103
nuclear@49 104 /* ---- multiple animations and animation blending ---- */
nuclear@49 105
nuclear@49 106 /* set active animation(s) */
nuclear@49 107 int anm_use_node_animation(struct anm_node *node, int aidx);
nuclear@49 108 int anm_use_node_animations(struct anm_node *node, int aidx, int bidx, float t);
nuclear@49 109 /* recursive variants */
nuclear@49 110 int anm_use_animation(struct anm_node *node, int aidx);
nuclear@49 111 int anm_use_animations(struct anm_node *node, int aidx, int bidx, float t);
nuclear@49 112
nuclear@49 113 /* set/get current animation offset(s) */
nuclear@49 114 void anm_set_node_animation_offset(struct anm_node *node, anm_time_t offs, int which);
nuclear@49 115 anm_time_t anm_get_animation_offset(const struct anm_node *node, int which);
nuclear@49 116 /* recursive variant */
nuclear@49 117 void anm_set_animation_offset(struct anm_node *node, anm_time_t offs, int which);
nuclear@49 118
nuclear@49 119 /* returns the requested current animation index, which can be 0 or 1 */
nuclear@49 120 int anm_get_active_animation_index(const struct anm_node *node, int which);
nuclear@49 121 /* returns the requested current animation, which can be 0 or 1 */
nuclear@49 122 struct anm_animation *anm_get_active_animation(const struct anm_node *node, int which);
nuclear@49 123 float anm_get_active_animation_mix(const struct anm_node *node);
nuclear@49 124
nuclear@49 125 int anm_get_animation_count(const struct anm_node *node);
nuclear@49 126
nuclear@49 127 /* add/remove an animation to the specified node */
nuclear@49 128 int anm_add_node_animation(struct anm_node *node);
nuclear@49 129 int anm_remove_node_animation(struct anm_node *node, int idx);
nuclear@49 130
nuclear@49 131 /* add/remove an animation to the specified node and all it's descendants */
nuclear@49 132 int anm_add_animation(struct anm_node *node);
nuclear@49 133 int anm_remove_animation(struct anm_node *node, int idx);
nuclear@49 134
nuclear@49 135 struct anm_animation *anm_get_animation(struct anm_node *node, int idx);
nuclear@49 136 struct anm_animation *anm_get_animation_by_name(struct anm_node *node, const char *name);
nuclear@49 137
nuclear@49 138 int anm_find_animation(struct anm_node *node, const char *name);
nuclear@49 139
nuclear@49 140 /* set the interpolator for the (first) currently active animation */
nuclear@49 141 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in);
nuclear@49 142 /* set the extrapolator for the (first) currently active animation */
nuclear@49 143 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex);
nuclear@49 144
nuclear@49 145 /* set the name of the currently active animation of this node only */
nuclear@49 146 void anm_set_node_active_animation_name(struct anm_node *node, const char *name);
nuclear@49 147 /* recursively set the name of the currently active animation for this node
nuclear@49 148 * and all it's descendants */
nuclear@49 149 void anm_set_active_animation_name(struct anm_node *node, const char *name);
nuclear@49 150 /* get the name of the currently active animation of this node */
nuclear@49 151 const char *anm_get_active_animation_name(struct anm_node *node);
nuclear@49 152
nuclear@49 153
nuclear@49 154 /* ---- high level animation blending interface ---- */
nuclear@49 155 /* XXX this convenience interface assumes monotonically increasing time values
nuclear@49 156 * in all subsequent calls to anm_get_* and anm_eval_* functions.
nuclear@49 157 *
nuclear@49 158 * anmidx: index of the animation to transition to
nuclear@49 159 * start: when to start the transition
nuclear@49 160 * dur: transition duration
nuclear@49 161 *
nuclear@49 162 * sets up a transition from the current animation (cur_anim[0]) to another animation.
nuclear@49 163 * at time start + dur, the transition will be completed, cur_anim[0] will be the new
nuclear@49 164 * animation and cur_anim_offset[0] will be equal to start.
nuclear@49 165 */
nuclear@49 166 void anm_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur);
nuclear@49 167 /* non-recursive variant, acts on a single node (you probably DON'T want to use this) */
nuclear@49 168 void anm_node_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur);
nuclear@49 169
nuclear@49 170
nuclear@49 171 /* ---- keyframes / PRS interpolation ---- */
nuclear@49 172
nuclear@27 173 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm);
nuclear@27 174 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm);
nuclear@27 175
nuclear@27 176 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm);
nuclear@27 177 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm);
nuclear@27 178
nuclear@27 179 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm);
nuclear@27 180 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm);
nuclear@27 181
nuclear@27 182 /* these three return the full p/r/s taking hierarchy into account */
nuclear@27 183 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm);
nuclear@27 184 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm);
nuclear@27 185 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm);
nuclear@27 186
nuclear@49 187 /* those return the start and end times of the whole tree */
nuclear@49 188 anm_time_t anm_get_start_time(struct anm_node *node);
nuclear@49 189 anm_time_t anm_get_end_time(struct anm_node *node);
nuclear@49 190
nuclear@49 191
nuclear@49 192 /* ---- transformation matrices ---- */
nuclear@27 193
nuclear@27 194 /* these calculate the matrix and inverse matrix of this node alone */
nuclear@27 195 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@27 196 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@27 197
nuclear@49 198 /* ---- top-down matrix calculation interface ---- */
nuclear@49 199
nuclear@49 200 /* calculate and set the matrix of this node */
nuclear@49 201 void anm_eval_node(struct anm_node *node, anm_time_t tm);
nuclear@49 202 /* calculate and set the matrix of this node and all its children recursively */
nuclear@49 203 void anm_eval(struct anm_node *node, anm_time_t tm);
nuclear@49 204
nuclear@49 205
nuclear@49 206 /* ---- bottom-up lazy matrix calculation interface ---- */
nuclear@49 207
nuclear@27 208 /* These calculate the matrix and inverse matrix of this node taking hierarchy
nuclear@27 209 * into account. The results are cached in thread-specific storage and returned
nuclear@27 210 * if there's no change in time or tracks from the last query...
nuclear@27 211 */
nuclear@27 212 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@27 213 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@27 214
nuclear@27 215 #ifdef __cplusplus
nuclear@27 216 }
nuclear@27 217 #endif
nuclear@27 218
nuclear@27 219 #endif /* LIBANIM_H_ */