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