libanim

view example/test.c @ 21:5993f405a1cb

implemented multiple animations per node, and blending between two animations
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 06:28:43 +0200
parents 3c2428cb38f7
children 9758004136f8
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);
132 for(i=0; i<NUM_NODES; i++) {
133 anm_set_position(nodes[i], parts[i].pos, 0);
134 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
135 }
137 /* upper leg animation */
138 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
139 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
140 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
142 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
143 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
144 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
146 /* lower leg animation */
147 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
148 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
149 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
150 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
152 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
153 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
154 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
155 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
157 /* head animation */
158 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
159 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
160 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
162 /* torso animation */
163 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
164 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
165 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
166 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
167 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
169 /* upper arm animation */
170 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
171 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
172 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
174 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
175 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
176 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
178 /* lower arm animation */
179 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
180 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
181 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
182 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
184 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
185 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
186 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
187 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
188 }
190 static void set_jump_animation(int idx)
191 {
192 int i;
194 anm_use_animation(root, idx);
196 for(i=0; i<NUM_NODES; i++) {
197 anm_set_position(nodes[i], parts[i].pos, 0);
198 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
199 }
201 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
202 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
203 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
204 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
205 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
206 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
207 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
208 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
210 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
211 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
212 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
213 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
214 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
215 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
216 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
217 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
219 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 0);
220 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, -1, 0)), 1000);
221 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, 2, 0)), 1500);
222 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 2000);
224 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
225 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
226 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
227 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
228 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
230 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
231 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
232 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
233 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
234 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
235 }
237 void disp(void)
238 {
239 int i;
240 float lpos[] = {-1, 1, 1.5, 0};
241 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
243 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
245 glMatrixMode(GL_MODELVIEW);
246 glLoadIdentity();
248 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
250 glTranslatef(0, 0, -cam_dist);
251 glRotatef(cam_phi, 1, 0, 0);
252 glRotatef(cam_theta, 0, 1, 0);
254 if(cur_anim != next_anim) {
255 float t = (msec - trans_start_tm) / 1000.0;
257 if(t >= 1.0) {
258 t = 1.0;
259 cur_anim = next_anim;
260 anm_use_animation(root, cur_anim);
261 } else {
262 anm_use_animations(root, cur_anim, next_anim, t);
263 }
264 }
266 /* first render a character with bottom-up lazy matrix calculation */
267 glPushMatrix();
268 glTranslatef(-2.5, 0, 0);
270 for(i=0; i<NUM_NODES; i++) {
271 float color[4] = {0, 0, 0, 1};
272 mat4_t xform, xform_transp;
274 color[0] = parts[i].color.x;
275 color[1] = parts[i].color.y;
276 color[2] = parts[i].color.z;
278 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
279 glColor4fv(color);
281 anm_get_matrix(nodes[i], xform, msec);
282 m4_transpose(xform_transp, xform);
284 glPushMatrix();
285 glMultMatrixf((float*)xform_transp);
287 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
288 glutSolidCube(1.0);
290 glPopMatrix();
291 }
292 glPopMatrix();
294 /* then render another one using simple top-down recursive evaluation */
295 glPushMatrix();
296 glTranslatef(2.5, 0, 0);
298 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
300 for(i=0; i<NUM_NODES; i++) {
301 float color[4] = {0, 0, 0, 1};
302 mat4_t xform_transp;
304 color[0] = parts[i].color.x;
305 color[1] = parts[i].color.y;
306 color[2] = parts[i].color.z;
308 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
309 glColor4fv(color);
311 m4_transpose(xform_transp, nodes[i]->matrix);
313 glPushMatrix();
314 glMultMatrixf((float*)xform_transp);
316 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
317 glutSolidCube(1.0);
319 glPopMatrix();
320 }
321 glPopMatrix();
323 glutSwapBuffers();
324 assert(glGetError() == GL_NO_ERROR);
325 }
327 void idle(void)
328 {
329 glutPostRedisplay();
330 }
332 void reshape(int x, int y)
333 {
334 glViewport(0, 0, x, y);
336 glMatrixMode(GL_PROJECTION);
337 glLoadIdentity();
338 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
339 }
341 void keyb(unsigned char key, int x, int y)
342 {
343 switch(key) {
344 case 27:
345 exit(0);
347 case ' ':
348 next_anim = (cur_anim + 1) % 2;
349 trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
350 break;
351 }
352 }
354 int bnstate[64];
355 int prev_x, prev_y;
357 void mouse(int bn, int state, int x, int y)
358 {
359 int idx = bn - GLUT_LEFT_BUTTON;
360 int down = state == GLUT_DOWN ? 1 : 0;
362 bnstate[idx] = down;
364 prev_x = x;
365 prev_y = y;
366 }
368 void motion(int x, int y)
369 {
370 int dx = x - prev_x;
371 int dy = y - prev_y;
372 prev_x = x;
373 prev_y = y;
375 if(bnstate[0]) {
376 cam_theta += dx * 0.5;
377 cam_phi += dy * 0.5;
379 if(cam_phi < -90) {
380 cam_phi = -90;
381 }
382 if(cam_phi > 90) {
383 cam_phi = 90;
384 }
385 glutPostRedisplay();
386 }
387 if(bnstate[2]) {
388 cam_dist += dy * 0.1;
389 if(cam_dist < 0.0) {
390 cam_dist = 0.0;
391 }
392 glutPostRedisplay();
393 }
394 }