dungeon_crawler

view prototype/anim/track.c @ 75:b05ab29cd17d

color grading done
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 22 Oct 2012 03:55:00 +0300
parents
children
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "track.h"
5 #include "dynarr.h"
7 static int keycmp(const void *a, const void *b);
8 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm);
10 static float interp_step(float v0, float v1, float v2, float v3, float t);
11 static float interp_linear(float v0, float v1, float v2, float v3, float t);
12 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
14 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
15 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
16 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
18 /* XXX keep this in sync with enum anm_interpolator at track.h */
19 static float (*interp[])(float, float, float, float, float) = {
20 interp_step,
21 interp_linear,
22 interp_cubic,
23 0
24 };
26 /* XXX keep this in sync with enum anm_extrapolator at track.h */
27 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
28 remap_extend,
29 remap_clamp,
30 remap_repeat,
31 0
32 };
34 int anm_init_track(struct anm_track *track)
35 {
36 memset(track, 0, sizeof *track);
38 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
39 return -1;
40 }
41 track->interp = ANM_INTERP_LINEAR;
42 track->extrap = ANM_EXTRAP_CLAMP;
43 return 0;
44 }
46 void anm_destroy_track(struct anm_track *track)
47 {
48 dynarr_free(track->keys);
49 }
51 struct anm_track *anm_create_track(void)
52 {
53 struct anm_track *track;
55 if((track = malloc(sizeof *track))) {
56 if(anm_init_track(track) == -1) {
57 free(track);
58 return 0;
59 }
60 }
61 return track;
62 }
64 void anm_free_track(struct anm_track *track)
65 {
66 anm_destroy_track(track);
67 free(track);
68 }
70 int anm_set_track_name(struct anm_track *track, const char *name)
71 {
72 char *tmp;
74 if(!(tmp = malloc(strlen(name) + 1))) {
75 return -1;
76 }
77 free(track->name);
78 track->name = tmp;
79 return 0;
80 }
82 const char *anm_get_track_name(struct anm_track *track)
83 {
84 return track->name;
85 }
87 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
88 {
89 track->interp = in;
90 }
92 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
93 {
94 track->extrap = ex;
95 }
97 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
98 {
99 return remap_time[track->extrap](tm, start, end);
100 }
102 void anm_set_track_default(struct anm_track *track, float def)
103 {
104 track->def_val = def;
105 }
107 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
108 {
109 int idx = anm_get_key_interval(track, key->time);
111 /* if we got a valid keyframe index, compare them... */
112 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
113 /* ... it's the same key, just update the value */
114 track->keys[idx].val = key->val;
115 } else {
116 /* ... it's a new key, add it and re-sort them */
117 void *tmp;
118 if(!(tmp = dynarr_push(track->keys, key))) {
119 return -1;
120 }
121 track->keys = tmp;
122 /* TODO lazy qsort */
123 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
124 }
125 return 0;
126 }
128 static int keycmp(const void *a, const void *b)
129 {
130 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
131 }
133 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
134 {
135 if(idx < 0 || idx >= track->count) {
136 return 0;
137 }
138 return track->keys + idx;
139 }
141 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
142 {
143 int last;
145 if(!track->count || tm < track->keys[0].time) {
146 return -1;
147 }
149 last = track->count - 1;
150 if(tm > track->keys[last].time) {
151 return last;
152 }
154 return find_prev_key(track->keys, 0, last, tm);
155 }
157 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
158 {
159 int mid;
161 if(end - start <= 1) {
162 return start;
163 }
165 mid = (start + end) / 2;
166 if(tm < arr[mid].time) {
167 return find_prev_key(arr, start, mid, tm);
168 }
169 if(tm > arr[mid].time) {
170 return find_prev_key(arr, mid, end, tm);
171 }
172 return mid;
173 }
175 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
176 {
177 struct anm_keyframe key;
178 key.time = tm;
179 key.val = val;
181 return anm_set_keyframe(track, &key);
182 }
184 float anm_get_value(struct anm_track *track, anm_time_t tm)
185 {
186 int idx0, idx1, last_idx;
187 anm_time_t tstart, tend;
188 float t, dt;
189 float v0, v1, v2, v3;
191 if(!track->count) {
192 return track->def_val;
193 }
195 last_idx = track->count - 1;
197 tstart = track->keys[0].time;
198 tend = track->keys[last_idx].time;
200 if(tstart == tend) {
201 return track->keys[0].val;
202 }
204 tm = remap_time[track->extrap](tm, tstart, tend);
206 idx0 = anm_get_key_interval(track, tm);
207 assert(idx0 >= 0 && idx0 < track->count);
208 idx1 = idx0 + 1;
210 if(idx0 == last_idx) {
211 return track->keys[idx0].val;
212 }
214 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
215 t = (float)(tm - track->keys[idx0].time) / dt;
217 v1 = track->keys[idx0].val;
218 v2 = track->keys[idx1].val;
220 /* get the neigboring values to allow for cubic interpolation */
221 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
222 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
224 return interp[track->interp](v0, v1, v2, v3, t);
225 }
228 static float interp_step(float v0, float v1, float v2, float v3, float t)
229 {
230 return v1;
231 }
233 static float interp_linear(float v0, float v1, float v2, float v3, float t)
234 {
235 return v1 + (v2 - v1) * t;
236 }
238 static float interp_cubic(float a, float b, float c, float d, float t)
239 {
240 float x, y, z, w;
241 float tsq = t * t;
243 x = -a + 3.0 * b - 3.0 * c + d;
244 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
245 z = c - a;
246 w = 2.0 * b;
248 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
249 }
251 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
252 {
253 return remap_repeat(tm, start, end);
254 }
256 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
257 {
258 return tm < start ? start : (tm >= end ? end - 1 : tm);
259 }
261 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
262 {
263 anm_time_t interv = end - start;
265 if(tm < start) {
266 while(tm < start) {
267 tm += interv;
268 }
269 return tm;
270 }
271 return (tm - start) % interv + start;
272 }