libanim

view src/anim.h @ 22:9758004136f8

added anm_set_animation_name
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 10:58:23 +0200
parents 5993f405a1cb
children 203c11299586
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 float cur_mix;
41 struct anm_animation *animations;
42 vec3_t pivot;
44 /* matrix cache */
45 struct mat_cache {
46 mat4_t matrix, inv_matrix;
47 anm_time_t time, inv_time;
48 struct mat_cache *next;
49 } *cache_list;
50 pthread_key_t cache_key;
51 pthread_mutex_t cache_list_lock;
53 /* matrix calculated by anm_eval functions (no locking, meant as a pre-pass) */
54 mat4_t matrix;
56 struct anm_node *parent;
57 struct anm_node *child;
58 struct anm_node *next;
59 };
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
65 int anm_init_animation(struct anm_animation *anim);
66 void anm_destroy_animation(struct anm_animation *anim);
68 void anm_set_animation_name(struct anm_animation *anim, const char *name);
71 /* node constructor and destructor */
72 int anm_init_node(struct anm_node *node);
73 void anm_destroy_node(struct anm_node *node);
75 /* recursively destroy an animation node tree */
76 void anm_destroy_node_tree(struct anm_node *tree);
78 /* helper functions to allocate/construct and destroy/free with
79 * a single call. They call anm_init_node and anm_destroy_node
80 * internally.
81 */
82 struct anm_node *anm_create_node(void);
83 void anm_free_node(struct anm_node *node);
85 /* recursively destroy and free the nodes of a node tree */
86 void anm_free_node_tree(struct anm_node *tree);
88 int anm_set_node_name(struct anm_node *node, const char *name);
89 const char *anm_get_node_name(struct anm_node *node);
91 /* link and unlink nodes with parent/child relations */
92 void anm_link_node(struct anm_node *parent, struct anm_node *child);
93 int anm_unlink_node(struct anm_node *parent, struct anm_node *child);
95 void anm_set_pivot(struct anm_node *node, vec3_t pivot);
96 vec3_t anm_get_pivot(struct anm_node *node);
98 /* set active animation(s) */
99 int anm_use_node_animation(struct anm_node *node, int aidx);
100 int anm_use_node_animations(struct anm_node *node, int aidx, int bidx, float t);
101 /* recursive variants */
102 int anm_use_animation(struct anm_node *node, int aidx);
103 int anm_use_animations(struct anm_node *node, int aidx, int bidx, float t);
105 /* returns the requested current animation index, which can be 0 or 1 */
106 int anm_get_active_animation_index(struct anm_node *node, int which);
107 /* returns the requested current animation, which can be 0 or 1 */
108 struct anm_animation *anm_get_active_animation(struct anm_node *node, int which);
109 float anm_get_active_animation_mix(struct anm_node *node);
111 int anm_get_animation_count(struct anm_node *node);
113 /* add/remove an animation to the specified node */
114 int anm_add_node_animation(struct anm_node *node);
115 int anm_remove_node_animation(struct anm_node *node, int idx);
117 /* add/remove an animation to the specified node and all it's descendants */
118 int anm_add_animation(struct anm_node *node);
119 int anm_remove_animation(struct anm_node *node, int idx);
121 struct anm_animation *anm_get_animation(struct anm_node *node, int idx);
122 struct anm_animation *anm_get_animation_by_name(struct anm_node *node, const char *name);
124 int anm_find_animation(struct anm_node *node, const char *name);
126 /* set the interpolator for the (first) currently active animation */
127 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in);
128 /* set the extrapolator for the (first) currently active animation */
129 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex);
131 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm);
132 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm);
134 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm);
135 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm);
137 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm);
138 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm);
140 /* these three return the full p/r/s taking hierarchy into account */
141 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm);
142 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm);
143 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm);
145 /* those return the start and end times of the whole tree */
146 anm_time_t anm_get_start_time(struct anm_node *node);
147 anm_time_t anm_get_end_time(struct anm_node *node);
149 /* these calculate the matrix and inverse matrix of this node alone */
150 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
151 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
153 /* ---- top-down matrix calculation interface ---- */
155 /* calculate and set the matrix of this node */
156 void anm_eval_node(struct anm_node *node, anm_time_t tm);
157 /* calculate and set the matrix of this node and all its children recursively */
158 void anm_eval(struct anm_node *node, anm_time_t tm);
161 /* ---- bottom-up lazy matrix calculation interface ---- */
163 /* These calculate the matrix and inverse matrix of this node taking hierarchy
164 * into account. The results are cached in thread-specific storage and returned
165 * if there's no change in time or tracks from the last query...
166 */
167 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
168 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
170 #ifdef __cplusplus
171 }
172 #endif
174 #endif /* LIBANIM_H_ */