libanim

annotate example/test.c @ 57:2da758956e50

added the option of lightweight pre-pass top-down recursive calculation of matrices instead of going through the existing lazy thread-specific caching algorithm.
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Dec 2013 04:06:30 +0200
parents b408f3f655e9
children 5993f405a1cb
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@8 51 void disp(void);
nuclear@8 52 void idle(void);
nuclear@8 53 void reshape(int x, int y);
nuclear@8 54 void keyb(unsigned char key, int x, int y);
nuclear@8 55 void mouse(int bn, int state, int x, int y);
nuclear@8 56 void motion(int x, int y);
nuclear@8 57
nuclear@10 58 float cam_theta = 200, cam_phi = 20, cam_dist = 15;
nuclear@8 59 struct anm_node *root;
nuclear@8 60
nuclear@8 61 struct anm_node *nodes[NUM_NODES];
nuclear@8 62
nuclear@8 63 int main(int argc, char **argv)
nuclear@8 64 {
nuclear@8 65 glutInitWindowSize(800, 600);
nuclear@8 66 glutInit(&argc, argv);
nuclear@8 67 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@8 68 glutCreateWindow("libanim example");
nuclear@8 69
nuclear@8 70 glutDisplayFunc(disp);
nuclear@8 71 glutIdleFunc(idle);
nuclear@8 72 glutReshapeFunc(reshape);
nuclear@8 73 glutKeyboardFunc(keyb);
nuclear@8 74 glutMouseFunc(mouse);
nuclear@8 75 glutMotionFunc(motion);
nuclear@8 76
nuclear@8 77 if(init() == -1) {
nuclear@8 78 return 1;
nuclear@8 79 }
nuclear@8 80 glutMainLoop();
nuclear@8 81 return 0;
nuclear@8 82 }
nuclear@8 83
nuclear@8 84 int init(void)
nuclear@8 85 {
nuclear@8 86 int i;
nuclear@8 87
nuclear@8 88 glPointSize(3);
nuclear@8 89
nuclear@8 90 glEnable(GL_DEPTH_TEST);
nuclear@8 91 glEnable(GL_CULL_FACE);
nuclear@8 92 glEnable(GL_LIGHTING);
nuclear@8 93 glEnable(GL_LIGHT0);
nuclear@8 94
nuclear@8 95 root = nodes[0];
nuclear@8 96
nuclear@8 97 for(i=0; i<NUM_NODES; i++) {
nuclear@8 98 nodes[i] = anm_create_node();
nuclear@8 99
nuclear@8 100 anm_set_pivot(nodes[i], parts[i].pivot);
nuclear@8 101 anm_set_position(nodes[i], parts[i].pos, 0);
nuclear@8 102 anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
nuclear@8 103 }
nuclear@8 104
nuclear@8 105 anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]);
nuclear@8 106 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]);
nuclear@8 107 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_LEG]);
nuclear@8 108 anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_ARM]);
nuclear@8 109 anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_ARM]);
nuclear@8 110 anm_link_node(nodes[NODE_LEFT_UPPER_LEG], nodes[NODE_LEFT_LOWER_LEG]);
nuclear@8 111 anm_link_node(nodes[NODE_RIGHT_UPPER_LEG], nodes[NODE_RIGHT_LOWER_LEG]);
nuclear@8 112 anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]);
nuclear@8 113 anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]);
nuclear@8 114
nuclear@8 115 /* upper leg animation */
nuclear@8 116 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
nuclear@8 117 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
nuclear@8 118 anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
nuclear@8 119
nuclear@8 120 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
nuclear@8 121 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
nuclear@8 122 anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
nuclear@8 123
nuclear@8 124 /* lower leg animation */
nuclear@8 125 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@8 126 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
nuclear@8 127 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
nuclear@8 128 anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@8 129
nuclear@8 130 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
nuclear@8 131 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
nuclear@8 132 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
nuclear@8 133 anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
nuclear@8 134
nuclear@8 135 /* head animation */
nuclear@8 136 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
nuclear@8 137 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
nuclear@8 138 anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
nuclear@8 139
nuclear@8 140 /* torso animation */
nuclear@8 141 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
nuclear@8 142 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
nuclear@8 143 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
nuclear@8 144 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@8 145 anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
nuclear@8 146
nuclear@8 147 /* upper arm animation */
nuclear@8 148 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
nuclear@8 149 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
nuclear@8 150 anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
nuclear@8 151
nuclear@8 152 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
nuclear@8 153 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
nuclear@8 154 anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
nuclear@8 155
nuclear@8 156 /* lower arm animation */
nuclear@8 157 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
nuclear@8 158 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
nuclear@8 159 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
nuclear@8 160 anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
nuclear@8 161
nuclear@8 162 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
nuclear@8 163 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
nuclear@8 164 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
nuclear@8 165 anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
nuclear@8 166
nuclear@8 167 return 0;
nuclear@8 168 }
nuclear@8 169
nuclear@8 170 void disp(void)
nuclear@8 171 {
nuclear@8 172 int i;
nuclear@8 173 float lpos[] = {-1, 1, 1.5, 0};
nuclear@8 174 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
nuclear@8 175
nuclear@8 176 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@8 177
nuclear@8 178 glMatrixMode(GL_MODELVIEW);
nuclear@8 179 glLoadIdentity();
nuclear@8 180
nuclear@8 181 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@8 182
nuclear@8 183 glTranslatef(0, 0, -cam_dist);
nuclear@8 184 glRotatef(cam_phi, 1, 0, 0);
nuclear@8 185 glRotatef(cam_theta, 0, 1, 0);
nuclear@8 186
nuclear@20 187 /* first render a character with bottom-up lazy matrix calculation */
nuclear@20 188 glPushMatrix();
nuclear@20 189 glTranslatef(-2.5, 0, 0);
nuclear@20 190
nuclear@8 191 for(i=0; i<NUM_NODES; i++) {
nuclear@8 192 float color[4] = {0, 0, 0, 1};
nuclear@10 193 mat4_t xform, xform_transp;
nuclear@8 194
nuclear@8 195 color[0] = parts[i].color.x;
nuclear@8 196 color[1] = parts[i].color.y;
nuclear@8 197 color[2] = parts[i].color.z;
nuclear@8 198
nuclear@8 199 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@8 200 glColor4fv(color);
nuclear@8 201
nuclear@8 202 anm_get_matrix(nodes[i], xform, msec);
nuclear@10 203 m4_transpose(xform_transp, xform);
nuclear@8 204
nuclear@8 205 glPushMatrix();
nuclear@10 206 glMultMatrixf((float*)xform_transp);
nuclear@8 207
nuclear@8 208 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@8 209 glutSolidCube(1.0);
nuclear@8 210
nuclear@8 211 glPopMatrix();
nuclear@8 212 }
nuclear@20 213 glPopMatrix();
nuclear@20 214
nuclear@20 215 /* then render another one using simple top-down recursive evaluation */
nuclear@20 216 glPushMatrix();
nuclear@20 217 glTranslatef(2.5, 0, 0);
nuclear@20 218
nuclear@20 219 anm_eval(nodes[NODE_TORSO], msec); /* calculate all matrices recursively */
nuclear@20 220
nuclear@20 221 for(i=0; i<NUM_NODES; i++) {
nuclear@20 222 float color[4] = {0, 0, 0, 1};
nuclear@20 223 mat4_t xform_transp;
nuclear@20 224
nuclear@20 225 color[0] = parts[i].color.x;
nuclear@20 226 color[1] = parts[i].color.y;
nuclear@20 227 color[2] = parts[i].color.z;
nuclear@20 228
nuclear@20 229 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@20 230 glColor4fv(color);
nuclear@20 231
nuclear@20 232 m4_transpose(xform_transp, nodes[i]->matrix);
nuclear@20 233
nuclear@20 234 glPushMatrix();
nuclear@20 235 glMultMatrixf((float*)xform_transp);
nuclear@20 236
nuclear@20 237 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@20 238 glutSolidCube(1.0);
nuclear@20 239
nuclear@20 240 glPopMatrix();
nuclear@20 241 }
nuclear@20 242 glPopMatrix();
nuclear@8 243
nuclear@8 244 glutSwapBuffers();
nuclear@8 245 assert(glGetError() == GL_NO_ERROR);
nuclear@8 246 }
nuclear@8 247
nuclear@8 248 void idle(void)
nuclear@8 249 {
nuclear@8 250 glutPostRedisplay();
nuclear@8 251 }
nuclear@8 252
nuclear@8 253 void reshape(int x, int y)
nuclear@8 254 {
nuclear@8 255 glViewport(0, 0, x, y);
nuclear@8 256
nuclear@8 257 glMatrixMode(GL_PROJECTION);
nuclear@8 258 glLoadIdentity();
nuclear@8 259 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@8 260 }
nuclear@8 261
nuclear@8 262 void keyb(unsigned char key, int x, int y)
nuclear@8 263 {
nuclear@8 264 switch(key) {
nuclear@8 265 case 27:
nuclear@8 266 exit(0);
nuclear@8 267 }
nuclear@8 268 }
nuclear@8 269
nuclear@8 270 int bnstate[64];
nuclear@8 271 int prev_x, prev_y;
nuclear@8 272
nuclear@8 273 void mouse(int bn, int state, int x, int y)
nuclear@8 274 {
nuclear@8 275 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@8 276 int down = state == GLUT_DOWN ? 1 : 0;
nuclear@8 277
nuclear@8 278 bnstate[idx] = down;
nuclear@8 279
nuclear@8 280 prev_x = x;
nuclear@8 281 prev_y = y;
nuclear@8 282 }
nuclear@8 283
nuclear@8 284 void motion(int x, int y)
nuclear@8 285 {
nuclear@8 286 int dx = x - prev_x;
nuclear@8 287 int dy = y - prev_y;
nuclear@8 288 prev_x = x;
nuclear@8 289 prev_y = y;
nuclear@8 290
nuclear@8 291 if(bnstate[0]) {
nuclear@8 292 cam_theta += dx * 0.5;
nuclear@8 293 cam_phi += dy * 0.5;
nuclear@8 294
nuclear@8 295 if(cam_phi < -90) {
nuclear@8 296 cam_phi = -90;
nuclear@8 297 }
nuclear@8 298 if(cam_phi > 90) {
nuclear@8 299 cam_phi = 90;
nuclear@8 300 }
nuclear@8 301 glutPostRedisplay();
nuclear@8 302 }
nuclear@8 303 if(bnstate[2]) {
nuclear@8 304 cam_dist += dy * 0.1;
nuclear@8 305 if(cam_dist < 0.0) {
nuclear@8 306 cam_dist = 0.0;
nuclear@8 307 }
nuclear@8 308 glutPostRedisplay();
nuclear@8 309 }
nuclear@8 310 }