goat3dgfx

diff src/xform_node.cc @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7d6b667821cf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/xform_node.cc	Thu Nov 14 05:27:09 2013 +0200
     1.3 @@ -0,0 +1,370 @@
     1.4 +#include <assert.h>
     1.5 +#include <algorithm>
     1.6 +#include "xform_node.h"
     1.7 +#include "anim/anim.h"
     1.8 +#include "anim/track.h"
     1.9 +
    1.10 +static inline anm_interpolator track_interpolator(Interp in);
    1.11 +static inline anm_extrapolator track_extrapolator(Extrap ex);
    1.12 +
    1.13 +XFormNode::XFormNode()
    1.14 +{
    1.15 +	anm = new anm_node;
    1.16 +	anm_init_node(anm);
    1.17 +}
    1.18 +
    1.19 +XFormNode::~XFormNode()
    1.20 +{
    1.21 +	anm_destroy_node(anm);
    1.22 +	delete anm;
    1.23 +}
    1.24 +
    1.25 +void XFormNode::set_name(const char *name)
    1.26 +{
    1.27 +	anm_set_node_name(anm, name);
    1.28 +}
    1.29 +
    1.30 +const char *XFormNode::get_name() const
    1.31 +{
    1.32 +	return anm_get_node_name(anm);
    1.33 +}
    1.34 +
    1.35 +void XFormNode::set_interpolator(Interp in)
    1.36 +{
    1.37 +	anm_set_interpolator(anm, track_interpolator(in));
    1.38 +	interp = in;
    1.39 +}
    1.40 +
    1.41 +Interp XFormNode::get_interpolator() const
    1.42 +{
    1.43 +	return interp;
    1.44 +}
    1.45 +
    1.46 +void XFormNode::set_extrapolator(Extrap ex)
    1.47 +{
    1.48 +	anm_set_extrapolator(anm, track_extrapolator(ex));
    1.49 +	extrap = ex;
    1.50 +}
    1.51 +
    1.52 +Extrap XFormNode::get_extrapolator() const
    1.53 +{
    1.54 +	return extrap;
    1.55 +}
    1.56 +
    1.57 +void XFormNode::add_child(XFormNode *child)
    1.58 +{
    1.59 +	children.push_back(child);
    1.60 +	anm_link_node(anm, child->anm);
    1.61 +}
    1.62 +
    1.63 +void XFormNode::remove_child(XFormNode *child)
    1.64 +{
    1.65 +	std::vector<XFormNode*>::iterator it;
    1.66 +	it = std::find(children.begin(), children.end(), child);
    1.67 +	if(it != children.end()) {
    1.68 +		children.erase(it);
    1.69 +		anm_unlink_node(anm, child->anm);
    1.70 +	}
    1.71 +}
    1.72 +
    1.73 +int XFormNode::get_children_count() const
    1.74 +{
    1.75 +	return (int)children.size();
    1.76 +}
    1.77 +
    1.78 +XFormNode *XFormNode::get_child(int idx)
    1.79 +{
    1.80 +	if(idx >= 0 && idx < get_children_count()) {
    1.81 +		return children[idx];
    1.82 +	}
    1.83 +	return 0;
    1.84 +}
    1.85 +
    1.86 +const XFormNode *XFormNode::get_child(int idx) const
    1.87 +{
    1.88 +	if(idx >= 0 && idx < get_children_count()) {
    1.89 +		return children[idx];
    1.90 +	}
    1.91 +	return 0;
    1.92 +}
    1.93 +
    1.94 +void XFormNode::set_position(const Vector3 &pos, long tmsec)
    1.95 +{
    1.96 +	anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
    1.97 +}
    1.98 +
    1.99 +Vector3 XFormNode::get_node_position(long tmsec) const
   1.100 +{
   1.101 +	vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
   1.102 +	return Vector3(p.x, p.y, p.z);
   1.103 +}
   1.104 +
   1.105 +void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
   1.106 +{
   1.107 +	anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
   1.108 +}
   1.109 +
   1.110 +Quaternion XFormNode::get_node_rotation(long tmsec) const
   1.111 +{
   1.112 +	quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
   1.113 +	return Quaternion(q.w, q.x, q.y, q.z);
   1.114 +}
   1.115 +
   1.116 +void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
   1.117 +{
   1.118 +	anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
   1.119 +}
   1.120 +
   1.121 +Vector3 XFormNode::get_node_scaling(long tmsec) const
   1.122 +{
   1.123 +	vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
   1.124 +	return Vector3(s.x, s.y, s.z);
   1.125 +}
   1.126 +
   1.127 +// these take hierarchy into account
   1.128 +Vector3 XFormNode::get_position(long tmsec) const
   1.129 +{
   1.130 +	vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
   1.131 +	return Vector3(v.x, v.y, v.z);
   1.132 +}
   1.133 +
   1.134 +Quaternion XFormNode::get_rotation(long tmsec) const
   1.135 +{
   1.136 +	quat_t q = anm_get_rotation(anm, tmsec);
   1.137 +	return Quaternion(q.w, q.x, q.y, q.z);
   1.138 +}
   1.139 +
   1.140 +Vector3 XFormNode::get_scaling(long tmsec) const
   1.141 +{
   1.142 +	vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
   1.143 +	return Vector3(v.x, v.y, v.z);
   1.144 +}
   1.145 +
   1.146 +void XFormNode::set_pivot(const Vector3 &pivot)
   1.147 +{
   1.148 +	anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
   1.149 +}
   1.150 +
   1.151 +Vector3 XFormNode::get_pivot() const
   1.152 +{
   1.153 +	vec3_t p = anm_get_pivot(anm);
   1.154 +	return Vector3(p.x, p.y, p.z);
   1.155 +}
   1.156 +
   1.157 +void XFormNode::set_local_matrix(const Matrix4x4 &mat)
   1.158 +{
   1.159 +	local_matrix = mat;
   1.160 +}
   1.161 +
   1.162 +const Matrix4x4 &XFormNode::get_local_matrix() const
   1.163 +{
   1.164 +	return local_matrix;
   1.165 +}
   1.166 +
   1.167 +void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
   1.168 +{
   1.169 +	bone_matrix = bmat;
   1.170 +}
   1.171 +
   1.172 +const Matrix4x4 &XFormNode::get_bone_matrix() const
   1.173 +{
   1.174 +	return bone_matrix;
   1.175 +}
   1.176 +
   1.177 +#define FOO
   1.178 +
   1.179 +void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
   1.180 +{
   1.181 +	anm_time_t tm = ANM_MSEC2TM(tmsec);
   1.182 +
   1.183 +	if(mat) {
   1.184 +		anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
   1.185 +#ifdef FOO
   1.186 +		*mat = local_matrix * *mat;
   1.187 +#else
   1.188 +		*mat = *mat * local_matrix;
   1.189 +#endif
   1.190 +	}
   1.191 +	if(inv_mat) {
   1.192 +		anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
   1.193 +	}
   1.194 +}
   1.195 +
   1.196 +void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
   1.197 +{
   1.198 +	anm_time_t tm = ANM_MSEC2TM(tmsec);
   1.199 +
   1.200 +	if(mat) {
   1.201 +		anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
   1.202 +#ifdef FOO
   1.203 +		*mat = local_matrix * *mat;
   1.204 +#else
   1.205 +		*mat = *mat * local_matrix;
   1.206 +#endif
   1.207 +	}
   1.208 +	if(inv_mat) {
   1.209 +		anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
   1.210 +	}
   1.211 +}
   1.212 +
   1.213 +
   1.214 +// ---- Track ----
   1.215 +
   1.216 +Track::Track()
   1.217 +{
   1.218 +	trk = new anm_track;
   1.219 +	anm_init_track(trk);
   1.220 +}
   1.221 +
   1.222 +Track::~Track()
   1.223 +{
   1.224 +	anm_destroy_track(trk);
   1.225 +	delete trk;
   1.226 +}
   1.227 +
   1.228 +Track::Track(const Track &rhs)
   1.229 +{
   1.230 +	trk = new anm_track;
   1.231 +	anm_init_track(trk);
   1.232 +	anm_copy_track(trk, rhs.trk);
   1.233 +	interp = rhs.interp;
   1.234 +	extrap = rhs.extrap;
   1.235 +}
   1.236 +
   1.237 +Track &Track::operator =(const Track &rhs)
   1.238 +{
   1.239 +	if(&rhs == this) {
   1.240 +		return *this;
   1.241 +	}
   1.242 +
   1.243 +	anm_copy_track(trk, rhs.trk);
   1.244 +	interp = rhs.interp;
   1.245 +	extrap = rhs.extrap;
   1.246 +	return *this;
   1.247 +}
   1.248 +
   1.249 +
   1.250 +void Track::set_interpolator(Interp in)
   1.251 +{
   1.252 +	anm_set_track_interpolator(trk, track_interpolator(in));
   1.253 +	interp = in;
   1.254 +}
   1.255 +
   1.256 +Interp Track::get_interpolator() const
   1.257 +{
   1.258 +	return interp;
   1.259 +}
   1.260 +
   1.261 +void Track::set_extrapolator(Extrap ex)
   1.262 +{
   1.263 +	anm_set_track_extrapolator(trk, track_extrapolator(ex));
   1.264 +	extrap = ex;
   1.265 +}
   1.266 +
   1.267 +Extrap Track::get_extrapolator() const
   1.268 +{
   1.269 +	return extrap;
   1.270 +}
   1.271 +
   1.272 +void Track::set_default(double def)
   1.273 +{
   1.274 +	anm_set_track_default(trk, def);
   1.275 +}
   1.276 +
   1.277 +void Track::set_value(float val, long tmsec)
   1.278 +{
   1.279 +	anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
   1.280 +}
   1.281 +
   1.282 +float Track::get_value(long tmsec) const
   1.283 +{
   1.284 +	return anm_get_value(trk, ANM_MSEC2TM(tmsec));
   1.285 +}
   1.286 +
   1.287 +float Track::operator ()(long tmsec) const
   1.288 +{
   1.289 +	return anm_get_value(trk, ANM_MSEC2TM(tmsec));
   1.290 +}
   1.291 +
   1.292 +
   1.293 +// ---- Track3 ----
   1.294 +
   1.295 +void Track3::set_interpolator(Interp in)
   1.296 +{
   1.297 +	for(int i=0; i<3; i++) {
   1.298 +		track[i].set_interpolator(in);
   1.299 +	}
   1.300 +}
   1.301 +
   1.302 +Interp Track3::get_interpolator() const
   1.303 +{
   1.304 +	return track[0].get_interpolator();
   1.305 +}
   1.306 +
   1.307 +void Track3::set_extrapolator(Extrap ex)
   1.308 +{
   1.309 +	for(int i=0; i<3; i++) {
   1.310 +		track[i].set_extrapolator(ex);
   1.311 +	}
   1.312 +}
   1.313 +
   1.314 +Extrap Track3::get_extrapolator() const
   1.315 +{
   1.316 +	return track[0].get_extrapolator();
   1.317 +}
   1.318 +
   1.319 +void Track3::set_default(const Vector3 &def)
   1.320 +{
   1.321 +	for(int i=0; i<3; i++) {
   1.322 +		track[i].set_default(def[i]);
   1.323 +	}
   1.324 +}
   1.325 +
   1.326 +void Track3::set_value(const Vector3 &val, long tmsec)
   1.327 +{
   1.328 +	for(int i=0; i<3; i++) {
   1.329 +		track[i].set_value(val[i], tmsec);
   1.330 +	}
   1.331 +}
   1.332 +
   1.333 +Vector3 Track3::get_value(long tmsec) const
   1.334 +{
   1.335 +	return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
   1.336 +}
   1.337 +
   1.338 +Vector3 Track3::operator ()(long tmsec) const
   1.339 +{
   1.340 +	return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
   1.341 +}
   1.342 +
   1.343 +
   1.344 +static inline anm_interpolator track_interpolator(Interp in)
   1.345 +{
   1.346 +	switch(in) {
   1.347 +	case INTERP_STEP:
   1.348 +		return ANM_INTERP_STEP;
   1.349 +	case INTERP_LINEAR:
   1.350 +		return ANM_INTERP_LINEAR;
   1.351 +	case INTERP_CUBIC:
   1.352 +		return ANM_INTERP_CUBIC;
   1.353 +	}
   1.354 +
   1.355 +	assert(0);
   1.356 +	return ANM_INTERP_STEP;
   1.357 +}
   1.358 +
   1.359 +static inline anm_extrapolator track_extrapolator(Extrap ex)
   1.360 +{
   1.361 +	switch(ex) {
   1.362 +	case EXTRAP_EXTEND:
   1.363 +		return ANM_EXTRAP_EXTEND;
   1.364 +	case EXTRAP_CLAMP:
   1.365 +		return ANM_EXTRAP_CLAMP;
   1.366 +	case EXTRAP_REPEAT:
   1.367 +		return ANM_EXTRAP_REPEAT;
   1.368 +	}
   1.369 +
   1.370 +	assert(0);
   1.371 +	return ANM_EXTRAP_EXTEND;
   1.372 +}
   1.373 +