goat3d
view src/xform_node.cc @ 30:0fe02696fb1e
yeeay, the max plugin works :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 29 Sep 2013 23:05:44 +0300 |
parents | 3d669155709d |
children | 498ca7ac7047 |
line source
1 #include <assert.h>
2 #include <algorithm>
3 #include "xform_node.h"
4 #include "anim.h"
5 #include "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);
15 parent = 0;
16 }
18 XFormNode::~XFormNode()
19 {
20 anm_destroy_node(anm);
21 delete anm;
22 }
24 void XFormNode::set_name(const char *name)
25 {
26 anm_set_node_name(anm, name);
27 }
29 const char *XFormNode::get_name() const
30 {
31 return anm_get_node_name(anm);
32 }
34 void XFormNode::set_interpolator(Interp in)
35 {
36 anm_set_interpolator(anm, track_interpolator(in));
37 interp = in;
38 }
40 Interp XFormNode::get_interpolator() const
41 {
42 return interp;
43 }
45 void XFormNode::set_extrapolator(Extrap ex)
46 {
47 anm_set_extrapolator(anm, track_extrapolator(ex));
48 extrap = ex;
49 }
51 Extrap XFormNode::get_extrapolator() const
52 {
53 return extrap;
54 }
56 void XFormNode::add_child(XFormNode *child)
57 {
58 children.push_back(child);
59 anm_link_node(anm, child->anm);
60 child->parent = this;
61 }
63 void XFormNode::remove_child(XFormNode *child)
64 {
65 std::vector<XFormNode*>::iterator it;
66 it = std::find(children.begin(), children.end(), child);
67 if(it != children.end()) {
68 children.erase(it);
69 anm_unlink_node(anm, child->anm);
70 }
71 child->parent = 0;
72 }
74 int XFormNode::get_children_count() const
75 {
76 return (int)children.size();
77 }
79 XFormNode *XFormNode::get_child(int idx)
80 {
81 if(idx >= 0 && idx < get_children_count()) {
82 return children[idx];
83 }
84 return 0;
85 }
87 const XFormNode *XFormNode::get_child(int idx) const
88 {
89 if(idx >= 0 && idx < get_children_count()) {
90 return children[idx];
91 }
92 return 0;
93 }
95 XFormNode *XFormNode::get_parent() const
96 {
97 return parent;
98 }
100 void XFormNode::set_position(const Vector3 &pos, long tmsec)
101 {
102 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
103 }
105 Vector3 XFormNode::get_node_position(long tmsec) const
106 {
107 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
108 return Vector3(p.x, p.y, p.z);
109 }
111 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
112 {
113 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
114 }
116 Quaternion XFormNode::get_node_rotation(long tmsec) const
117 {
118 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
119 return Quaternion(q.w, q.x, q.y, q.z);
120 }
122 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
123 {
124 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
125 }
127 Vector3 XFormNode::get_node_scaling(long tmsec) const
128 {
129 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
130 return Vector3(s.x, s.y, s.z);
131 }
133 // these take hierarchy into account
134 Vector3 XFormNode::get_position(long tmsec) const
135 {
136 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
137 return Vector3(v.x, v.y, v.z);
138 }
140 Quaternion XFormNode::get_rotation(long tmsec) const
141 {
142 quat_t q = anm_get_rotation(anm, tmsec);
143 return Quaternion(q.w, q.x, q.y, q.z);
144 }
146 Vector3 XFormNode::get_scaling(long tmsec) const
147 {
148 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
149 return Vector3(v.x, v.y, v.z);
150 }
152 void XFormNode::set_pivot(const Vector3 &pivot)
153 {
154 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
155 }
157 Vector3 XFormNode::get_pivot() const
158 {
159 vec3_t p = anm_get_pivot(anm);
160 return Vector3(p.x, p.y, p.z);
161 }
163 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
164 {
165 local_matrix = mat;
166 }
168 const Matrix4x4 &XFormNode::get_local_matrix() const
169 {
170 return local_matrix;
171 }
173 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
174 {
175 bone_matrix = bmat;
176 }
178 const Matrix4x4 &XFormNode::get_bone_matrix() const
179 {
180 return bone_matrix;
181 }
183 #define FOO
185 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
186 {
187 anm_time_t tm = ANM_MSEC2TM(tmsec);
189 if(mat) {
190 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
191 #ifdef FOO
192 *mat = local_matrix * *mat;
193 #else
194 *mat = *mat * local_matrix;
195 #endif
196 }
197 if(inv_mat) {
198 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
199 }
200 }
202 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
203 {
204 anm_time_t tm = ANM_MSEC2TM(tmsec);
206 if(mat) {
207 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
208 #ifdef FOO
209 *mat = local_matrix * *mat;
210 #else
211 *mat = *mat * local_matrix;
212 #endif
213 }
214 if(inv_mat) {
215 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
216 }
217 }
220 // ---- Track ----
222 Track::Track()
223 {
224 trk = new anm_track;
225 anm_init_track(trk);
226 }
228 Track::~Track()
229 {
230 anm_destroy_track(trk);
231 delete trk;
232 }
234 Track::Track(const Track &rhs)
235 {
236 trk = new anm_track;
237 anm_init_track(trk);
238 anm_copy_track(trk, rhs.trk);
239 interp = rhs.interp;
240 extrap = rhs.extrap;
241 }
243 Track &Track::operator =(const Track &rhs)
244 {
245 if(&rhs == this) {
246 return *this;
247 }
249 anm_copy_track(trk, rhs.trk);
250 interp = rhs.interp;
251 extrap = rhs.extrap;
252 return *this;
253 }
256 void Track::set_interpolator(Interp in)
257 {
258 anm_set_track_interpolator(trk, track_interpolator(in));
259 interp = in;
260 }
262 Interp Track::get_interpolator() const
263 {
264 return interp;
265 }
267 void Track::set_extrapolator(Extrap ex)
268 {
269 anm_set_track_extrapolator(trk, track_extrapolator(ex));
270 extrap = ex;
271 }
273 Extrap Track::get_extrapolator() const
274 {
275 return extrap;
276 }
278 void Track::set_default(double def)
279 {
280 anm_set_track_default(trk, def);
281 }
283 void Track::set_value(float val, long tmsec)
284 {
285 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
286 }
288 float Track::get_value(long tmsec) const
289 {
290 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
291 }
293 float Track::operator ()(long tmsec) const
294 {
295 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
296 }
299 // ---- Track3 ----
301 void Track3::set_interpolator(Interp in)
302 {
303 for(int i=0; i<3; i++) {
304 track[i].set_interpolator(in);
305 }
306 }
308 Interp Track3::get_interpolator() const
309 {
310 return track[0].get_interpolator();
311 }
313 void Track3::set_extrapolator(Extrap ex)
314 {
315 for(int i=0; i<3; i++) {
316 track[i].set_extrapolator(ex);
317 }
318 }
320 Extrap Track3::get_extrapolator() const
321 {
322 return track[0].get_extrapolator();
323 }
325 void Track3::set_default(const Vector3 &def)
326 {
327 for(int i=0; i<3; i++) {
328 track[i].set_default(def[i]);
329 }
330 }
332 void Track3::set_value(const Vector3 &val, long tmsec)
333 {
334 for(int i=0; i<3; i++) {
335 track[i].set_value(val[i], tmsec);
336 }
337 }
339 Vector3 Track3::get_value(long tmsec) const
340 {
341 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
342 }
344 Vector3 Track3::operator ()(long tmsec) const
345 {
346 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
347 }
350 static inline anm_interpolator track_interpolator(Interp in)
351 {
352 switch(in) {
353 case INTERP_STEP:
354 return ANM_INTERP_STEP;
355 case INTERP_LINEAR:
356 return ANM_INTERP_LINEAR;
357 case INTERP_CUBIC:
358 return ANM_INTERP_CUBIC;
359 }
361 assert(0);
362 return ANM_INTERP_STEP;
363 }
365 static inline anm_extrapolator track_extrapolator(Extrap ex)
366 {
367 switch(ex) {
368 case EXTRAP_EXTEND:
369 return ANM_EXTRAP_EXTEND;
370 case EXTRAP_CLAMP:
371 return ANM_EXTRAP_CLAMP;
372 case EXTRAP_REPEAT:
373 return ANM_EXTRAP_REPEAT;
374 }
376 assert(0);
377 return ANM_EXTRAP_EXTEND;
378 }