libanim

view example/test.c @ 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
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #ifndef __APPLE__
6 #include <GL/glut.h>
7 #else
8 #include <GLUT/glut.h>
9 #endif
11 #include <vmath/vmath.h>
12 #include "anim.h"
14 struct body_part {
15 vec3_t pos, pivot, sz, color;
16 } parts[] = {
17 /* position pivot size color */
18 {{0, 1, 0}, {0, -1, 0}, {1.8, 2.8, 0.8},{1, 0, 1}}, /* torso */
20 {{-0.5, -2.5, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 0, 0}}, /* left-upper leg */
21 {{0.5, -2.5, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0, 1, 0}}, /* right-upper leg */
23 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 0.5, 0.5}}, /* left-lower leg */
24 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0.5, 1, 0.5}}, /* right-lower leg */
26 {{0, 2.6, 0}, {0, -0.5, 0}, {1.2, 1.2, 1.2},{0, 1, 1}}, /* head */
28 {{-1.3, 0.4, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0, 0, 1}}, /* left-upper arm */
29 {{1.3, 0.4, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 1, 0}}, /* right-upper arm */
31 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0.5, 0.5, 1}}, /* left-lower arm */
32 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 1, 0.5}}, /* right-lower arm */
33 };
35 enum {
36 NODE_TORSO,
37 NODE_LEFT_UPPER_LEG,
38 NODE_RIGHT_UPPER_LEG,
39 NODE_LEFT_LOWER_LEG,
40 NODE_RIGHT_LOWER_LEG,
41 NODE_HEAD,
42 NODE_LEFT_UPPER_ARM,
43 NODE_RIGHT_UPPER_ARM,
44 NODE_LEFT_LOWER_ARM,
45 NODE_RIGHT_LOWER_ARM,
47 NUM_NODES
48 };
50 int init(void);
51 static void set_walk_animation(int idx);
52 static void set_jump_animation(int idx);
53 void disp(void);
54 void idle(void);
55 void reshape(int x, int y);
56 void keyb(unsigned char key, int x, int y);
57 void mouse(int bn, int state, int x, int y);
58 void motion(int x, int y);
60 float cam_theta = 200, cam_phi = 20, cam_dist = 15;
61 struct anm_node *root;
63 struct anm_node *nodes[NUM_NODES];
65 int cur_anim = 0, next_anim = 0;
66 unsigned int trans_start_tm;
68 int main(int argc, char **argv)
69 {
70 glutInitWindowSize(800, 600);
71 glutInit(&argc, argv);
72 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
73 glutCreateWindow("libanim example");
75 glutDisplayFunc(disp);
76 glutIdleFunc(idle);
77 glutReshapeFunc(reshape);
78 glutKeyboardFunc(keyb);
79 glutMouseFunc(mouse);
80 glutMotionFunc(motion);
82 if(init() == -1) {
83 return 1;
84 }
85 glutMainLoop();
86 return 0;
87 }
89 int init(void)
90 {
91 int i;
93 glPointSize(3);
95 glEnable(GL_DEPTH_TEST);
96 glEnable(GL_CULL_FACE);
97 glEnable(GL_LIGHTING);
98 glEnable(GL_LIGHT0);
100 for(i=0; i<NUM_NODES; i++) {
101 nodes[i] = anm_create_node();
102 anm_set_pivot(nodes[i], parts[i].pivot);
103 }
104 root = nodes[0];
106 anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]);
107 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]);
108 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_LEG]);
109 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_ARM]);
110 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_ARM]);
111 anm_link_node(nodes[NODE_LEFT_UPPER_LEG], nodes[NODE_LEFT_LOWER_LEG]);
112 anm_link_node(nodes[NODE_RIGHT_UPPER_LEG], nodes[NODE_RIGHT_LOWER_LEG]);
113 anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]);
114 anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]);
116 set_walk_animation(0);
118 anm_add_animation(root); /* recursively add another animation slot to all nodes */
119 set_jump_animation(1);
121 anm_use_animation(root, cur_anim);
123 return 0;
124 }
126 static void set_walk_animation(int idx)
127 {
128 int i;
130 anm_use_animation(root, idx);
131 anm_set_active_animation_name(root, "walk");
133 for(i=0; i<NUM_NODES; i++) {
134 anm_set_position(nodes[i], parts[i].pos, 0);
135 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
136 }
138 /* upper leg animation */
139 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
140 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
141 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
143 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
144 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
145 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
147 /* lower leg animation */
148 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
149 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
150 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
151 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
153 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
154 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
155 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
156 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
158 /* head animation */
159 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
160 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
161 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
163 /* torso animation */
164 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
165 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
166 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
167 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
168 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
170 /* upper arm animation */
171 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
172 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
173 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
175 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
176 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
177 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
179 /* lower arm animation */
180 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
181 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
182 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
183 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
185 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
186 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
187 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
188 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
189 }
191 static void set_jump_animation(int idx)
192 {
193 int i;
195 anm_use_animation(root, idx);
196 anm_set_active_animation_name(root, "jump");
198 for(i=0; i<NUM_NODES; i++) {
199 anm_set_position(nodes[i], parts[i].pos, 0);
200 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
201 }
203 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
204 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
205 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
206 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
207 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
208 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
209 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
210 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
212 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
213 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
214 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
215 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
216 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
217 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
218 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
219 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
221 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 0);
222 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, -1, 0)), 1000);
223 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, 2, 0)), 1500);
224 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 2000);
226 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
227 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
228 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
229 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
230 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
232 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
233 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
234 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
235 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
236 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
237 }
239 void disp(void)
240 {
241 int i;
242 float lpos[] = {-1, 1, 1.5, 0};
243 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
245 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
247 glMatrixMode(GL_MODELVIEW);
248 glLoadIdentity();
250 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
252 glTranslatef(0, 0, -cam_dist);
253 glRotatef(cam_phi, 1, 0, 0);
254 glRotatef(cam_theta, 0, 1, 0);
256 /* animation blending if we're in transition */
257 if(cur_anim != next_anim) {
258 float t = (msec - trans_start_tm) / 1500.0;
260 struct anm_animation *from, *to;
261 from = anm_get_animation(root, cur_anim);
262 to = anm_get_animation(root, next_anim);
264 if(t >= 1.0) {
265 t = 1.0;
266 cur_anim = next_anim;
267 anm_use_animation(root, cur_anim);
268 } else {
269 anm_use_animations(root, cur_anim, next_anim, t);
270 }
272 printf("transitioning from \"%s\" to \"%s\": %.2f \r", from->name, to->name, t);
273 if(cur_anim == next_anim) {
274 putchar('\n');
275 }
276 fflush(stdout);
277 }
279 /* first render a character with bottom-up lazy matrix calculation */
280 glPushMatrix();
281 glTranslatef(-2.5, 0, 0);
283 for(i=0; i<NUM_NODES; i++) {
284 float color[4] = {0, 0, 0, 1};
285 mat4_t xform, xform_transp;
287 color[0] = parts[i].color.x;
288 color[1] = parts[i].color.y;
289 color[2] = parts[i].color.z;
291 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
292 glColor4fv(color);
294 anm_get_matrix(nodes[i], xform, msec);
295 m4_transpose(xform_transp, xform);
297 glPushMatrix();
298 glMultMatrixf((float*)xform_transp);
300 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
301 glutSolidCube(1.0);
303 glPopMatrix();
304 }
305 glPopMatrix();
307 /* then render another one using simple top-down recursive evaluation */
308 glPushMatrix();
309 glTranslatef(2.5, 0, 0);
311 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
313 for(i=0; i<NUM_NODES; i++) {
314 float color[4] = {0, 0, 0, 1};
315 mat4_t xform_transp;
317 color[0] = parts[i].color.x;
318 color[1] = parts[i].color.y;
319 color[2] = parts[i].color.z;
321 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
322 glColor4fv(color);
324 m4_transpose(xform_transp, nodes[i]->matrix);
326 glPushMatrix();
327 glMultMatrixf((float*)xform_transp);
329 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
330 glutSolidCube(1.0);
332 glPopMatrix();
333 }
334 glPopMatrix();
336 glutSwapBuffers();
337 assert(glGetError() == GL_NO_ERROR);
338 }
340 void idle(void)
341 {
342 glutPostRedisplay();
343 }
345 void reshape(int x, int y)
346 {
347 glViewport(0, 0, x, y);
349 glMatrixMode(GL_PROJECTION);
350 glLoadIdentity();
351 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
352 }
354 void keyb(unsigned char key, int x, int y)
355 {
356 switch(key) {
357 case 27:
358 exit(0);
360 case ' ':
361 next_anim = (cur_anim + 1) % 2;
362 trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
363 break;
364 }
365 }
367 int bnstate[64];
368 int prev_x, prev_y;
370 void mouse(int bn, int state, int x, int y)
371 {
372 int idx = bn - GLUT_LEFT_BUTTON;
373 int down = state == GLUT_DOWN ? 1 : 0;
375 bnstate[idx] = down;
377 prev_x = x;
378 prev_y = y;
379 }
381 void motion(int x, int y)
382 {
383 int dx = x - prev_x;
384 int dy = y - prev_y;
385 prev_x = x;
386 prev_y = y;
388 if(bnstate[0]) {
389 cam_theta += dx * 0.5;
390 cam_phi += dy * 0.5;
392 if(cam_phi < -90) {
393 cam_phi = -90;
394 }
395 if(cam_phi > 90) {
396 cam_phi = 90;
397 }
398 glutPostRedisplay();
399 }
400 if(bnstate[2]) {
401 cam_dist += dy * 0.1;
402 if(cam_dist < 0.0) {
403 cam_dist = 0.0;
404 }
405 glutPostRedisplay();
406 }
407 }