aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-08-01 16:48:27 +0200
committerladevieq <ladevie.quentin@gmail.com>2021-08-01 16:48:27 +0200
commit0dafba17af044532fa2f14c61176e7994336f56c (patch)
tree076570f30f590b44e7e076ae497fddd18fd80ed9 /src
parent17ccf3158443a245ceeabc49cd62f50129a8852d (diff)
Use SAH to generate BVH & fix ray triangle intersection
Diffstat (limited to 'src')
-rw-r--r--src/gltf.cpp31
-rw-r--r--src/main.cpp2
-rw-r--r--src/scene.cpp10
-rw-r--r--src/vec3.cpp29
4 files changed, 21 insertions, 51 deletions
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<std::vec
auto indices_length = indices_view["byteLength"].get<size_t>();
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<std::vec
mesh_part.positions.resize(positions_length / (sizeof(float) * 3));
for (size_t i = 0; i < positions_length / (sizeof(float) * 3); i++) {
- mesh_part.positions[i] = vec3(positions_start[i * 3], positions_start[i * 3 + 1], positions_start[i * 3 + 2]);
+ mesh_part.positions[i] = vec3(positions_start[i * 3], positions_start[i * 3 + 1], positions_start[i * 3 + 2]) * scale;
}
- // std::memcpy(mesh_part.positions.data(), positions_start, positions_length);
}
- {
- auto normals_accessor_index = primitive["attributes"]["NORMAL"].get<uint32_t>();
- auto normals_view_index = accessors[normals_accessor_index]["bufferView"].get<uint32_t>();
- auto normals_view = gltf_json["bufferViews"][normals_view_index];
+ // {
+ // auto normals_accessor_index = primitive["attributes"]["NORMAL"].get<uint32_t>();
+ // auto normals_view_index = accessors[normals_accessor_index]["bufferView"].get<uint32_t>();
+ // auto normals_view = gltf_json["bufferViews"][normals_view_index];
- auto normals_buffer_index = normals_view["buffer"].get<uint32_t>();
- auto normals_offset = normals_view["byteOffset"].get<size_t>();
- auto normals_length = normals_view["byteLength"].get<size_t>();
+ // auto normals_buffer_index = normals_view["buffer"].get<uint32_t>();
+ // auto normals_offset = normals_view["byteOffset"].get<size_t>();
+ // auto normals_length = normals_view["byteLength"].get<size_t>();
- 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;
- }
-}