libanim

annotate example/test.c @ 59:dae22390c34c

added anm_set_animation_name
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 10:58:23 +0200
parents 5993f405a1cb
children 203c11299586
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@8 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@21 131
nuclear@21 132 for(i=0; i<NUM_NODES; i++) {
nuclear@21 133 anm_set_position(nodes[i], parts[i].pos, 0);
nuclear@21 134 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
nuclear@21 135 }
nuclear@21 136
nuclear@8 137 /* upper leg animation */
nuclear@8 138 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
nuclear@8 139 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
nuclear@8 140 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
nuclear@8 141
nuclear@8 142 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
nuclear@8 143 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
nuclear@8 144 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
nuclear@8 145
nuclear@8 146 /* lower leg animation */
nuclear@8 147 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@8 148 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
nuclear@8 149 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
nuclear@8 150 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@8 151
nuclear@8 152 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
nuclear@8 153 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
nuclear@8 154 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
nuclear@8 155 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
nuclear@8 156
nuclear@8 157 /* head animation */
nuclear@8 158 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
nuclear@8 159 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
nuclear@8 160 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
nuclear@8 161
nuclear@8 162 /* torso animation */
nuclear@8 163 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
nuclear@8 164 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
nuclear@8 165 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
nuclear@8 166 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@8 167 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
nuclear@8 168
nuclear@8 169 /* upper arm animation */
nuclear@8 170 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
nuclear@8 171 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
nuclear@8 172 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
nuclear@8 173
nuclear@8 174 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
nuclear@8 175 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
nuclear@8 176 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
nuclear@8 177
nuclear@8 178 /* lower arm animation */
nuclear@8 179 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
nuclear@8 180 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
nuclear@8 181 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@8 182 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
nuclear@8 183
nuclear@8 184 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@8 185 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
nuclear@8 186 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@8 187 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 188 }
nuclear@8 189
nuclear@21 190 static void set_jump_animation(int idx)
nuclear@21 191 {
nuclear@21 192 int i;
nuclear@21 193
nuclear@21 194 anm_use_animation(root, idx);
nuclear@21 195
nuclear@21 196 for(i=0; i<NUM_NODES; i++) {
nuclear@21 197 anm_set_position(nodes[i], parts[i].pos, 0);
nuclear@21 198 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
nuclear@21 199 }
nuclear@21 200
nuclear@21 201 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 202 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@21 203 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 204 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 205 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 206 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@21 207 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 208 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 209
nuclear@21 210 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 211 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
nuclear@21 212 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 213 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 214 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 215 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
nuclear@21 216 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@21 217 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 218
nuclear@21 219 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 0);
nuclear@21 220 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, -1, 0)), 1000);
nuclear@21 221 anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, 2, 0)), 1500);
nuclear@21 222 anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 2000);
nuclear@21 223
nuclear@21 224 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 225 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
nuclear@21 226 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
nuclear@21 227 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
nuclear@21 228 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@21 229
nuclear@21 230 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@21 231 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
nuclear@21 232 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
nuclear@21 233 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
nuclear@21 234 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@8 235 }
nuclear@8 236
nuclear@8 237 void disp(void)
nuclear@8 238 {
nuclear@8 239 int i;
nuclear@8 240 float lpos[] = {-1, 1, 1.5, 0};
nuclear@8 241 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
nuclear@8 242
nuclear@8 243 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@8 244
nuclear@8 245 glMatrixMode(GL_MODELVIEW);
nuclear@8 246 glLoadIdentity();
nuclear@8 247
nuclear@8 248 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@8 249
nuclear@8 250 glTranslatef(0, 0, -cam_dist);
nuclear@8 251 glRotatef(cam_phi, 1, 0, 0);
nuclear@8 252 glRotatef(cam_theta, 0, 1, 0);
nuclear@8 253
nuclear@21 254 if(cur_anim != next_anim) {
nuclear@22 255 float t = (msec - trans_start_tm) / 1500.0;
nuclear@21 256
nuclear@21 257 if(t >= 1.0) {
nuclear@21 258 t = 1.0;
nuclear@21 259 cur_anim = next_anim;
nuclear@21 260 anm_use_animation(root, cur_anim);
nuclear@21 261 } else {
nuclear@21 262 anm_use_animations(root, cur_anim, next_anim, t);
nuclear@21 263 }
nuclear@21 264 }
nuclear@21 265
nuclear@20 266 /* first render a character with bottom-up lazy matrix calculation */
nuclear@20 267 glPushMatrix();
nuclear@20 268 glTranslatef(-2.5, 0, 0);
nuclear@20 269
nuclear@8 270 for(i=0; i<NUM_NODES; i++) {
nuclear@8 271 float color[4] = {0, 0, 0, 1};
nuclear@10 272 mat4_t xform, xform_transp;
nuclear@8 273
nuclear@8 274 color[0] = parts[i].color.x;
nuclear@8 275 color[1] = parts[i].color.y;
nuclear@8 276 color[2] = parts[i].color.z;
nuclear@8 277
nuclear@8 278 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@8 279 glColor4fv(color);
nuclear@8 280
nuclear@8 281 anm_get_matrix(nodes[i], xform, msec);
nuclear@10 282 m4_transpose(xform_transp, xform);
nuclear@8 283
nuclear@8 284 glPushMatrix();
nuclear@10 285 glMultMatrixf((float*)xform_transp);
nuclear@8 286
nuclear@8 287 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@8 288 glutSolidCube(1.0);
nuclear@8 289
nuclear@8 290 glPopMatrix();
nuclear@8 291 }
nuclear@20 292 glPopMatrix();
nuclear@20 293
nuclear@20 294 /* then render another one using simple top-down recursive evaluation */
nuclear@20 295 glPushMatrix();
nuclear@20 296 glTranslatef(2.5, 0, 0);
nuclear@20 297
nuclear@20 298 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
nuclear@20 299
nuclear@20 300 for(i=0; i<NUM_NODES; i++) {
nuclear@20 301 float color[4] = {0, 0, 0, 1};
nuclear@20 302 mat4_t xform_transp;
nuclear@20 303
nuclear@20 304 color[0] = parts[i].color.x;
nuclear@20 305 color[1] = parts[i].color.y;
nuclear@20 306 color[2] = parts[i].color.z;
nuclear@20 307
nuclear@20 308 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@20 309 glColor4fv(color);
nuclear@20 310
nuclear@20 311 m4_transpose(xform_transp, nodes[i]->matrix);
nuclear@20 312
nuclear@20 313 glPushMatrix();
nuclear@20 314 glMultMatrixf((float*)xform_transp);
nuclear@20 315
nuclear@20 316 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@20 317 glutSolidCube(1.0);
nuclear@20 318
nuclear@20 319 glPopMatrix();
nuclear@20 320 }
nuclear@20 321 glPopMatrix();
nuclear@8 322
nuclear@8 323 glutSwapBuffers();
nuclear@8 324 assert(glGetError() == GL_NO_ERROR);
nuclear@8 325 }
nuclear@8 326
nuclear@8 327 void idle(void)
nuclear@8 328 {
nuclear@8 329 glutPostRedisplay();
nuclear@8 330 }
nuclear@8 331
nuclear@8 332 void reshape(int x, int y)
nuclear@8 333 {
nuclear@8 334 glViewport(0, 0, x, y);
nuclear@8 335
nuclear@8 336 glMatrixMode(GL_PROJECTION);
nuclear@8 337 glLoadIdentity();
nuclear@8 338 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@8 339 }
nuclear@8 340
nuclear@8 341 void keyb(unsigned char key, int x, int y)
nuclear@8 342 {
nuclear@8 343 switch(key) {
nuclear@8 344 case 27:
nuclear@8 345 exit(0);
nuclear@21 346
nuclear@21 347 case ' ':
nuclear@21 348 next_anim = (cur_anim + 1) % 2;
nuclear@21 349 trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
nuclear@21 350 break;
nuclear@8 351 }
nuclear@8 352 }
nuclear@8 353
nuclear@8 354 int bnstate[64];
nuclear@8 355 int prev_x, prev_y;
nuclear@8 356
nuclear@8 357 void mouse(int bn, int state, int x, int y)
nuclear@8 358 {
nuclear@8 359 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@8 360 int down = state == GLUT_DOWN ? 1 : 0;
nuclear@8 361
nuclear@8 362 bnstate[idx] = down;
nuclear@8 363
nuclear@8 364 prev_x = x;
nuclear@8 365 prev_y = y;
nuclear@8 366 }
nuclear@8 367
nuclear@8 368 void motion(int x, int y)
nuclear@8 369 {
nuclear@8 370 int dx = x - prev_x;
nuclear@8 371 int dy = y - prev_y;
nuclear@8 372 prev_x = x;
nuclear@8 373 prev_y = y;
nuclear@8 374
nuclear@8 375 if(bnstate[0]) {
nuclear@8 376 cam_theta += dx * 0.5;
nuclear@8 377 cam_phi += dy * 0.5;
nuclear@8 378
nuclear@8 379 if(cam_phi < -90) {
nuclear@8 380 cam_phi = -90;
nuclear@8 381 }
nuclear@8 382 if(cam_phi > 90) {
nuclear@8 383 cam_phi = 90;
nuclear@8 384 }
nuclear@8 385 glutPostRedisplay();
nuclear@8 386 }
nuclear@8 387 if(bnstate[2]) {
nuclear@8 388 cam_dist += dy * 0.1;
nuclear@8 389 if(cam_dist < 0.0) {
nuclear@8 390 cam_dist = 0.0;
nuclear@8 391 }
nuclear@8 392 glutPostRedisplay();
nuclear@8 393 }
nuclear@8 394 }