X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fsnode.cc;h=fb2877df435c7f6a90b511e96e2fea0130e0f487;hp=101fe62994e3bbbdf5c31b10c31afbd2266e89b3;hb=9480e20f4de41693ebd1f22e63d3bcecde878f70;hpb=873255c40ab793b8c569eca3728d918339686000 diff --git a/src/snode.cc b/src/snode.cc index 101fe62..fb2877d 100644 --- a/src/snode.cc +++ b/src/snode.cc @@ -2,19 +2,27 @@ #include #include #include "snode.h" +#include "objmesh.h" +#include "dbg_gui.h" SceneNode::SceneNode() : scale(1, 1, 1) { + scene = 0; parent = 0; name = 0; + visible = true; + local_bvol_valid = false; } SceneNode::SceneNode(Object *obj) : scale(1, 1, 1) { + scene = 0; parent = 0; name = 0; + visible = true; + local_bvol_valid = false; add_object(obj); } @@ -48,6 +56,7 @@ void SceneNode::add_child(SceneNode *node) children.push_back(node); node->parent = this; + node->scene = scene; } bool SceneNode::remove_child(SceneNode *node) @@ -58,6 +67,8 @@ bool SceneNode::remove_child(SceneNode *node) if(it != children.end()) { assert(node->parent == this); node->parent = 0; + node->scene = 0; + children.erase(it); return true; } return false; @@ -88,6 +99,8 @@ void SceneNode::add_object(Object *obj) this->obj.push_back(obj); obj->node = this; + + local_bvol_valid = false; } bool SceneNode::remove_object(Object *o) @@ -102,6 +115,8 @@ bool SceneNode::remove_object(Object *o) return false; } obj.erase(it); + + local_bvol_valid = false; return true; } @@ -149,17 +164,17 @@ const Vec3 &SceneNode::get_node_scaling() const Vec3 SceneNode::get_position() const { - return xform * Vec3(0, 0, 0); + return xform.get_translation(); } Quat SceneNode::get_rotation() const { - return rot; // TODO + return xform.get_rotation(); } Vec3 SceneNode::get_scaling() const { - return scale; // TODO + return xform.get_scaling(); } const Mat4 &SceneNode::get_matrix() const @@ -188,13 +203,96 @@ void SceneNode::update_node(float dt) void SceneNode::update(float dt) { + bool expanded = false; + + if(debug_gui) { + if(parent_expanded) { + ImGui::PushID(this); + ImGui::AlignTextToFramePadding(); + + int flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick; + if(children.empty()) flags |= ImGuiTreeNodeFlags_Leaf; + if(dbg_sel_node == this) flags |= ImGuiTreeNodeFlags_Selected; + expanded = ImGui::TreeNodeEx(name ? name : "", flags); + if(ImGui::IsItemClicked()) { + dbg_sel_node = this; + } + + ImGui::NextColumn(); + ImGui::Checkbox("##vis", &visible); + ImGui::SameLine(); + if(ImGui::Button("xform")) { + ImGui::OpenPopup("xform_popup"); + } + if(ImGui::BeginPopup("xform_popup")) { + ImGui::Text("Local transform"); + Vec3 p = get_node_position(); + ImGui::BulletText("P: %g %g %g", p.x, p.y, p.z); + Quat q = get_node_rotation(); + ImGui::BulletText("R: %g %g %g %g", q.x, q.y, q.z, q.w); + Vec3 s = get_node_scaling(); + ImGui::BulletText("S: %g %g %g", s.x, s.y, s.z); + + ImGui::Separator(); + ImGui::Text("Global transform"); + p = get_position(); + ImGui::BulletText("P: %g %g %g", p.x, p.y, p.z); + q = get_rotation(); + ImGui::BulletText("R: %g %g %g %g", q.x, q.y, q.z, q.w); + s = get_scaling(); + ImGui::BulletText("S: %g %g %g", s.x, s.y, s.z); + + const Mat4 &mat = get_matrix(); + ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[0][0], mat[0][1], mat[0][2], mat[0][3]); + ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[1][0], mat[1][1], mat[1][2], mat[1][3]); + ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[2][0], mat[2][1], mat[2][2], mat[2][3]); + ImGui::BulletText("| %3.3f %3.3f %3.3f %3.3f |", mat[3][0], mat[3][1], mat[3][2], mat[3][3]); + + ImGui::EndPopup(); + } + ImGui::NextColumn(); + ImGui::PopID(); + } + } + update_node(dt); - for(size_t i=0; iupdate(dt); } + + if(debug_gui && expanded) { + ImGui::TreePop(); + } } +void SceneNode::apply_xform() +{ + update_node(); + + // apply post-order to make sure we don't affect the children xform by our reset + + int nchild = children.size(); + for(int i=0; iapply_xform(); + } + + int nobj = obj.size(); + for(int i=0; iget_type() == OBJ_MESH) { + ObjMesh *om = (ObjMesh*)obj[i]; + if(om->mesh) { + om->mesh->apply_xform(xform); + } + } + } + + pos = Vec3(0, 0, 0); + rot = Quat::identity; + scale = Vec3(1, 1, 1); +} bool SceneNode::intersect(const Ray &ray, HitPoint *hit) const { @@ -229,3 +327,55 @@ bool SceneNode::intersect(const Ray &ray, HitPoint *hit) const } return false; } + +const AABox &SceneNode::calc_local_bounds() +{ + local_bvol = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX)); + + // calculate the axis-aligned bounding box of all objects in this node + int nobj = obj.size(); + for(int i=0; iget_aabox(); + calc_bounding_aabox(&local_bvol, &local_bvol, &tmp); + } + + local_bvol_valid = true; + return local_bvol; +} + +const AABox &SceneNode::get_local_bounds() const +{ + if(!local_bvol_valid) { + ((SceneNode*)this)->calc_local_bounds(); + } + return local_bvol; +} + +AABox SceneNode::get_node_bounds() const +{ + get_local_bounds(); // validate local_bvol + + // calculate the transformed local_bvol + Box node_bbox = Box(local_bvol, xform); + + // then calculate the axis-aligned bounding box + AABox aabox; + calc_bounding_aabox(&aabox, &node_bbox); + return aabox; +} + +AABox SceneNode::get_bounds() const +{ + AABox sub_aabb = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX)); + + // calculate the bounding box of all children + int nchild = children.size(); + for(int i=0; iget_bounds(); + calc_bounding_aabox(&sub_aabb, &sub_aabb, &tmp); + } + + AABox aabb = get_node_bounds(); + calc_bounding_aabox(&aabb, &aabb, &sub_aabb); + return aabb; +}