From 2c07c0b7df2b13cd5deea8233e1c4eb61a85a766 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 23 May 2021 13:32:02 +0200 Subject: Naive BVH working + camera simple camera rotation --- include/bvh-node.hpp | 86 ++++++++++++++++++++++++++++++++++------------------ include/camera.hpp | 4 ++- 2 files changed, 59 insertions(+), 31 deletions(-) (limited to 'include') diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp index ee5068b..3a4ac4b 100644 --- a/include/bvh-node.hpp +++ b/include/bvh-node.hpp @@ -52,9 +52,8 @@ class bvh_node { class bvh { public: - // TODO: Fix one level too deep BVH void build(sphere* spheres, uint32_t spheres_count) { - nodes = std::vector(spheres_count * spheres_count); + nodes = std::vector(2 * spheres_count - 1); boxes = std::vector(spheres_count); for (uint32_t sphere_id = 0; sphere_id < spheres_count; sphere_id++) { @@ -73,31 +72,49 @@ public: void exporter() { std::cout << "digraph bvh {" << std::endl; - std::cout << "\t r"; - preorder(nodes[0].left_id, nodes[0].sphere_id); + std::cout << "\t 0"; + // preorder(nodes[0].left_id, nodes[0].sphere_id); + traverse(nodes[0].left_id); - std::cout << "\t r"; - preorder(nodes[0].left_id + 1, nodes[0].sphere_id); + std::cout << "\t 0"; + // preorder(nodes[0].left_id + 1, nodes[0].sphere_id); + traverse(nodes[0].left_id + 1); std::cout << "}" << std::endl; } - void preorder(int32_t id, int32_t sphere_id) { + bool traverse(int32_t id) { if (id <= 0) { - if (sphere_id < 0) { - std::cout << ";" << std::endl; - } else { - std::cout << " -> sphere_" << sphere_id << ";" << std::endl; - } - return; + std::cout << ";" << std::endl; + return false; } std::cout << " -> " << id; - preorder(nodes[id].left_id, nodes[id].sphere_id); + if (traverse(nodes[id].left_id)) { + std::cout << "\t " << id; + traverse(nodes[id].left_id + 1); + } + + return true; + } + + void preorder(int32_t id, int32_t sphere_id) { + if (id <= 0) { + return; + } if (nodes[id].left_id > -1) { + std::cout << " -> " << id; + preorder(nodes[id].left_id, nodes[id].sphere_id); + std::cout << "\t " << id; - preorder(nodes[id].left_id + 1, nodes[id].sphere_id); + if (nodes[nodes[id].left_id + 1].sphere_id == -1) { + preorder(nodes[id].left_id + 1, nodes[id].sphere_id); + } else { + std::cout << " -> sphere_" << nodes[nodes[id].left_id + 1].sphere_id << ";" << std::endl; + } + } else if (nodes[id].sphere_id > -1) { + std::cout << " -> sphere_" << nodes[id].sphere_id << ";" << std::endl; } } @@ -114,35 +131,44 @@ private: } void subdivide(uint32_t parent_id, uint32_t begin, uint32_t end) { - if ((end - begin) == 0) { - return; - } - - auto left_id = parent_id * 2 + 1; - auto right_id = parent_id * 2 + 2; + if ((end - begin) == 2) { + auto left_id = ++next_node_id; + auto right_id = ++next_node_id; - nodes[parent_id].left_id = left_id; + nodes[parent_id].left_id = left_id; - nodes[left_id] = bvh_node(); - nodes[right_id] = bvh_node(); + nodes[left_id] = bvh_node(); + nodes[right_id] = bvh_node(); - nodes[left_id].bounding_box = compute_bounds(begin, begin + ((end - begin) / 2)); - nodes[right_id].bounding_box = compute_bounds(begin + ((end - begin) / 2), end); + nodes[left_id].bounding_box = boxes[begin]; + nodes[right_id].bounding_box = boxes[begin + 1]; - if ((end - begin) == 2) { nodes[left_id].sphere_id = begin; nodes[right_id].sphere_id = begin + 1; } else if ((end - begin) == 1) { - nodes[left_id].sphere_id = begin; + nodes[parent_id].bounding_box = boxes[begin]; + nodes[parent_id].sphere_id = begin; } else { - subdivide(left_id, begin, begin + ((end - begin) / 2)); - subdivide(right_id, begin + ((end - begin) / 2), end); + auto left_id = ++next_node_id; + auto right_id = ++next_node_id; + + nodes[parent_id].left_id = left_id; + + nodes[left_id] = bvh_node(); + nodes[right_id] = bvh_node(); + + nodes[left_id].bounding_box = compute_bounds(begin, begin + ((end - begin) / 2) + 1); + nodes[right_id].bounding_box = compute_bounds(begin + ((end - begin) / 2) + 1, end); + + subdivide(left_id, begin, begin + ((end - begin) / 2) + 1); + subdivide(right_id, begin + ((end - begin) / 2) + 1, end); } } public: std::vector nodes; std::vector boxes; + uint32_t next_node_id = 0; }; #endif // !__BVH_NODE_HPP_ diff --git a/include/camera.hpp b/include/camera.hpp index 6adac87..60ca5b5 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -18,6 +18,8 @@ class camera { void move(vec3 v); + void rotate_y(float theta); + point3 position; @@ -33,7 +35,7 @@ class camera { float lens_radius; float fov; float focus_distance; - float padding; + float aspect_ratio; }; #endif // !__CAMERA_HPP_ -- cgit v1.3