From 13acb0321b5f2ed392c94e1f7c64069d4242971a Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 11 Jul 2022 09:08:45 +0200 Subject: Refactor bvh file --- include/aabb.hpp | 10 ++------- include/bvh.hpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/scene.hpp | 4 ++-- include/triangle.hpp | 7 +++--- 4 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 include/bvh.hpp (limited to 'include') diff --git a/include/aabb.hpp b/include/aabb.hpp index dcab8c9..ca6e84a 100644 --- a/include/aabb.hpp +++ b/include/aabb.hpp @@ -17,14 +17,8 @@ class aabb { auto &v2 = tri.p2; auto &v3 = tri.p3; - // TODO: Use vector 3 functions - minimum.v[0] = fmin(fmin(v1.v[0], v2.v[0]), v3.v[0]); - minimum.v[1] = fmin(fmin(v1.v[1], v2.v[1]), v3.v[1]); - minimum.v[2] = fmin(fmin(v1.v[2], v2.v[2]), v3.v[2]); - - maximum.v[0] = fmax(fmax(v1.v[0], v2.v[0]), v3.v[0]); - maximum.v[1] = fmax(fmax(v1.v[1], v2.v[1]), v3.v[1]); - maximum.v[2] = fmax(fmax(v1.v[2], v2.v[2]), v3.v[2]); + minimum = v1.min(v2.min(v3)); + maximum = v1.max(v2.max(v3)); } void union_with(const aabb &box) { diff --git a/include/bvh.hpp b/include/bvh.hpp new file mode 100644 index 0000000..ba847c1 --- /dev/null +++ b/include/bvh.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include + +#include "aabb.hpp" +// #include "sphere.hpp" +#include "triangle.hpp" + +struct bvh_node { + aabb bounding_box; + + int32_t next_id = -1; + int32_t primitive_id = -1; + uint32_t padding[2]; +}; + +struct packed_bvh_node { + float min[3]; + int32_t next_id = -1; + float max[3]; + int32_t primitive_id = -1; +}; + +struct temp_node : public bvh_node { + int32_t left_id; + int32_t df_id; // depth firt id +}; + +class bvh { +public: + // bvh(std::vector& spheres, std::vector& packed_nodes); + + bvh(std::vector& triangles, std::vector& packed_nodes); + + void exporter(); + + static bool traverse_depth_first(std::vector& nodes, int32_t id); + + static int32_t traverse_depth_first_ordered(std::vector& nodes, int32_t parent_id, int32_t id); + + std::vector temp_nodes; + std::vector leafs; + + // std::vector& spheres; + std::vector& triangles; + + uint32_t next_node_id = 0; + +private: + + static constexpr uint32_t buckets_count = 12; + + aabb compute_bounds(uint32_t begin, uint32_t end); + + void subdivide(uint32_t parent_id, uint32_t begin, uint32_t end); + + void set_depth_first_order(); + + void depth_first_order(int32_t id, int32_t next_id, int32_t &new_id); +}; diff --git a/include/scene.hpp b/include/scene.hpp index cabd10e..379d888 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -2,7 +2,7 @@ #define __SCENE_HPP_ #include "camera.hpp" -#include "bvh-node.hpp" +#include "bvh.hpp" #include "material.hpp" class vkrenderer; @@ -20,7 +20,7 @@ class scene { uint32_t sample_index = 0; uint32_t enable_dof = (uint32_t)false; - uint32_t debug_bvh = (uint32_t)false; + uint32_t debug_bvh = (uint32_t)true; int32_t downscale_factor = 1; metadata(camera cam, uint32_t width, uint32_t height) diff --git a/include/triangle.hpp b/include/triangle.hpp index 4d3550b..327573c 100644 --- a/include/triangle.hpp +++ b/include/triangle.hpp @@ -7,15 +7,14 @@ struct triangle { triangle() = default; triangle(vec3 p1, vec3 p2, vec3 p3) : p1(p1), p2(p2), p3(p3) - {} - - vec3 center() { - return (p1 + p2 + p3) / 3.f; + { + center = (p1 + p2 + p3) / 3.f; } vec3 p1; vec3 p2; vec3 p3; + vec3 center; }; #endif // !__TRIANGLE_HPP_ -- cgit v1.3