libanim

view example/test.c @ 78:769ae86eee31

makefile modified to build on mingw
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 16 Sep 2015 04:24:12 +0300
parents 203c11299586
children
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 /* first render a character with bottom-up lazy matrix calculation */
257 glPushMatrix();
258 glTranslatef(-2.5, 0, 0);
260 for(i=0; i<NUM_NODES; i++) {
261 float color[4] = {0, 0, 0, 1};
262 mat4_t xform, xform_transp;
264 color[0] = parts[i].color.x;
265 color[1] = parts[i].color.y;
266 color[2] = parts[i].color.z;
268 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
269 glColor4fv(color);
271 anm_get_matrix(nodes[i], xform, msec);
272 m4_transpose(xform_transp, xform);
274 glPushMatrix();
275 glMultMatrixf((float*)xform_transp);
277 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
278 glutSolidCube(1.0);
280 glPopMatrix();
281 }
282 glPopMatrix();
284 /* then render another one using simple top-down recursive evaluation */
285 glPushMatrix();
286 glTranslatef(2.5, 0, 0);
288 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
290 for(i=0; i<NUM_NODES; i++) {
291 float color[4] = {0, 0, 0, 1};
292 mat4_t xform_transp;
294 color[0] = parts[i].color.x;
295 color[1] = parts[i].color.y;
296 color[2] = parts[i].color.z;
298 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
299 glColor4fv(color);
301 m4_transpose(xform_transp, nodes[i]->matrix);
303 glPushMatrix();
304 glMultMatrixf((float*)xform_transp);
306 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
307 glutSolidCube(1.0);
309 glPopMatrix();
310 }
311 glPopMatrix();
313 glutSwapBuffers();
314 assert(glGetError() == GL_NO_ERROR);
315 }
317 void idle(void)
318 {
319 glutPostRedisplay();
320 }
322 void reshape(int x, int y)
323 {
324 glViewport(0, 0, x, y);
326 glMatrixMode(GL_PROJECTION);
327 glLoadIdentity();
328 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
329 }
331 void keyb(unsigned char key, int x, int y)
332 {
333 switch(key) {
334 case 27:
335 exit(0);
337 case ' ':
338 cur_anim = anm_get_active_animation_index(root, 0);
339 next_anim = (cur_anim + 1) % 2;
340 trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
341 anm_transition(root, next_anim, trans_start_tm, 1500);
342 break;
343 }
344 }
346 int bnstate[64];
347 int prev_x, prev_y;
349 void mouse(int bn, int state, int x, int y)
350 {
351 int idx = bn - GLUT_LEFT_BUTTON;
352 int down = state == GLUT_DOWN ? 1 : 0;
354 bnstate[idx] = down;
356 prev_x = x;
357 prev_y = y;
358 }
360 void motion(int x, int y)
361 {
362 int dx = x - prev_x;
363 int dy = y - prev_y;
364 prev_x = x;
365 prev_y = y;
367 if(bnstate[0]) {
368 cam_theta += dx * 0.5;
369 cam_phi += dy * 0.5;
371 if(cam_phi < -90) {
372 cam_phi = -90;
373 }
374 if(cam_phi > 90) {
375 cam_phi = 90;
376 }
377 glutPostRedisplay();
378 }
379 if(bnstate[2]) {
380 cam_dist += dy * 0.1;
381 if(cam_dist < 0.0) {
382 cam_dist = 0.0;
383 }
384 glutPostRedisplay();
385 }
386 }