goat3d

diff src/goat3d_writexml.cc @ 48:9ef9de80649c

implemented animation track XML saving
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Dec 2013 06:01:59 +0200
parents 498ca7ac7047
children fa5c52ea9d59
line diff
     1.1 --- a/src/goat3d_writexml.cc	Sat Dec 28 06:47:39 2013 +0200
     1.2 +++ b/src/goat3d_writexml.cc	Sun Dec 29 06:01:59 2013 +0200
     1.3 @@ -180,48 +180,35 @@
     1.4  
     1.5  static bool write_node_anim(goat3d_io *io, const XFormNode *node, int level)
     1.6  {
     1.7 +	/* NOTE: the order of names must correspond to the order of
     1.8 +	 * XFormNode::POSITION_TRACK/ROTATION_TRACK/SCALING_TRACK enum
     1.9 +	 */
    1.10  	static const char *attr_names[] = { "position", "rotation", "scaling" };
    1.11 -	struct anm_node *anode = node->get_libanim_node();
    1.12 -	struct anm_animation *anim = anm_get_active_animation(anode, 0);
    1.13  
    1.14 -	if(!anode || !anim) {
    1.15 -		return false;
    1.16 -	}
    1.17 -
    1.18 -	struct anm_track *trk[4];
    1.19 -
    1.20 -	for(int i=0; i<3; i++) {	// 3 attributes
    1.21 -		switch(i) {
    1.22 -		case 0:	// position
    1.23 -			trk[0] = anim->tracks + ANM_TRACK_POS_X;
    1.24 -			trk[1] = anim->tracks + ANM_TRACK_POS_Y;
    1.25 -			trk[2] = anim->tracks + ANM_TRACK_POS_Z;
    1.26 -			trk[3] = 0;
    1.27 -			break;
    1.28 -
    1.29 -		case 1:	// rotation
    1.30 -			trk[0] = anim->tracks + ANM_TRACK_ROT_X;
    1.31 -			trk[1] = anim->tracks + ANM_TRACK_ROT_Y;
    1.32 -			trk[2] = anim->tracks + ANM_TRACK_ROT_Z;
    1.33 -			trk[3] = anim->tracks + ANM_TRACK_ROT_W;
    1.34 -			break;
    1.35 -
    1.36 -		case 2:	// scaling
    1.37 -			trk[0] = anim->tracks + ANM_TRACK_SCL_X;
    1.38 -			trk[1] = anim->tracks + ANM_TRACK_SCL_Y;
    1.39 -			trk[2] = anim->tracks + ANM_TRACK_SCL_Z;
    1.40 -			trk[3] = 0;
    1.41 -		}
    1.42 -
    1.43 -		if(trk[0]->count <= 0) {
    1.44 -			continue;	// skip tracks without any keyframes
    1.45 -		}
    1.46 +	// for each of: position/rotation/scaling
    1.47 +	for(int i=0; i<3; i++) {
    1.48 +		int num_keys = node->get_key_count(i);
    1.49 +		if(!num_keys) continue;
    1.50  
    1.51  		xmlout(io, level + 1, "<track>\n");
    1.52  		xmlout(io, level + 2, "<node string=\"%s\"/>\n", node->get_name());
    1.53 -		xmlout(io, level + 2, "<attr string=\"%s\"/>\n", attr_names[i]);
    1.54 +		xmlout(io, level + 2, "<attr string=\"%s\"/>\n\n", attr_names[i]);
    1.55  
    1.56 -		// TODO cont: move all the keyframe retreival to XFormNode and use that...
    1.57 +		// for each key in that track
    1.58 +		for(int j=0; j<num_keys; j++) {
    1.59 +			long tm = node->get_key_time(i, j);
    1.60 +
    1.61 +			float value[4];
    1.62 +			int num_elems = node->get_key_value(i, j, value);
    1.63 +
    1.64 +			if(num_elems == 3) {
    1.65 +				xmlout(io, level + 2, "<key><time int=\"%ld\"/><value float3=\"%g %g %g\"/></key>\n",
    1.66 +						tm, value[0], value[1], value[2]);
    1.67 +			} else {
    1.68 +				xmlout(io, level + 2, "<key><time int=\"%ld\"/><value float4=\"%g %g %g %g\"/></key>\n",
    1.69 +						tm, value[0], value[1], value[2], value[3]);
    1.70 +			}
    1.71 +		}
    1.72  
    1.73  		xmlout(io, level + 1, "</track>\n");
    1.74  	}