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