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