libanim

annotate 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
rev   line source
nuclear@8 1 #include <stdio.h>
nuclear@8 2 #include <stdlib.h>
nuclear@8 3 #include <assert.h>
nuclear@8 4
nuclear@8 5 #ifndef __APPLE__
nuclear@8 6 #include <GL/glut.h>
nuclear@8 7 #else
nuclear@8 8 #include <GLUT/glut.h>
nuclear@8 9 #endif
nuclear@8 10
nuclear@8 11 #include <vmath/vmath.h>
nuclear@8 12 #include "anim.h"
nuclear@8 13
nuclear@8 14 struct body_part {
nuclear@8 15 vec3_t pos, pivot, sz, color;
nuclear@8 16 } parts[] = {
nuclear@8 17 /* position pivot size color */
nuclear@8 18 {{0, 1, 0}, {0, -1, 0}, {1.8, 2.8, 0.8},{1, 0, 1}}, /* torso */
nuclear@8 19
nuclear@8 20 {{-0.5, -2.5, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 0, 0}}, /* left-upper leg */
nuclear@8 21 {{0.5, -2.5, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0, 1, 0}}, /* right-upper leg */
nuclear@8 22
nuclear@8 23 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 0.5, 0.5}}, /* left-lower leg */
nuclear@8 24 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0.5, 1, 0.5}}, /* right-lower leg */
nuclear@8 25
nuclear@24 26 {{0, 2.6, 0}, {0, -0.5, 0}, {1.2, 1.2, 1.2},{0, 1, 1}}, /* head */
nuclear@8 27
nuclear@8 28 {{-1.3, 0.4, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0, 0, 1}}, /* left-upper arm */
nuclear@8 29 {{1.3, 0.4, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 1, 0}}, /* right-upper arm */
nuclear@8 30
nuclear@8 31 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0.5, 0.5, 1}}, /* left-lower arm */
nuclear@8 32 {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 1, 0.5}}, /* right-lower arm */
nuclear@8 33 };
nuclear@8 34
nuclear@8 35 enum {
nuclear@8 36 NODE_TORSO,
nuclear@8 37 NODE_LEFT_UPPER_LEG,
nuclear@8 38 NODE_RIGHT_UPPER_LEG,
nuclear@8 39 NODE_LEFT_LOWER_LEG,
nuclear@8 40 NODE_RIGHT_LOWER_LEG,
nuclear@8 41 NODE_HEAD,
nuclear@8 42 NODE_LEFT_UPPER_ARM,
nuclear@8 43 NODE_RIGHT_UPPER_ARM,
nuclear@8 44 NODE_LEFT_LOWER_ARM,
nuclear@8 45 NODE_RIGHT_LOWER_ARM,
nuclear@8 46
nuclear@8 47 NUM_NODES
nuclear@8 48 };
nuclear@8 49
nuclear@8 50 int init(void);
nuclear@21 51 static void set_walk_animation(int idx);
nuclear@21 52 static void set_jump_animation(int idx);
nuclear@8 53 void disp(void);
nuclear@8 54 void idle(void);
nuclear@8 55 void reshape(int x, int y);
nuclear@8 56 void keyb(unsigned char key, int x, int y);
nuclear@8 57 void mouse(int bn, int state, int x, int y);
nuclear@8 58 void motion(int x, int y);
nuclear@8 59
nuclear@10 60 float cam_theta = 200, cam_phi = 20, cam_dist = 15;
nuclear@8 61 struct anm_node *root;
nuclear@8 62
nuclear@8 63 struct anm_node *nodes[NUM_NODES];
nuclear@8 64
nuclear@21 65 int cur_anim = 0, next_anim = 0;
nuclear@21 66 unsigned int trans_start_tm;
nuclear@21 67
nuclear@8 68 int main(int argc, char **argv)
nuclear@8 69 {
nuclear@8 70 glutInitWindowSize(800, 600);
nuclear@8 71 glutInit(&argc, argv);
nuclear@8 72 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@8 73 glutCreateWindow("libanim example");
nuclear@8 74
nuclear@8 75 glutDisplayFunc(disp);
nuclear@8 76 glutIdleFunc(idle);
nuclear@8 77 glutReshapeFunc(reshape);
nuclear@8 78 glutKeyboardFunc(keyb);
nuclear@8 79 glutMouseFunc(mouse);
nuclear@8 80 glutMotionFunc(motion);
nuclear@8 81
nuclear@8 82 if(init() == -1) {
nuclear@8 83 return 1;
nuclear@8 84 }
nuclear@8 85 glutMainLoop();
nuclear@8 86 return 0;
nuclear@8 87 }
nuclear@8 88
nuclear@8 89 int init(void)
nuclear@8 90 {
nuclear@8 91 int i;
nuclear@8 92
nuclear@8 93 glPointSize(3);
nuclear@8 94
nuclear@8 95 glEnable(GL_DEPTH_TEST);
nuclear@8 96 glEnable(GL_CULL_FACE);
nuclear@8 97 glEnable(GL_LIGHTING);
nuclear@8 98 glEnable(GL_LIGHT0);
nuclear@8 99
nuclear@8 100 for(i=0; i<NUM_NODES; i++) {
nuclear@8 101 nodes[i] = anm_create_node();
nuclear@8 102 anm_set_pivot(nodes[i], parts[i].pivot);
nuclear@8 103 }
nuclear@21 104 root = nodes[0];
nuclear@8 105
nuclear@8 106 anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]);
nuclear@8 107 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]);
nuclear@8 108 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_LEG]);
nuclear@8 109 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_ARM]);
nuclear@8 110 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_ARM]);
nuclear@8 111 anm_link_node(nodes[NODE_LEFT_UPPER_LEG], nodes[NODE_LEFT_LOWER_LEG]);
nuclear@8 112 anm_link_node(nodes[NODE_RIGHT_UPPER_LEG], nodes[NODE_RIGHT_LOWER_LEG]);
nuclear@8 113 anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]);
nuclear@8 114 anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]);
nuclear@8 115
nuclear@21 116 set_walk_animation(0);
nuclear@21 117
nuclear@21 118 anm_add_animation(root); /* recursively add another animation slot to all nodes */
nuclear@21 119 set_jump_animation(1);
nuclear@21 120
nuclear@21 121 anm_use_animation(root, cur_anim);
nuclear@21 122
nuclear@21 123 return 0;
nuclear@21 124 }
nuclear@21 125
nuclear@21 126 static void set_walk_animation(int idx)
nuclear@21 127 {
nuclear@21 128 int i;
nuclear@21 129
nuclear@21 130 anm_use_animation(root, idx);
nuclear@23 131 anm_set_active_animation_name(root, "walk");
nuclear@21 132
nuclear@21 133 for(i=0; i<NUM_NODES; i++) {
nuclear@21 134 anm_set_position(nodes[i], parts[i].pos, 0);
nuclear@21 135 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
nuclear@21 136 }
nuclear@21 137
nuclear@8 138 /* upper leg animation */
nuclear@8 139 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
nuclear@8 140 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
nuclear@8 141 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
nuclear@8 142
nuclear@8 143 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
nuclear@8 144 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
nuclear@8 145 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
nuclear@8 146
nuclear@8 147 /* lower leg animation */
nuclear@8 148 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@8 149 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
nuclear@8 150 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
nuclear@8 151 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@8 152
nuclear@8 153 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
nuclear@8 154 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
nuclear@8 155 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
nuclear@8 156 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
nuclear@8 157
nuclear@8 158 /* head animation */
nuclear@8 159 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
nuclear@8 160 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
nuclear@8 161 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
nuclear@8 162
nuclear@8 163 /* torso animation */
nuclear@8 164 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
nuclear@8 165 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
nuclear@8 166 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
nuclear@8 167 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@8 168 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
nuclear@8 169
nuclear@8 170 /* upper arm animation */
nuclear@8 171 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
nuclear@8 172 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
nuclear@8 173 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
nuclear@8 174
nuclear@8 175 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
nuclear@8 176 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
nuclear@8 177 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
nuclear@8 178
nuclear@8 179 /* lower arm animation */
nuclear@8 180 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
nuclear@8 181 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
nuclear@8 182 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@8 183 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
nuclear@8 184
nuclear@8 185 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@8 186 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
nuclear@8 187 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@8 188 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 189 }
nuclear@8 190
nuclear@21 191 static void set_jump_animation(int idx)
nuclear@21 192 {
nuclear@21 193 int i;
nuclear@21 194
nuclear@21 195 anm_use_animation(root, idx);
nuclear@23 196 anm_set_active_animation_name(root, "jump");
nuclear@21 197
nuclear@21 198 for(i=0; i<NUM_NODES; i++) {
nuclear@21 199 anm_set_position(nodes[i], parts[i].pos, 0);
nuclear@21 200 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
nuclear@21 201 }
nuclear@21 202
nuclear@21 203 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 204 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@21 205 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 206 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 207 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 208 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@21 209 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 210 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 211
nuclear@21 212 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 213 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
nuclear@21 214 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 215 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 216 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 217 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
nuclear@21 218 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 219 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 220
nuclear@21 221 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 0);
nuclear@21 222 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, -1, 0)), 1000);
nuclear@21 223 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, 2, 0)), 1500);
nuclear@21 224 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 2000);
nuclear@21 225
nuclear@21 226 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 227 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
nuclear@21 228 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
nuclear@21 229 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
nuclear@21 230 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 231
nuclear@21 232 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 233 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
nuclear@21 234 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
nuclear@21 235 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
nuclear@21 236 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@8 237 }
nuclear@8 238
nuclear@8 239 void disp(void)
nuclear@8 240 {
nuclear@8 241 int i;
nuclear@8 242 float lpos[] = {-1, 1, 1.5, 0};
nuclear@8 243 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
nuclear@8 244
nuclear@8 245 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@8 246
nuclear@8 247 glMatrixMode(GL_MODELVIEW);
nuclear@8 248 glLoadIdentity();
nuclear@8 249
nuclear@8 250 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@8 251
nuclear@8 252 glTranslatef(0, 0, -cam_dist);
nuclear@8 253 glRotatef(cam_phi, 1, 0, 0);
nuclear@8 254 glRotatef(cam_theta, 0, 1, 0);
nuclear@8 255
nuclear@20 256 /* first render a character with bottom-up lazy matrix calculation */
nuclear@20 257 glPushMatrix();
nuclear@20 258 glTranslatef(-2.5, 0, 0);
nuclear@20 259
nuclear@8 260 for(i=0; i<NUM_NODES; i++) {
nuclear@8 261 float color[4] = {0, 0, 0, 1};
nuclear@10 262 mat4_t xform, xform_transp;
nuclear@8 263
nuclear@8 264 color[0] = parts[i].color.x;
nuclear@8 265 color[1] = parts[i].color.y;
nuclear@8 266 color[2] = parts[i].color.z;
nuclear@8 267
nuclear@8 268 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@8 269 glColor4fv(color);
nuclear@8 270
nuclear@8 271 anm_get_matrix(nodes[i], xform, msec);
nuclear@10 272 m4_transpose(xform_transp, xform);
nuclear@8 273
nuclear@8 274 glPushMatrix();
nuclear@10 275 glMultMatrixf((float*)xform_transp);
nuclear@8 276
nuclear@8 277 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@8 278 glutSolidCube(1.0);
nuclear@8 279
nuclear@8 280 glPopMatrix();
nuclear@8 281 }
nuclear@20 282 glPopMatrix();
nuclear@20 283
nuclear@20 284 /* then render another one using simple top-down recursive evaluation */
nuclear@20 285 glPushMatrix();
nuclear@20 286 glTranslatef(2.5, 0, 0);
nuclear@20 287
nuclear@20 288 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
nuclear@20 289
nuclear@20 290 for(i=0; i<NUM_NODES; i++) {
nuclear@20 291 float color[4] = {0, 0, 0, 1};
nuclear@20 292 mat4_t xform_transp;
nuclear@20 293
nuclear@20 294 color[0] = parts[i].color.x;
nuclear@20 295 color[1] = parts[i].color.y;
nuclear@20 296 color[2] = parts[i].color.z;
nuclear@20 297
nuclear@20 298 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@20 299 glColor4fv(color);
nuclear@20 300
nuclear@20 301 m4_transpose(xform_transp, nodes[i]->matrix);
nuclear@20 302
nuclear@20 303 glPushMatrix();
nuclear@20 304 glMultMatrixf((float*)xform_transp);
nuclear@20 305
nuclear@20 306 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@20 307 glutSolidCube(1.0);
nuclear@20 308
nuclear@20 309 glPopMatrix();
nuclear@20 310 }
nuclear@20 311 glPopMatrix();
nuclear@8 312
nuclear@8 313 glutSwapBuffers();
nuclear@8 314 assert(glGetError() == GL_NO_ERROR);
nuclear@8 315 }
nuclear@8 316
nuclear@8 317 void idle(void)
nuclear@8 318 {
nuclear@8 319 glutPostRedisplay();
nuclear@8 320 }
nuclear@8 321
nuclear@8 322 void reshape(int x, int y)
nuclear@8 323 {
nuclear@8 324 glViewport(0, 0, x, y);
nuclear@8 325
nuclear@8 326 glMatrixMode(GL_PROJECTION);
nuclear@8 327 glLoadIdentity();
nuclear@8 328 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@8 329 }
nuclear@8 330
nuclear@8 331 void keyb(unsigned char key, int x, int y)
nuclear@8 332 {
nuclear@8 333 switch(key) {
nuclear@8 334 case 27:
nuclear@8 335 exit(0);
nuclear@21 336
nuclear@21 337 case ' ':
nuclear@24 338 cur_anim = anm_get_active_animation_index(root, 0);
nuclear@21 339 next_anim = (cur_anim + 1) % 2;
nuclear@21 340 trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
nuclear@24 341 anm_transition(root, next_anim, trans_start_tm, 1500);
nuclear@21 342 break;
nuclear@8 343 }
nuclear@8 344 }
nuclear@8 345
nuclear@8 346 int bnstate[64];
nuclear@8 347 int prev_x, prev_y;
nuclear@8 348
nuclear@8 349 void mouse(int bn, int state, int x, int y)
nuclear@8 350 {
nuclear@8 351 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@8 352 int down = state == GLUT_DOWN ? 1 : 0;
nuclear@8 353
nuclear@8 354 bnstate[idx] = down;
nuclear@8 355
nuclear@8 356 prev_x = x;
nuclear@8 357 prev_y = y;
nuclear@8 358 }
nuclear@8 359
nuclear@8 360 void motion(int x, int y)
nuclear@8 361 {
nuclear@8 362 int dx = x - prev_x;
nuclear@8 363 int dy = y - prev_y;
nuclear@8 364 prev_x = x;
nuclear@8 365 prev_y = y;
nuclear@8 366
nuclear@8 367 if(bnstate[0]) {
nuclear@8 368 cam_theta += dx * 0.5;
nuclear@8 369 cam_phi += dy * 0.5;
nuclear@8 370
nuclear@8 371 if(cam_phi < -90) {
nuclear@8 372 cam_phi = -90;
nuclear@8 373 }
nuclear@8 374 if(cam_phi > 90) {
nuclear@8 375 cam_phi = 90;
nuclear@8 376 }
nuclear@8 377 glutPostRedisplay();
nuclear@8 378 }
nuclear@8 379 if(bnstate[2]) {
nuclear@8 380 cam_dist += dy * 0.1;
nuclear@8 381 if(cam_dist < 0.0) {
nuclear@8 382 cam_dist = 0.0;
nuclear@8 383 }
nuclear@8 384 glutPostRedisplay();
nuclear@8 385 }
nuclear@8 386 }