libanim

view example/test.c @ 57:2da758956e50

added the option of lightweight pre-pass top-down recursive calculation of matrices instead of going through the existing lazy thread-specific caching algorithm.
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Dec 2013 04:06:30 +0200
parents b408f3f655e9
children 5993f405a1cb
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 void disp(void);
52 void idle(void);
53 void reshape(int x, int y);
54 void keyb(unsigned char key, int x, int y);
55 void mouse(int bn, int state, int x, int y);
56 void motion(int x, int y);
58 float cam_theta = 200, cam_phi = 20, cam_dist = 15;
59 struct anm_node *root;
61 struct anm_node *nodes[NUM_NODES];
63 int main(int argc, char **argv)
64 {
65 glutInitWindowSize(800, 600);
66 glutInit(&argc, argv);
67 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
68 glutCreateWindow("libanim example");
70 glutDisplayFunc(disp);
71 glutIdleFunc(idle);
72 glutReshapeFunc(reshape);
73 glutKeyboardFunc(keyb);
74 glutMouseFunc(mouse);
75 glutMotionFunc(motion);
77 if(init() == -1) {
78 return 1;
79 }
80 glutMainLoop();
81 return 0;
82 }
84 int init(void)
85 {
86 int i;
88 glPointSize(3);
90 glEnable(GL_DEPTH_TEST);
91 glEnable(GL_CULL_FACE);
92 glEnable(GL_LIGHTING);
93 glEnable(GL_LIGHT0);
95 root = nodes[0];
97 for(i=0; i<NUM_NODES; i++) {
98 nodes[i] = anm_create_node();
100 anm_set_pivot(nodes[i], parts[i].pivot);
101 anm_set_position(nodes[i], parts[i].pos, 0);
102 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
103 }
105 anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]);
106 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]);
107 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_LEG]);
108 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_ARM]);
109 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_ARM]);
110 anm_link_node(nodes[NODE_LEFT_UPPER_LEG], nodes[NODE_LEFT_LOWER_LEG]);
111 anm_link_node(nodes[NODE_RIGHT_UPPER_LEG], nodes[NODE_RIGHT_LOWER_LEG]);
112 anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]);
113 anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]);
115 /* upper leg animation */
116 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
117 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
118 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
120 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
121 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
122 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
124 /* lower leg animation */
125 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
126 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
127 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
128 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
130 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
131 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
132 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
133 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
135 /* head animation */
136 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
137 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
138 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
140 /* torso animation */
141 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
142 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
143 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
144 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
145 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
147 /* upper arm animation */
148 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
149 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
150 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
152 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
153 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
154 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
156 /* lower arm animation */
157 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
158 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
159 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
160 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
162 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
163 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
164 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
165 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
167 return 0;
168 }
170 void disp(void)
171 {
172 int i;
173 float lpos[] = {-1, 1, 1.5, 0};
174 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
176 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
178 glMatrixMode(GL_MODELVIEW);
179 glLoadIdentity();
181 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
183 glTranslatef(0, 0, -cam_dist);
184 glRotatef(cam_phi, 1, 0, 0);
185 glRotatef(cam_theta, 0, 1, 0);
187 /* first render a character with bottom-up lazy matrix calculation */
188 glPushMatrix();
189 glTranslatef(-2.5, 0, 0);
191 for(i=0; i<NUM_NODES; i++) {
192 float color[4] = {0, 0, 0, 1};
193 mat4_t xform, xform_transp;
195 color[0] = parts[i].color.x;
196 color[1] = parts[i].color.y;
197 color[2] = parts[i].color.z;
199 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
200 glColor4fv(color);
202 anm_get_matrix(nodes[i], xform, msec);
203 m4_transpose(xform_transp, xform);
205 glPushMatrix();
206 glMultMatrixf((float*)xform_transp);
208 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
209 glutSolidCube(1.0);
211 glPopMatrix();
212 }
213 glPopMatrix();
215 /* then render another one using simple top-down recursive evaluation */
216 glPushMatrix();
217 glTranslatef(2.5, 0, 0);
219 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
221 for(i=0; i<NUM_NODES; i++) {
222 float color[4] = {0, 0, 0, 1};
223 mat4_t xform_transp;
225 color[0] = parts[i].color.x;
226 color[1] = parts[i].color.y;
227 color[2] = parts[i].color.z;
229 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
230 glColor4fv(color);
232 m4_transpose(xform_transp, nodes[i]->matrix);
234 glPushMatrix();
235 glMultMatrixf((float*)xform_transp);
237 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
238 glutSolidCube(1.0);
240 glPopMatrix();
241 }
242 glPopMatrix();
244 glutSwapBuffers();
245 assert(glGetError() == GL_NO_ERROR);
246 }
248 void idle(void)
249 {
250 glutPostRedisplay();
251 }
253 void reshape(int x, int y)
254 {
255 glViewport(0, 0, x, y);
257 glMatrixMode(GL_PROJECTION);
258 glLoadIdentity();
259 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
260 }
262 void keyb(unsigned char key, int x, int y)
263 {
264 switch(key) {
265 case 27:
266 exit(0);
267 }
268 }
270 int bnstate[64];
271 int prev_x, prev_y;
273 void mouse(int bn, int state, int x, int y)
274 {
275 int idx = bn - GLUT_LEFT_BUTTON;
276 int down = state == GLUT_DOWN ? 1 : 0;
278 bnstate[idx] = down;
280 prev_x = x;
281 prev_y = y;
282 }
284 void motion(int x, int y)
285 {
286 int dx = x - prev_x;
287 int dy = y - prev_y;
288 prev_x = x;
289 prev_y = y;
291 if(bnstate[0]) {
292 cam_theta += dx * 0.5;
293 cam_phi += dy * 0.5;
295 if(cam_phi < -90) {
296 cam_phi = -90;
297 }
298 if(cam_phi > 90) {
299 cam_phi = 90;
300 }
301 glutPostRedisplay();
302 }
303 if(bnstate[2]) {
304 cam_dist += dy * 0.1;
305 if(cam_dist < 0.0) {
306 cam_dist = 0.0;
307 }
308 glutPostRedisplay();
309 }
310 }