goat3d

view src/xform_node.cc @ 67:8970ca3d55e0

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Apr 2014 08:01:37 +0300
parents 3751aabbc5b3 dad392c710df
children 8ecaf9cd3ce7
line source
1 /*
2 goat3d - 3D scene, character, and animation file format library.
3 Copyright (C) 2013-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <assert.h>
19 #include <algorithm>
20 #include "xform_node.h"
21 #include "anim/anim.h"
22 #include "anim/track.h"
24 using namespace g3dimpl;
26 static inline anm_interpolator track_interpolator(Interp in);
27 static inline anm_extrapolator track_extrapolator(Extrap ex);
29 XFormNode::XFormNode()
30 {
31 anm = new anm_node;
32 anm_init_node(anm);
33 parent = 0;
35 // TODO read them from anm to get the correct initial values
36 interp = INTERP_LINEAR;
37 extrap = EXTRAP_EXTEND;
38 }
40 XFormNode::~XFormNode()
41 {
42 anm_destroy_node(anm);
43 delete anm;
44 }
46 struct anm_node *XFormNode::get_libanim_node() const
47 {
48 return anm;
49 }
51 void XFormNode::set_name(const char *name)
52 {
53 anm_set_node_name(anm, name);
54 }
56 const char *XFormNode::get_name() const
57 {
58 return anm_get_node_name(anm);
59 }
61 void XFormNode::set_interpolator(Interp in)
62 {
63 anm_set_interpolator(anm, track_interpolator(in));
64 interp = in;
65 }
67 Interp XFormNode::get_interpolator() const
68 {
69 return interp;
70 }
72 void XFormNode::set_extrapolator(Extrap ex)
73 {
74 anm_set_extrapolator(anm, track_extrapolator(ex));
75 extrap = ex;
76 }
78 Extrap XFormNode::get_extrapolator() const
79 {
80 return extrap;
81 }
83 XFormNode *XFormNode::get_parent()
84 {
85 return parent;
86 }
88 const XFormNode *XFormNode::get_parent() const
89 {
90 return parent;
91 }
93 void XFormNode::add_child(XFormNode *child)
94 {
95 if(!child || child == this) return;
97 children.push_back(child);
98 anm_link_node(anm, child->anm);
99 child->parent = this;
100 }
102 void XFormNode::remove_child(XFormNode *child)
103 {
104 std::vector<XFormNode*>::iterator it;
105 it = std::find(children.begin(), children.end(), child);
106 if(it != children.end()) {
107 children.erase(it);
108 anm_unlink_node(anm, child->anm);
110 if(child->parent == this) {
111 child->parent = 0;
112 }
113 }
114 }
116 int XFormNode::get_children_count() const
117 {
118 return (int)children.size();
119 }
121 XFormNode *XFormNode::get_child(int idx)
122 {
123 if(idx >= 0 && idx < get_children_count()) {
124 return children[idx];
125 }
126 return 0;
127 }
129 const XFormNode *XFormNode::get_child(int idx) const
130 {
131 if(idx >= 0 && idx < get_children_count()) {
132 return children[idx];
133 }
134 return 0;
135 }
138 void XFormNode::use_animation(int idx)
139 {
140 if(idx >= 0) {
141 anm_use_animation(anm, idx);
142 }
143 }
145 void XFormNode::use_animation(const char *name)
146 {
147 anm_use_animation(anm, anm_find_animation(anm, name));
148 }
150 void XFormNode::use_animation(int aidx, int bidx, float t)
151 {
152 anm_use_animations(anm, aidx, bidx, t);
153 }
155 void XFormNode::use_animation(const char *aname, const char *bname, float t)
156 {
157 int aidx = anm_find_animation(anm, aname);
158 int bidx = anm_find_animation(anm, bname);
160 if(aidx == -1) {
161 use_animation(bidx);
162 }
163 if(bidx == -1) {
164 use_animation(aidx);
165 }
166 anm_use_animations(anm, aidx, bidx, t);
167 }
169 int XFormNode::get_active_animation_index(int which) const
170 {
171 return anm_get_active_animation_index(anm, which);
172 }
174 float XFormNode::get_active_animation_mix() const
175 {
176 return anm_get_active_animation_mix(anm);
177 }
179 int XFormNode::get_animation_count() const
180 {
181 return anm_get_animation_count(anm);
182 }
184 void XFormNode::add_animation(const char *name)
185 {
186 int idx = get_animation_count();
188 anm_add_animation(anm);
189 use_animation(idx);
191 if(name) {
192 set_animation_name(name);
193 }
194 }
196 void XFormNode::set_animation_name(const char *name)
197 {
198 anm_set_active_animation_name(anm, name);
199 }
201 const char *XFormNode::get_animation_name() const
202 {
203 return anm_get_active_animation_name(anm);
204 }
206 static const int track_type_base[] = {ANM_TRACK_POS_X, ANM_TRACK_ROT_X, ANM_TRACK_SCL_X};
207 static const int track_type_nelem[] = {3, 4, 3};
209 int XFormNode::get_key_count(int trackid) const
210 {
211 struct anm_animation *anim = anm_get_active_animation(anm, 0);
212 return anim->tracks[track_type_base[trackid]].count;
213 }
215 int XFormNode::get_position_key_count() const
216 {
217 return get_key_count(POSITION_TRACK);
218 }
220 int XFormNode::get_rotation_key_count() const
221 {
222 return get_key_count(ROTATION_TRACK);
223 }
225 int XFormNode::get_scaling_key_count() const
226 {
227 return get_key_count(SCALING_TRACK);
228 }
230 long XFormNode::get_key_time(int trackid, int idx) const
231 {
232 struct anm_animation *anim = anm_get_active_animation(anm, 0);
233 struct anm_keyframe *key = anm_get_keyframe(anim->tracks + track_type_base[trackid], idx);
234 return ANM_TM2MSEC(key->time);
235 }
237 long XFormNode::get_position_key_time(int idx) const
238 {
239 return get_key_time(POSITION_TRACK, idx);
240 }
242 long XFormNode::get_rotation_key_time(int idx) const
243 {
244 return get_key_time(ROTATION_TRACK, idx);
245 }
247 long XFormNode::get_scaling_key_time(int idx) const
248 {
249 return get_key_time(SCALING_TRACK, idx);
250 }
252 int XFormNode::get_key_value(int trackid, int idx, float *val) const
253 {
254 struct anm_animation *anim = anm_get_active_animation(anm, 0);
256 int nelem = track_type_nelem[trackid];
257 for(int i=0; i<nelem; i++) {
258 struct anm_keyframe *key = anm_get_keyframe(anim->tracks + track_type_base[trackid] + i, idx);
259 val[i] = key->val;
260 }
261 return nelem;
262 }
264 Vector3 XFormNode::get_position_key_value(int idx) const
265 {
266 float val[3];
267 get_key_value(POSITION_TRACK, idx, val);
268 return Vector3(val[0], val[1], val[2]);
269 }
271 Quaternion XFormNode::get_rotation_key_value(int idx) const
272 {
273 float val[4];
274 get_key_value(ROTATION_TRACK, idx, val);
275 return Quaternion(val[3], val[0], val[1], val[2]);
276 }
278 Vector3 XFormNode::get_scaling_key_value(int idx) const
279 {
280 float val[3];
281 get_key_value(SCALING_TRACK, idx, val);
282 return Vector3(val[0], val[1], val[2]);
283 }
285 void XFormNode::set_position(const Vector3 &pos, long tmsec)
286 {
287 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
288 }
290 Vector3 XFormNode::get_node_position(long tmsec) const
291 {
292 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
293 return Vector3(p.x, p.y, p.z);
294 }
296 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
297 {
298 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
299 }
301 Quaternion XFormNode::get_node_rotation(long tmsec) const
302 {
303 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
304 return Quaternion(q.w, q.x, q.y, q.z);
305 }
307 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
308 {
309 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
310 }
312 Vector3 XFormNode::get_node_scaling(long tmsec) const
313 {
314 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
315 return Vector3(s.x, s.y, s.z);
316 }
318 // these take hierarchy into account
319 Vector3 XFormNode::get_position(long tmsec) const
320 {
321 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
322 return Vector3(v.x, v.y, v.z);
323 }
325 Quaternion XFormNode::get_rotation(long tmsec) const
326 {
327 quat_t q = anm_get_rotation(anm, tmsec);
328 return Quaternion(q.w, q.x, q.y, q.z);
329 }
331 Vector3 XFormNode::get_scaling(long tmsec) const
332 {
333 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
334 return Vector3(v.x, v.y, v.z);
335 }
337 void XFormNode::set_pivot(const Vector3 &pivot)
338 {
339 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
340 }
342 Vector3 XFormNode::get_pivot() const
343 {
344 vec3_t p = anm_get_pivot(anm);
345 return Vector3(p.x, p.y, p.z);
346 }
348 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
349 {
350 local_matrix = mat;
351 }
353 const Matrix4x4 &XFormNode::get_local_matrix() const
354 {
355 return local_matrix;
356 }
358 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
359 {
360 bone_matrix = bmat;
361 }
363 const Matrix4x4 &XFormNode::get_bone_matrix() const
364 {
365 return bone_matrix;
366 }
368 #define FOO
370 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
371 {
372 anm_time_t tm = ANM_MSEC2TM(tmsec);
374 if(mat) {
375 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
376 #ifdef FOO
377 *mat = local_matrix * *mat;
378 #else
379 *mat = *mat * local_matrix;
380 #endif
381 }
382 if(inv_mat) {
383 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
384 }
385 }
387 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
388 {
389 anm_time_t tm = ANM_MSEC2TM(tmsec);
391 if(mat) {
392 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
393 #ifdef FOO
394 *mat = local_matrix * *mat;
395 #else
396 *mat = *mat * local_matrix;
397 #endif
398 }
399 if(inv_mat) {
400 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
401 }
402 }
405 // ---- Track ----
407 Track::Track()
408 {
409 trk = new anm_track;
410 anm_init_track(trk);
411 }
413 Track::~Track()
414 {
415 anm_destroy_track(trk);
416 delete trk;
417 }
419 Track::Track(const Track &rhs)
420 {
421 trk = new anm_track;
422 anm_init_track(trk);
423 anm_copy_track(trk, rhs.trk);
424 interp = rhs.interp;
425 extrap = rhs.extrap;
426 }
428 Track &Track::operator =(const Track &rhs)
429 {
430 if(&rhs == this) {
431 return *this;
432 }
434 anm_copy_track(trk, rhs.trk);
435 interp = rhs.interp;
436 extrap = rhs.extrap;
437 return *this;
438 }
441 void Track::set_interpolator(Interp in)
442 {
443 anm_set_track_interpolator(trk, track_interpolator(in));
444 interp = in;
445 }
447 Interp Track::get_interpolator() const
448 {
449 return interp;
450 }
452 void Track::set_extrapolator(Extrap ex)
453 {
454 anm_set_track_extrapolator(trk, track_extrapolator(ex));
455 extrap = ex;
456 }
458 Extrap Track::get_extrapolator() const
459 {
460 return extrap;
461 }
463 void Track::set_default(double def)
464 {
465 anm_set_track_default(trk, def);
466 }
468 void Track::set_value(float val, long tmsec)
469 {
470 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
471 }
473 float Track::get_value(long tmsec) const
474 {
475 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
476 }
478 float Track::operator ()(long tmsec) const
479 {
480 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
481 }
484 // ---- Track3 ----
486 void Track3::set_interpolator(Interp in)
487 {
488 for(int i=0; i<3; i++) {
489 track[i].set_interpolator(in);
490 }
491 }
493 Interp Track3::get_interpolator() const
494 {
495 return track[0].get_interpolator();
496 }
498 void Track3::set_extrapolator(Extrap ex)
499 {
500 for(int i=0; i<3; i++) {
501 track[i].set_extrapolator(ex);
502 }
503 }
505 Extrap Track3::get_extrapolator() const
506 {
507 return track[0].get_extrapolator();
508 }
510 void Track3::set_default(const Vector3 &def)
511 {
512 for(int i=0; i<3; i++) {
513 track[i].set_default(def[i]);
514 }
515 }
517 void Track3::set_value(const Vector3 &val, long tmsec)
518 {
519 for(int i=0; i<3; i++) {
520 track[i].set_value(val[i], tmsec);
521 }
522 }
524 Vector3 Track3::get_value(long tmsec) const
525 {
526 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
527 }
529 Vector3 Track3::operator ()(long tmsec) const
530 {
531 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
532 }
535 static inline anm_interpolator track_interpolator(Interp in)
536 {
537 switch(in) {
538 case INTERP_STEP:
539 return ANM_INTERP_STEP;
540 case INTERP_LINEAR:
541 return ANM_INTERP_LINEAR;
542 case INTERP_CUBIC:
543 return ANM_INTERP_CUBIC;
544 }
546 assert(0);
547 return ANM_INTERP_STEP;
548 }
550 static inline anm_extrapolator track_extrapolator(Extrap ex)
551 {
552 switch(ex) {
553 case EXTRAP_EXTEND:
554 return ANM_EXTRAP_EXTEND;
555 case EXTRAP_CLAMP:
556 return ANM_EXTRAP_CLAMP;
557 case EXTRAP_REPEAT:
558 return ANM_EXTRAP_REPEAT;
559 }
561 assert(0);
562 return ANM_EXTRAP_EXTEND;
563 }