conworlds
diff src/xform_node.cc @ 13:283cdfa7dda2
added a crapload of code from goat3dgfx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 24 Aug 2014 09:41:24 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/xform_node.cc Sun Aug 24 09:41:24 2014 +0300 1.3 @@ -0,0 +1,461 @@ 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 + parent = 0; 1.18 + 1.19 + // TODO read them from anm to get the correct initial values 1.20 + interp = INTERP_LINEAR; 1.21 + extrap = EXTRAP_EXTEND; 1.22 +} 1.23 + 1.24 +XFormNode::~XFormNode() 1.25 +{ 1.26 + anm_destroy_node(anm); 1.27 + delete anm; 1.28 +} 1.29 + 1.30 +void XFormNode::set_name(const char *name) 1.31 +{ 1.32 + anm_set_node_name(anm, name); 1.33 +} 1.34 + 1.35 +const char *XFormNode::get_name() const 1.36 +{ 1.37 + return anm_get_node_name(anm); 1.38 +} 1.39 + 1.40 +void XFormNode::set_interpolator(Interp in) 1.41 +{ 1.42 + anm_set_interpolator(anm, track_interpolator(in)); 1.43 + interp = in; 1.44 +} 1.45 + 1.46 +Interp XFormNode::get_interpolator() const 1.47 +{ 1.48 + return interp; 1.49 +} 1.50 + 1.51 +void XFormNode::set_extrapolator(Extrap ex) 1.52 +{ 1.53 + anm_set_extrapolator(anm, track_extrapolator(ex)); 1.54 + extrap = ex; 1.55 +} 1.56 + 1.57 +Extrap XFormNode::get_extrapolator() const 1.58 +{ 1.59 + return extrap; 1.60 +} 1.61 + 1.62 +XFormNode *XFormNode::get_parent() 1.63 +{ 1.64 + return parent; 1.65 +} 1.66 + 1.67 +const XFormNode *XFormNode::get_parent() const 1.68 +{ 1.69 + return parent; 1.70 +} 1.71 + 1.72 +void XFormNode::add_child(XFormNode *child) 1.73 +{ 1.74 + children.push_back(child); 1.75 + anm_link_node(anm, child->anm); 1.76 + child->parent = this; 1.77 +} 1.78 + 1.79 +void XFormNode::remove_child(XFormNode *child) 1.80 +{ 1.81 + std::vector<XFormNode*>::iterator it; 1.82 + it = std::find(children.begin(), children.end(), child); 1.83 + if(it != children.end()) { 1.84 + children.erase(it); 1.85 + anm_unlink_node(anm, child->anm); 1.86 + 1.87 + if(child->parent == this) { 1.88 + child->parent = 0; 1.89 + } 1.90 + } 1.91 +} 1.92 + 1.93 +int XFormNode::get_children_count() const 1.94 +{ 1.95 + return (int)children.size(); 1.96 +} 1.97 + 1.98 +XFormNode *XFormNode::get_child(int idx) 1.99 +{ 1.100 + if(idx >= 0 && idx < get_children_count()) { 1.101 + return children[idx]; 1.102 + } 1.103 + return 0; 1.104 +} 1.105 + 1.106 +const XFormNode *XFormNode::get_child(int idx) const 1.107 +{ 1.108 + if(idx >= 0 && idx < get_children_count()) { 1.109 + return children[idx]; 1.110 + } 1.111 + return 0; 1.112 +} 1.113 + 1.114 + 1.115 +void XFormNode::use_animation(int idx) 1.116 +{ 1.117 + if(idx >= 0) { 1.118 + anm_use_animation(anm, idx); 1.119 + } 1.120 +} 1.121 + 1.122 +void XFormNode::use_animation(const char *name) 1.123 +{ 1.124 + anm_use_animation(anm, anm_find_animation(anm, name)); 1.125 +} 1.126 + 1.127 +void XFormNode::use_animation(int aidx, int bidx, float t) 1.128 +{ 1.129 + anm_use_animations(anm, aidx, bidx, t); 1.130 +} 1.131 + 1.132 +void XFormNode::use_animation(const char *aname, const char *bname, float t) 1.133 +{ 1.134 + int aidx = anm_find_animation(anm, aname); 1.135 + int bidx = anm_find_animation(anm, bname); 1.136 + 1.137 + if(aidx == -1) { 1.138 + use_animation(bidx); 1.139 + } 1.140 + if(bidx == -1) { 1.141 + use_animation(aidx); 1.142 + } 1.143 + anm_use_animations(anm, aidx, bidx, t); 1.144 +} 1.145 + 1.146 +int XFormNode::get_active_animation_index(int which) const 1.147 +{ 1.148 + return anm_get_active_animation_index(anm, which); 1.149 +} 1.150 + 1.151 +float XFormNode::get_active_animation_mix() const 1.152 +{ 1.153 + return anm_get_active_animation_mix(anm); 1.154 +} 1.155 + 1.156 +int XFormNode::get_animation_count() const 1.157 +{ 1.158 + return anm_get_animation_count(anm); 1.159 +} 1.160 + 1.161 +void XFormNode::add_animation(const char *name) 1.162 +{ 1.163 + int idx = get_animation_count(); 1.164 + 1.165 + anm_add_animation(anm); 1.166 + use_animation(idx); 1.167 + 1.168 + if(name) { 1.169 + set_animation_name(name); 1.170 + } 1.171 +} 1.172 + 1.173 +void XFormNode::set_animation_name(const char *name) 1.174 +{ 1.175 + anm_set_active_animation_name(anm, name); 1.176 +} 1.177 + 1.178 +const char *XFormNode::get_animation_name() const 1.179 +{ 1.180 + return anm_get_active_animation_name(anm); 1.181 +} 1.182 + 1.183 + 1.184 + 1.185 +void XFormNode::set_position(const Vector3 &pos, long tmsec) 1.186 +{ 1.187 + anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec)); 1.188 +} 1.189 + 1.190 +Vector3 XFormNode::get_node_position(long tmsec) const 1.191 +{ 1.192 + vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec)); 1.193 + return Vector3(p.x, p.y, p.z); 1.194 +} 1.195 + 1.196 +void XFormNode::set_rotation(const Quaternion &quat, long tmsec) 1.197 +{ 1.198 + anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec)); 1.199 +} 1.200 + 1.201 +Quaternion XFormNode::get_node_rotation(long tmsec) const 1.202 +{ 1.203 + quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec)); 1.204 + return Quaternion(q.w, q.x, q.y, q.z); 1.205 +} 1.206 + 1.207 +void XFormNode::set_scaling(const Vector3 &pos, long tmsec) 1.208 +{ 1.209 + anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec)); 1.210 +} 1.211 + 1.212 +Vector3 XFormNode::get_node_scaling(long tmsec) const 1.213 +{ 1.214 + vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec)); 1.215 + return Vector3(s.x, s.y, s.z); 1.216 +} 1.217 + 1.218 +// these take hierarchy into account 1.219 +Vector3 XFormNode::get_position(long tmsec) const 1.220 +{ 1.221 + vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec)); 1.222 + return Vector3(v.x, v.y, v.z); 1.223 +} 1.224 + 1.225 +Quaternion XFormNode::get_rotation(long tmsec) const 1.226 +{ 1.227 + quat_t q = anm_get_rotation(anm, tmsec); 1.228 + return Quaternion(q.w, q.x, q.y, q.z); 1.229 +} 1.230 + 1.231 +Vector3 XFormNode::get_scaling(long tmsec) const 1.232 +{ 1.233 + vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec)); 1.234 + return Vector3(v.x, v.y, v.z); 1.235 +} 1.236 + 1.237 +void XFormNode::set_pivot(const Vector3 &pivot) 1.238 +{ 1.239 + anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z)); 1.240 +} 1.241 + 1.242 +Vector3 XFormNode::get_pivot() const 1.243 +{ 1.244 + vec3_t p = anm_get_pivot(anm); 1.245 + return Vector3(p.x, p.y, p.z); 1.246 +} 1.247 + 1.248 +void XFormNode::set_local_matrix(const Matrix4x4 &mat) 1.249 +{ 1.250 + local_matrix = mat; 1.251 +} 1.252 + 1.253 +const Matrix4x4 &XFormNode::get_local_matrix() const 1.254 +{ 1.255 + return local_matrix; 1.256 +} 1.257 + 1.258 +void XFormNode::set_bone_matrix(const Matrix4x4 &bmat) 1.259 +{ 1.260 + bone_matrix = bmat; 1.261 +} 1.262 + 1.263 +const Matrix4x4 &XFormNode::get_bone_matrix() const 1.264 +{ 1.265 + return bone_matrix; 1.266 +} 1.267 + 1.268 +#define FOO 1.269 + 1.270 +void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const 1.271 +{ 1.272 + anm_time_t tm = ANM_MSEC2TM(tmsec); 1.273 + 1.274 + if(mat) { 1.275 + anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm); 1.276 +#ifdef FOO 1.277 + *mat = local_matrix * *mat; 1.278 +#else 1.279 + *mat = *mat * local_matrix; 1.280 +#endif 1.281 + } 1.282 + if(inv_mat) { 1.283 + anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm); 1.284 + } 1.285 +} 1.286 + 1.287 +void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const 1.288 +{ 1.289 + anm_time_t tm = ANM_MSEC2TM(tmsec); 1.290 + 1.291 + if(mat) { 1.292 + anm_get_matrix(anm, (scalar_t(*)[4])mat, tm); 1.293 +#ifdef FOO 1.294 + *mat = local_matrix * *mat; 1.295 +#else 1.296 + *mat = *mat * local_matrix; 1.297 +#endif 1.298 + } 1.299 + if(inv_mat) { 1.300 + anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm); 1.301 + } 1.302 +} 1.303 + 1.304 + 1.305 +// ---- Track ---- 1.306 + 1.307 +Track::Track() 1.308 +{ 1.309 + trk = new anm_track; 1.310 + anm_init_track(trk); 1.311 +} 1.312 + 1.313 +Track::~Track() 1.314 +{ 1.315 + anm_destroy_track(trk); 1.316 + delete trk; 1.317 +} 1.318 + 1.319 +Track::Track(const Track &rhs) 1.320 +{ 1.321 + trk = new anm_track; 1.322 + anm_init_track(trk); 1.323 + anm_copy_track(trk, rhs.trk); 1.324 + interp = rhs.interp; 1.325 + extrap = rhs.extrap; 1.326 +} 1.327 + 1.328 +Track &Track::operator =(const Track &rhs) 1.329 +{ 1.330 + if(&rhs == this) { 1.331 + return *this; 1.332 + } 1.333 + 1.334 + anm_copy_track(trk, rhs.trk); 1.335 + interp = rhs.interp; 1.336 + extrap = rhs.extrap; 1.337 + return *this; 1.338 +} 1.339 + 1.340 + 1.341 +void Track::set_interpolator(Interp in) 1.342 +{ 1.343 + anm_set_track_interpolator(trk, track_interpolator(in)); 1.344 + interp = in; 1.345 +} 1.346 + 1.347 +Interp Track::get_interpolator() const 1.348 +{ 1.349 + return interp; 1.350 +} 1.351 + 1.352 +void Track::set_extrapolator(Extrap ex) 1.353 +{ 1.354 + anm_set_track_extrapolator(trk, track_extrapolator(ex)); 1.355 + extrap = ex; 1.356 +} 1.357 + 1.358 +Extrap Track::get_extrapolator() const 1.359 +{ 1.360 + return extrap; 1.361 +} 1.362 + 1.363 +void Track::set_default(double def) 1.364 +{ 1.365 + anm_set_track_default(trk, def); 1.366 +} 1.367 + 1.368 +void Track::set_value(float val, long tmsec) 1.369 +{ 1.370 + anm_set_value(trk, ANM_MSEC2TM(tmsec), val); 1.371 +} 1.372 + 1.373 +float Track::get_value(long tmsec) const 1.374 +{ 1.375 + return anm_get_value(trk, ANM_MSEC2TM(tmsec)); 1.376 +} 1.377 + 1.378 +float Track::operator ()(long tmsec) const 1.379 +{ 1.380 + return anm_get_value(trk, ANM_MSEC2TM(tmsec)); 1.381 +} 1.382 + 1.383 + 1.384 +// ---- Track3 ---- 1.385 + 1.386 +void Track3::set_interpolator(Interp in) 1.387 +{ 1.388 + for(int i=0; i<3; i++) { 1.389 + track[i].set_interpolator(in); 1.390 + } 1.391 +} 1.392 + 1.393 +Interp Track3::get_interpolator() const 1.394 +{ 1.395 + return track[0].get_interpolator(); 1.396 +} 1.397 + 1.398 +void Track3::set_extrapolator(Extrap ex) 1.399 +{ 1.400 + for(int i=0; i<3; i++) { 1.401 + track[i].set_extrapolator(ex); 1.402 + } 1.403 +} 1.404 + 1.405 +Extrap Track3::get_extrapolator() const 1.406 +{ 1.407 + return track[0].get_extrapolator(); 1.408 +} 1.409 + 1.410 +void Track3::set_default(const Vector3 &def) 1.411 +{ 1.412 + for(int i=0; i<3; i++) { 1.413 + track[i].set_default(def[i]); 1.414 + } 1.415 +} 1.416 + 1.417 +void Track3::set_value(const Vector3 &val, long tmsec) 1.418 +{ 1.419 + for(int i=0; i<3; i++) { 1.420 + track[i].set_value(val[i], tmsec); 1.421 + } 1.422 +} 1.423 + 1.424 +Vector3 Track3::get_value(long tmsec) const 1.425 +{ 1.426 + return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec)); 1.427 +} 1.428 + 1.429 +Vector3 Track3::operator ()(long tmsec) const 1.430 +{ 1.431 + return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec)); 1.432 +} 1.433 + 1.434 + 1.435 +static inline anm_interpolator track_interpolator(Interp in) 1.436 +{ 1.437 + switch(in) { 1.438 + case INTERP_STEP: 1.439 + return ANM_INTERP_STEP; 1.440 + case INTERP_LINEAR: 1.441 + return ANM_INTERP_LINEAR; 1.442 + case INTERP_CUBIC: 1.443 + return ANM_INTERP_CUBIC; 1.444 + } 1.445 + 1.446 + assert(0); 1.447 + return ANM_INTERP_STEP; 1.448 +} 1.449 + 1.450 +static inline anm_extrapolator track_extrapolator(Extrap ex) 1.451 +{ 1.452 + switch(ex) { 1.453 + case EXTRAP_EXTEND: 1.454 + return ANM_EXTRAP_EXTEND; 1.455 + case EXTRAP_CLAMP: 1.456 + return ANM_EXTRAP_CLAMP; 1.457 + case EXTRAP_REPEAT: 1.458 + return ANM_EXTRAP_REPEAT; 1.459 + } 1.460 + 1.461 + assert(0); 1.462 + return ANM_EXTRAP_EXTEND; 1.463 +} 1.464 +