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 +++++++++++++++++++++++++-------------------------- include/gltf.hpp | 16 ++++--- include/material.hpp | 3 +- include/scene.hpp | 6 +-- include/triangle.hpp | 1 + 5 files changed, 79 insertions(+), 75 deletions(-) (limited to 'include') 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; diff --git a/include/gltf.hpp b/include/gltf.hpp index 7008e8a..6633811 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -9,12 +9,14 @@ using json = nlohmann::json; -#include +#include "material.hpp" +#include "vec3.hpp" struct mesh_part { - std::vector indices; - std::vector positions; - // std::vector normals; + std::vector indices; + std::vector positions; + std::vector normals; + material mat; }; struct mesh { @@ -34,9 +36,11 @@ private: static void load_node(json &gltf_json, uint32_t index, node &parent, std::vector &meshes); - static std::vector load_meshes(json &gltf_json, std::vector> &buffers_content); + static std::vector load_meshes(json &gltf_json, std::vector> &buffers_content, std::vector &materials); - static mesh_part load_primitive(json &gltf_json, json &primitive, std::vector> &buffers_content); + static std::vector load_materials(json &gltf_json); + + static mesh_part load_primitive(json &gltf_json, json &primitive, std::vector> &buffers_content, std::vector &materials); }; #endif diff --git a/include/material.hpp b/include/material.hpp index 4853279..ae42179 100644 --- a/include/material.hpp +++ b/include/material.hpp @@ -4,8 +4,7 @@ #include "vec3.hpp" struct material { - color albedo; - color emissive; + color base_color; float metalness = 0.f; float roughness = 1.f; uint32_t padding[2]; diff --git a/include/scene.hpp b/include/scene.hpp index 4eed567..cb0ea88 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -29,7 +29,7 @@ class scene { : cam(cam), width(width), height(height) {} }; - void random_scene(); + // void random_scene(); public: scene(camera cam, uint32_t width, uint32_t height); @@ -37,8 +37,8 @@ public: metadata meta; // Geometry & BVH - std::vector spheres; - // std::vector triangles; + // std::vector spheres; + std::vector triangles; std::vector packed_nodes; }; diff --git a/include/triangle.hpp b/include/triangle.hpp index 9993efe..456a238 100644 --- a/include/triangle.hpp +++ b/include/triangle.hpp @@ -3,6 +3,7 @@ struct vertex { vec3 position; + vec3 normal; }; struct triangle { -- cgit v1.3