libanim

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