2 libanim - hierarchical keyframe animation library
3 Copyright (C) 2012-2018 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #define _TIMESPEC_DEFINED
27 #ifdef ANIM_THREAD_SAFE
50 struct anm_animation {
52 struct anm_track tracks[ANM_NUM_TRACKS];
59 anm_time_t cur_anim_offset[2];
62 /* high-level animation blending transition duration */
65 struct anm_animation *animations;
70 float matrix[16], inv_matrix[16];
71 anm_time_t time, inv_time;
72 struct mat_cache *next;
73 #ifdef ANIM_THREAD_SAFE
75 pthread_key_t cache_key;
76 pthread_mutex_t cache_list_lock;
81 /* matrix calculated by anm_eval functions (no locking, meant as a pre-pass) */
84 struct anm_node *parent;
85 struct anm_node *child;
86 struct anm_node *next;
88 void *data; /* user data pointer */
95 int anm_init_animation(struct anm_animation *anim);
96 void anm_destroy_animation(struct anm_animation *anim);
98 void anm_set_animation_name(struct anm_animation *anim, const char *name);
101 /* ---- node/hierarchy management ---- */
103 /* node constructor and destructor */
104 int anm_init_node(struct anm_node *node);
105 void anm_destroy_node(struct anm_node *node);
107 /* recursively destroy an animation node tree */
108 void anm_destroy_node_tree(struct anm_node *tree);
110 /* helper functions to allocate/construct and destroy/free with
111 * a single call. They call anm_init_node and anm_destroy_node
114 struct anm_node *anm_create_node(void);
115 void anm_free_node(struct anm_node *node);
117 /* recursively destroy and free the nodes of a node tree */
118 void anm_free_node_tree(struct anm_node *tree);
120 int anm_set_node_name(struct anm_node *node, const char *name);
121 const char *anm_get_node_name(struct anm_node *node);
123 /* link and unlink nodes with parent/child relations */
124 void anm_link_node(struct anm_node *parent, struct anm_node *child);
125 int anm_unlink_node(struct anm_node *parent, struct anm_node *child);
127 void anm_set_pivot(struct anm_node *node, float x, float y, float z);
128 void anm_get_pivot(struct anm_node *node, float *x, float *y, float *z);
130 /* ---- multiple animations and animation blending ---- */
132 /* set active animation(s) */
133 int anm_use_node_animation(struct anm_node *node, int aidx);
134 int anm_use_node_animations(struct anm_node *node, int aidx, int bidx, float t);
135 /* recursive variants */
136 int anm_use_animation(struct anm_node *node, int aidx);
137 int anm_use_animations(struct anm_node *node, int aidx, int bidx, float t);
139 /* set/get current animation offset(s) */
140 void anm_set_node_animation_offset(struct anm_node *node, anm_time_t offs, int which);
141 anm_time_t anm_get_animation_offset(const struct anm_node *node, int which);
142 /* recursive variant */
143 void anm_set_animation_offset(struct anm_node *node, anm_time_t offs, int which);
145 /* returns the requested current animation index, which can be 0 or 1 */
146 int anm_get_active_animation_index(const struct anm_node *node, int which);
147 /* returns the requested current animation, which can be 0 or 1 */
148 struct anm_animation *anm_get_active_animation(const struct anm_node *node, int which);
149 float anm_get_active_animation_mix(const struct anm_node *node);
151 int anm_get_animation_count(const struct anm_node *node);
153 /* add/remove an animation to the specified node */
154 int anm_add_node_animation(struct anm_node *node);
155 int anm_remove_node_animation(struct anm_node *node, int idx);
157 /* add/remove an animation to the specified node and all it's descendants */
158 int anm_add_animation(struct anm_node *node);
159 int anm_remove_animation(struct anm_node *node, int idx);
161 struct anm_animation *anm_get_animation(struct anm_node *node, int idx);
162 struct anm_animation *anm_get_animation_by_name(struct anm_node *node, const char *name);
164 int anm_find_animation(struct anm_node *node, const char *name);
166 /* set the interpolator for the (first) currently active animation */
167 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in);
168 /* set the extrapolator for the (first) currently active animation */
169 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex);
171 /* set the name of the currently active animation of this node only */
172 void anm_set_node_active_animation_name(struct anm_node *node, const char *name);
173 /* recursively set the name of the currently active animation for this node
174 * and all it's descendants */
175 void anm_set_active_animation_name(struct anm_node *node, const char *name);
176 /* get the name of the currently active animation of this node */
177 const char *anm_get_active_animation_name(struct anm_node *node);
180 /* ---- high level animation blending interface ---- */
181 /* XXX this convenience interface assumes monotonically increasing time values
182 * in all subsequent calls to anm_get_* and anm_eval_* functions.
184 * anmidx: index of the animation to transition to
185 * start: when to start the transition
186 * dur: transition duration
188 * sets up a transition from the current animation (cur_anim[0]) to another animation.
189 * at time start + dur, the transition will be completed, cur_anim[0] will be the new
190 * animation and cur_anim_offset[0] will be equal to start.
192 void anm_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur);
193 /* non-recursive variant, acts on a single node (you probably DON'T want to use this) */
194 void anm_node_transition(struct anm_node *node, int anmidx, anm_time_t start, anm_time_t dur);
197 /* ---- keyframes / PRS interpolation ---- */
199 void anm_set_position(struct anm_node *node, const float *pos, anm_time_t tm);
200 void anm_set_position3f(struct anm_node *node, float x, float y, float z, anm_time_t tm);
201 void anm_get_node_position(struct anm_node *node, float *pos, anm_time_t tm);
203 void anm_set_rotation(struct anm_node *node, const float *qrot, anm_time_t tm);
204 void anm_set_rotation4f(struct anm_node *node, float x, float y, float z, float w, anm_time_t tm);
205 void anm_set_rotation_axis(struct anm_node *node, float angle, float x, float y, float z, anm_time_t tm);
206 void anm_get_node_rotation(struct anm_node *node, float *qrot, anm_time_t tm);
208 void anm_set_scaling(struct anm_node *node, const float *scale, anm_time_t tm);
209 void anm_set_scaling3f(struct anm_node *node, float x, float y, float z, anm_time_t tm);
210 void anm_get_node_scaling(struct anm_node *node, float *scale, anm_time_t tm);
212 /* these three return the full p/r/s taking hierarchy into account */
213 void anm_get_position(struct anm_node *node, float *pos, anm_time_t tm);
214 void anm_get_rotation(struct anm_node *node, float *qrot, anm_time_t tm);
215 void anm_get_scaling(struct anm_node *node, float *scale, anm_time_t tm);
217 /* those return the start and end times of the whole tree */
218 anm_time_t anm_get_start_time(struct anm_node *node);
219 anm_time_t anm_get_end_time(struct anm_node *node);
222 /* ---- transformation matrices ---- */
224 /* these calculate the matrix and inverse matrix of this node alone */
225 void anm_get_node_matrix(struct anm_node *node, float *mat, anm_time_t tm);
226 void anm_get_node_inv_matrix(struct anm_node *node, float *mat, anm_time_t tm);
228 /* ---- top-down matrix calculation interface ---- */
230 /* calculate and set the matrix of this node */
231 void anm_eval_node(struct anm_node *node, anm_time_t tm);
232 /* calculate and set the matrix of this node and all its children recursively */
233 void anm_eval(struct anm_node *node, anm_time_t tm);
236 /* ---- bottom-up lazy matrix calculation interface ---- */
238 /* These calculate the matrix and inverse matrix of this node taking hierarchy
239 * into account. The results are cached in thread-specific storage and returned
240 * if there's no change in time or tracks from the last query...
242 * A pointer to the internal cached matrix is returned, and also if mat is not
243 * null, the matrix is copied there.
245 float *anm_get_matrix(struct anm_node *node, float *mat, anm_time_t tm);
246 float *anm_get_inv_matrix(struct anm_node *node, float *mat, anm_time_t tm);
252 #endif /* LIBANIM_H_ */