rayzor

view src/snode.cc @ 13:964f8ea5f095

missed quite a lot of things in my last commit apparently
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Apr 2014 23:37:55 +0300
parents
children a9a948809c6f
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "snode.h"
6 SceneNode::SceneNode()
7 {
8 parent = 0;
9 name = 0;
10 type = NODE_NULL;
11 scale = Vector3(1, 1, 1);
12 invalidate();
13 }
15 SceneNode::~SceneNode()
16 {
17 delete [] name;
18 }
20 void SceneNode::set_name(const char *name)
21 {
22 this->name = new char[strlen(name) + 1];
23 strcpy(this->name, name);
24 }
26 const char *SceneNode::get_name() const
27 {
28 return name ? name : "<unnamed>";
29 }
31 NodeType SceneNode::get_type() const
32 {
33 return type;
34 }
36 SceneNode *SceneNode::get_parent()
37 {
38 return parent;
39 }
41 const SceneNode *SceneNode::get_parent() const
42 {
43 return parent;
44 }
46 void SceneNode::add_child(SceneNode *child)
47 {
48 children.push_back(child);
49 child->parent = this;
50 invalidate();
51 }
53 void SceneNode::remove_child(SceneNode *child)
54 {
55 // TODO
56 invalidate();
57 }
59 int SceneNode::get_children_count() const
60 {
61 return (int)children.size();
62 }
64 SceneNode *SceneNode::get_child(int idx)
65 {
66 if(idx >= 0 && idx < get_children_count()) {
67 return children[idx];
68 }
69 return 0;
70 }
72 const SceneNode *SceneNode::get_child(int idx) const
73 {
74 if(idx >= 0 && idx < get_children_count()) {
75 return children[idx];
76 }
77 return 0;
78 }
82 void SceneNode::set_position(const Vector3 &pos)
83 {
84 this->pos = pos;
85 invalidate();
86 }
88 Vector3 SceneNode::get_node_position() const
89 {
90 return pos;
91 }
93 void SceneNode::set_rotation(const Quat &quat)
94 {
95 rot = quat;
96 invalidate();
97 }
99 Quat SceneNode::get_node_rotation() const
100 {
101 return rot;
102 }
104 void SceneNode::set_scaling(const Vector3 &scale)
105 {
106 this->scale = scale;
107 invalidate();
108 }
110 Vector3 SceneNode::get_node_scaling() const
111 {
112 return scale;
113 }
115 // these take hierarchy into account
116 Vector3 SceneNode::get_position() const
117 {
118 return transform(get_matrix(), Vector3(0, 0, 0));
119 }
121 Quat SceneNode::get_rotation() const
122 {
123 if(parent) {
124 return parent->get_rotation() * rot;
125 }
126 return rot;
127 }
129 Vector3 SceneNode::get_scaling() const
130 {
131 if(parent) {
132 return parent->get_scaling() * scale;
133 }
134 return scale;
135 }
137 void SceneNode::set_pivot(const Vector3 &pivot)
138 {
139 this->pivot = pivot;
140 invalidate();
141 }
143 Vector3 SceneNode::get_pivot() const
144 {
145 return pivot;
146 }
148 const Matrix4x4 &SceneNode::get_matrix() const
149 {
150 calc_matrix();
151 return xform;
152 }
154 const Matrix4x4 &SceneNode::get_inv_matrix() const
155 {
156 calc_inv_matrix();
157 return inv_xform;
158 }
160 void SceneNode::invalidate() const
161 {
162 xform_valid = inv_xform_valid = false;
163 }
165 // TODO: hierarchy
166 void SceneNode::calc_matrix() const
167 {
168 xform.set_identity();
169 xform.translate(pivot.x, pivot.y, pivot.z);
170 xform = xform * rot.get_matrix();
171 xform.translate(pos.x, pos.y, pos.z);
172 xform.scale(scale.x, scale.y, scale.z);
173 xform.translate(-pivot.x, -pivot.y, -pivot.z);
175 xform_valid = true;
176 }
178 void SceneNode::calc_inv_matrix() const
179 {
180 calc_matrix();
182 inv_xform = xform.inverse();
183 inv_xform_valid = true;
184 }
186 void SceneNode::draw() const
187 {
188 }
190 bool SceneNode::intersect(const Ray &ray, float *dist) const
191 {
192 return false;
193 }