aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/bvh-node.hpp97
-rw-r--r--include/scene.hpp14
-rw-r--r--include/triangle.hpp4
3 files changed, 61 insertions, 54 deletions
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<sphere>& spheres, std::vector<bvh_node>& nodes)
- :spheres(spheres), nodes(nodes) {
- nodes = std::vector<bvh_node>(2 * spheres.size() - 1);
- temp_nodes = std::vector<temp_node>(2 * spheres.size() - 1);
- leafs = std::vector<temp_node>(spheres.size());
+ // bvh(std::vector<sphere>& spheres, std::vector<bvh_node>& nodes)
+ // :spheres(spheres), nodes(nodes) {
+ // nodes = std::vector<bvh_node>(2 * spheres.size() - 1);
+ // temp_nodes = std::vector<temp_node>(2 * spheres.size() - 1);
+ // leafs = std::vector<temp_node>(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<triangle>& triangles, std::vector<bvh_node>& nodes)
- // :triangles(triangles), nodes(nodes) {
- // nodes.resize(2 * triangles.size() - 1);
- // temp_nodes = std::vector<temp_node>(2 * triangles.size() - 1);
- // leafs = std::vector<temp_node>(triangles.size());
-
- // for (uint32_t id = 0; id < triangles.size(); id++) {
- // leafs[id].bounding_box = triangles[id].bounding_box;
+ // 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<triangle>& triangles, std::vector<bvh_node>& nodes)
+ :triangles(triangles), nodes(nodes) {
+ nodes.resize(2 * triangles.size() - 1);
+ temp_nodes = std::vector<temp_node>(2 * triangles.size() - 1);
+ leafs = std::vector<temp_node>(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_node> temp_nodes;
std::vector<temp_node> leafs;
- std::vector<sphere>& spheres;
- // std::vector<triangle>& triangles;
+ // std::vector<sphere>& spheres;
+ std::vector<triangle>& 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<sphere> spheres;
- // std::vector<triangle> triangles;
+ // std::vector<sphere> spheres;
+ std::vector<triangle> triangles;
std::vector<bvh_node> 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;