vrshoot

annotate src/xform_node.cc @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
rev   line source
nuclear@0 1 #include <assert.h>
nuclear@0 2 #include <algorithm>
nuclear@0 3 #include "xform_node.h"
nuclear@0 4 #include "anim/anim.h"
nuclear@0 5 #include "anim/track.h"
nuclear@0 6
nuclear@0 7 static inline anm_interpolator track_interpolator(Interp in);
nuclear@0 8 static inline anm_extrapolator track_extrapolator(Extrap ex);
nuclear@0 9
nuclear@0 10 XFormNode::XFormNode()
nuclear@0 11 {
nuclear@0 12 type = NODE_NULL;
nuclear@0 13
nuclear@0 14 anm = new anm_node;
nuclear@0 15 anm_init_node(anm);
nuclear@0 16
nuclear@0 17 parent = 0;
nuclear@0 18 }
nuclear@0 19
nuclear@0 20 XFormNode::~XFormNode()
nuclear@0 21 {
nuclear@0 22 anm_destroy_node(anm);
nuclear@0 23 delete anm;
nuclear@0 24 }
nuclear@0 25
nuclear@0 26 void XFormNode::set_name(const char *name)
nuclear@0 27 {
nuclear@0 28 anm_set_node_name(anm, name);
nuclear@0 29 }
nuclear@0 30
nuclear@0 31 const char *XFormNode::get_name() const
nuclear@0 32 {
nuclear@0 33 return anm_get_node_name(anm);
nuclear@0 34 }
nuclear@0 35
nuclear@0 36 unsigned int XFormNode::get_type() const
nuclear@0 37 {
nuclear@0 38 return type;
nuclear@0 39 }
nuclear@0 40
nuclear@0 41 void XFormNode::set_interpolator(Interp in)
nuclear@0 42 {
nuclear@0 43 anm_set_interpolator(anm, track_interpolator(in));
nuclear@0 44 interp = in;
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 Interp XFormNode::get_interpolator() const
nuclear@0 48 {
nuclear@0 49 return interp;
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 void XFormNode::set_extrapolator(Extrap ex)
nuclear@0 53 {
nuclear@0 54 anm_set_extrapolator(anm, track_extrapolator(ex));
nuclear@0 55 extrap = ex;
nuclear@0 56 }
nuclear@0 57
nuclear@0 58 Extrap XFormNode::get_extrapolator() const
nuclear@0 59 {
nuclear@0 60 return extrap;
nuclear@0 61 }
nuclear@0 62
nuclear@0 63 void XFormNode::add_child(XFormNode *child)
nuclear@0 64 {
nuclear@0 65 children.push_back(child);
nuclear@0 66 anm_link_node(anm, child->anm);
nuclear@0 67
nuclear@0 68 child->parent = this;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 void XFormNode::remove_child(XFormNode *child)
nuclear@0 72 {
nuclear@0 73 std::vector<XFormNode*>::iterator it;
nuclear@0 74 it = std::find(children.begin(), children.end(), child);
nuclear@0 75 if(it != children.end()) {
nuclear@0 76 children.erase(it);
nuclear@0 77 anm_unlink_node(anm, child->anm);
nuclear@0 78
nuclear@0 79 if(child->parent == this) {
nuclear@0 80 child->parent = 0;
nuclear@0 81 }
nuclear@0 82 }
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 int XFormNode::get_children_count() const
nuclear@0 86 {
nuclear@0 87 return (int)children.size();
nuclear@0 88 }
nuclear@0 89
nuclear@0 90 XFormNode *XFormNode::get_child(int idx)
nuclear@0 91 {
nuclear@0 92 if(idx >= 0 && idx < get_children_count()) {
nuclear@0 93 return children[idx];
nuclear@0 94 }
nuclear@0 95 return 0;
nuclear@0 96 }
nuclear@0 97
nuclear@0 98 const XFormNode *XFormNode::get_child(int idx) const
nuclear@0 99 {
nuclear@0 100 if(idx >= 0 && idx < get_children_count()) {
nuclear@0 101 return children[idx];
nuclear@0 102 }
nuclear@0 103 return 0;
nuclear@0 104 }
nuclear@0 105
nuclear@0 106 XFormNode *XFormNode::get_parent()
nuclear@0 107 {
nuclear@0 108 return parent;
nuclear@0 109 }
nuclear@0 110
nuclear@0 111 const XFormNode *XFormNode::get_parent() const
nuclear@0 112 {
nuclear@0 113 return parent;
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 void XFormNode::clear_xform()
nuclear@0 117 {
nuclear@0 118 anm_clear(anm);
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 void XFormNode::set_position(const Vector3 &pos, long tmsec)
nuclear@0 122 {
nuclear@0 123 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
nuclear@0 124 }
nuclear@0 125
nuclear@0 126 Vector3 XFormNode::get_node_position(long tmsec) const
nuclear@0 127 {
nuclear@0 128 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
nuclear@0 129 return Vector3(p.x, p.y, p.z);
nuclear@0 130 }
nuclear@0 131
nuclear@0 132 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
nuclear@0 133 {
nuclear@0 134 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 Quaternion XFormNode::get_node_rotation(long tmsec) const
nuclear@0 138 {
nuclear@0 139 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
nuclear@0 140 return Quaternion(q.w, q.x, q.y, q.z);
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
nuclear@0 144 {
nuclear@0 145 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
nuclear@0 146 }
nuclear@0 147
nuclear@0 148 Vector3 XFormNode::get_node_scaling(long tmsec) const
nuclear@0 149 {
nuclear@0 150 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
nuclear@0 151 return Vector3(s.x, s.y, s.z);
nuclear@0 152 }
nuclear@0 153
nuclear@0 154 // these take hierarchy into account
nuclear@0 155 Vector3 XFormNode::get_position(long tmsec) const
nuclear@0 156 {
nuclear@0 157 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
nuclear@0 158 return Vector3(v.x, v.y, v.z);
nuclear@0 159 }
nuclear@0 160
nuclear@0 161 Quaternion XFormNode::get_rotation(long tmsec) const
nuclear@0 162 {
nuclear@0 163 quat_t q = anm_get_rotation(anm, tmsec);
nuclear@0 164 return Quaternion(q.w, q.x, q.y, q.z);
nuclear@0 165 }
nuclear@0 166
nuclear@0 167 Vector3 XFormNode::get_scaling(long tmsec) const
nuclear@0 168 {
nuclear@0 169 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
nuclear@0 170 return Vector3(v.x, v.y, v.z);
nuclear@0 171 }
nuclear@0 172
nuclear@0 173 void XFormNode::set_pivot(const Vector3 &pivot)
nuclear@0 174 {
nuclear@0 175 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
nuclear@0 176 }
nuclear@0 177
nuclear@0 178 Vector3 XFormNode::get_pivot() const
nuclear@0 179 {
nuclear@0 180 vec3_t p = anm_get_pivot(anm);
nuclear@0 181 return Vector3(p.x, p.y, p.z);
nuclear@0 182 }
nuclear@0 183
nuclear@0 184 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
nuclear@0 185 {
nuclear@0 186 local_matrix = mat;
nuclear@0 187 }
nuclear@0 188
nuclear@0 189 const Matrix4x4 &XFormNode::get_local_matrix() const
nuclear@0 190 {
nuclear@0 191 return local_matrix;
nuclear@0 192 }
nuclear@0 193
nuclear@0 194 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
nuclear@0 195 {
nuclear@0 196 bone_matrix = bmat;
nuclear@0 197 }
nuclear@0 198
nuclear@0 199 const Matrix4x4 &XFormNode::get_bone_matrix() const
nuclear@0 200 {
nuclear@0 201 return bone_matrix;
nuclear@0 202 }
nuclear@0 203
nuclear@0 204 #define FOO
nuclear@0 205
nuclear@0 206 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
nuclear@0 207 {
nuclear@0 208 anm_time_t tm = ANM_MSEC2TM(tmsec);
nuclear@0 209
nuclear@0 210 if(mat) {
nuclear@0 211 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
nuclear@0 212 #ifdef FOO
nuclear@0 213 *mat = local_matrix * *mat;
nuclear@0 214 #else
nuclear@0 215 *mat = *mat * local_matrix;
nuclear@0 216 #endif
nuclear@0 217 }
nuclear@0 218 if(inv_mat) {
nuclear@0 219 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
nuclear@0 220 }
nuclear@0 221 }
nuclear@0 222
nuclear@0 223 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
nuclear@0 224 {
nuclear@0 225 anm_time_t tm = ANM_MSEC2TM(tmsec);
nuclear@0 226
nuclear@0 227 if(mat) {
nuclear@0 228 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
nuclear@0 229 #ifdef FOO
nuclear@0 230 *mat = local_matrix * *mat;
nuclear@0 231 #else
nuclear@0 232 *mat = *mat * local_matrix;
nuclear@0 233 #endif
nuclear@0 234 }
nuclear@0 235 if(inv_mat) {
nuclear@0 236 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
nuclear@0 237 }
nuclear@0 238 }
nuclear@0 239
nuclear@0 240
nuclear@0 241 std::list<XFormNode*> XFormNode::get_all_nodes(unsigned int type_mask)
nuclear@0 242 {
nuclear@0 243 std::list<XFormNode*> res;
nuclear@0 244
nuclear@0 245 if(type & type_mask) {
nuclear@0 246 res.push_back(this);
nuclear@0 247 }
nuclear@0 248
nuclear@0 249 int nchildren = get_children_count();
nuclear@0 250 for(int i=0; i<nchildren; i++) {
nuclear@0 251 std::list<XFormNode*> sublist = get_child(i)->get_all_nodes(type_mask);
nuclear@0 252 res.splice(res.end(), sublist);
nuclear@0 253 }
nuclear@0 254
nuclear@0 255 return res;
nuclear@0 256 }
nuclear@0 257
nuclear@0 258 std::list<const XFormNode*> XFormNode::get_all_nodes(unsigned int type_mask) const
nuclear@0 259 {
nuclear@0 260 std::list<const XFormNode*> res;
nuclear@0 261
nuclear@0 262 if(type & type_mask) {
nuclear@0 263 res.push_back(this);
nuclear@0 264 }
nuclear@0 265
nuclear@0 266 int nchildren = get_children_count();
nuclear@0 267 for(int i=0; i<nchildren; i++) {
nuclear@0 268 std::list<const XFormNode*> sublist = get_child(i)->get_all_nodes(type_mask);
nuclear@0 269 res.splice(res.end(), sublist);
nuclear@0 270 }
nuclear@0 271
nuclear@0 272 return res;
nuclear@0 273 }
nuclear@0 274
nuclear@0 275 void XFormNode::draw(long msec) const
nuclear@0 276 {
nuclear@0 277 int nchild = get_children_count();
nuclear@0 278 for(int i=0; i<nchild; i++) {
nuclear@0 279 get_child(i)->draw(msec);
nuclear@0 280 }
nuclear@0 281 }
nuclear@0 282
nuclear@0 283 // ---- Track ----
nuclear@0 284
nuclear@0 285 Track::Track()
nuclear@0 286 {
nuclear@0 287 trk = new anm_track;
nuclear@0 288 anm_init_track(trk);
nuclear@0 289 }
nuclear@0 290
nuclear@0 291 Track::~Track()
nuclear@0 292 {
nuclear@0 293 anm_destroy_track(trk);
nuclear@0 294 delete trk;
nuclear@0 295 }
nuclear@0 296
nuclear@0 297 Track::Track(const Track &rhs)
nuclear@0 298 {
nuclear@0 299 trk = new anm_track;
nuclear@0 300 anm_init_track(trk);
nuclear@0 301 anm_copy_track(trk, rhs.trk);
nuclear@0 302 interp = rhs.interp;
nuclear@0 303 extrap = rhs.extrap;
nuclear@0 304 }
nuclear@0 305
nuclear@0 306 Track &Track::operator =(const Track &rhs)
nuclear@0 307 {
nuclear@0 308 if(&rhs == this) {
nuclear@0 309 return *this;
nuclear@0 310 }
nuclear@0 311
nuclear@0 312 anm_copy_track(trk, rhs.trk);
nuclear@0 313 interp = rhs.interp;
nuclear@0 314 extrap = rhs.extrap;
nuclear@0 315 return *this;
nuclear@0 316 }
nuclear@0 317
nuclear@0 318
nuclear@0 319 void Track::set_interpolator(Interp in)
nuclear@0 320 {
nuclear@0 321 anm_set_track_interpolator(trk, track_interpolator(in));
nuclear@0 322 interp = in;
nuclear@0 323 }
nuclear@0 324
nuclear@0 325 Interp Track::get_interpolator() const
nuclear@0 326 {
nuclear@0 327 return interp;
nuclear@0 328 }
nuclear@0 329
nuclear@0 330 void Track::set_extrapolator(Extrap ex)
nuclear@0 331 {
nuclear@0 332 anm_set_track_extrapolator(trk, track_extrapolator(ex));
nuclear@0 333 extrap = ex;
nuclear@0 334 }
nuclear@0 335
nuclear@0 336 Extrap Track::get_extrapolator() const
nuclear@0 337 {
nuclear@0 338 return extrap;
nuclear@0 339 }
nuclear@0 340
nuclear@0 341 void Track::set_default(double def)
nuclear@0 342 {
nuclear@0 343 anm_set_track_default(trk, def);
nuclear@0 344 }
nuclear@0 345
nuclear@0 346 void Track::set_value(float val, long tmsec)
nuclear@0 347 {
nuclear@0 348 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
nuclear@0 349 }
nuclear@0 350
nuclear@0 351 float Track::get_value(long tmsec) const
nuclear@0 352 {
nuclear@0 353 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
nuclear@0 354 }
nuclear@0 355
nuclear@0 356 float Track::operator ()(long tmsec) const
nuclear@0 357 {
nuclear@0 358 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
nuclear@0 359 }
nuclear@0 360
nuclear@0 361
nuclear@0 362 // ---- Track3 ----
nuclear@0 363
nuclear@0 364 void Track3::set_interpolator(Interp in)
nuclear@0 365 {
nuclear@0 366 for(int i=0; i<3; i++) {
nuclear@0 367 track[i].set_interpolator(in);
nuclear@0 368 }
nuclear@0 369 }
nuclear@0 370
nuclear@0 371 Interp Track3::get_interpolator() const
nuclear@0 372 {
nuclear@0 373 return track[0].get_interpolator();
nuclear@0 374 }
nuclear@0 375
nuclear@0 376 void Track3::set_extrapolator(Extrap ex)
nuclear@0 377 {
nuclear@0 378 for(int i=0; i<3; i++) {
nuclear@0 379 track[i].set_extrapolator(ex);
nuclear@0 380 }
nuclear@0 381 }
nuclear@0 382
nuclear@0 383 Extrap Track3::get_extrapolator() const
nuclear@0 384 {
nuclear@0 385 return track[0].get_extrapolator();
nuclear@0 386 }
nuclear@0 387
nuclear@0 388 void Track3::set_default(const Vector3 &def)
nuclear@0 389 {
nuclear@0 390 for(int i=0; i<3; i++) {
nuclear@0 391 track[i].set_default(def[i]);
nuclear@0 392 }
nuclear@0 393 }
nuclear@0 394
nuclear@0 395 void Track3::set_value(const Vector3 &val, long tmsec)
nuclear@0 396 {
nuclear@0 397 for(int i=0; i<3; i++) {
nuclear@0 398 track[i].set_value(val[i], tmsec);
nuclear@0 399 }
nuclear@0 400 }
nuclear@0 401
nuclear@0 402 Vector3 Track3::get_value(long tmsec) const
nuclear@0 403 {
nuclear@0 404 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
nuclear@0 405 }
nuclear@0 406
nuclear@0 407 Vector3 Track3::operator ()(long tmsec) const
nuclear@0 408 {
nuclear@0 409 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
nuclear@0 410 }
nuclear@0 411
nuclear@0 412
nuclear@0 413 static inline anm_interpolator track_interpolator(Interp in)
nuclear@0 414 {
nuclear@0 415 switch(in) {
nuclear@0 416 case INTERP_STEP:
nuclear@0 417 return ANM_INTERP_STEP;
nuclear@0 418 case INTERP_LINEAR:
nuclear@0 419 return ANM_INTERP_LINEAR;
nuclear@0 420 case INTERP_CUBIC:
nuclear@0 421 return ANM_INTERP_CUBIC;
nuclear@0 422 }
nuclear@0 423
nuclear@0 424 assert(0);
nuclear@0 425 return ANM_INTERP_STEP;
nuclear@0 426 }
nuclear@0 427
nuclear@0 428 static inline anm_extrapolator track_extrapolator(Extrap ex)
nuclear@0 429 {
nuclear@0 430 switch(ex) {
nuclear@0 431 case EXTRAP_EXTEND:
nuclear@0 432 return ANM_EXTRAP_EXTEND;
nuclear@0 433 case EXTRAP_CLAMP:
nuclear@0 434 return ANM_EXTRAP_CLAMP;
nuclear@0 435 case EXTRAP_REPEAT:
nuclear@0 436 return ANM_EXTRAP_REPEAT;
nuclear@0 437 }
nuclear@0 438
nuclear@0 439 assert(0);
nuclear@0 440 return ANM_EXTRAP_EXTEND;
nuclear@0 441 }
nuclear@0 442