libanim

annotate src/anim.h @ 24:09e267e7ed4a

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