From 0dafba17af044532fa2f14c61176e7994336f56c Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 1 Aug 2021 16:48:27 +0200 Subject: Use SAH to generate BVH & fix ray triangle intersection --- src/gltf.cpp | 31 ++++++++++++++++--------------- src/main.cpp | 2 +- src/scene.cpp | 10 ++++------ src/vec3.cpp | 29 ----------------------------- 4 files changed, 21 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/gltf.cpp b/src/gltf.cpp index 72219d6..055f2a6 100644 --- a/src/gltf.cpp +++ b/src/gltf.cpp @@ -5,6 +5,8 @@ #include "utils.hpp" +const vec3 scale (0.00800000037997961); + mesh gltf::load(std::filesystem::path &path, std::string &filename) { std::fstream f(path / filename); json gltf_json; @@ -61,7 +63,7 @@ void gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); auto indices_buffer = buffers_content[indices_buffer_index]; - void* indices_start = indices_buffer.data() + indices_offset; + uint16_t* indices_start = (uint16_t*)(indices_buffer.data() + indices_offset); mesh_part.indices.resize(indices_length / sizeof(uint16_t)); @@ -83,25 +85,24 @@ void gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); - auto normals_view_index = accessors[normals_accessor_index]["bufferView"].get(); - auto normals_view = gltf_json["bufferViews"][normals_view_index]; + // { + // auto normals_accessor_index = primitive["attributes"]["NORMAL"].get(); + // auto normals_view_index = accessors[normals_accessor_index]["bufferView"].get(); + // auto normals_view = gltf_json["bufferViews"][normals_view_index]; - auto normals_buffer_index = normals_view["buffer"].get(); - auto normals_offset = normals_view["byteOffset"].get(); - auto normals_length = normals_view["byteLength"].get(); + // auto normals_buffer_index = normals_view["buffer"].get(); + // auto normals_offset = normals_view["byteOffset"].get(); + // auto normals_length = normals_view["byteLength"].get(); - auto normals_buffer = buffers_content[normals_buffer_index]; - void* normals_start = normals_buffer.data() + normals_offset; + // auto normals_buffer = buffers_content[normals_buffer_index]; + // void* normals_start = normals_buffer.data() + normals_offset; - mesh_part.normals.resize(normals_length / (sizeof(float) * 3)); + // mesh_part.normals.resize(normals_length / (sizeof(float) * 3)); - std::memcpy(mesh_part.normals.data(), normals_start, normals_length); - } + // std::memcpy(mesh_part.normals.data(), normals_start, normals_length); + // } } diff --git a/src/main.cpp b/src/main.cpp index 2634371..d0500e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,7 +57,7 @@ int main() { point3 position = { 13.0, 2.0, -3.0 }; point3 target = { 0.0, 0.0, 0.0 }; - auto v_fov = 20; + auto v_fov = 90; auto aperture = 0.1; auto focus_distance = 10; auto cam = camera(position, target, v_fov, (float)width / (float)height, aperture, focus_distance); diff --git a/src/scene.cpp b/src/scene.cpp index 8e25be7..b44abd5 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -80,9 +80,9 @@ scene::scene(camera cam, uint32_t width, uint32_t height) auto i2 = mesh_part.indices[i + 1]; auto i3 = mesh_part.indices[i + 2]; - auto v1 = vertex { .position = mesh_part.positions[i1], .normal = mesh_part.normals[i1] }; - auto v2 = vertex { .position = mesh_part.positions[i2], .normal = mesh_part.normals[i2] }; - auto v3 = vertex { .position = mesh_part.positions[i3], .normal = mesh_part.normals[i3] }; + auto v1 = vertex { .position = mesh_part.positions[i1] }; + auto v2 = vertex { .position = mesh_part.positions[i2] }; + auto v3 = vertex { .position = mesh_part.positions[i3] }; triangles.emplace_back(v1, v2, v3); } @@ -94,7 +94,5 @@ scene::scene(camera cam, uint32_t width, uint32_t height) // bvh builder(spheres, nodes); // builder.exporter(); - for (uint32_t idx = 0; idx < builder.nodes.size(); idx++) { - nodes[idx] = builder.nodes[idx]; - } + std::memcpy(nodes.data(), builder.nodes.data(), sizeof(nodes[0]) * nodes.size()); } diff --git a/src/vec3.cpp b/src/vec3.cpp index 8f7a720..166f681 100644 --- a/src/vec3.cpp +++ b/src/vec3.cpp @@ -139,32 +139,3 @@ vec3 refract(const vec3& uv, const vec3& n, float etai_over_etat) { std::ostream& operator<<(std::ostream& out, vec3 vec) { return out << vec.x << ' ' << vec.y << ' ' << vec.z << std::endl; } - -// TODO: Use something else to find random point onto a sphere -vec3 random_in_unit_sphere() { - while (true) { - auto p = vec3::random(-1,1); - if (p.length_sq() >= 1) continue; - return p; - } -} - -vec3 random_unit_vector() { - return random_in_unit_sphere().unit(); -} - -vec3 random_in_hemisphere(const vec3& normal) { - vec3 in_unit_sphere = random_in_unit_sphere(); - if (in_unit_sphere.dot(normal) > 0.0) // In the same hemisphere as the normal - return in_unit_sphere; - else - return -in_unit_sphere; -} - -vec3 random_in_unit_disk() { - while (true) { - auto p = vec3(randd(-1, 1), randd(-1, 1), 0); - if (p.length_sq() >= 1) continue; - return p; - } -} -- cgit v1.3