goat3dgfx

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