From 3bfd2d67e1c2d7e2d77c431f54dd68356c94d787 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 25 Jul 2021 16:52:28 +0200 Subject: Slowly rendering sponza --- include/bvh-node.hpp | 99 +++++++++++++++++++++++++++------------------------- include/scene.hpp | 14 ++++---- include/triangle.hpp | 4 +++ 3 files changed, 62 insertions(+), 55 deletions(-) (limited to 'include') diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp index 805edf2..4821fc7 100644 --- a/include/bvh-node.hpp +++ b/include/bvh-node.hpp @@ -27,54 +27,21 @@ struct temp_node : public bvh_node { class bvh { public: - bvh(std::vector& spheres, std::vector& nodes) - :spheres(spheres), nodes(nodes) { - nodes = std::vector(2 * spheres.size() - 1); - temp_nodes = std::vector(2 * spheres.size() - 1); - leafs = std::vector(spheres.size()); - - for (uint32_t id = 0; id < spheres.size(); id++) { - leafs[id].bounding_box = spheres[id].bounding_box; - leafs[id].primitive_id = id; - } - - temp_nodes[0] = temp_node(); - - temp_nodes[0].bounding_box = compute_bounds(0, spheres.size()); - subdivide(0, 0, spheres.size()); - - set_depth_first_order(); - - for (uint32_t id = 0; id < temp_nodes.size(); id++) { - auto old_node = temp_nodes[id]; - auto &node = nodes[old_node.df_id]; - - node.primitive_id = old_node.primitive_id; - node.bounding_box = old_node.bounding_box; - - if (old_node.next_id != -1) { - node.next_id = temp_nodes[old_node.next_id].df_id; - } - } - - // exporter(); - } - - // bvh(std::vector& triangles, std::vector& nodes) - // :triangles(triangles), nodes(nodes) { - // nodes.resize(2 * triangles.size() - 1); - // temp_nodes = std::vector(2 * triangles.size() - 1); - // leafs = std::vector(triangles.size()); - - // for (uint32_t id = 0; id < triangles.size(); id++) { - // leafs[id].bounding_box = triangles[id].bounding_box; + // bvh(std::vector& spheres, std::vector& nodes) + // :spheres(spheres), nodes(nodes) { + // nodes = std::vector(2 * spheres.size() - 1); + // temp_nodes = std::vector(2 * spheres.size() - 1); + // leafs = std::vector(spheres.size()); + + // for (uint32_t id = 0; id < spheres.size(); id++) { + // leafs[id].bounding_box = spheres[id].bounding_box; // leafs[id].primitive_id = id; // } // temp_nodes[0] = temp_node(); - // temp_nodes[0].bounding_box = compute_bounds(0, triangles.size()); - // subdivide(0, 0, triangles.size()); + // temp_nodes[0].bounding_box = compute_bounds(0, spheres.size()); + // subdivide(0, 0, spheres.size()); // set_depth_first_order(); @@ -93,6 +60,39 @@ public: // // exporter(); // } + bvh(std::vector& triangles, std::vector& nodes) + :triangles(triangles), nodes(nodes) { + nodes.resize(2 * triangles.size() - 1); + temp_nodes = std::vector(2 * triangles.size() - 1); + leafs = std::vector(triangles.size()); + + for (uint32_t id = 0; id < triangles.size(); id++) { + leafs[id].bounding_box = triangles[id].bounding_box; + leafs[id].primitive_id = id; + } + + temp_nodes[0] = temp_node(); + + temp_nodes[0].bounding_box = compute_bounds(0, triangles.size()); + subdivide(0, 0, triangles.size()); + + set_depth_first_order(); + + for (uint32_t id = 0; id < temp_nodes.size(); id++) { + auto old_node = temp_nodes[id]; + auto &node = nodes[old_node.df_id]; + + node.primitive_id = old_node.primitive_id; + node.bounding_box = old_node.bounding_box; + + if (old_node.next_id != -1) { + node.next_id = temp_nodes[old_node.next_id].df_id; + } + } + + // exporter(); + } + //------------------------- // BVH exporter for graphviz //------------------------- @@ -153,8 +153,8 @@ private: for (uint32_t box_id = begin; box_id < end; box_id++) { auto leaf = leafs[box_id]; - global_box = aabb::surrounding_box(global_box, spheres[leaf.primitive_id].bounding_box); - // global_box = aabb::surrounding_box(global_box, triangles[leaf.primitive_id].bounding_box); + // global_box = aabb::surrounding_box(global_box, spheres[leaf.primitive_id].bounding_box); + global_box = aabb::surrounding_box(global_box, triangles[leaf.primitive_id].bounding_box); } return global_box; @@ -195,8 +195,11 @@ private: int32_t split_axis = find_split_axis(temp_nodes[parent_id].bounding_box); std::sort(leafs.begin() + begin, leafs.begin() + end, + // [&](const bvh_node& a, const bvh_node& b) { + // return spheres[a.primitive_id].center[split_axis] < spheres[b.primitive_id].center[split_axis]; + // } [&](const bvh_node& a, const bvh_node& b) { - return spheres[a.primitive_id].center[split_axis] < spheres[b.primitive_id].center[split_axis]; + return triangles[a.primitive_id].center()[split_axis] < triangles[b.primitive_id].center()[split_axis]; } ); @@ -230,8 +233,8 @@ public: std::vector temp_nodes; std::vector leafs; - std::vector& spheres; - // std::vector& triangles; + // std::vector& spheres; + std::vector& triangles; uint32_t next_node_id = 0; }; diff --git a/include/scene.hpp b/include/scene.hpp index 364e3a7..2a9ebf2 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -15,7 +15,8 @@ class scene { camera cam; - uint32_t max_bounce = 50; + // uint32_t max_bounce = 50; + uint32_t max_bounce = 1; // Output image resolution uint32_t width; @@ -23,9 +24,9 @@ class scene { uint32_t sample_index = 0; - uint32_t spheres_count = 0; - // uint32_t triangles_count = 0; - uint32_t enable_dof = true; + // uint32_t spheres_count = 0; + uint32_t triangles_count = 0; + uint32_t enable_dof = false; uint32_t debug_bvh = false; uint32_t padding; @@ -40,10 +41,9 @@ public: metadata meta; - // Geometry & BVH - std::vector spheres; - // std::vector triangles; + // std::vector spheres; + std::vector triangles; std::vector nodes; }; diff --git a/include/triangle.hpp b/include/triangle.hpp index 12c0b69..57b42ca 100644 --- a/include/triangle.hpp +++ b/include/triangle.hpp @@ -15,6 +15,10 @@ struct triangle { : v1(v1), v2(v2), v3(v3), bounding_box(v1.position, v2.position, v3.position) {} + vec3 center() { + return (v2.position + v1.position + v3.position) / 3.f; + } + vertex v1; vertex v2; vertex v3; -- cgit v1.3