libanim
diff example/test.c @ 8:2fe32e62e2a7
added an example program
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 01 Mar 2013 09:46:53 +0200 |
parents | |
children | b408f3f655e9 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/example/test.c Fri Mar 01 09:46:53 2013 +0200 1.3 @@ -0,0 +1,276 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <assert.h> 1.7 + 1.8 +#ifndef __APPLE__ 1.9 +#include <GL/glut.h> 1.10 +#else 1.11 +#include <GLUT/glut.h> 1.12 +#endif 1.13 + 1.14 +#include <vmath/vmath.h> 1.15 +#include "anim.h" 1.16 + 1.17 +struct body_part { 1.18 + vec3_t pos, pivot, sz, color; 1.19 +} parts[] = { 1.20 + /* position pivot size color */ 1.21 + {{0, 1, 0}, {0, -1, 0}, {1.8, 2.8, 0.8},{1, 0, 1}}, /* torso */ 1.22 + 1.23 + {{-0.5, -2.5, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 0, 0}}, /* left-upper leg */ 1.24 + {{0.5, -2.5, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0, 1, 0}}, /* right-upper leg */ 1.25 + 1.26 + {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 0.5, 0.5}}, /* left-lower leg */ 1.27 + {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0.5, 1, 0.5}}, /* right-lower leg */ 1.28 + 1.29 + {{0, 2.6, 0}, {0, -0.5, 0}, {1.2, 1.2, 1.2},{0, 1, 1}}, /* head */ 1.30 + 1.31 + {{-1.3, 0.4, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0, 0, 1}}, /* left-upper arm */ 1.32 + {{1.3, 0.4, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 1, 0}}, /* right-upper arm */ 1.33 + 1.34 + {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {0.5, 0.5, 1}}, /* left-lower arm */ 1.35 + {{0, -2.1, 0}, {0, 1, 0}, {0.8, 2, 0.8}, {1, 1, 0.5}}, /* right-lower arm */ 1.36 +}; 1.37 + 1.38 +enum { 1.39 + NODE_TORSO, 1.40 + NODE_LEFT_UPPER_LEG, 1.41 + NODE_RIGHT_UPPER_LEG, 1.42 + NODE_LEFT_LOWER_LEG, 1.43 + NODE_RIGHT_LOWER_LEG, 1.44 + NODE_HEAD, 1.45 + NODE_LEFT_UPPER_ARM, 1.46 + NODE_RIGHT_UPPER_ARM, 1.47 + NODE_LEFT_LOWER_ARM, 1.48 + NODE_RIGHT_LOWER_ARM, 1.49 + 1.50 + NUM_NODES 1.51 +}; 1.52 + 1.53 +int init(void); 1.54 +void disp(void); 1.55 +void idle(void); 1.56 +void reshape(int x, int y); 1.57 +void keyb(unsigned char key, int x, int y); 1.58 +void mouse(int bn, int state, int x, int y); 1.59 +void motion(int x, int y); 1.60 + 1.61 +float cam_theta = 20, cam_phi = 20, cam_dist = 15; 1.62 +struct anm_node *root; 1.63 + 1.64 +struct anm_node *nodes[NUM_NODES]; 1.65 + 1.66 +int main(int argc, char **argv) 1.67 +{ 1.68 + glutInitWindowSize(800, 600); 1.69 + glutInit(&argc, argv); 1.70 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 1.71 + glutCreateWindow("libanim example"); 1.72 + 1.73 + glutDisplayFunc(disp); 1.74 + glutIdleFunc(idle); 1.75 + glutReshapeFunc(reshape); 1.76 + glutKeyboardFunc(keyb); 1.77 + glutMouseFunc(mouse); 1.78 + glutMotionFunc(motion); 1.79 + 1.80 + if(init() == -1) { 1.81 + return 1; 1.82 + } 1.83 + glutMainLoop(); 1.84 + return 0; 1.85 +} 1.86 + 1.87 +int init(void) 1.88 +{ 1.89 + int i; 1.90 + 1.91 + glPointSize(3); 1.92 + 1.93 + glEnable(GL_DEPTH_TEST); 1.94 + glEnable(GL_CULL_FACE); 1.95 + glEnable(GL_LIGHTING); 1.96 + glEnable(GL_LIGHT0); 1.97 + 1.98 + root = nodes[0]; 1.99 + 1.100 + for(i=0; i<NUM_NODES; i++) { 1.101 + nodes[i] = anm_create_node(); 1.102 + 1.103 + anm_set_pivot(nodes[i], parts[i].pivot); 1.104 + anm_set_position(nodes[i], parts[i].pos, 0); 1.105 + anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT); 1.106 + } 1.107 + 1.108 + anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]); 1.109 + anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]); 1.110 + anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_LEG]); 1.111 + anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_ARM]); 1.112 + anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_ARM]); 1.113 + anm_link_node(nodes[NODE_LEFT_UPPER_LEG], nodes[NODE_LEFT_LOWER_LEG]); 1.114 + anm_link_node(nodes[NODE_RIGHT_UPPER_LEG], nodes[NODE_RIGHT_LOWER_LEG]); 1.115 + anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]); 1.116 + anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]); 1.117 + 1.118 + /* upper leg animation */ 1.119 + anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0); 1.120 + anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000); 1.121 + anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000); 1.122 + 1.123 + anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0); 1.124 + anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000); 1.125 + anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000); 1.126 + 1.127 + /* lower leg animation */ 1.128 + anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0); 1.129 + anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500); 1.130 + anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000); 1.131 + anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000); 1.132 + 1.133 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0); 1.134 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000); 1.135 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500); 1.136 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000); 1.137 + 1.138 + /* head animation */ 1.139 + anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0); 1.140 + anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000); 1.141 + anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000); 1.142 + 1.143 + /* torso animation */ 1.144 + anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0); 1.145 + anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500); 1.146 + anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000); 1.147 + anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500); 1.148 + anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000); 1.149 + 1.150 + /* upper arm animation */ 1.151 + anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0); 1.152 + anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000); 1.153 + anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000); 1.154 + 1.155 + anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0); 1.156 + anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000); 1.157 + anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000); 1.158 + 1.159 + /* lower arm animation */ 1.160 + anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0); 1.161 + anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000); 1.162 + anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500); 1.163 + anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000); 1.164 + 1.165 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0); 1.166 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500); 1.167 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000); 1.168 + anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000); 1.169 + 1.170 + return 0; 1.171 +} 1.172 + 1.173 +void disp(void) 1.174 +{ 1.175 + int i; 1.176 + float lpos[] = {-1, 1, 1.5, 0}; 1.177 + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 1.178 + 1.179 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.180 + 1.181 + glMatrixMode(GL_MODELVIEW); 1.182 + glLoadIdentity(); 1.183 + 1.184 + glLightfv(GL_LIGHT0, GL_POSITION, lpos); 1.185 + 1.186 + glTranslatef(0, 0, -cam_dist); 1.187 + glRotatef(cam_phi, 1, 0, 0); 1.188 + glRotatef(cam_theta, 0, 1, 0); 1.189 + 1.190 + for(i=0; i<NUM_NODES; i++) { 1.191 + float color[4] = {0, 0, 0, 1}; 1.192 + mat4_t xform; 1.193 + 1.194 + color[0] = parts[i].color.x; 1.195 + color[1] = parts[i].color.y; 1.196 + color[2] = parts[i].color.z; 1.197 + 1.198 + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color); 1.199 + glColor4fv(color); 1.200 + 1.201 + anm_get_matrix(nodes[i], xform, msec); 1.202 + 1.203 + glPushMatrix(); 1.204 + glMultTransposeMatrixf((float*)xform); 1.205 + 1.206 + glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z); 1.207 + glutSolidCube(1.0); 1.208 + /*glutWireSphere(0.6, 16, 8);*/ 1.209 + 1.210 + glPopMatrix(); 1.211 + } 1.212 + 1.213 + glutSwapBuffers(); 1.214 + assert(glGetError() == GL_NO_ERROR); 1.215 +} 1.216 + 1.217 +void idle(void) 1.218 +{ 1.219 + glutPostRedisplay(); 1.220 +} 1.221 + 1.222 +void reshape(int x, int y) 1.223 +{ 1.224 + glViewport(0, 0, x, y); 1.225 + 1.226 + glMatrixMode(GL_PROJECTION); 1.227 + glLoadIdentity(); 1.228 + gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0); 1.229 +} 1.230 + 1.231 +void keyb(unsigned char key, int x, int y) 1.232 +{ 1.233 + switch(key) { 1.234 + case 27: 1.235 + exit(0); 1.236 + } 1.237 +} 1.238 + 1.239 +int bnstate[64]; 1.240 +int prev_x, prev_y; 1.241 + 1.242 +void mouse(int bn, int state, int x, int y) 1.243 +{ 1.244 + int idx = bn - GLUT_LEFT_BUTTON; 1.245 + int down = state == GLUT_DOWN ? 1 : 0; 1.246 + 1.247 + bnstate[idx] = down; 1.248 + 1.249 + prev_x = x; 1.250 + prev_y = y; 1.251 +} 1.252 + 1.253 +void motion(int x, int y) 1.254 +{ 1.255 + int dx = x - prev_x; 1.256 + int dy = y - prev_y; 1.257 + prev_x = x; 1.258 + prev_y = y; 1.259 + 1.260 + if(bnstate[0]) { 1.261 + cam_theta += dx * 0.5; 1.262 + cam_phi += dy * 0.5; 1.263 + 1.264 + if(cam_phi < -90) { 1.265 + cam_phi = -90; 1.266 + } 1.267 + if(cam_phi > 90) { 1.268 + cam_phi = 90; 1.269 + } 1.270 + glutPostRedisplay(); 1.271 + } 1.272 + if(bnstate[2]) { 1.273 + cam_dist += dy * 0.1; 1.274 + if(cam_dist < 0.0) { 1.275 + cam_dist = 0.0; 1.276 + } 1.277 + glutPostRedisplay(); 1.278 + } 1.279 +}