libanim

view example/test.c @ 53:d36ef79e9ada

added -dbg suffix to the debug version of the win64 library name.
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 02:14:51 +0300
parents 2fe32e62e2a7
children 3c2428cb38f7
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 for(i=0; i<NUM_NODES; i++) {
188 float color[4] = {0, 0, 0, 1};
189 mat4_t xform, xform_transp;
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);
199 m4_transpose(xform_transp, xform);
201 glPushMatrix();
202 glMultMatrixf((float*)xform_transp);
204 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
205 glutSolidCube(1.0);
206 /*glutWireSphere(0.6, 16, 8);*/
208 glPopMatrix();
209 }
211 glutSwapBuffers();
212 assert(glGetError() == GL_NO_ERROR);
213 }
215 void idle(void)
216 {
217 glutPostRedisplay();
218 }
220 void reshape(int x, int y)
221 {
222 glViewport(0, 0, x, y);
224 glMatrixMode(GL_PROJECTION);
225 glLoadIdentity();
226 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
227 }
229 void keyb(unsigned char key, int x, int y)
230 {
231 switch(key) {
232 case 27:
233 exit(0);
234 }
235 }
237 int bnstate[64];
238 int prev_x, prev_y;
240 void mouse(int bn, int state, int x, int y)
241 {
242 int idx = bn - GLUT_LEFT_BUTTON;
243 int down = state == GLUT_DOWN ? 1 : 0;
245 bnstate[idx] = down;
247 prev_x = x;
248 prev_y = y;
249 }
251 void motion(int x, int y)
252 {
253 int dx = x - prev_x;
254 int dy = y - prev_y;
255 prev_x = x;
256 prev_y = y;
258 if(bnstate[0]) {
259 cam_theta += dx * 0.5;
260 cam_phi += dy * 0.5;
262 if(cam_phi < -90) {
263 cam_phi = -90;
264 }
265 if(cam_phi > 90) {
266 cam_phi = 90;
267 }
268 glutPostRedisplay();
269 }
270 if(bnstate[2]) {
271 cam_dist += dy * 0.1;
272 if(cam_dist < 0.0) {
273 cam_dist = 0.0;
274 }
275 glutPostRedisplay();
276 }
277 }