conworlds

view src/xform_node.cc @ 22:5f53272ff612

removed the vr wrapper and added an external dependency to libgoatvr
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 29 Aug 2014 07:44:26 +0300
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 anm = new anm_node;
13 anm_init_node(anm);
14 parent = 0;
16 // TODO read them from anm to get the correct initial values
17 interp = INTERP_LINEAR;
18 extrap = EXTRAP_EXTEND;
19 }
21 XFormNode::~XFormNode()
22 {
23 anm_destroy_node(anm);
24 delete anm;
25 }
27 void XFormNode::set_name(const char *name)
28 {
29 anm_set_node_name(anm, name);
30 }
32 const char *XFormNode::get_name() const
33 {
34 return anm_get_node_name(anm);
35 }
37 void XFormNode::set_interpolator(Interp in)
38 {
39 anm_set_interpolator(anm, track_interpolator(in));
40 interp = in;
41 }
43 Interp XFormNode::get_interpolator() const
44 {
45 return interp;
46 }
48 void XFormNode::set_extrapolator(Extrap ex)
49 {
50 anm_set_extrapolator(anm, track_extrapolator(ex));
51 extrap = ex;
52 }
54 Extrap XFormNode::get_extrapolator() const
55 {
56 return extrap;
57 }
59 XFormNode *XFormNode::get_parent()
60 {
61 return parent;
62 }
64 const XFormNode *XFormNode::get_parent() const
65 {
66 return parent;
67 }
69 void XFormNode::add_child(XFormNode *child)
70 {
71 children.push_back(child);
72 anm_link_node(anm, child->anm);
73 child->parent = this;
74 }
76 void XFormNode::remove_child(XFormNode *child)
77 {
78 std::vector<XFormNode*>::iterator it;
79 it = std::find(children.begin(), children.end(), child);
80 if(it != children.end()) {
81 children.erase(it);
82 anm_unlink_node(anm, child->anm);
84 if(child->parent == this) {
85 child->parent = 0;
86 }
87 }
88 }
90 int XFormNode::get_children_count() const
91 {
92 return (int)children.size();
93 }
95 XFormNode *XFormNode::get_child(int idx)
96 {
97 if(idx >= 0 && idx < get_children_count()) {
98 return children[idx];
99 }
100 return 0;
101 }
103 const XFormNode *XFormNode::get_child(int idx) const
104 {
105 if(idx >= 0 && idx < get_children_count()) {
106 return children[idx];
107 }
108 return 0;
109 }
112 void XFormNode::use_animation(int idx)
113 {
114 if(idx >= 0) {
115 anm_use_animation(anm, idx);
116 }
117 }
119 void XFormNode::use_animation(const char *name)
120 {
121 anm_use_animation(anm, anm_find_animation(anm, name));
122 }
124 void XFormNode::use_animation(int aidx, int bidx, float t)
125 {
126 anm_use_animations(anm, aidx, bidx, t);
127 }
129 void XFormNode::use_animation(const char *aname, const char *bname, float t)
130 {
131 int aidx = anm_find_animation(anm, aname);
132 int bidx = anm_find_animation(anm, bname);
134 if(aidx == -1) {
135 use_animation(bidx);
136 }
137 if(bidx == -1) {
138 use_animation(aidx);
139 }
140 anm_use_animations(anm, aidx, bidx, t);
141 }
143 int XFormNode::get_active_animation_index(int which) const
144 {
145 return anm_get_active_animation_index(anm, which);
146 }
148 float XFormNode::get_active_animation_mix() const
149 {
150 return anm_get_active_animation_mix(anm);
151 }
153 int XFormNode::get_animation_count() const
154 {
155 return anm_get_animation_count(anm);
156 }
158 void XFormNode::add_animation(const char *name)
159 {
160 int idx = get_animation_count();
162 anm_add_animation(anm);
163 use_animation(idx);
165 if(name) {
166 set_animation_name(name);
167 }
168 }
170 void XFormNode::set_animation_name(const char *name)
171 {
172 anm_set_active_animation_name(anm, name);
173 }
175 const char *XFormNode::get_animation_name() const
176 {
177 return anm_get_active_animation_name(anm);
178 }
182 void XFormNode::set_position(const Vector3 &pos, long tmsec)
183 {
184 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
185 }
187 Vector3 XFormNode::get_node_position(long tmsec) const
188 {
189 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
190 return Vector3(p.x, p.y, p.z);
191 }
193 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
194 {
195 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
196 }
198 Quaternion XFormNode::get_node_rotation(long tmsec) const
199 {
200 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
201 return Quaternion(q.w, q.x, q.y, q.z);
202 }
204 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
205 {
206 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
207 }
209 Vector3 XFormNode::get_node_scaling(long tmsec) const
210 {
211 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
212 return Vector3(s.x, s.y, s.z);
213 }
215 // these take hierarchy into account
216 Vector3 XFormNode::get_position(long tmsec) const
217 {
218 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
219 return Vector3(v.x, v.y, v.z);
220 }
222 Quaternion XFormNode::get_rotation(long tmsec) const
223 {
224 quat_t q = anm_get_rotation(anm, tmsec);
225 return Quaternion(q.w, q.x, q.y, q.z);
226 }
228 Vector3 XFormNode::get_scaling(long tmsec) const
229 {
230 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
231 return Vector3(v.x, v.y, v.z);
232 }
234 void XFormNode::set_pivot(const Vector3 &pivot)
235 {
236 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
237 }
239 Vector3 XFormNode::get_pivot() const
240 {
241 vec3_t p = anm_get_pivot(anm);
242 return Vector3(p.x, p.y, p.z);
243 }
245 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
246 {
247 local_matrix = mat;
248 }
250 const Matrix4x4 &XFormNode::get_local_matrix() const
251 {
252 return local_matrix;
253 }
255 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
256 {
257 bone_matrix = bmat;
258 }
260 const Matrix4x4 &XFormNode::get_bone_matrix() const
261 {
262 return bone_matrix;
263 }
265 #define FOO
267 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
268 {
269 anm_time_t tm = ANM_MSEC2TM(tmsec);
271 if(mat) {
272 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
273 #ifdef FOO
274 *mat = local_matrix * *mat;
275 #else
276 *mat = *mat * local_matrix;
277 #endif
278 }
279 if(inv_mat) {
280 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
281 }
282 }
284 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
285 {
286 anm_time_t tm = ANM_MSEC2TM(tmsec);
288 if(mat) {
289 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
290 #ifdef FOO
291 *mat = local_matrix * *mat;
292 #else
293 *mat = *mat * local_matrix;
294 #endif
295 }
296 if(inv_mat) {
297 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
298 }
299 }
302 // ---- Track ----
304 Track::Track()
305 {
306 trk = new anm_track;
307 anm_init_track(trk);
308 }
310 Track::~Track()
311 {
312 anm_destroy_track(trk);
313 delete trk;
314 }
316 Track::Track(const Track &rhs)
317 {
318 trk = new anm_track;
319 anm_init_track(trk);
320 anm_copy_track(trk, rhs.trk);
321 interp = rhs.interp;
322 extrap = rhs.extrap;
323 }
325 Track &Track::operator =(const Track &rhs)
326 {
327 if(&rhs == this) {
328 return *this;
329 }
331 anm_copy_track(trk, rhs.trk);
332 interp = rhs.interp;
333 extrap = rhs.extrap;
334 return *this;
335 }
338 void Track::set_interpolator(Interp in)
339 {
340 anm_set_track_interpolator(trk, track_interpolator(in));
341 interp = in;
342 }
344 Interp Track::get_interpolator() const
345 {
346 return interp;
347 }
349 void Track::set_extrapolator(Extrap ex)
350 {
351 anm_set_track_extrapolator(trk, track_extrapolator(ex));
352 extrap = ex;
353 }
355 Extrap Track::get_extrapolator() const
356 {
357 return extrap;
358 }
360 void Track::set_default(double def)
361 {
362 anm_set_track_default(trk, def);
363 }
365 void Track::set_value(float val, long tmsec)
366 {
367 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
368 }
370 float Track::get_value(long tmsec) const
371 {
372 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
373 }
375 float Track::operator ()(long tmsec) const
376 {
377 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
378 }
381 // ---- Track3 ----
383 void Track3::set_interpolator(Interp in)
384 {
385 for(int i=0; i<3; i++) {
386 track[i].set_interpolator(in);
387 }
388 }
390 Interp Track3::get_interpolator() const
391 {
392 return track[0].get_interpolator();
393 }
395 void Track3::set_extrapolator(Extrap ex)
396 {
397 for(int i=0; i<3; i++) {
398 track[i].set_extrapolator(ex);
399 }
400 }
402 Extrap Track3::get_extrapolator() const
403 {
404 return track[0].get_extrapolator();
405 }
407 void Track3::set_default(const Vector3 &def)
408 {
409 for(int i=0; i<3; i++) {
410 track[i].set_default(def[i]);
411 }
412 }
414 void Track3::set_value(const Vector3 &val, long tmsec)
415 {
416 for(int i=0; i<3; i++) {
417 track[i].set_value(val[i], tmsec);
418 }
419 }
421 Vector3 Track3::get_value(long tmsec) const
422 {
423 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
424 }
426 Vector3 Track3::operator ()(long tmsec) const
427 {
428 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
429 }
432 static inline anm_interpolator track_interpolator(Interp in)
433 {
434 switch(in) {
435 case INTERP_STEP:
436 return ANM_INTERP_STEP;
437 case INTERP_LINEAR:
438 return ANM_INTERP_LINEAR;
439 case INTERP_CUBIC:
440 return ANM_INTERP_CUBIC;
441 }
443 assert(0);
444 return ANM_INTERP_STEP;
445 }
447 static inline anm_extrapolator track_extrapolator(Extrap ex)
448 {
449 switch(ex) {
450 case EXTRAP_EXTEND:
451 return ANM_EXTRAP_EXTEND;
452 case EXTRAP_CLAMP:
453 return ANM_EXTRAP_CLAMP;
454 case EXTRAP_REPEAT:
455 return ANM_EXTRAP_REPEAT;
456 }
458 assert(0);
459 return ANM_EXTRAP_EXTEND;
460 }