goat3d

annotate src/xform_node.cc @ 17:1d85d7dd0038

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