goat3d

changeset 68:8ecaf9cd3ce7

progress on the exporter
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Apr 2014 12:43:26 +0300
parents 8970ca3d55e0
children 66cd8266f078 0bb33d04f279
files exporters/maxgoat/src/maxgoat.cc src/xform_node.cc
diffstat 2 files changed, 197 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/exporters/maxgoat/src/maxgoat.cc	Sat Apr 19 08:01:37 2014 +0300
     1.2 +++ b/exporters/maxgoat/src/maxgoat.cc	Sun Apr 20 12:43:26 2014 +0300
     1.3 @@ -4,6 +4,7 @@
     1.4  #include <ctype.h>
     1.5  #include <errno.h>
     1.6  #include <map>
     1.7 +#include <vector>
     1.8  #include <windows.h>
     1.9  #include <shlobj.h>
    1.10  #include "max.h"
    1.11 @@ -39,6 +40,11 @@
    1.12  
    1.13  static INT_PTR CALLBACK scene_gui_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
    1.14  static INT_PTR CALLBACK anim_gui_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
    1.15 +static void get_position_keys(IGameControl *ctrl, goat3d_node *node);
    1.16 +static void get_xyz_position_keys(IGameControl *ctrl, goat3d_node *node);
    1.17 +static void get_rotation_keys(IGameControl *ctrl, goat3d_node *node);
    1.18 +static void get_euler_keys(IGameControl *ctrl, goat3d_node *node);
    1.19 +static void get_scaling_keys(IGameControl *ctrl, goat3d_node *node);
    1.20  static const char *max_string(const MCHAR *wstr);
    1.21  
    1.22  HINSTANCE hinst;
    1.23 @@ -294,21 +300,199 @@
    1.24  	// grab the animation data
    1.25  	IGameControl *ctrl = maxnode->GetIGameControl();
    1.26  
    1.27 -	IGameKeyTab tm_keys;
    1.28 -	if(ctrl->GetFullSampledKeys(tm_keys, 1, IGAME_TM)) {
    1.29 -		maxlog("node: %s has %d keys\n", name, tm_keys.Count());
    1.30 -		/*for(int i=0; i<pkeys.Count(); i++) {
    1.31 -			Point3 p = pkeys[i].linearKey.pval;
    1.32 -			TimeValue t = pkeys[i].t;
    1.33 -			goat3d_set_node_position(node, p.x, p.y, p.z, TicksToSec(t));
    1.34 -		}*/
    1.35 -	}	
    1.36 +	// TODO sample keys if requested
    1.37 +
    1.38 +	if(ctrl->IsAnimated(IGAME_POS) || ctrl->IsAnimated(IGAME_POS_X) ||
    1.39 +			ctrl->IsAnimated(IGAME_POS_Y) || ctrl->IsAnimated(IGAME_POS_Z)) {
    1.40 +		get_position_keys(ctrl, node);
    1.41 +	}
    1.42 +	if(ctrl->IsAnimated(IGAME_ROT) || ctrl->IsAnimated(IGAME_EULER_X) ||
    1.43 +			ctrl->IsAnimated(IGAME_EULER_Y) || ctrl->IsAnimated(IGAME_EULER_Z)) {
    1.44 +		get_rotation_keys(ctrl, node);
    1.45 +	}
    1.46 +	if(ctrl->IsAnimated(IGAME_SCALE)) {
    1.47 +		get_scaling_keys(ctrl, node);
    1.48 +	}
    1.49  
    1.50  	for(int i=0; i<maxnode->GetChildCount(); i++) {
    1.51  		process_node(goat, node, maxnode->GetNodeChild(i));
    1.52  	}
    1.53  }
    1.54  
    1.55 +#define KEY_TIME(key)	((long)(TicksToSec(key.t) * 1000.0))
    1.56 +
    1.57 +static void get_position_keys(IGameControl *ctrl, goat3d_node *node)
    1.58 +{
    1.59 +	const char *nodename = goat3d_get_node_name(node);
    1.60 +	IGameKeyTab keys;
    1.61 +
    1.62 +	if(ctrl->GetLinearKeys(keys, IGAME_POS)) {
    1.63 +		maxlog("node %s: getting %d linear position keys\n", nodename, keys.Count());
    1.64 +		for(int i=0; i<keys.Count(); i++) {
    1.65 +			Point3 p = keys[i].linearKey.pval;
    1.66 +			goat3d_set_node_position(node, p.x, p.y, p.z, KEY_TIME(keys[i]));
    1.67 +		}
    1.68 +	} else if(ctrl->GetBezierKeys(keys, IGAME_POS)) {
    1.69 +		maxlog("node %s: getting %d bezier position keys\n", nodename, keys.Count());
    1.70 +		for(int i=0; i<keys.Count(); i++) {
    1.71 +			Point3 p = keys[i].bezierKey.pval;
    1.72 +			goat3d_set_node_position(node, p.x, p.y, p.z, KEY_TIME(keys[i]));
    1.73 +		}
    1.74 +	} else if(ctrl->GetTCBKeys(keys, IGAME_POS)) {
    1.75 +		maxlog("node %s: getting %d tcb position keys\n", nodename, keys.Count());
    1.76 +		for(int i=0; i<keys.Count(); i++) {
    1.77 +			Point3 p = keys[i].tcbKey.pval;
    1.78 +			goat3d_set_node_position(node, p.x, p.y, p.z, KEY_TIME(keys[i]));
    1.79 +		}
    1.80 +	} else {
    1.81 +		get_xyz_position_keys(ctrl, node);
    1.82 +	}
    1.83 +}
    1.84 +
    1.85 +static void get_xyz_position_keys(IGameControl *ctrl, goat3d_node *node)
    1.86 +{
    1.87 +	const char *nodename = goat3d_get_node_name(node);
    1.88 +	IGameKeyTab keys;
    1.89 +	IGameControlType postype[] = {IGAME_POS_X, IGAME_POS_Y, IGAME_POS_Z};
    1.90 +	std::map<long, Point3> pos;
    1.91 +
    1.92 +	for(int i=0; i<3; i++) {
    1.93 +		if(ctrl->GetLinearKeys(keys, postype[i])) {
    1.94 +			maxlog("node %s: getting %d linear position %c keys\n", nodename, keys.Count(), "xyz"[i]);
    1.95 +			for(int j=0; j<keys.Count(); j++) {
    1.96 +				long tm = KEY_TIME(keys[j]);
    1.97 +				Point3 v = pos[tm];
    1.98 +				v[i] = keys[j].linearKey.fval;
    1.99 +				pos[tm] = v;
   1.100 +			}
   1.101 +		} else if(ctrl->GetBezierKeys(keys, postype[i])) {
   1.102 +			maxlog("node %s: getting %d bezier position %c keys\n", nodename, keys.Count(), "xyz"[i]);
   1.103 +			for(int j=0; j<keys.Count(); j++) {
   1.104 +				long tm = KEY_TIME(keys[j]);
   1.105 +				Point3 v = pos[tm];
   1.106 +				v[i] = keys[j].bezierKey.fval;
   1.107 +				pos[tm] = v;
   1.108 +			}
   1.109 +		} else if(ctrl->GetTCBKeys(keys, postype[i])) {
   1.110 +			maxlog("node %s: getting %d tcb position %c keys\n", nodename, keys.Count(), "xyz"[i]);
   1.111 +			for(int j=0; j<keys.Count(); j++) {
   1.112 +				long tm = KEY_TIME(keys[j]);
   1.113 +				Point3 v = pos[tm];
   1.114 +				v[i] = keys[j].tcbKey.fval;
   1.115 +				pos[tm] = v;
   1.116 +			}
   1.117 +		}
   1.118 +	}
   1.119 +
   1.120 +	std::map<long, Point3>::iterator it = pos.begin();
   1.121 +	while(it != pos.end()) {
   1.122 +		Point3 p = it->second;
   1.123 +		goat3d_set_node_position(node, p.x, p.y, p.z, it->first);
   1.124 +		++it;
   1.125 +	}
   1.126 +}
   1.127 +
   1.128 +static void get_rotation_keys(IGameControl *ctrl, goat3d_node *node)
   1.129 +{
   1.130 +	const char *nodename = goat3d_get_node_name(node);
   1.131 +	IGameKeyTab rkeys;
   1.132 +
   1.133 +	if(ctrl->GetLinearKeys(rkeys, IGAME_ROT)) {
   1.134 +		maxlog("node %s: getting %d linear rotation keys\n", nodename, rkeys.Count());
   1.135 +		for(int i=0; i<rkeys.Count(); i++) {
   1.136 +			Quat q = rkeys[i].linearKey.qval;
   1.137 +			goat3d_set_node_rotation(node, q.x, q.y, q.z, q.w, KEY_TIME(rkeys[i]));
   1.138 +		}
   1.139 +	} else if(ctrl->GetBezierKeys(rkeys, IGAME_ROT)) {
   1.140 +		maxlog("node %s: getting %d bezier rotation keys\n", nodename, rkeys.Count());
   1.141 +		for(int i=0; i<rkeys.Count(); i++) {
   1.142 +			Quat q = rkeys[i].bezierKey.qval;
   1.143 +			goat3d_set_node_rotation(node, q.x, q.y, q.z, q.w, KEY_TIME(rkeys[i]));
   1.144 +		}
   1.145 +	} else if(ctrl->GetTCBKeys(rkeys, IGAME_ROT)) {
   1.146 +		maxlog("node %s: getting %d TCB rotation keys\n", nodename, rkeys.Count());
   1.147 +		for(int i=0; i<rkeys.Count(); i++) {
   1.148 +			Quat q(rkeys[i].tcbKey.aval);
   1.149 +			goat3d_set_node_rotation(node, q.x, q.y, q.z, q.w, KEY_TIME(rkeys[i]));
   1.150 +		}
   1.151 +	} else {
   1.152 +		get_euler_keys(ctrl, node);
   1.153 +	}
   1.154 +}
   1.155 +
   1.156 +static void get_euler_keys(IGameControl *ctrl, goat3d_node *node)
   1.157 +{
   1.158 +	const char *nodename = goat3d_get_node_name(node);
   1.159 +	IGameKeyTab keys;
   1.160 +	IGameControlType eulertype[] = {IGAME_EULER_X, IGAME_EULER_Y, IGAME_EULER_Z};
   1.161 +	std::map<long, Point3> euler;
   1.162 +
   1.163 +	for(int i=0; i<3; i++) {
   1.164 +		if(ctrl->GetLinearKeys(keys, eulertype[i])) {
   1.165 +			maxlog("node %s: getting %d linear euler %c keys\n", nodename, keys.Count(), "xyz"[i]);
   1.166 +			for(int j=0; j<keys.Count(); j++) {
   1.167 +				long tm = KEY_TIME(keys[j]);
   1.168 +				Point3 v = euler[tm];
   1.169 +				v[i] = keys[j].linearKey.fval;
   1.170 +				euler[tm] = v;
   1.171 +			}
   1.172 +		} else if(ctrl->GetBezierKeys(keys, eulertype[i])) {
   1.173 +			maxlog("node %s: getting %d bezier euler %c keys\n", nodename, keys.Count(), "xyz"[i]);
   1.174 +			for(int j=0; j<keys.Count(); j++) {
   1.175 +				long tm = KEY_TIME(keys[j]);
   1.176 +				Point3 v = euler[tm];
   1.177 +				v[i] = keys[j].bezierKey.fval;
   1.178 +				euler[tm] = v;
   1.179 +			}
   1.180 +		} else if(ctrl->GetTCBKeys(keys, eulertype[i])) {
   1.181 +			maxlog("node %s: getting %d tcb euler %c keys\n", nodename, keys.Count(), "xyz"[i]);
   1.182 +			for(int j=0; j<keys.Count(); j++) {
   1.183 +				long tm = KEY_TIME(keys[j]);
   1.184 +				Point3 v = euler[tm];
   1.185 +				v[i] = keys[j].tcbKey.fval;
   1.186 +				euler[tm] = v;
   1.187 +			}
   1.188 +		}
   1.189 +	}
   1.190 +
   1.191 +	int order = ctrl->GetEulerOrder();
   1.192 +	std::map<long, Point3>::iterator it = euler.begin();
   1.193 +	while(it != euler.end()) {
   1.194 +		Quat q;
   1.195 +		EulerToQuat(it->second, q, order);
   1.196 +		goat3d_set_node_rotation(node, q.x, q.y, q.z, q.w, it->first);
   1.197 +		++it;
   1.198 +	}
   1.199 +}
   1.200 +
   1.201 +static void get_scaling_keys(IGameControl *ctrl, goat3d_node *node)
   1.202 +{
   1.203 +	const char *nodename = goat3d_get_node_name(node);
   1.204 +	IGameKeyTab keys;
   1.205 +
   1.206 +	// XXX the way I'm using the ScaleValue is wrong, but fuck it...
   1.207 +
   1.208 +	if(ctrl->GetLinearKeys(keys, IGAME_SCALE)) {
   1.209 +		maxlog("node %s: getting %d linear scaling keys\n", nodename, keys.Count());
   1.210 +		for(int i=0; i<keys.Count(); i++) {
   1.211 +			ScaleValue s = keys[i].linearKey.sval;
   1.212 +			goat3d_set_node_scaling(node, s.s.x, s.s.y, s.s.z, KEY_TIME(keys[i]));
   1.213 +		}
   1.214 +	} else if(ctrl->GetBezierKeys(keys, IGAME_SCALE)) {
   1.215 +		maxlog("node %s: getting %d bezier scaling keys\n", nodename, keys.Count());
   1.216 +		for(int i=0; i<keys.Count(); i++) {
   1.217 +			ScaleValue s = keys[i].bezierKey.sval;
   1.218 +			goat3d_set_node_scaling(node, s.s.x, s.s.y, s.s.z, KEY_TIME(keys[i]));
   1.219 +		}
   1.220 +	} else if(ctrl->GetTCBKeys(keys, IGAME_SCALE)) {
   1.221 +		maxlog("node %s: getting %d tcb scaling keys\n", nodename, keys.Count());
   1.222 +		for(int i=0; i<keys.Count(); i++) {
   1.223 +			ScaleValue s = keys[i].tcbKey.sval;
   1.224 +			goat3d_set_node_scaling(node, s.s.x, s.s.y, s.s.z, KEY_TIME(keys[i]));
   1.225 +		}
   1.226 +	}
   1.227 +}
   1.228 +
   1.229  void GoatExporter::process_mesh(goat3d *goat, goat3d_mesh *mesh, IGameObject *maxobj)
   1.230  {
   1.231  	IGameMesh *maxmesh = (IGameMesh*)maxobj;
     2.1 --- a/src/xform_node.cc	Sat Apr 19 08:01:37 2014 +0300
     2.2 +++ b/src/xform_node.cc	Sun Apr 20 12:43:26 2014 +0300
     2.3 @@ -209,7 +209,10 @@
     2.4  int XFormNode::get_key_count(int trackid) const
     2.5  {
     2.6  	struct anm_animation *anim = anm_get_active_animation(anm, 0);
     2.7 -	return anim->tracks[track_type_base[trackid]].count;
     2.8 +	if(anim) {
     2.9 +		return anim->tracks[track_type_base[trackid]].count;
    2.10 +	}
    2.11 +	return 0;
    2.12  }
    2.13  
    2.14  int XFormNode::get_position_key_count() const