libanim

annotate src/anim.h @ 60:8f7193d00555

set/get currently active animation name and minor enhancements in the example
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 11:29:42 +0200
parents 9758004136f8
children 09e267e7ed4a
rev   line source
nuclear@0 1 #ifndef LIBANIM_H_
nuclear@0 2 #define LIBANIM_H_
nuclear@0 3
nuclear@0 4 #include "config.h"
nuclear@0 5
nuclear@0 6 #include <pthread.h>
nuclear@0 7
nuclear@1 8 #include <vmath/vector.h>
nuclear@1 9 #include <vmath/quat.h>
nuclear@1 10 #include <vmath/matrix.h>
nuclear@0 11 #include "track.h"
nuclear@0 12
nuclear@0 13 enum {
nuclear@0 14 ANM_TRACK_POS_X,
nuclear@0 15 ANM_TRACK_POS_Y,
nuclear@0 16 ANM_TRACK_POS_Z,
nuclear@0 17
nuclear@0 18 ANM_TRACK_ROT_X,
nuclear@0 19 ANM_TRACK_ROT_Y,
nuclear@0 20 ANM_TRACK_ROT_Z,
nuclear@0 21 ANM_TRACK_ROT_W,
nuclear@0 22
nuclear@0 23 ANM_TRACK_SCL_X,
nuclear@0 24 ANM_TRACK_SCL_Y,
nuclear@0 25 ANM_TRACK_SCL_Z,
nuclear@0 26
nuclear@0 27 ANM_NUM_TRACKS
nuclear@0 28 };
nuclear@0 29
nuclear@21 30 struct anm_animation {
nuclear@21 31 char *name;
nuclear@21 32 struct anm_track tracks[ANM_NUM_TRACKS];
nuclear@21 33 };
nuclear@21 34
nuclear@0 35 struct anm_node {
nuclear@0 36 char *name;
nuclear@0 37
nuclear@21 38 int cur_anim[2];
nuclear@21 39 float cur_mix;
nuclear@21 40
nuclear@21 41 struct anm_animation *animations;
nuclear@0 42 vec3_t pivot;
nuclear@0 43
nuclear@0 44 /* matrix cache */
nuclear@0 45 struct mat_cache {
nuclear@0 46 mat4_t matrix, inv_matrix;
nuclear@0 47 anm_time_t time, inv_time;
nuclear@0 48 struct mat_cache *next;
nuclear@0 49 } *cache_list;
nuclear@0 50 pthread_key_t cache_key;
nuclear@0 51 pthread_mutex_t cache_list_lock;
nuclear@0 52
nuclear@20 53 /* matrix calculated by anm_eval functions (no locking, meant as a pre-pass) */
nuclear@20 54 mat4_t matrix;
nuclear@20 55
nuclear@0 56 struct anm_node *parent;
nuclear@0 57 struct anm_node *child;
nuclear@0 58 struct anm_node *next;
nuclear@0 59 };
nuclear@0 60
nuclear@0 61 #ifdef __cplusplus
nuclear@0 62 extern "C" {
nuclear@0 63 #endif
nuclear@0 64
nuclear@21 65 int anm_init_animation(struct anm_animation *anim);
nuclear@21 66 void anm_destroy_animation(struct anm_animation *anim);
nuclear@21 67
nuclear@22 68 void anm_set_animation_name(struct anm_animation *anim, const char *name);
nuclear@22 69
nuclear@22 70
nuclear@0 71 /* node constructor and destructor */
nuclear@0 72 int anm_init_node(struct anm_node *node);
nuclear@0 73 void anm_destroy_node(struct anm_node *node);
nuclear@0 74
nuclear@0 75 /* recursively destroy an animation node tree */
nuclear@0 76 void anm_destroy_node_tree(struct anm_node *tree);
nuclear@0 77
nuclear@0 78 /* helper functions to allocate/construct and destroy/free with
nuclear@0 79 * a single call. They call anm_init_node and anm_destroy_node
nuclear@0 80 * internally.
nuclear@0 81 */
nuclear@0 82 struct anm_node *anm_create_node(void);
nuclear@0 83 void anm_free_node(struct anm_node *node);
nuclear@0 84
nuclear@0 85 /* recursively destroy and free the nodes of a node tree */
nuclear@0 86 void anm_free_node_tree(struct anm_node *tree);
nuclear@0 87
nuclear@0 88 int anm_set_node_name(struct anm_node *node, const char *name);
nuclear@0 89 const char *anm_get_node_name(struct anm_node *node);
nuclear@0 90
nuclear@0 91 /* link and unlink nodes with parent/child relations */
nuclear@0 92 void anm_link_node(struct anm_node *parent, struct anm_node *child);
nuclear@0 93 int anm_unlink_node(struct anm_node *parent, struct anm_node *child);
nuclear@0 94
nuclear@21 95 void anm_set_pivot(struct anm_node *node, vec3_t pivot);
nuclear@21 96 vec3_t anm_get_pivot(struct anm_node *node);
nuclear@21 97
nuclear@21 98 /* set active animation(s) */
nuclear@21 99 int anm_use_node_animation(struct anm_node *node, int aidx);
nuclear@21 100 int anm_use_node_animations(struct anm_node *node, int aidx, int bidx, float t);
nuclear@21 101 /* recursive variants */
nuclear@21 102 int anm_use_animation(struct anm_node *node, int aidx);
nuclear@21 103 int anm_use_animations(struct anm_node *node, int aidx, int bidx, float t);
nuclear@21 104
nuclear@21 105 /* returns the requested current animation index, which can be 0 or 1 */
nuclear@21 106 int anm_get_active_animation_index(struct anm_node *node, int which);
nuclear@21 107 /* returns the requested current animation, which can be 0 or 1 */
nuclear@21 108 struct anm_animation *anm_get_active_animation(struct anm_node *node, int which);
nuclear@21 109 float anm_get_active_animation_mix(struct anm_node *node);
nuclear@21 110
nuclear@21 111 int anm_get_animation_count(struct anm_node *node);
nuclear@21 112
nuclear@21 113 /* add/remove an animation to the specified node */
nuclear@21 114 int anm_add_node_animation(struct anm_node *node);
nuclear@21 115 int anm_remove_node_animation(struct anm_node *node, int idx);
nuclear@21 116
nuclear@21 117 /* add/remove an animation to the specified node and all it's descendants */
nuclear@21 118 int anm_add_animation(struct anm_node *node);
nuclear@21 119 int anm_remove_animation(struct anm_node *node, int idx);
nuclear@21 120
nuclear@21 121 struct anm_animation *anm_get_animation(struct anm_node *node, int idx);
nuclear@21 122 struct anm_animation *anm_get_animation_by_name(struct anm_node *node, const char *name);
nuclear@21 123
nuclear@21 124 int anm_find_animation(struct anm_node *node, const char *name);
nuclear@21 125
nuclear@21 126 /* set the interpolator for the (first) currently active animation */
nuclear@21 127 void anm_set_interpolator(struct anm_node *node, enum anm_interpolator in);
nuclear@21 128 /* set the extrapolator for the (first) currently active animation */
nuclear@21 129 void anm_set_extrapolator(struct anm_node *node, enum anm_extrapolator ex);
nuclear@21 130
nuclear@23 131 /* set the name of the currently active animation of this node only */
nuclear@23 132 void anm_set_node_active_animation_name(struct anm_node *node, const char *name);
nuclear@23 133 /* recursively set the name of the currently active animation for this node
nuclear@23 134 * and all it's descendants */
nuclear@23 135 void anm_set_active_animation_name(struct anm_node *node, const char *name);
nuclear@23 136 /* get the name of the currently active animation of this node */
nuclear@23 137 const char *anm_get_active_animation_name(struct anm_node *node);
nuclear@23 138
nuclear@0 139 void anm_set_position(struct anm_node *node, vec3_t pos, anm_time_t tm);
nuclear@0 140 vec3_t anm_get_node_position(struct anm_node *node, anm_time_t tm);
nuclear@0 141
nuclear@0 142 void anm_set_rotation(struct anm_node *node, quat_t rot, anm_time_t tm);
nuclear@0 143 quat_t anm_get_node_rotation(struct anm_node *node, anm_time_t tm);
nuclear@0 144
nuclear@0 145 void anm_set_scaling(struct anm_node *node, vec3_t scl, anm_time_t tm);
nuclear@0 146 vec3_t anm_get_node_scaling(struct anm_node *node, anm_time_t tm);
nuclear@0 147
nuclear@0 148 /* these three return the full p/r/s taking hierarchy into account */
nuclear@0 149 vec3_t anm_get_position(struct anm_node *node, anm_time_t tm);
nuclear@0 150 quat_t anm_get_rotation(struct anm_node *node, anm_time_t tm);
nuclear@0 151 vec3_t anm_get_scaling(struct anm_node *node, anm_time_t tm);
nuclear@0 152
nuclear@20 153 /* those return the start and end times of the whole tree */
nuclear@20 154 anm_time_t anm_get_start_time(struct anm_node *node);
nuclear@20 155 anm_time_t anm_get_end_time(struct anm_node *node);
nuclear@20 156
nuclear@5 157 /* these calculate the matrix and inverse matrix of this node alone */
nuclear@5 158 void anm_get_node_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@5 159 void anm_get_node_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@5 160
nuclear@20 161 /* ---- top-down matrix calculation interface ---- */
nuclear@20 162
nuclear@20 163 /* calculate and set the matrix of this node */
nuclear@20 164 void anm_eval_node(struct anm_node *node, anm_time_t tm);
nuclear@20 165 /* calculate and set the matrix of this node and all its children recursively */
nuclear@20 166 void anm_eval(struct anm_node *node, anm_time_t tm);
nuclear@20 167
nuclear@20 168
nuclear@20 169 /* ---- bottom-up lazy matrix calculation interface ---- */
nuclear@20 170
nuclear@5 171 /* These calculate the matrix and inverse matrix of this node taking hierarchy
nuclear@5 172 * into account. The results are cached in thread-specific storage and returned
nuclear@5 173 * if there's no change in time or tracks from the last query...
nuclear@5 174 */
nuclear@0 175 void anm_get_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@0 176 void anm_get_inv_matrix(struct anm_node *node, mat4_t mat, anm_time_t tm);
nuclear@0 177
nuclear@0 178 #ifdef __cplusplus
nuclear@0 179 }
nuclear@0 180 #endif
nuclear@0 181
nuclear@0 182 #endif /* LIBANIM_H_ */