libanim

annotate src/anim.h @ 77:b1c6448d56bc

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