libanim

diff example/test.c @ 21:5993f405a1cb

implemented multiple animations per node, and blending between two animations
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 06:28:43 +0200
parents 3c2428cb38f7
children 9758004136f8
line diff
     1.1 --- a/example/test.c	Mon Dec 09 04:06:30 2013 +0200
     1.2 +++ b/example/test.c	Fri Dec 27 06:28:43 2013 +0200
     1.3 @@ -48,6 +48,8 @@
     1.4  };
     1.5  
     1.6  int init(void);
     1.7 +static void set_walk_animation(int idx);
     1.8 +static void set_jump_animation(int idx);
     1.9  void disp(void);
    1.10  void idle(void);
    1.11  void reshape(int x, int y);
    1.12 @@ -60,6 +62,9 @@
    1.13  
    1.14  struct anm_node *nodes[NUM_NODES];
    1.15  
    1.16 +int cur_anim = 0, next_anim = 0;
    1.17 +unsigned int trans_start_tm;
    1.18 +
    1.19  int main(int argc, char **argv)
    1.20  {
    1.21  	glutInitWindowSize(800, 600);
    1.22 @@ -92,15 +97,11 @@
    1.23  	glEnable(GL_LIGHTING);
    1.24  	glEnable(GL_LIGHT0);
    1.25  
    1.26 -	root = nodes[0];
    1.27 -
    1.28  	for(i=0; i<NUM_NODES; i++) {
    1.29  		nodes[i] = anm_create_node();
    1.30 -
    1.31  		anm_set_pivot(nodes[i], parts[i].pivot);
    1.32 -		anm_set_position(nodes[i], parts[i].pos, 0);
    1.33 -		anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
    1.34  	}
    1.35 +	root = nodes[0];
    1.36  
    1.37  	anm_link_node(nodes[NODE_TORSO], nodes[NODE_HEAD]);
    1.38  	anm_link_node(nodes[NODE_TORSO], nodes[NODE_LEFT_UPPER_LEG]);
    1.39 @@ -112,6 +113,27 @@
    1.40  	anm_link_node(nodes[NODE_LEFT_UPPER_ARM], nodes[NODE_LEFT_LOWER_ARM]);
    1.41  	anm_link_node(nodes[NODE_RIGHT_UPPER_ARM], nodes[NODE_RIGHT_LOWER_ARM]);
    1.42  
    1.43 +	set_walk_animation(0);
    1.44 +
    1.45 +	anm_add_animation(root);	/* recursively add another animation slot to all nodes */
    1.46 +	set_jump_animation(1);
    1.47 +
    1.48 +	anm_use_animation(root, cur_anim);
    1.49 +
    1.50 +	return 0;
    1.51 +}
    1.52 +
    1.53 +static void set_walk_animation(int idx)
    1.54 +{
    1.55 +	int i;
    1.56 +
    1.57 +	anm_use_animation(root, idx);
    1.58 +
    1.59 +	for(i=0; i<NUM_NODES; i++) {
    1.60 +		anm_set_position(nodes[i], parts[i].pos, 0);
    1.61 +		anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
    1.62 +	}
    1.63 +
    1.64  	/* upper leg animation */
    1.65  	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-15), 1, 0, 0), 0);
    1.66  	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(45), 1, 0, 0), 1000);
    1.67 @@ -163,8 +185,53 @@
    1.68  	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 500);
    1.69  	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
    1.70  	anm_set_rotation(nodes[NODE_RIGHT_LOWER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
    1.71 +}
    1.72  
    1.73 -	return 0;
    1.74 +static void set_jump_animation(int idx)
    1.75 +{
    1.76 +	int i;
    1.77 +
    1.78 +	anm_use_animation(root, idx);
    1.79 +
    1.80 +	for(i=0; i<NUM_NODES; i++) {
    1.81 +		anm_set_position(nodes[i], parts[i].pos, 0);
    1.82 +		anm_set_extrapolator(nodes[i], ANM_EXTRAP_REPEAT);
    1.83 +	}
    1.84 +
    1.85 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
    1.86 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
    1.87 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
    1.88 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
    1.89 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
    1.90 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(40), 1, 0, 0), 1000);
    1.91 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
    1.92 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
    1.93 +
    1.94 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
    1.95 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
    1.96 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
    1.97 +	anm_set_rotation(nodes[NODE_LEFT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
    1.98 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
    1.99 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(-80), 1, 0, 0), 1000);
   1.100 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 1500);
   1.101 +	anm_set_rotation(nodes[NODE_RIGHT_LOWER_LEG], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
   1.102 +
   1.103 +	anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 0);
   1.104 +	anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, -1, 0)), 1000);
   1.105 +	anm_set_position(nodes[NODE_TORSO], v3_add(parts[NODE_TORSO].pos, v3_cons(0, 2, 0)), 1500);
   1.106 +	anm_set_position(nodes[NODE_TORSO], parts[NODE_TORSO].pos, 2000);
   1.107 +
   1.108 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
   1.109 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
   1.110 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
   1.111 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
   1.112 +	anm_set_rotation(nodes[NODE_LEFT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
   1.113 +
   1.114 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 0);
   1.115 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(-20), 1, 0, 0), 1000);
   1.116 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(20), 1, 0, 0), 1200);
   1.117 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(170), 1, 0, 0), 1500);
   1.118 +	anm_set_rotation(nodes[NODE_RIGHT_UPPER_ARM], quat_rotate(quat_identity(), DEG_TO_RAD(0), 1, 0, 0), 2000);
   1.119  }
   1.120  
   1.121  void disp(void)
   1.122 @@ -184,6 +251,18 @@
   1.123  	glRotatef(cam_phi, 1, 0, 0);
   1.124  	glRotatef(cam_theta, 0, 1, 0);
   1.125  
   1.126 +	if(cur_anim != next_anim) {
   1.127 +		float t = (msec - trans_start_tm) / 1000.0;
   1.128 +
   1.129 +		if(t >= 1.0) {
   1.130 +			t = 1.0;
   1.131 +			cur_anim = next_anim;
   1.132 +			anm_use_animation(root, cur_anim);
   1.133 +		} else {
   1.134 +			anm_use_animations(root, cur_anim, next_anim, t);
   1.135 +		}
   1.136 +	}
   1.137 +
   1.138  	/* first render a character with bottom-up lazy matrix calculation */
   1.139  	glPushMatrix();
   1.140  	glTranslatef(-2.5, 0, 0);
   1.141 @@ -264,6 +343,11 @@
   1.142  	switch(key) {
   1.143  	case 27:
   1.144  		exit(0);
   1.145 +
   1.146 +	case ' ':
   1.147 +		next_anim = (cur_anim + 1) % 2;
   1.148 +		trans_start_tm = glutGet(GLUT_ELAPSED_TIME);
   1.149 +		break;
   1.150  	}
   1.151  }
   1.152