libanim

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