From 3bfd2d67e1c2d7e2d77c431f54dd68356c94d787 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 25 Jul 2021 16:52:28 +0200 Subject: Slowly rendering sponza --- src/gltf.cpp | 7 ++- src/main.cpp | 11 +++-- src/scene.cpp | 154 +++++++++++++++++++++++++++++----------------------------- 3 files changed, 88 insertions(+), 84 deletions(-) (limited to 'src') diff --git a/src/gltf.cpp b/src/gltf.cpp index 0e74aaf..72219d6 100644 --- a/src/gltf.cpp +++ b/src/gltf.cpp @@ -78,11 +78,14 @@ void gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); auto positions_buffer = buffers_content[positions_buffer_index]; - void* positions_start = positions_buffer.data() + positions_offset; + float* positions_start = (float*)(positions_buffer.data() + positions_offset); mesh_part.positions.resize(positions_length / (sizeof(float) * 3)); - std::memcpy(mesh_part.positions.data(), positions_start, positions_length); + 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]); + } + // std::memcpy(mesh_part.positions.data(), positions_start, positions_length); } { diff --git a/src/main.cpp b/src/main.cpp index 875527f..2634371 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,7 +55,7 @@ int main() { auto start = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now(); - point3 position = { 13.0, 2.0, 3.0 }; + point3 position = { 13.0, 2.0, -3.0 }; point3 target = { 0.0, 0.0, 0.0 }; auto v_fov = 20; auto aperture = 0.1; @@ -63,12 +63,13 @@ int main() { auto cam = camera(position, target, v_fov, (float)width / (float)height, aperture, focus_distance); auto main_scene = scene(cam, width, height); auto can_render = true; - // vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.triangles[0]) * main_scene.triangles.size(), sizeof(main_scene.nodes[0]) * main_scene.nodes.size() }; - vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.spheres[0]) * main_scene.spheres.size(), sizeof(main_scene.nodes[0]) * main_scene.nodes.size() }; + vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.triangles[1]) * main_scene.triangles.size(), sizeof(main_scene.nodes[0]) * main_scene.nodes.size() }; + // vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.spheres[0]) * main_scene.spheres.size(), sizeof(main_scene.nodes[0]) * main_scene.nodes.size() }; // Upload scene content std::memcpy(renderer.scene_buffer_ptr(), &main_scene.meta, sizeof(main_scene.meta)); - std::memcpy(renderer.geometry_buffer_ptr(), main_scene.spheres.data(), sizeof(main_scene.spheres[0]) * main_scene.spheres.size()); + // std::memcpy(renderer.geometry_buffer_ptr(), main_scene.spheres.data(), sizeof(main_scene.spheres[0]) * main_scene.spheres.size()); + std::memcpy(renderer.geometry_buffer_ptr(), main_scene.triangles.data(), sizeof(main_scene.triangles[0]) * main_scene.triangles.size()); std::memcpy(renderer.bvh_buffer_ptr(), main_scene.nodes.data(), sizeof(main_scene.nodes[0]) * main_scene.nodes.size()); ImGuiIO& io = ImGui::GetIO(); @@ -216,7 +217,7 @@ int main() { reset_accumulation = true; } - ImGui::SliderInt("bounces", (int32_t*)&((scene*) renderer.scene_buffer_ptr())->meta.max_bounce, 2, 250); + ImGui::SliderInt("bounces", (int32_t*)&((scene*) renderer.scene_buffer_ptr())->meta.max_bounce, 1, 250); if (ImGui::Checkbox("debug bvh", (bool*)&((scene*) renderer.scene_buffer_ptr())->meta.debug_bvh)) { reset_accumulation = true; diff --git a/src/scene.cpp b/src/scene.cpp index 9fd57eb..8e25be7 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -6,92 +6,92 @@ #include "gltf.hpp" // Return the number of instantiated spheres -void scene::random_scene() { - material ground_material = { { 0.5, 0.5, 0.5 } }; - ground_material.type = MATERIAL_TYPE::LAMBERTIAN; - spheres.emplace_back(vec3 { 0, -1000, 0 }, ground_material, 1000); - - int32_t spheres_count = 11; - for (int a = -spheres_count; a < spheres_count; a++) { - for (int b = -spheres_count; b < spheres_count; b++) { - auto choose_mat = randd(); - point3 center(a + 0.9 * randd(), 0.2, b + 0.9 * randd()); - - if ((center - point3{ 4, 0.2, 0 }).length() > 0.9) { - material sphere_material = {}; - - if (choose_mat < 0.8) { - // diffuse - sphere_material.albedo = color::random() * color::random(); - sphere_material.type = MATERIAL_TYPE::LAMBERTIAN; - } else if (choose_mat < 0.95) { - // metal - sphere_material.albedo = color::random(0.5, 1); - sphere_material.fuzz = randd(0, 0.5); - sphere_material.type = MATERIAL_TYPE::METAL; - } else { - // glass - sphere_material.ior = 1.5; - sphere_material.type = MATERIAL_TYPE::DIELECTRIC; - } - - spheres.emplace_back(center, sphere_material, 0.2); - } - } - } - - material material1 = {}; - material1.ior = 1.5; - material1.type = MATERIAL_TYPE::DIELECTRIC; - spheres.emplace_back(vec3 { 0, 1, 0 }, material1, 1.0); - - material material2 = {}; - material2.albedo = color{ 0.4, 0.2, 0.1 }; - material2.type = MATERIAL_TYPE::LAMBERTIAN; - spheres.emplace_back(vec3 { -4, 1, 0 }, material2, 1.0); - - material material3 = {}; - material3.albedo = color{ 0.7, 0.6, 0.5 }; - material3.fuzz = 0.0; - material3.type = MATERIAL_TYPE::METAL; - spheres.emplace_back(vec3 { 4, 1, 0 }, material3, 1.0); - - material material4 = {}; - material4.albedo = color{ 0.0, 0.0, 0.0 }; - material4.emissive = color{ 1.0, 1.0, 1.0 } * 1000.0; - material4.fuzz = 0.0; - material4.type = MATERIAL_TYPE::EMISSIVE; - spheres.emplace_back(vec3 { 2, 1.5, 2 }, material4, 0.5); - - spheres_count = spheres.size(); -} +// void scene::random_scene() { +// material ground_material = { { 0.5, 0.5, 0.5 } }; +// ground_material.type = MATERIAL_TYPE::LAMBERTIAN; +// spheres.emplace_back(vec3 { 0, -1000, 0 }, ground_material, 1000); +// +// int32_t spheres_count = 11; +// for (int a = -spheres_count; a < spheres_count; a++) { +// for (int b = -spheres_count; b < spheres_count; b++) { +// auto choose_mat = randd(); +// point3 center(a + 0.9 * randd(), 0.2, b + 0.9 * randd()); +// +// if ((center - point3{ 4, 0.2, 0 }).length() > 0.9) { +// material sphere_material = {}; +// +// if (choose_mat < 0.8) { +// // diffuse +// sphere_material.albedo = color::random() * color::random(); +// sphere_material.type = MATERIAL_TYPE::LAMBERTIAN; +// } else if (choose_mat < 0.95) { +// // metal +// sphere_material.albedo = color::random(0.5, 1); +// sphere_material.fuzz = randd(0, 0.5); +// sphere_material.type = MATERIAL_TYPE::METAL; +// } else { +// // glass +// sphere_material.ior = 1.5; +// sphere_material.type = MATERIAL_TYPE::DIELECTRIC; +// } +// +// spheres.emplace_back(center, sphere_material, 0.2); +// } +// } +// } +// +// material material1 = {}; +// material1.ior = 1.5; +// material1.type = MATERIAL_TYPE::DIELECTRIC; +// spheres.emplace_back(vec3 { 0, 1, 0 }, material1, 1.0); +// +// material material2 = {}; +// material2.albedo = color{ 0.4, 0.2, 0.1 }; +// material2.type = MATERIAL_TYPE::LAMBERTIAN; +// spheres.emplace_back(vec3 { -4, 1, 0 }, material2, 1.0); +// +// material material3 = {}; +// material3.albedo = color{ 0.7, 0.6, 0.5 }; +// material3.fuzz = 0.0; +// material3.type = MATERIAL_TYPE::METAL; +// spheres.emplace_back(vec3 { 4, 1, 0 }, material3, 1.0); +// +// material material4 = {}; +// material4.albedo = color{ 0.0, 0.0, 0.0 }; +// material4.emissive = color{ 1.0, 1.0, 1.0 } * 1000.0; +// material4.fuzz = 0.0; +// material4.type = MATERIAL_TYPE::EMISSIVE; +// spheres.emplace_back(vec3 { 2, 1.5, 2 }, material4, 0.5); +// +// spheres_count = spheres.size(); +// } scene::scene(camera cam, uint32_t width, uint32_t height) :meta(cam, width, height){ - // auto sponza_path = std::filesystem::path("../models/sponza"); - // auto sponza_filename = std::string("Sponza.gltf"); - // auto mesh = gltf::load(sponza_path, sponza_filename); + auto sponza_path = std::filesystem::path("../models/sponza"); + auto sponza_filename = std::string("Sponza.gltf"); + auto mesh = gltf::load(sponza_path, sponza_filename); - // auto node = mesh.nodes[0]; - // for (auto& mesh_part: node) { - // for (size_t i = 0; i < mesh_part.indices.size(); i += 3) { - // auto i1 = mesh_part.indices[i]; - // auto i2 = mesh_part.indices[i + 1]; - // auto i3 = mesh_part.indices[i + 2]; + auto node = mesh.nodes[0]; + for (auto& mesh_part: node) { + for (size_t i = 0; i < mesh_part.indices.size(); i += 3) { + auto i1 = mesh_part.indices[i]; + 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], .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] }; - // triangles.emplace_back(v1, v2, v3); - // } - // } + triangles.emplace_back(v1, v2, v3); + } + } - random_scene(); + // random_scene(); - // bvh builder(triangles, nodes); - bvh builder(spheres, nodes); + bvh builder(triangles, nodes); + // bvh builder(spheres, nodes); // builder.exporter(); for (uint32_t idx = 0; idx < builder.nodes.size(); idx++) { -- cgit v1.3