libanim

view example/test.c @ 8:2fe32e62e2a7

added an example program
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Mar 2013 09:46:53 +0200
parents
children b408f3f655e9
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 = 20, 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 for(i=0; i<NUM_NODES; i++) {
188 float color[4] = {0, 0, 0, 1};
189 mat4_t xform;
191 color[0] = parts[i].color.x;
192 color[1] = parts[i].color.y;
193 color[2] = parts[i].color.z;
195 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
196 glColor4fv(color);
198 anm_get_matrix(nodes[i], xform, msec);
200 glPushMatrix();
201 glMultTransposeMatrixf((float*)xform);
203 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
204 glutSolidCube(1.0);
205 /*glutWireSphere(0.6, 16, 8);*/
207 glPopMatrix();
208 }
210 glutSwapBuffers();
211 assert(glGetError() == GL_NO_ERROR);
212 }
214 void idle(void)
215 {
216 glutPostRedisplay();
217 }
219 void reshape(int x, int y)
220 {
221 glViewport(0, 0, x, y);
223 glMatrixMode(GL_PROJECTION);
224 glLoadIdentity();
225 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
226 }
228 void keyb(unsigned char key, int x, int y)
229 {
230 switch(key) {
231 case 27:
232 exit(0);
233 }
234 }
236 int bnstate[64];
237 int prev_x, prev_y;
239 void mouse(int bn, int state, int x, int y)
240 {
241 int idx = bn - GLUT_LEFT_BUTTON;
242 int down = state == GLUT_DOWN ? 1 : 0;
244 bnstate[idx] = down;
246 prev_x = x;
247 prev_y = y;
248 }
250 void motion(int x, int y)
251 {
252 int dx = x - prev_x;
253 int dy = y - prev_y;
254 prev_x = x;
255 prev_y = y;
257 if(bnstate[0]) {
258 cam_theta += dx * 0.5;
259 cam_phi += dy * 0.5;
261 if(cam_phi < -90) {
262 cam_phi = -90;
263 }
264 if(cam_phi > 90) {
265 cam_phi = 90;
266 }
267 glutPostRedisplay();
268 }
269 if(bnstate[2]) {
270 cam_dist += dy * 0.1;
271 if(cam_dist < 0.0) {
272 cam_dist = 0.0;
273 }
274 glutPostRedisplay();
275 }
276 }