goat3dgfx

view src/xform_node.cc @ 17:f2da87b02fcd

fixes mostly visual studio stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Dec 2013 02:25:29 +0200
parents 1873dfd13f2d
children 7c593721547f
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 }
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 }
62 void XFormNode::remove_child(XFormNode *child)
63 {
64 std::vector<XFormNode*>::iterator it;
65 it = std::find(children.begin(), children.end(), child);
66 if(it != children.end()) {
67 children.erase(it);
68 anm_unlink_node(anm, child->anm);
69 }
70 }
72 int XFormNode::get_children_count() const
73 {
74 return (int)children.size();
75 }
77 XFormNode *XFormNode::get_child(int idx)
78 {
79 if(idx >= 0 && idx < get_children_count()) {
80 return children[idx];
81 }
82 return 0;
83 }
85 const XFormNode *XFormNode::get_child(int idx) const
86 {
87 if(idx >= 0 && idx < get_children_count()) {
88 return children[idx];
89 }
90 return 0;
91 }
93 void XFormNode::set_position(const Vector3 &pos, long tmsec)
94 {
95 anm_set_position(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
96 }
98 Vector3 XFormNode::get_node_position(long tmsec) const
99 {
100 vec3_t p = anm_get_node_position(anm, ANM_MSEC2TM(tmsec));
101 return Vector3(p.x, p.y, p.z);
102 }
104 void XFormNode::set_rotation(const Quaternion &quat, long tmsec)
105 {
106 anm_set_rotation(anm, quat_cons(quat.s, quat.v.x, quat.v.y, quat.v.z), ANM_MSEC2TM(tmsec));
107 }
109 Quaternion XFormNode::get_node_rotation(long tmsec) const
110 {
111 quat_t q = anm_get_node_rotation(anm, ANM_MSEC2TM(tmsec));
112 return Quaternion(q.w, q.x, q.y, q.z);
113 }
115 void XFormNode::set_scaling(const Vector3 &pos, long tmsec)
116 {
117 anm_set_scaling(anm, v3_cons(pos.x, pos.y, pos.z), ANM_MSEC2TM(tmsec));
118 }
120 Vector3 XFormNode::get_node_scaling(long tmsec) const
121 {
122 vec3_t s = anm_get_node_scaling(anm, ANM_MSEC2TM(tmsec));
123 return Vector3(s.x, s.y, s.z);
124 }
126 // these take hierarchy into account
127 Vector3 XFormNode::get_position(long tmsec) const
128 {
129 vec3_t v = anm_get_position(anm, ANM_MSEC2TM(tmsec));
130 return Vector3(v.x, v.y, v.z);
131 }
133 Quaternion XFormNode::get_rotation(long tmsec) const
134 {
135 quat_t q = anm_get_rotation(anm, tmsec);
136 return Quaternion(q.w, q.x, q.y, q.z);
137 }
139 Vector3 XFormNode::get_scaling(long tmsec) const
140 {
141 vec3_t v = anm_get_scaling(anm, ANM_MSEC2TM(tmsec));
142 return Vector3(v.x, v.y, v.z);
143 }
145 void XFormNode::set_pivot(const Vector3 &pivot)
146 {
147 anm_set_pivot(anm, v3_cons(pivot.x, pivot.y, pivot.z));
148 }
150 Vector3 XFormNode::get_pivot() const
151 {
152 vec3_t p = anm_get_pivot(anm);
153 return Vector3(p.x, p.y, p.z);
154 }
156 void XFormNode::set_local_matrix(const Matrix4x4 &mat)
157 {
158 local_matrix = mat;
159 }
161 const Matrix4x4 &XFormNode::get_local_matrix() const
162 {
163 return local_matrix;
164 }
166 void XFormNode::set_bone_matrix(const Matrix4x4 &bmat)
167 {
168 bone_matrix = bmat;
169 }
171 const Matrix4x4 &XFormNode::get_bone_matrix() const
172 {
173 return bone_matrix;
174 }
176 #define FOO
178 void XFormNode::get_node_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
179 {
180 anm_time_t tm = ANM_MSEC2TM(tmsec);
182 if(mat) {
183 anm_get_node_matrix(anm, (scalar_t(*)[4])mat, tm);
184 #ifdef FOO
185 *mat = local_matrix * *mat;
186 #else
187 *mat = *mat * local_matrix;
188 #endif
189 }
190 if(inv_mat) {
191 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
192 }
193 }
195 void XFormNode::get_xform(long tmsec, Matrix4x4 *mat, Matrix4x4 *inv_mat) const
196 {
197 anm_time_t tm = ANM_MSEC2TM(tmsec);
199 if(mat) {
200 anm_get_matrix(anm, (scalar_t(*)[4])mat, tm);
201 #ifdef FOO
202 *mat = local_matrix * *mat;
203 #else
204 *mat = *mat * local_matrix;
205 #endif
206 }
207 if(inv_mat) {
208 anm_get_inv_matrix(anm, (scalar_t(*)[4])inv_mat, tm);
209 }
210 }
213 // ---- Track ----
215 Track::Track()
216 {
217 trk = new anm_track;
218 anm_init_track(trk);
219 }
221 Track::~Track()
222 {
223 anm_destroy_track(trk);
224 delete trk;
225 }
227 Track::Track(const Track &rhs)
228 {
229 trk = new anm_track;
230 anm_init_track(trk);
231 anm_copy_track(trk, rhs.trk);
232 interp = rhs.interp;
233 extrap = rhs.extrap;
234 }
236 Track &Track::operator =(const Track &rhs)
237 {
238 if(&rhs == this) {
239 return *this;
240 }
242 anm_copy_track(trk, rhs.trk);
243 interp = rhs.interp;
244 extrap = rhs.extrap;
245 return *this;
246 }
249 void Track::set_interpolator(Interp in)
250 {
251 anm_set_track_interpolator(trk, track_interpolator(in));
252 interp = in;
253 }
255 Interp Track::get_interpolator() const
256 {
257 return interp;
258 }
260 void Track::set_extrapolator(Extrap ex)
261 {
262 anm_set_track_extrapolator(trk, track_extrapolator(ex));
263 extrap = ex;
264 }
266 Extrap Track::get_extrapolator() const
267 {
268 return extrap;
269 }
271 void Track::set_default(double def)
272 {
273 anm_set_track_default(trk, def);
274 }
276 void Track::set_value(float val, long tmsec)
277 {
278 anm_set_value(trk, ANM_MSEC2TM(tmsec), val);
279 }
281 float Track::get_value(long tmsec) const
282 {
283 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
284 }
286 float Track::operator ()(long tmsec) const
287 {
288 return anm_get_value(trk, ANM_MSEC2TM(tmsec));
289 }
292 // ---- Track3 ----
294 void Track3::set_interpolator(Interp in)
295 {
296 for(int i=0; i<3; i++) {
297 track[i].set_interpolator(in);
298 }
299 }
301 Interp Track3::get_interpolator() const
302 {
303 return track[0].get_interpolator();
304 }
306 void Track3::set_extrapolator(Extrap ex)
307 {
308 for(int i=0; i<3; i++) {
309 track[i].set_extrapolator(ex);
310 }
311 }
313 Extrap Track3::get_extrapolator() const
314 {
315 return track[0].get_extrapolator();
316 }
318 void Track3::set_default(const Vector3 &def)
319 {
320 for(int i=0; i<3; i++) {
321 track[i].set_default(def[i]);
322 }
323 }
325 void Track3::set_value(const Vector3 &val, long tmsec)
326 {
327 for(int i=0; i<3; i++) {
328 track[i].set_value(val[i], tmsec);
329 }
330 }
332 Vector3 Track3::get_value(long tmsec) const
333 {
334 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
335 }
337 Vector3 Track3::operator ()(long tmsec) const
338 {
339 return Vector3(track[0](tmsec), track[1](tmsec), track[2](tmsec));
340 }
343 static inline anm_interpolator track_interpolator(Interp in)
344 {
345 switch(in) {
346 case INTERP_STEP:
347 return ANM_INTERP_STEP;
348 case INTERP_LINEAR:
349 return ANM_INTERP_LINEAR;
350 case INTERP_CUBIC:
351 return ANM_INTERP_CUBIC;
352 }
354 assert(0);
355 return ANM_INTERP_STEP;
356 }
358 static inline anm_extrapolator track_extrapolator(Extrap ex)
359 {
360 switch(ex) {
361 case EXTRAP_EXTEND:
362 return ANM_EXTRAP_EXTEND;
363 case EXTRAP_CLAMP:
364 return ANM_EXTRAP_CLAMP;
365 case EXTRAP_REPEAT:
366 return ANM_EXTRAP_REPEAT;
367 }
369 assert(0);
370 return ANM_EXTRAP_EXTEND;
371 }