aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/bvh-node.hpp130
-rw-r--r--include/material.hpp14
-rw-r--r--include/scene.hpp12
-rw-r--r--include/sphere.hpp5
4 files changed, 77 insertions, 84 deletions
diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp
index 698977a..5a46273 100644
--- a/include/bvh-node.hpp
+++ b/include/bvh-node.hpp
@@ -8,16 +8,12 @@
#include "sphere.hpp"
#include "triangle.hpp"
-class bvh_node {
- public:
- bvh_node() = default;
- bvh_node(sphere& sphere, int32_t sphere_id): bounding_box(sphere.bounding_box), primitive_id(sphere_id){}
+struct bvh_node {
+ aabb bounding_box;
- aabb bounding_box;
-
- int32_t next_id = -1;
- int32_t primitive_id = -1;
- uint32_t padding[2];
+ int32_t next_id = -1;
+ int32_t primitive_id = -1;
+ uint32_t padding[2];
};
struct packed_bvh_node {
@@ -34,54 +30,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());
-
- // 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;
+ bvh(std::vector<sphere>& spheres, std::vector<packed_bvh_node>& packed_nodes)
+ :spheres(spheres) {
+ packed_nodes.resize(2 * spheres.size() - 1);
+ temp_nodes = std::vector<temp_node>(2 * spheres.size() - 1);
+ leafs = std::vector<temp_node>(spheres.size());
- // 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<packed_bvh_node>& packed_nodes)
- :triangles(triangles) {
- packed_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 = aabb(triangles[id]);
+ for (uint32_t id = 0; id < spheres.size(); id++) {
+ leafs[id].bounding_box = aabb(spheres[id].center, spheres[id].radius);
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();
@@ -107,6 +70,46 @@ public:
// exporter();
}
+ // bvh(std::vector<triangle>& triangles, std::vector<packed_bvh_node>& packed_nodes)
+ // :triangles(triangles) {
+ // packed_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 = aabb(triangles[id]);
+ // 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 = packed_nodes[old_node.df_id];
+
+ // node.primitive_id = old_node.primitive_id;
+
+ // node.min[0] = old_node.bounding_box.minimum.v[0];
+ // node.min[1] = old_node.bounding_box.minimum.v[1];
+ // node.min[2] = old_node.bounding_box.minimum.v[2];
+
+ // node.max[0] = old_node.bounding_box.maximum.v[0];
+ // node.max[1] = old_node.bounding_box.maximum.v[1];
+ // node.max[2] = old_node.bounding_box.maximum.v[2];
+
+ // if (old_node.next_id != -1) {
+ // node.next_id = temp_nodes[old_node.next_id].df_id;
+ // }
+ // }
+
+ // // exporter();
+ // }
+
//-------------------------
// BVH exporter for graphviz
//-------------------------
@@ -163,8 +166,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;
@@ -176,7 +179,6 @@ private:
aabb global_box = aabb();
for (uint32_t id = begin; id < end; id++) {
- // global_box.union_with(spheres[leaf.primitive_id].bounding_box);
global_box.union_with(leafs[id].bounding_box);
}
@@ -205,12 +207,12 @@ private:
if (count <= 4) {
// Subdivide the node by splitting the set in two equal parts
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 triangles[a.primitive_id].center()[split_axis] < triangles[b.primitive_id].center()[split_axis];
+ return spheres[a.primitive_id].center[split_axis] < spheres[b.primitive_id].center[split_axis];
}
+ // [&](const bvh_node& a, const bvh_node& b) {
+ // return triangles[a.primitive_id].center()[split_axis] < triangles[b.primitive_id].center()[split_axis];
+ // }
);
temp_nodes[left_id].bounding_box = compute_bounds(begin, begin + (count / 2));
@@ -231,14 +233,18 @@ private:
aabb centroid_bounds;
for (size_t i = begin; i < end; i++) {
- auto primitive = triangles[leafs[i].primitive_id];
- centroid_bounds.union_with(primitive.center());
+ // auto primitive = triangles[leafs[i].primitive_id];
+ // centroid_bounds.union_with(primitive.center());
+ auto primitive = spheres[leafs[i].primitive_id];
+ centroid_bounds.union_with(primitive.center);
}
float split_axis_size = centroid_bounds.maximum[split_axis] - centroid_bounds.minimum[split_axis];
for (size_t i = begin; i < end; i++) {
- auto primitive = triangles[leafs[i].primitive_id];
- auto normalized_offset = (primitive.center()[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
+ // auto primitive = triangles[leafs[i].primitive_id];
+ // auto normalized_offset = (primitive.center()[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
+ auto primitive = spheres[leafs[i].primitive_id];
+ auto normalized_offset = (primitive.center[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
uint32_t bucket_index = normalized_offset * buckets_count;
if (bucket_index == buckets_count) {
bucket_index -= 1;
diff --git a/include/material.hpp b/include/material.hpp
index ee4281b..4853279 100644
--- a/include/material.hpp
+++ b/include/material.hpp
@@ -3,20 +3,12 @@
#include "vec3.hpp"
-enum MATERIAL_TYPE: uint32_t {
- LAMBERTIAN = 0,
- METAL = 1,
- DIELECTRIC = 2,
- EMISSIVE = 3,
-};
-
struct material {
color albedo;
color emissive;
- float fuzz;
- float ior;
- uint32_t type;
- uint32_t padding;
+ float metalness = 0.f;
+ float roughness = 1.f;
+ uint32_t padding[2];
};
#endif // !__MATERIAL_HPP_
diff --git a/include/scene.hpp b/include/scene.hpp
index 3c69425..4eed567 100644
--- a/include/scene.hpp
+++ b/include/scene.hpp
@@ -14,18 +14,16 @@ class scene {
// uint32_t max_bounce = 50;
uint32_t max_bounce = 3;
+ uint32_t min_bounce = 1;
- // Output image resolution
uint32_t width;
uint32_t height;
uint32_t sample_index = 0;
- // uint32_t spheres_count = 0;
- uint32_t triangles_count = 0;
uint32_t enable_dof = false;
- uint32_t debug_bvh = false;
- uint32_t downscale_factor = 4;
+ uint32_t debug_bvh = false;
+ int32_t downscale_factor = 4;
metadata(camera cam, uint32_t width, uint32_t height)
: cam(cam), width(width), height(height) {}
@@ -39,8 +37,8 @@ public:
metadata meta;
// Geometry & BVH
- // std::vector<sphere> spheres;
- std::vector<triangle> triangles;
+ std::vector<sphere> spheres;
+ // std::vector<triangle> triangles;
std::vector<packed_bvh_node> packed_nodes;
};
diff --git a/include/sphere.hpp b/include/sphere.hpp
index 28bb0bc..3a29207 100644
--- a/include/sphere.hpp
+++ b/include/sphere.hpp
@@ -7,16 +7,13 @@
struct sphere {
sphere() = default;
sphere(point3 center, material material, float radius)
- : center(center), radius(radius), mat(material),
- bounding_box(center, radius)
+ : center(center), radius(radius), mat(material)
{}
point3 center;
material mat;
- aabb bounding_box;
-
float radius;
float padding[3];
};