From d9560941f2cd848902708e9a36bc1360c9db4753 Mon Sep 17 00:00:00 2001 From: Yuki Nishidate Date: Sat, 26 Oct 2024 19:36:55 +0900 Subject: [PATCH] Removed hardcoded hacks --- .../extensions/ray_queries/ray_queries.cpp | 53 ++++++++++++------- samples/extensions/ray_queries/ray_queries.h | 1 + 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/samples/extensions/ray_queries/ray_queries.cpp b/samples/extensions/ray_queries/ray_queries.cpp index 6324cddfb..3c7a03a45 100644 --- a/samples/extensions/ray_queries/ray_queries.cpp +++ b/samples/extensions/ray_queries/ray_queries.cpp @@ -186,8 +186,8 @@ bool RayQueries::prepare(const vkb::ApplicationOptions &options) camera.type = vkb::CameraType::FirstPerson; camera.set_perspective(60.0f, static_cast(width) / static_cast(height), 0.1f, 512.0f); - camera.set_rotation(glm::vec3(0.0f, 0.0f, 0.0f)); - camera.set_translation(glm::vec3(0.0f, 1.5f, 0.f)); + camera.set_rotation(glm::vec3(0.0f, 90.0f, 0.0f)); + camera.set_translation(glm::vec3(0.0f, -2.0f, 0.f)); load_scene(); create_bottom_level_acceleration_structure(); @@ -282,16 +282,15 @@ void RayQueries::create_bottom_level_acceleration_structure() bottom_level_acceleration_structure->build(queue, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR, VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR); } -void RayQueries::load_scene() +void RayQueries::load_node(vkb::sg::Node &node) { - model = {}; - - vkb::GLTFLoader loader{get_device()}; - auto scene = loader.read_scene_from_file("scenes/sponza/Sponza01.gltf"); - - for (auto &&mesh : scene->get_components()) + if (node.has_component()) { - for (auto &&sub_mesh : mesh->get_submeshes()) + auto &mesh = node.get_component(); + auto &transform_matrix = node.get_transform().get_world_matrix(); + auto &normal_matrix = glm::transpose(glm::inverse(glm::mat3(transform_matrix))); + + for (auto &&sub_mesh : mesh.get_submeshes()) { auto pts_ = CopyBuffer{}(sub_mesh->vertex_buffers, "position"); const auto normals_ = CopyBuffer{}(sub_mesh->vertex_buffers, "normal"); @@ -303,8 +302,9 @@ void RayQueries::load_scene() const float sponza_scale = 0.01f; for (size_t i = 0; i < pts_.size(); ++i) { - model.vertices[vertex_start_index + i].position = sponza_scale * pts_[i].yzx; - model.vertices[vertex_start_index + i].normal = normals_[i].yzx; + // For simplicity, pre-multiply the transformation + model.vertices[vertex_start_index + i].position = transform_matrix * sponza_scale * glm::vec4(pts_[i], 1.0f); + model.vertices[vertex_start_index + i].normal = normal_matrix * normals_[i]; } } @@ -332,6 +332,21 @@ void RayQueries::load_scene() } } } + + for (auto &child : node.get_children()) + { + load_node(*child); + } +} + +void RayQueries::load_scene() +{ + model = {}; + + vkb::GLTFLoader loader{get_device()}; + auto scene = loader.read_scene_from_file("scenes/sponza/Sponza01.gltf"); + + load_node(scene->get_root_node()); } void RayQueries::create_descriptor_pool() @@ -395,7 +410,7 @@ void RayQueries::prepare_pipelines() { VkPipelineInputAssemblyStateCreateInfo input_assembly_state = vkb::initializers::pipeline_input_assembly_state_create_info(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); - VkPipelineRasterizationStateCreateInfo rasterization_state = vkb::initializers::pipeline_rasterization_state_create_info(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0); + VkPipelineRasterizationStateCreateInfo rasterization_state = vkb::initializers::pipeline_rasterization_state_create_info(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); VkPipelineColorBlendAttachmentState blend_attachment_state = vkb::initializers::pipeline_color_blend_attachment_state(0xf, VK_FALSE); @@ -488,13 +503,15 @@ void RayQueries::update_uniform_buffers() { assert(!!uniform_buffer); global_uniform.camera_position = camera.position; - global_uniform.proj = camera.matrices.perspective; + global_uniform.proj = vkb::rendering::vulkan_style_projection(camera.matrices.perspective); global_uniform.view = camera.matrices.view; - const float R = 1.f; - const float P = 2.f * 3.14159f / 5000.f; - const float t = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - start_time).count()) / 1000.f; - global_uniform.light_position = glm::vec3(2 * R * cosf(t * P), R * sinf(t * P), -10.f); + const float PI = 3.14159f; + const float radius = 100.f; + const float speed = 2.f * PI / 10000.f; + const float time = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - start_time).count()); + const float angle = glm::mod(time * speed, PI); + global_uniform.light_position = glm::vec3(0.0f, radius * sinf(angle), radius * cosf(angle)); uniform_buffer->update(&global_uniform, sizeof(global_uniform)); } diff --git a/samples/extensions/ray_queries/ray_queries.h b/samples/extensions/ray_queries/ray_queries.h index 75dd5a9d2..808de6966 100644 --- a/samples/extensions/ray_queries/ray_queries.h +++ b/samples/extensions/ray_queries/ray_queries.h @@ -88,6 +88,7 @@ class RayQueries : public ApiVulkanSample void build_command_buffers() override; void create_uniforms(); + void load_node(vkb::sg::Node &node); void load_scene(); void create_descriptor_pool(); void create_descriptor_sets();