goat3dgfx

annotate src/xform_node.cc @ 17:f2da87b02fcd

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