libanim

annotate example/test.c @ 45:603db9031600

added an example program
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Mar 2013 09:46:53 +0200
parents
children b408f3f655e9
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@8 58 float cam_theta = 20, 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@8 187 for(i=0; i<NUM_NODES; i++) {
nuclear@8 188 float color[4] = {0, 0, 0, 1};
nuclear@8 189 mat4_t xform;
nuclear@8 190
nuclear@8 191 color[0] = parts[i].color.x;
nuclear@8 192 color[1] = parts[i].color.y;
nuclear@8 193 color[2] = parts[i].color.z;
nuclear@8 194
nuclear@8 195 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@8 196 glColor4fv(color);
nuclear@8 197
nuclear@8 198 anm_get_matrix(nodes[i], xform, msec);
nuclear@8 199
nuclear@8 200 glPushMatrix();
nuclear@8 201 glMultTransposeMatrixf((float*)xform);
nuclear@8 202
nuclear@8 203 glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
nuclear@8 204 glutSolidCube(1.0);
nuclear@8 205 /*glutWireSphere(0.6, 16, 8);*/
nuclear@8 206
nuclear@8 207 glPopMatrix();
nuclear@8 208 }
nuclear@8 209
nuclear@8 210 glutSwapBuffers();
nuclear@8 211 assert(glGetError() == GL_NO_ERROR);
nuclear@8 212 }
nuclear@8 213
nuclear@8 214 void idle(void)
nuclear@8 215 {
nuclear@8 216 glutPostRedisplay();
nuclear@8 217 }
nuclear@8 218
nuclear@8 219 void reshape(int x, int y)
nuclear@8 220 {
nuclear@8 221 glViewport(0, 0, x, y);
nuclear@8 222
nuclear@8 223 glMatrixMode(GL_PROJECTION);
nuclear@8 224 glLoadIdentity();
nuclear@8 225 gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
nuclear@8 226 }
nuclear@8 227
nuclear@8 228 void keyb(unsigned char key, int x, int y)
nuclear@8 229 {
nuclear@8 230 switch(key) {
nuclear@8 231 case 27:
nuclear@8 232 exit(0);
nuclear@8 233 }
nuclear@8 234 }
nuclear@8 235
nuclear@8 236 int bnstate[64];
nuclear@8 237 int prev_x, prev_y;
nuclear@8 238
nuclear@8 239 void mouse(int bn, int state, int x, int y)
nuclear@8 240 {
nuclear@8 241 int idx = bn - GLUT_LEFT_BUTTON;
nuclear@8 242 int down = state == GLUT_DOWN ? 1 : 0;
nuclear@8 243
nuclear@8 244 bnstate[idx] = down;
nuclear@8 245
nuclear@8 246 prev_x = x;
nuclear@8 247 prev_y = y;
nuclear@8 248 }
nuclear@8 249
nuclear@8 250 void motion(int x, int y)
nuclear@8 251 {
nuclear@8 252 int dx = x - prev_x;
nuclear@8 253 int dy = y - prev_y;
nuclear@8 254 prev_x = x;
nuclear@8 255 prev_y = y;
nuclear@8 256
nuclear@8 257 if(bnstate[0]) {
nuclear@8 258 cam_theta += dx * 0.5;
nuclear@8 259 cam_phi += dy * 0.5;
nuclear@8 260
nuclear@8 261 if(cam_phi < -90) {
nuclear@8 262 cam_phi = -90;
nuclear@8 263 }
nuclear@8 264 if(cam_phi > 90) {
nuclear@8 265 cam_phi = 90;
nuclear@8 266 }
nuclear@8 267 glutPostRedisplay();
nuclear@8 268 }
nuclear@8 269 if(bnstate[2]) {
nuclear@8 270 cam_dist += dy * 0.1;
nuclear@8 271 if(cam_dist < 0.0) {
nuclear@8 272 cam_dist = 0.0;
nuclear@8 273 }
nuclear@8 274 glutPostRedisplay();
nuclear@8 275 }
nuclear@8 276 }