libanim

changeset 8:2fe32e62e2a7

added an example program
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Mar 2013 09:46:53 +0200
parents ffe668a61bca
children 710658962108
files example/Makefile example/test.c
diffstat 2 files changed, 295 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/example/Makefile	Fri Mar 01 09:46:53 2013 +0200
     1.3 @@ -0,0 +1,19 @@
     1.4 +src = $(wildcard *.c)
     1.5 +obj = $(src:.c=.o)
     1.6 +bin = test
     1.7 +
     1.8 +CFLAGS = -pedantic -Wall -g -I../src
     1.9 +LDFLAGS = -L.. -lanim -lvmath $(libgl) -lm
    1.10 +
    1.11 +ifeq ($(shell uname -s), Darwin)
    1.12 +	libgl = -framework OpenGL -framework GLUT
    1.13 +else
    1.14 +	libgl = -lGL -lGLU -lglut
    1.15 +endif
    1.16 +
    1.17 +$(bin): $(obj) ../libanim.a
    1.18 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.19 +
    1.20 +.PHONY: clean
    1.21 +clean:
    1.22 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/example/test.c	Fri Mar 01 09:46:53 2013 +0200
     2.3 @@ -0,0 +1,276 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <assert.h>
     2.7 +
     2.8 +#ifndef __APPLE__
     2.9 +#include <GL/glut.h>
    2.10 +#else
    2.11 +#include <GLUT/glut.h>
    2.12 +#endif
    2.13 +
    2.14 +#include <vmath/vmath.h>
    2.15 +#include "anim.h"
    2.16 +
    2.17 +struct body_part {
    2.18 +	vec3_t pos, pivot, sz, color;
    2.19 +} parts[] = {
    2.20 +	/* position			pivot			size			color */
    2.21 +	{{0, 1, 0},			{0, -1, 0},		{1.8, 2.8, 0.8},{1, 0, 1}},		/* torso */
    2.22 +
    2.23 +	{{-0.5, -2.5, 0},	{0, 1, 0},		{0.8, 2, 0.8},	{1, 0, 0}},		/* left-upper leg */
    2.24 +	{{0.5, -2.5, 0},	{0, 1, 0},		{0.8, 2, 0.8},	{0, 1, 0}},		/* right-upper leg */
    2.25 +
    2.26 +	{{0, -2.1, 0},		{0, 1, 0},		{0.8, 2, 0.8},	{1, 0.5, 0.5}},	/* left-lower leg */
    2.27 +	{{0, -2.1, 0},		{0, 1, 0},		{0.8, 2, 0.8},	{0.5, 1, 0.5}},	/* right-lower leg */
    2.28 +
    2.29 +	{{0, 2.6, 0},		{0, -0.5, 0},	{1.2, 1.2, 1.2},{0, 1, 1}},	/* head */
    2.30 +
    2.31 +	{{-1.3, 0.4, 0},		{0, 1, 0},		{0.8, 2, 0.8},	{0, 0, 1}},	/* left-upper arm */
    2.32 +	{{1.3, 0.4, 0},		{0, 1, 0},		{0.8, 2, 0.8},	{1, 1, 0}},		/* right-upper arm */
    2.33 +
    2.34 +	{{0, -2.1, 0},		{0, 1, 0},		{0.8, 2, 0.8},	{0.5, 0.5, 1}},	/* left-lower arm */
    2.35 +	{{0, -2.1, 0},		{0, 1, 0},		{0.8, 2, 0.8},	{1, 1, 0.5}},	/* right-lower arm */
    2.36 +};
    2.37 +
    2.38 +enum {
    2.39 +	NODE_TORSO,
    2.40 +	NODE_LEFT_UPPER_LEG,
    2.41 +	NODE_RIGHT_UPPER_LEG,
    2.42 +	NODE_LEFT_LOWER_LEG,
    2.43 +	NODE_RIGHT_LOWER_LEG,
    2.44 +	NODE_HEAD,
    2.45 +	NODE_LEFT_UPPER_ARM,
    2.46 +	NODE_RIGHT_UPPER_ARM,
    2.47 +	NODE_LEFT_LOWER_ARM,
    2.48 +	NODE_RIGHT_LOWER_ARM,
    2.49 +
    2.50 +	NUM_NODES
    2.51 +};
    2.52 +
    2.53 +int init(void);
    2.54 +void disp(void);
    2.55 +void idle(void);
    2.56 +void reshape(int x, int y);
    2.57 +void keyb(unsigned char key, int x, int y);
    2.58 +void mouse(int bn, int state, int x, int y);
    2.59 +void motion(int x, int y);
    2.60 +
    2.61 +float cam_theta = 20, cam_phi = 20, cam_dist = 15;
    2.62 +struct anm_node *root;
    2.63 +
    2.64 +struct anm_node *nodes[NUM_NODES];
    2.65 +
    2.66 +int main(int argc, char **argv)
    2.67 +{
    2.68 +	glutInitWindowSize(800, 600);
    2.69 +	glutInit(&argc, argv);
    2.70 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    2.71 +	glutCreateWindow("libanim example");
    2.72 +
    2.73 +	glutDisplayFunc(disp);
    2.74 +	glutIdleFunc(idle);
    2.75 +	glutReshapeFunc(reshape);
    2.76 +	glutKeyboardFunc(keyb);
    2.77 +	glutMouseFunc(mouse);
    2.78 +	glutMotionFunc(motion);
    2.79 +
    2.80 +	if(init() == -1) {
    2.81 +		return 1;
    2.82 +	}
    2.83 +	glutMainLoop();
    2.84 +	return 0;
    2.85 +}
    2.86 +
    2.87 +int init(void)
    2.88 +{
    2.89 +	int i;
    2.90 +
    2.91 +	glPointSize(3);
    2.92 +
    2.93 +	glEnable(GL_DEPTH_TEST);
    2.94 +	glEnable(GL_CULL_FACE);
    2.95 +	glEnable(GL_LIGHTING);
    2.96 +	glEnable(GL_LIGHT0);
    2.97 +
    2.98 +	root = nodes[0];
    2.99 +
   2.100 +	for(i=0; i<NUM_NODES; i++) {
   2.101 +		nodes[i] = anm_create_node();
   2.102 +
   2.103 +		anm_set_pivot(nodes[i], parts[i].pivot);
   2.104 +		anm_set_position(nodes[i], parts[i].pos, 0);
   2.105 +		anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
   2.106 +	}
   2.107 +
   2.108 +	anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]);
   2.109 +	anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]);
   2.110 +	anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_LEG]);
   2.111 +	anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_ARM]);
   2.112 +	anm_link_node(nodes[NODE_TORSO], nodes[NODE_RIGHT_UPPER_ARM]);
   2.113 +	anm_link_node(nodes[NODE_LEFT_UPPER_LEG], nodes[NODE_LEFT_LOWER_LEG]);
   2.114 +	anm_link_node(nodes[NODE_RIGHT_UPPER_LEG], nodes[NODE_RIGHT_LOWER_LEG]);
   2.115 +	anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]);
   2.116 +	anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]);
   2.117 +
   2.118 +	/* upper leg animation */
   2.119 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
   2.120 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
   2.121 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 2000);
   2.122 +
   2.123 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 0);
   2.124 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 1000);
   2.125 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 2000);
   2.126 +
   2.127 +	/* lower leg animation */
   2.128 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
   2.129 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 500);
   2.130 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 1000);
   2.131 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
   2.132 +
   2.133 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 0);
   2.134 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
   2.135 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-90), 1, 0, 0), 1500);
   2.136 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 1, 0, 0), 2000);
   2.137 +
   2.138 +	/* head animation */
   2.139 +	anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 0);
   2.140 +	anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(10), 0, 1, 0), 1000);
   2.141 +	anm_set_rotation(nodes[NODE_HEAD], quat_rotate(quat_identity(), DEG_TO_RAD(-10), 0, 1, 0), 2000);
   2.142 +
   2.143 +	/* torso animation */
   2.144 +	anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 0);
   2.145 +	anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
   2.146 +	anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 1000);
   2.147 +	anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
   2.148 +	anm_set_rotation(nodes[NODE_TORSO], quat_rotate(quat_identity(), DEG_TO_RAD(-8), 1, 0, 0), 2000);
   2.149 +
   2.150 +	/* upper arm animation */
   2.151 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 0);
   2.152 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 1000);
   2.153 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 2000);
   2.154 +
   2.155 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 0);
   2.156 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(30), 1, 0, 0), 1000);
   2.157 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-25), 1, 0, 0), 2000);
   2.158 +
   2.159 +	/* lower arm animation */
   2.160 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 0);
   2.161 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1000);
   2.162 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
   2.163 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 2000);
   2.164 +
   2.165 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
   2.166 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
   2.167 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
   2.168 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
   2.169 +
   2.170 +	return 0;
   2.171 +}
   2.172 +
   2.173 +void disp(void)
   2.174 +{
   2.175 +	int i;
   2.176 +	float lpos[] = {-1, 1, 1.5, 0};
   2.177 +	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
   2.178 +
   2.179 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   2.180 +
   2.181 +	glMatrixMode(GL_MODELVIEW);
   2.182 +	glLoadIdentity();
   2.183 +
   2.184 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   2.185 +
   2.186 +	glTranslatef(0, 0, -cam_dist);
   2.187 +	glRotatef(cam_phi, 1, 0, 0);
   2.188 +	glRotatef(cam_theta, 0, 1, 0);
   2.189 +
   2.190 +	for(i=0; i<NUM_NODES; i++) {
   2.191 +		float color[4] = {0, 0, 0, 1};
   2.192 +		mat4_t xform;
   2.193 +
   2.194 +		color[0] = parts[i].color.x;
   2.195 +		color[1] = parts[i].color.y;
   2.196 +		color[2] = parts[i].color.z;
   2.197 +
   2.198 +		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
   2.199 +		glColor4fv(color);
   2.200 +
   2.201 +		anm_get_matrix(nodes[i], xform, msec);
   2.202 +
   2.203 +		glPushMatrix();
   2.204 +		glMultTransposeMatrixf((float*)xform);
   2.205 +
   2.206 +		glScalef(parts[i].sz.x, parts[i].sz.y, parts[i].sz.z);
   2.207 +		glutSolidCube(1.0);
   2.208 +		/*glutWireSphere(0.6, 16, 8);*/
   2.209 +
   2.210 +		glPopMatrix();
   2.211 +	}
   2.212 +
   2.213 +	glutSwapBuffers();
   2.214 +	assert(glGetError() == GL_NO_ERROR);
   2.215 +}
   2.216 +
   2.217 +void idle(void)
   2.218 +{
   2.219 +	glutPostRedisplay();
   2.220 +}
   2.221 +
   2.222 +void reshape(int x, int y)
   2.223 +{
   2.224 +	glViewport(0, 0, x, y);
   2.225 +
   2.226 +	glMatrixMode(GL_PROJECTION);
   2.227 +	glLoadIdentity();
   2.228 +	gluPerspective(45.0, (float)x / (float)y, 0.5, 500.0);
   2.229 +}
   2.230 +
   2.231 +void keyb(unsigned char key, int x, int y)
   2.232 +{
   2.233 +	switch(key) {
   2.234 +	case 27:
   2.235 +		exit(0);
   2.236 +	}
   2.237 +}
   2.238 +
   2.239 +int bnstate[64];
   2.240 +int prev_x, prev_y;
   2.241 +
   2.242 +void mouse(int bn, int state, int x, int y)
   2.243 +{
   2.244 +	int idx = bn - GLUT_LEFT_BUTTON;
   2.245 +	int down = state == GLUT_DOWN ? 1 : 0;
   2.246 +
   2.247 +	bnstate[idx] = down;
   2.248 +
   2.249 +	prev_x = x;
   2.250 +	prev_y = y;
   2.251 +}
   2.252 +
   2.253 +void motion(int x, int y)
   2.254 +{
   2.255 +	int dx = x - prev_x;
   2.256 +	int dy = y - prev_y;
   2.257 +	prev_x = x;
   2.258 +	prev_y = y;
   2.259 +
   2.260 +	if(bnstate[0]) {
   2.261 +		cam_theta += dx * 0.5;
   2.262 +		cam_phi += dy * 0.5;
   2.263 +
   2.264 +		if(cam_phi < -90) {
   2.265 +			cam_phi = -90;
   2.266 +		}
   2.267 +		if(cam_phi > 90) {
   2.268 +			cam_phi = 90;
   2.269 +		}
   2.270 +		glutPostRedisplay();
   2.271 +	}
   2.272 +	if(bnstate[2]) {
   2.273 +		cam_dist += dy * 0.1;
   2.274 +		if(cam_dist < 0.0) {
   2.275 +			cam_dist = 0.0;
   2.276 +		}
   2.277 +		glutPostRedisplay();
   2.278 +	}
   2.279 +}