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