From 7c160731e702fd52e5403e0e34a600d6bf0e5848 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Mon, 2 Dec 2024 21:06:37 +0100 Subject: [PATCH] Initial camera camera repositioning --- ray-tracing-core/src/math/pos3.rs | 12 +++++++ ray-tracing-egui/src/main.rs | 57 ++++++++++++++++++++++++------- ray-tracing-egui/src/render.rs | 22 ++++-------- ray-tracing-egui/src/setup.rs | 21 ++++++------ ray-tracing-image/src/main.rs | 4 +-- ray-tracing-scene/src/examples.rs | 6 ++-- ray-tracing-tev/src/main.rs | 4 +-- 7 files changed, 82 insertions(+), 44 deletions(-) diff --git a/ray-tracing-core/src/math/pos3.rs b/ray-tracing-core/src/math/pos3.rs index 659d957..d676524 100644 --- a/ray-tracing-core/src/math/pos3.rs +++ b/ray-tracing-core/src/math/pos3.rs @@ -71,6 +71,18 @@ impl Add for Pos3 { } } +impl Sub for Pos3 { + type Output = Pos3; + + fn sub(self, rhs: Dir3) -> Self::Output { + Pos3 { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + } + } +} + impl Index for Pos3 { type Output = Float; diff --git a/ray-tracing-egui/src/main.rs b/ray-tracing-egui/src/main.rs index 9fda1cf..8532c66 100644 --- a/ray-tracing-egui/src/main.rs +++ b/ray-tracing-egui/src/main.rs @@ -1,19 +1,17 @@ -use egui::Widget; +use egui::{ahash::HashMap, Widget}; use egui_winit_vulkano::{Gui, GuiConfig}; use ray_tracing_core::prelude::*; use ray_tracing_scene::examples::example_scenes; use render::{render_thread, RENDERER}; -use setup::{get_compute_pipeline, get_framebuffers}; -use std::sync::Arc; +use setup::{get_compute_pipeline, get_framebuffers, SetupResult}; +use std::{collections::HashSet, sync::Arc}; use vulkano::{ buffer::{Buffer, BufferCreateInfo, BufferUsage}, command_buffer::{allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder}, descriptor_set::{ allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, }, - memory::allocator::{ - AllocationCreateInfo, MemoryAllocator, MemoryTypeFilter, StandardMemoryAllocator, - }, + memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator}, pipeline::Pipeline, swapchain::{self, SwapchainCreateInfo, SwapchainPresentInfo}, sync::{self, future::FenceSignalFuture, GpuFuture}, @@ -60,9 +58,9 @@ mod cs { } fn main() { - let (event_loop, physical_device, device, queue, window, surface) = setup::setup(); + let SetupResult(event_loop, physical_device, device, queue, window, surface) = setup::setup(); - let (mut swapchain, mut images) = setup::create_swapchain( + let (mut swapchain, images) = setup::create_swapchain( physical_device.clone(), device.clone(), surface.clone(), @@ -100,7 +98,7 @@ fn main() { scene: scene.0, renderer_id: 0, camera_pos: e.camera_pos, - camera_dir: e.camera_dir, + camera_look_at: e.camera_look_at, camera_up: e.camera_up, camera_horizontal_fov: e.horizontal_fov, } @@ -124,6 +122,7 @@ fn main() { let mut buffer = None; let cs = cs::load(device.clone()).unwrap(); + let mut keypressed = HashSet::new(); let cpu_buffers: Vec<_> = framebuffers .iter() @@ -148,8 +147,20 @@ fn main() { let mut pipeline = get_compute_pipeline(device.clone(), cs.clone()); event_loop.run(move |event, _, control_flow| match event { - Event::WindowEvent { window_id, event } => { - gui.update(&event); + Event::WindowEvent { + window_id: _, + event, + } => { + if !gui.update(&event) { + if let WindowEvent::KeyboardInput { input, .. } = event { + if let Some(virt) = input.virtual_keycode { + match input.state { + winit::event::ElementState::Pressed => keypressed.insert(virt), + winit::event::ElementState::Released => keypressed.remove(&virt), + }; + }; + }; + } match event { WindowEvent::Resized(_) => { recreate_swapchain = true; @@ -178,6 +189,27 @@ fn main() { } let mut settings_changed = false; + if keypressed.contains(&winit::event::VirtualKeyCode::Right) { + let translation = Dir3::cross( + settings.camera_look_at - settings.camera_pos, + settings.camera_up, + ) + .normalize(); + settings.camera_pos = settings.camera_pos + translation * 0.1; + settings.camera_look_at = settings.camera_look_at + translation * 0.1; + settings_changed = true; + } + if keypressed.contains(&winit::event::VirtualKeyCode::Left) { + let translation = Dir3::cross( + settings.camera_look_at - settings.camera_pos, + settings.camera_up, + ) + .normalize(); + settings.camera_pos = settings.camera_pos - translation * 0.1; + settings.camera_look_at = settings.camera_look_at - translation * 0.1; + settings_changed = true; + } + gui.immediate_ui(|gui| { let ctx = gui.context(); egui::Window::new("panel").show(&ctx, |ui| { @@ -195,7 +227,7 @@ fn main() { settings_changed = true; let e = scenes[settings.scene](); settings.camera_pos = e.camera_pos; - settings.camera_dir = e.camera_dir; + settings.camera_look_at = e.camera_look_at; settings.camera_up = e.camera_up; settings.camera_horizontal_fov = e.horizontal_fov; } @@ -340,6 +372,7 @@ fn main() { .then_signal_fence_and_flush(); fences[image_i as usize] = match future.map_err(Validated::unwrap) { + #[allow(clippy::arc_with_non_send_sync)] Ok(value) => Some(Arc::new(value)), Err(VulkanError::OutOfDate) => { recreate_swapchain = true; diff --git a/ray-tracing-egui/src/render.rs b/ray-tracing-egui/src/render.rs index a509744..4d01df9 100644 --- a/ray-tracing-egui/src/render.rs +++ b/ray-tracing-egui/src/render.rs @@ -11,17 +11,14 @@ use ray_tracing_renderer::{ depth_renderer::DepthRenderer, next_event_estimation::NextEventEstimation, path_tracer::PathTracer, path_tracer_importance::PathTracerImportance, }; -use ray_tracing_scene::examples::{self, example_scenes, ExampleScene}; +use ray_tracing_scene::examples; use rayon::{ iter::{IndexedParallelIterator, ParallelIterator}, slice::ParallelSliceMut, }; use vulkano::{ buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer}, - memory::allocator::{ - AllocationCreateInfo, FreeListAllocator, GenericMemoryAllocator, MemoryAllocator, - MemoryTypeFilter, - }, + memory::allocator::{AllocationCreateInfo, MemoryAllocator, MemoryTypeFilter}, }; type DynRenderer = @@ -48,15 +45,11 @@ pub struct RenderSettings { pub scene: &'static str, pub renderer_id: usize, pub camera_pos: Pos3, - pub camera_dir: Dir3, + pub camera_look_at: Pos3, pub camera_up: Dir3, pub camera_horizontal_fov: Float, } -pub enum ControlMessages { - SetSettings(RenderSettings), -} - pub struct Data { pub width: u32, pub height: u32, @@ -79,11 +72,11 @@ pub fn render_thread( let e = example_scenes[settings.scene](); let mut scene = (e.scene)(); - let mut camera = BasicCamera::new( + let mut camera = BasicCamera::from_look_at( settings.width, settings.height, settings.camera_pos, - settings.camera_dir, + settings.camera_look_at, settings.camera_up, settings.camera_horizontal_fov, ); @@ -97,15 +90,14 @@ pub fn render_thread( settings = s; let e = example_scenes[settings.scene](); scene = (e.scene)(); - camera = BasicCamera::new( + camera = BasicCamera::from_look_at( settings.width, settings.height, settings.camera_pos, - settings.camera_dir, + settings.camera_look_at, settings.camera_up, settings.camera_horizontal_fov, ); - dbg!(&camera); buffer = vec![0.0; settings.width as usize * settings.height as usize * 3]; renderer = (RENDERER[settings.renderer_id].1)(settings.width, settings.height); samples = 0; diff --git a/ray-tracing-egui/src/setup.rs b/ray-tracing-egui/src/setup.rs index 4a2d8ae..2a53064 100644 --- a/ray-tracing-egui/src/setup.rs +++ b/ray-tracing-egui/src/setup.rs @@ -5,7 +5,6 @@ use vulkano::{ physical::{PhysicalDevice, PhysicalDeviceType}, Device, DeviceCreateInfo, DeviceExtensions, Features, Queue, QueueCreateInfo, QueueFlags, }, - format::Format, image::{view::ImageView, Image, ImageUsage}, instance::{Instance, InstanceCreateFlags, InstanceCreateInfo}, pipeline::{ @@ -54,14 +53,16 @@ fn select_physical_device( .expect("no device available") } -pub fn setup() -> ( - EventLoop<()>, - Arc, - Arc, - Arc, - Arc, - Arc, -) { +pub struct SetupResult( + pub EventLoop<()>, + pub Arc, + pub Arc, + pub Arc, + pub Arc, + pub Arc, +); + +pub fn setup() -> SetupResult { let event_loop: EventLoop<()> = EventLoopBuilder::default().build(); let library = VulkanLibrary::new().unwrap(); @@ -111,7 +112,7 @@ pub fn setup() -> ( let queue = queues.next().unwrap(); - (event_loop, physical_device, device, queue, window, surface) + SetupResult(event_loop, physical_device, device, queue, window, surface) } pub fn create_swapchain( diff --git a/ray-tracing-image/src/main.rs b/ray-tracing-image/src/main.rs index b4b25a3..2c9423f 100644 --- a/ray-tracing-image/src/main.rs +++ b/ray-tracing-image/src/main.rs @@ -47,11 +47,11 @@ fn main() -> ImageResult<()> { // let s = BasicScene::new(); let s = basic_cornell(); - let c = BasicCamera::new( + let c = BasicCamera::from_look_at( 400, 400, s.camera_pos, - s.camera_dir, + s.camera_look_at, s.camera_up, s.horizontal_fov, ); diff --git a/ray-tracing-scene/src/examples.rs b/ray-tracing-scene/src/examples.rs index d2777f0..3899241 100644 --- a/ray-tracing-scene/src/examples.rs +++ b/ray-tracing-scene/src/examples.rs @@ -10,7 +10,7 @@ use std::fmt::Debug; pub struct ExampleScene { pub scene: fn() -> Box + Sync>, pub camera_pos: Pos3, - pub camera_dir: Dir3, + pub camera_look_at: Pos3, pub camera_up: Dir3, pub horizontal_fov: Float, } @@ -57,7 +57,7 @@ pub fn cornell2() -> ExampleScene { ExampleScene { scene: f, camera_pos: Pos3::new(275.0, 275.0, -800.0), - camera_dir: Dir3::new(0.0, 0.0, 1.0), + camera_look_at: Pos3::new(275.0, 275.0, 275.0), camera_up: Dir3::up(), horizontal_fov: 90.0_f32.to_radians(), } @@ -137,7 +137,7 @@ pub fn basic_cornell() -> ExampleScene { ExampleScene { scene: f, camera_pos: Pos3::new(-6.0, 0.0, 0.0), - camera_dir: Dir3::new(1.0, 0.0, 0.0), + camera_look_at: Pos3::new(0.0, 0.0, 0.0), camera_up: Dir3::up(), horizontal_fov: Float::to_radians(90.0), } diff --git a/ray-tracing-tev/src/main.rs b/ray-tracing-tev/src/main.rs index eda26b8..29996b6 100644 --- a/ray-tracing-tev/src/main.rs +++ b/ray-tracing-tev/src/main.rs @@ -99,11 +99,11 @@ fn main() { let f = map.get(scene).unwrap(); let e = f(); - let c = BasicCamera::new( + let c = BasicCamera::from_look_at( args.width, args.height, e.camera_pos, - e.camera_dir, + e.camera_look_at, e.camera_up, e.horizontal_fov, );