From bf512216323f3df55627421bb0187ed364d58340 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Wed, 1 Sep 2021 00:18:04 +0200 Subject: White furnace test on gltf scenes & load normals --- include/bvh-node.hpp | 128 +++++++++++++++++++++++++-------------------------- 1 file changed, 64 insertions(+), 64 deletions(-) (limited to 'include/bvh-node.hpp') diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp index 5a46273..99b6d62 100644 --- a/include/bvh-node.hpp +++ b/include/bvh-node.hpp @@ -30,61 +30,21 @@ struct temp_node : public bvh_node { class bvh { public: - bvh(std::vector& spheres, std::vector& packed_nodes) - :spheres(spheres) { - packed_nodes.resize(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 = 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, 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 = 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(std::vector& triangles, std::vector& packed_nodes) - // :triangles(triangles) { - // packed_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 = aabb(triangles[id]); + // bvh(std::vector& spheres, std::vector& packed_nodes) + // :spheres(spheres) { + // packed_nodes.resize(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 = 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(); @@ -110,6 +70,46 @@ public: // // exporter(); // } + bvh(std::vector& triangles, std::vector& packed_nodes) + :triangles(triangles) { + packed_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 = 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 //------------------------- @@ -166,8 +166,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; @@ -207,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)); @@ -233,18 +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 = spheres[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 = spheres[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; -- cgit v1.3