libanim

annotate example/test.c @ 60:8f7193d00555

set/get currently active animation name and minor enhancements in the example
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 11:29:42 +0200
parents 9758004136f8
children 09e267e7ed4a
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@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@23 256 /* animation blending if we're in transition */
nuclear@21 257 if(cur_anim != next_anim) {
nuclear@22 258 float t = (msec - trans_start_tm) / 1500.0;
nuclear@21 259
nuclear@23 260 struct anm_animation *from, *to;
nuclear@23 261 from = anm_get_animation(root, cur_anim);
nuclear@23 262 to = anm_get_animation(root, next_anim);
nuclear@23 263
nuclear@21 264 if(t >= 1.0) {
nuclear@21 265 t = 1.0;
nuclear@21 266 cur_anim = next_anim;
nuclear@21 267 anm_use_animation(root, cur_anim);
nuclear@21 268 } else {
nuclear@21 269 anm_use_animations(root, cur_anim, next_anim, t);
nuclear@21 270 }
nuclear@23 271
nuclear@23 272 printf("transitioning from \"%s\" to \"%s\": %.2f \r", from->name, to->name, t);
nuclear@23 273 if(cur_anim == next_anim) {
nuclear@23 274 putchar('\n');
nuclear@23 275 }
nuclear@23 276 fflush(stdout);
nuclear@21 277 }
nuclear@21 278
nuclear@20 279 /* first render a character with bottom-up lazy matrix calculation */
nuclear@20 280 glPushMatrix();
nuclear@20 281 glTranslatef(-2.5, 0, 0);
nuclear@20 282
nuclear@8 283 for(i=0; i<NUM_NODES; i++) {
nuclear@8 284 float color[4] = {0, 0, 0, 1};
nuclear@10 285 mat4_t xform, xform_transp;
nuclear@8 286
nuclear@8 287 color[0] = parts[i].color.x;
nuclear@8 288 color[1] = parts[i].color.y;
nuclear@8 289 color[2] = parts[i].color.z;
nuclear@8 290
nuclear@8 291 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@8 292 glColor4fv(color);
nuclear@8 293
nuclear@8 294 anm_get_matrix(nodes[i], xform, msec);
nuclear@10 295 m4_transpose(xform_transp, xform);
nuclear@8 296
nuclear@8 297 glPushMatrix();
nuclear@10 298 glMultMatrixf((float*)xform_transp);
nuclear@8 299
nuclear@8 300 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@8 301 glutSolidCube(1.0);
nuclear@8 302
nuclear@8 303 glPopMatrix();
nuclear@8 304 }
nuclear@20 305 glPopMatrix();
nuclear@20 306
nuclear@20 307 /* then render another one using simple top-down recursive evaluation */
nuclear@20 308 glPushMatrix();
nuclear@20 309 glTranslatef(2.5, 0, 0);
nuclear@20 310
nuclear@20 311 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
nuclear@20 312
nuclear@20 313 for(i=0; i<NUM_NODES; i++) {
nuclear@20 314 float color[4] = {0, 0, 0, 1};
nuclear@20 315 mat4_t xform_transp;
nuclear@20 316
nuclear@20 317 color[0] = parts[i].color.x;
nuclear@20 318 color[1] = parts[i].color.y;
nuclear@20 319 color[2] = parts[i].color.z;
nuclear@20 320
nuclear@20 321 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@20 322 glColor4fv(color);
nuclear@20 323
nuclear@20 324 m4_transpose(xform_transp, nodes[i]->matrix);
nuclear@20 325
nuclear@20 326 glPushMatrix();
nuclear@20 327 glMultMatrixf((float*)xform_transp);
nuclear@20 328
nuclear@20 329 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@20 330 glutSolidCube(1.0);
nuclear@20 331
nuclear@20 332 glPopMatrix();
nuclear@20 333 }
nuclear@20 334 glPopMatrix();
nuclear@8 335
nuclear@8 336 glutSwapBuffers();
nuclear@8 337 assert(glGetError() == GL_NO_ERROR);
nuclear@8 338 }
nuclear@8 339
nuclear@8 340 void idle(void)
nuclear@8 341 {
nuclear@8 342 glutPostRedisplay();
nuclear@8 343 }
nuclear@8 344
nuclear@8 345 void reshape(int x, int y)
nuclear@8 346 {
nuclear@8 347 glViewport(0, 0, x, y);
nuclear@8 348
nuclear@8 349 glMatrixMode(GL_PROJECTION);
nuclear@8 350 glLoadIdentity();
nuclear@8 351 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@8 352 }
nuclear@8 353
nuclear@8 354 void keyb(unsigned char key, int x, int y)
nuclear@8 355 {
nuclear@8 356 switch(key) {
nuclear@8 357 case 27:
nuclear@8 358 exit(0);
nuclear@21 359
nuclear@21 360 case ' ':
nuclear@21 361 next_anim = (cur_anim + 1) % 2;
nuclear@21 362 trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
nuclear@21 363 break;
nuclear@8 364 }
nuclear@8 365 }
nuclear@8 366
nuclear@8 367 int bnstate[64];
nuclear@8 368 int prev_x, prev_y;
nuclear@8 369
nuclear@8 370 void mouse(int bn, int state, int x, int y)
nuclear@8 371 {
nuclear@8 372 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@8 373 int down = state == GLUT_DOWN ? 1 : 0;
nuclear@8 374
nuclear@8 375 bnstate[idx] = down;
nuclear@8 376
nuclear@8 377 prev_x = x;
nuclear@8 378 prev_y = y;
nuclear@8 379 }
nuclear@8 380
nuclear@8 381 void motion(int x, int y)
nuclear@8 382 {
nuclear@8 383 int dx = x - prev_x;
nuclear@8 384 int dy = y - prev_y;
nuclear@8 385 prev_x = x;
nuclear@8 386 prev_y = y;
nuclear@8 387
nuclear@8 388 if(bnstate[0]) {
nuclear@8 389 cam_theta += dx * 0.5;
nuclear@8 390 cam_phi += dy * 0.5;
nuclear@8 391
nuclear@8 392 if(cam_phi < -90) {
nuclear@8 393 cam_phi = -90;
nuclear@8 394 }
nuclear@8 395 if(cam_phi > 90) {
nuclear@8 396 cam_phi = 90;
nuclear@8 397 }
nuclear@8 398 glutPostRedisplay();
nuclear@8 399 }
nuclear@8 400 if(bnstate[2]) {
nuclear@8 401 cam_dist += dy * 0.1;
nuclear@8 402 if(cam_dist < 0.0) {
nuclear@8 403 cam_dist = 0.0;
nuclear@8 404 }
nuclear@8 405 glutPostRedisplay();
nuclear@8 406 }
nuclear@8 407 }