2484e6eed4b2d60fb7c947358e5fe8d3f2f82d46
[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.get_translation();
168 }
169
170 Quat SceneNode::get_rotation() const
171 {
172         return xform.get_rotation();
173 }
174
175 Vec3 SceneNode::get_scaling() const
176 {
177         return xform.get_scaling();
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::SameLine();
224                         if(ImGui::Button("xform")) {
225                                 ImGui::OpenPopup("xform_popup");
226                         }
227                         ImGui::SameLine();
228                         if(ImGui::Button("bbox")) {
229                                 ImGui::OpenPopup("bbox_popup");
230                         }
231                         if(ImGui::BeginPopup("xform_popup")) {
232                                 ImGui::Text("Local transform");
233                                 Vec3 p = get_node_position();
234                                 ImGui::BulletText("P: %g %g %g", p.x, p.y, p.z);
235                                 Quat q = get_node_rotation();
236                                 ImGui::BulletText("R: %g %g %g %g", q.x, q.y, q.z, q.w);
237                                 Vec3 s = get_node_scaling();
238                                 ImGui::BulletText("S: %g %g %g", s.x, s.y, s.z);
239
240                                 ImGui::Separator();
241                                 ImGui::Text("Global transform");
242                                 p = get_position();
243                                 ImGui::BulletText("P: %g %g %g", p.x, p.y, p.z);
244                                 q = get_rotation();
245                                 ImGui::BulletText("R: %g %g %g %g", q.x, q.y, q.z, q.w);
246                                 s = get_scaling();
247                                 ImGui::BulletText("S: %g %g %g", s.x, s.y, s.z);
248
249                                 const Mat4 &mat = get_matrix();
250                                 ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[0][0], mat[0][1], mat[0][2], mat[0][3]);
251                                 ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[1][0], mat[1][1], mat[1][2], mat[1][3]);
252                                 ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[2][0], mat[2][1], mat[2][2], mat[2][3]);
253                                 ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
254
255                                 ImGui::EndPopup();
256                         }
257                         if(ImGui::BeginPopup("bbox_popup")) {
258                                 AABox bloc = get_local_bounds();
259                                 ImGui::Text("Local bounds:");
260                                 if(bloc.max.x < bloc.min.x || bloc.max.y < bloc.min.y || bloc.max.z < bloc.min.z) {
261                                         ImGui::BulletText("invalid");
262                                 } else {
263                                         ImGui::BulletText("X: %f - %f", bloc.min.x, bloc.max.x);
264                                         ImGui::BulletText("Y: %f - %f", bloc.min.y, bloc.max.y);
265                                         ImGui::BulletText("Z: %f - %f", bloc.min.z, bloc.max.z);
266                                 }
267                                 ImGui::Separator();
268                                 AABox bbox = get_bounds();
269                                 ImGui::Text("Global bounds:");
270                                 if(bbox.max.x < bbox.min.x || bbox.max.y < bbox.min.y || bbox.max.z < bbox.min.z) {
271                                         ImGui::BulletText("invalid");
272                                 } else {
273                                         ImGui::BulletText("X: %f - %f", bbox.min.x, bbox.max.x);
274                                         ImGui::BulletText("Y: %f - %f", bbox.min.y, bbox.max.y);
275                                         ImGui::BulletText("Z: %f - %f", bbox.min.z, bbox.max.z);
276                                 }
277                                 ImGui::EndPopup();
278                         }
279                         ImGui::NextColumn();
280                         ImGui::PopID();
281                 }
282         }
283
284         update_node(dt);
285
286         int num = children.size();
287         for(int i=0; i<num; i++) {
288                 parent_expanded = expanded;
289                 children[i]->update(dt);
290         }
291
292         if(debug_gui && expanded) {
293                 ImGui::TreePop();
294         }
295 }
296
297 void SceneNode::apply_xform()
298 {
299         update_node();
300
301         // apply post-order to make sure we don't affect the children xform by our reset
302
303         int nchild = children.size();
304         for(int i=0; i<nchild; i++) {
305                 children[i]->apply_xform();
306         }
307
308         int nobj = obj.size();
309         for(int i=0; i<nobj; i++) {
310                 if(obj[i]->get_type() == OBJ_MESH) {
311                         ObjMesh *om = (ObjMesh*)obj[i];
312                         if(om->mesh) {
313                                 om->mesh->apply_xform(xform);
314                         }
315                 }
316         }
317
318         pos = Vec3(0, 0, 0);
319         rot = Quat::identity;
320         scale = Vec3(1, 1, 1);
321 }
322
323 bool SceneNode::intersect(const Ray &ray, HitPoint *hit) const
324 {
325         Ray local_ray = inv_xform * ray;
326
327         HitPoint nearest;
328         nearest.dist = FLT_MAX;
329         for(size_t i=0; i<obj.size(); i++) {
330                 if(obj[i]->intersect(local_ray, hit)) {
331                         if(!hit) return true;
332                         if(hit->dist < nearest.dist) {
333                                 nearest = *hit;
334                                 nearest.data = (void*)this;
335                                 nearest.local_ray = local_ray;
336                         }
337                 }
338         }
339
340         for(size_t i=0; i<children.size(); i++) {
341                 if(children[i]->intersect(ray, hit)) {
342                         if(!hit) return true;
343                         if(hit->dist < nearest.dist) {
344                                 nearest = *hit;
345                         }
346                 }
347         }
348
349         if(nearest.dist < FLT_MAX) {
350                 *hit = nearest;
351                 hit->ray = ray;
352                 return true;
353         }
354         return false;
355 }
356
357 const AABox &SceneNode::calc_local_bounds()
358 {
359         local_bvol = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX));
360
361         // calculate the axis-aligned bounding box of all objects in this node
362         int nobj = obj.size();
363         for(int i=0; i<nobj; i++) {
364                 AABox tmp = obj[i]->get_aabox();
365                 calc_bounding_aabox(&local_bvol, &local_bvol, &tmp);
366         }
367
368         local_bvol_valid = true;
369         return local_bvol;
370 }
371
372 const AABox &SceneNode::get_local_bounds() const
373 {
374         if(!local_bvol_valid) {
375                 ((SceneNode*)this)->calc_local_bounds();
376         }
377         return local_bvol;
378 }
379
380 AABox SceneNode::get_node_bounds() const
381 {
382         get_local_bounds();     // validate local_bvol
383
384         // calculate the transformed local_bvol
385         Box node_bbox = Box(local_bvol, xform);
386
387         // then calculate the axis-aligned bounding box
388         AABox aabox;
389         calc_bounding_aabox(&aabox, &node_bbox);
390         return aabox;
391 }
392
393 AABox SceneNode::get_bounds() const
394 {
395         AABox sub_aabb = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX));
396
397         // calculate the bounding box of all children
398         int nchild = children.size();
399         for(int i=0; i<nchild; i++) {
400                 AABox tmp = children[i]->get_bounds();
401                 calc_bounding_aabox(&sub_aabb, &sub_aabb, &tmp);
402         }
403
404         AABox aabb = get_node_bounds();
405         calc_bounding_aabox(&aabb, &aabb, &sub_aabb);
406         return aabb;
407 }