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