conworlds

annotate src/xform_node.cc @ 17:c814f77d177e

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