debug gui select/hide nodes
[laserbrain_demo] / src / snode.cc
1 #include <float.h>
2 #include <assert.h>
3 #include <algorithm>
4 #include "snode.h"
5 #include "objmesh.h"
6 #include "dbg_gui.h"
7
8 SceneNode::SceneNode()
9         : scale(1, 1, 1)
10 {
11         scene = 0;
12         parent = 0;
13         name = 0;
14         visible = true;
15         local_bvol_valid = false;
16 }
17
18 SceneNode::SceneNode(Object *obj)
19         : scale(1, 1, 1)
20 {
21         scene = 0;
22         parent = 0;
23         name = 0;
24         visible = true;
25         local_bvol_valid = false;
26         add_object(obj);
27 }
28
29 SceneNode::~SceneNode()
30 {
31         delete [] name;
32 }
33
34 void SceneNode::set_name(const char *s)
35 {
36         delete [] name;
37         name = new char[strlen(s) + 1];
38         strcpy(name, s);
39 }
40
41 const char *SceneNode::get_name() const
42 {
43         return name;
44 }
45
46 void SceneNode::add_child(SceneNode *node)
47 {
48         if(!node) return;
49
50         if(node->parent) {
51                 if(node->parent == this) {
52                         return;
53                 }
54                 node->parent->remove_child(node);
55         }
56
57         children.push_back(node);
58         node->parent = this;
59         node->scene = scene;
60 }
61
62 bool SceneNode::remove_child(SceneNode *node)
63 {
64         if(!node) return false;
65
66         auto it = std::find(children.begin(), children.end(), node);
67         if(it != children.end()) {
68                 assert(node->parent == this);
69                 node->parent = 0;
70                 node->scene = 0;
71                 children.erase(it);
72                 return true;
73         }
74         return false;
75 }
76
77 int SceneNode::get_num_children() const
78 {
79         return (int)children.size();
80 }
81
82 SceneNode *SceneNode::get_child(int idx) const
83 {
84         return children[idx];
85 }
86
87 SceneNode *SceneNode::get_parent() const
88 {
89         return parent;
90 }
91
92 void SceneNode::add_object(Object *obj)
93 {
94         if(obj->node == this) return;
95
96         if(obj->node) {
97                 obj->node->remove_object(obj);
98         }
99
100         this->obj.push_back(obj);
101         obj->node = this;
102
103         local_bvol_valid = false;
104 }
105
106 bool SceneNode::remove_object(Object *o)
107 {
108         if(o->node != this) {
109                 return false;
110         }
111         o->node = 0;
112
113         auto it = std::find(obj.begin(), obj.end(), o);
114         if(it == obj.end()) {
115                 return false;
116         }
117         obj.erase(it);
118
119         local_bvol_valid = false;
120         return true;
121 }
122
123 int SceneNode::get_num_objects() const
124 {
125         return (int)obj.size();
126 }
127
128 Object *SceneNode::get_object(int idx) const
129 {
130         return obj[idx];
131 }
132
133 void SceneNode::set_position(const Vec3 &pos)
134 {
135         this->pos = pos;
136 }
137
138 void SceneNode::set_rotation(const Quat &rot)
139 {
140         this->rot = rot;
141 }
142
143 void SceneNode::set_scaling(const Vec3 &scale)
144 {
145         this->scale = scale;
146 }
147
148
149 const Vec3 &SceneNode::get_node_position() const
150 {
151         return pos;
152 }
153
154 const Quat &SceneNode::get_node_rotation() const
155 {
156         return rot;
157 }
158
159 const Vec3 &SceneNode::get_node_scaling() const
160 {
161         return scale;
162 }
163
164
165 Vec3 SceneNode::get_position() const
166 {
167         return xform * Vec3(0, 0, 0);
168 }
169
170 Quat SceneNode::get_rotation() const
171 {
172         return rot;     // TODO
173 }
174
175 Vec3 SceneNode::get_scaling() const
176 {
177         return scale;   // TODO
178 }
179
180 const Mat4 &SceneNode::get_matrix() const
181 {
182         return xform;
183 }
184
185 const Mat4 &SceneNode::get_inv_matrix() const
186 {
187         return inv_xform;
188 }
189
190
191 void SceneNode::update_node(float dt)
192 {
193         xform = Mat4::identity;
194         xform.pre_translate(pos);
195         xform.pre_rotate(rot);
196         xform.pre_scale(scale);
197
198         if(parent) {
199                 xform = xform * parent->xform;
200         }
201         inv_xform = inverse(xform);
202 }
203
204 void SceneNode::update(float dt)
205 {
206         bool expanded = false;
207
208         if(debug_gui) {
209                 if(parent_expanded) {
210                         ImGui::PushID(this);
211                         ImGui::AlignTextToFramePadding();
212
213                         int flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
214                         if(children.empty()) flags |= ImGuiTreeNodeFlags_Leaf;
215                         if(dbg_sel_node == this) flags |= ImGuiTreeNodeFlags_Selected;
216                         expanded = ImGui::TreeNodeEx(name ? name : "<nameless node>", flags);
217                         if(ImGui::IsItemClicked()) {
218                                 dbg_sel_node = this;
219                         }
220
221                         ImGui::NextColumn();
222                         ImGui::Checkbox("##vis", &visible);
223                         ImGui::NextColumn();
224                         ImGui::PopID();
225                 }
226         }
227
228         update_node(dt);
229
230         int num = children.size();
231         for(int i=0; i<num; i++) {
232                 parent_expanded = expanded;
233                 children[i]->update(dt);
234         }
235
236         if(debug_gui && expanded) {
237                 ImGui::TreePop();
238         }
239 }
240
241 void SceneNode::apply_xform()
242 {
243         update_node();
244
245         // apply post-order to make sure we don't affect the children xform by our reset
246
247         int nchild = children.size();
248         for(int i=0; i<nchild; i++) {
249                 children[i]->apply_xform();
250         }
251
252         int nobj = obj.size();
253         for(int i=0; i<nobj; i++) {
254                 if(obj[i]->get_type() == OBJ_MESH) {
255                         ObjMesh *om = (ObjMesh*)obj[i];
256                         if(om->mesh) {
257                                 om->mesh->apply_xform(xform);
258                         }
259                 }
260         }
261
262         pos = Vec3(0, 0, 0);
263         rot = Quat::identity;
264         scale = Vec3(1, 1, 1);
265 }
266
267 bool SceneNode::intersect(const Ray &ray, HitPoint *hit) const
268 {
269         Ray local_ray = inv_xform * ray;
270
271         HitPoint nearest;
272         nearest.dist = FLT_MAX;
273         for(size_t i=0; i<obj.size(); i++) {
274                 if(obj[i]->intersect(local_ray, hit)) {
275                         if(!hit) return true;
276                         if(hit->dist < nearest.dist) {
277                                 nearest = *hit;
278                                 nearest.data = (void*)this;
279                                 nearest.local_ray = local_ray;
280                         }
281                 }
282         }
283
284         for(size_t i=0; i<children.size(); i++) {
285                 if(children[i]->intersect(ray, hit)) {
286                         if(!hit) return true;
287                         if(hit->dist < nearest.dist) {
288                                 nearest = *hit;
289                         }
290                 }
291         }
292
293         if(nearest.dist < FLT_MAX) {
294                 *hit = nearest;
295                 hit->ray = ray;
296                 return true;
297         }
298         return false;
299 }
300
301 const AABox &SceneNode::calc_local_bounds()
302 {
303         local_bvol = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX));
304
305         // calculate the axis-aligned bounding box of all objects in this node
306         int nobj = obj.size();
307         for(int i=0; i<nobj; i++) {
308                 AABox tmp = obj[i]->get_aabox();
309                 calc_bounding_aabox(&local_bvol, &local_bvol, &tmp);
310         }
311
312         local_bvol_valid = true;
313         return local_bvol;
314 }
315
316 const AABox &SceneNode::get_local_bounds() const
317 {
318         if(!local_bvol_valid) {
319                 ((SceneNode*)this)->calc_local_bounds();
320         }
321         return local_bvol;
322 }
323
324 AABox SceneNode::get_node_bounds() const
325 {
326         get_local_bounds();     // validate local_bvol
327
328         // calculate the transformed local_bvol
329         Box node_bbox = Box(local_bvol, xform);
330
331         // then calculate the axis-aligned bounding box
332         AABox aabox;
333         calc_bounding_aabox(&aabox, &node_bbox);
334         return aabox;
335 }
336
337 AABox SceneNode::get_bounds() const
338 {
339         AABox sub_aabb = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX));
340
341         // calculate the bounding box of all children
342         int nchild = children.size();
343         for(int i=0; i<nchild; i++) {
344                 AABox tmp = children[i]->get_bounds();
345                 calc_bounding_aabox(&sub_aabb, &sub_aabb, &tmp);
346         }
347
348         AABox aabb = get_node_bounds();
349         calc_bounding_aabox(&aabb, &aabb, &sub_aabb);
350         return aabb;
351 }