Initial camera camera repositioning

This commit is contained in:
hal8174 2024-12-02 21:06:37 +01:00
parent e61964c513
commit 7c160731e7
7 changed files with 82 additions and 44 deletions

View file

@ -71,6 +71,18 @@ impl Add<Dir3> for Pos3 {
} }
} }
impl Sub<Dir3> 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<usize> for Pos3 { impl Index<usize> for Pos3 {
type Output = Float; type Output = Float;

View file

@ -1,19 +1,17 @@
use egui::Widget; use egui::{ahash::HashMap, Widget};
use egui_winit_vulkano::{Gui, GuiConfig}; use egui_winit_vulkano::{Gui, GuiConfig};
use ray_tracing_core::prelude::*; use ray_tracing_core::prelude::*;
use ray_tracing_scene::examples::example_scenes; use ray_tracing_scene::examples::example_scenes;
use render::{render_thread, RENDERER}; use render::{render_thread, RENDERER};
use setup::{get_compute_pipeline, get_framebuffers}; use setup::{get_compute_pipeline, get_framebuffers, SetupResult};
use std::sync::Arc; use std::{collections::HashSet, sync::Arc};
use vulkano::{ use vulkano::{
buffer::{Buffer, BufferCreateInfo, BufferUsage}, buffer::{Buffer, BufferCreateInfo, BufferUsage},
command_buffer::{allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder}, command_buffer::{allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder},
descriptor_set::{ descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
}, },
memory::allocator::{ memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
AllocationCreateInfo, MemoryAllocator, MemoryTypeFilter, StandardMemoryAllocator,
},
pipeline::Pipeline, pipeline::Pipeline,
swapchain::{self, SwapchainCreateInfo, SwapchainPresentInfo}, swapchain::{self, SwapchainCreateInfo, SwapchainPresentInfo},
sync::{self, future::FenceSignalFuture, GpuFuture}, sync::{self, future::FenceSignalFuture, GpuFuture},
@ -60,9 +58,9 @@ mod cs {
} }
fn main() { 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(), physical_device.clone(),
device.clone(), device.clone(),
surface.clone(), surface.clone(),
@ -100,7 +98,7 @@ fn main() {
scene: scene.0, scene: scene.0,
renderer_id: 0, renderer_id: 0,
camera_pos: e.camera_pos, camera_pos: e.camera_pos,
camera_dir: e.camera_dir, camera_look_at: e.camera_look_at,
camera_up: e.camera_up, camera_up: e.camera_up,
camera_horizontal_fov: e.horizontal_fov, camera_horizontal_fov: e.horizontal_fov,
} }
@ -124,6 +122,7 @@ fn main() {
let mut buffer = None; let mut buffer = None;
let cs = cs::load(device.clone()).unwrap(); let cs = cs::load(device.clone()).unwrap();
let mut keypressed = HashSet::new();
let cpu_buffers: Vec<_> = framebuffers let cpu_buffers: Vec<_> = framebuffers
.iter() .iter()
@ -148,8 +147,20 @@ fn main() {
let mut pipeline = get_compute_pipeline(device.clone(), cs.clone()); let mut pipeline = get_compute_pipeline(device.clone(), cs.clone());
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent { window_id, event } => { Event::WindowEvent {
gui.update(&event); 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 { match event {
WindowEvent::Resized(_) => { WindowEvent::Resized(_) => {
recreate_swapchain = true; recreate_swapchain = true;
@ -178,6 +189,27 @@ fn main() {
} }
let mut settings_changed = false; 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| { gui.immediate_ui(|gui| {
let ctx = gui.context(); let ctx = gui.context();
egui::Window::new("panel").show(&ctx, |ui| { egui::Window::new("panel").show(&ctx, |ui| {
@ -195,7 +227,7 @@ fn main() {
settings_changed = true; settings_changed = true;
let e = scenes[settings.scene](); let e = scenes[settings.scene]();
settings.camera_pos = e.camera_pos; 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_up = e.camera_up;
settings.camera_horizontal_fov = e.horizontal_fov; settings.camera_horizontal_fov = e.horizontal_fov;
} }
@ -340,6 +372,7 @@ fn main() {
.then_signal_fence_and_flush(); .then_signal_fence_and_flush();
fences[image_i as usize] = match future.map_err(Validated::unwrap) { fences[image_i as usize] = match future.map_err(Validated::unwrap) {
#[allow(clippy::arc_with_non_send_sync)]
Ok(value) => Some(Arc::new(value)), Ok(value) => Some(Arc::new(value)),
Err(VulkanError::OutOfDate) => { Err(VulkanError::OutOfDate) => {
recreate_swapchain = true; recreate_swapchain = true;

View file

@ -11,17 +11,14 @@ use ray_tracing_renderer::{
depth_renderer::DepthRenderer, next_event_estimation::NextEventEstimation, depth_renderer::DepthRenderer, next_event_estimation::NextEventEstimation,
path_tracer::PathTracer, path_tracer_importance::PathTracerImportance, path_tracer::PathTracer, path_tracer_importance::PathTracerImportance,
}; };
use ray_tracing_scene::examples::{self, example_scenes, ExampleScene}; use ray_tracing_scene::examples;
use rayon::{ use rayon::{
iter::{IndexedParallelIterator, ParallelIterator}, iter::{IndexedParallelIterator, ParallelIterator},
slice::ParallelSliceMut, slice::ParallelSliceMut,
}; };
use vulkano::{ use vulkano::{
buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer}, buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer},
memory::allocator::{ memory::allocator::{AllocationCreateInfo, MemoryAllocator, MemoryTypeFilter},
AllocationCreateInfo, FreeListAllocator, GenericMemoryAllocator, MemoryAllocator,
MemoryTypeFilter,
},
}; };
type DynRenderer = type DynRenderer =
@ -48,15 +45,11 @@ pub struct RenderSettings {
pub scene: &'static str, pub scene: &'static str,
pub renderer_id: usize, pub renderer_id: usize,
pub camera_pos: Pos3, pub camera_pos: Pos3,
pub camera_dir: Dir3, pub camera_look_at: Pos3,
pub camera_up: Dir3, pub camera_up: Dir3,
pub camera_horizontal_fov: Float, pub camera_horizontal_fov: Float,
} }
pub enum ControlMessages {
SetSettings(RenderSettings),
}
pub struct Data { pub struct Data {
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
@ -79,11 +72,11 @@ pub fn render_thread(
let e = example_scenes[settings.scene](); let e = example_scenes[settings.scene]();
let mut scene = (e.scene)(); let mut scene = (e.scene)();
let mut camera = BasicCamera::new( let mut camera = BasicCamera::from_look_at(
settings.width, settings.width,
settings.height, settings.height,
settings.camera_pos, settings.camera_pos,
settings.camera_dir, settings.camera_look_at,
settings.camera_up, settings.camera_up,
settings.camera_horizontal_fov, settings.camera_horizontal_fov,
); );
@ -97,15 +90,14 @@ pub fn render_thread(
settings = s; settings = s;
let e = example_scenes[settings.scene](); let e = example_scenes[settings.scene]();
scene = (e.scene)(); scene = (e.scene)();
camera = BasicCamera::new( camera = BasicCamera::from_look_at(
settings.width, settings.width,
settings.height, settings.height,
settings.camera_pos, settings.camera_pos,
settings.camera_dir, settings.camera_look_at,
settings.camera_up, settings.camera_up,
settings.camera_horizontal_fov, settings.camera_horizontal_fov,
); );
dbg!(&camera);
buffer = vec![0.0; settings.width as usize * settings.height as usize * 3]; buffer = vec![0.0; settings.width as usize * settings.height as usize * 3];
renderer = (RENDERER[settings.renderer_id].1)(settings.width, settings.height); renderer = (RENDERER[settings.renderer_id].1)(settings.width, settings.height);
samples = 0; samples = 0;

View file

@ -5,7 +5,6 @@ use vulkano::{
physical::{PhysicalDevice, PhysicalDeviceType}, physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, Features, Queue, QueueCreateInfo, QueueFlags, Device, DeviceCreateInfo, DeviceExtensions, Features, Queue, QueueCreateInfo, QueueFlags,
}, },
format::Format,
image::{view::ImageView, Image, ImageUsage}, image::{view::ImageView, Image, ImageUsage},
instance::{Instance, InstanceCreateFlags, InstanceCreateInfo}, instance::{Instance, InstanceCreateFlags, InstanceCreateInfo},
pipeline::{ pipeline::{
@ -54,14 +53,16 @@ fn select_physical_device(
.expect("no device available") .expect("no device available")
} }
pub fn setup() -> ( pub struct SetupResult(
EventLoop<()>, pub EventLoop<()>,
Arc<PhysicalDevice>, pub Arc<PhysicalDevice>,
Arc<Device>, pub Arc<Device>,
Arc<Queue>, pub Arc<Queue>,
Arc<Window>, pub Arc<Window>,
Arc<Surface>, pub Arc<Surface>,
) { );
pub fn setup() -> SetupResult {
let event_loop: EventLoop<()> = EventLoopBuilder::default().build(); let event_loop: EventLoop<()> = EventLoopBuilder::default().build();
let library = VulkanLibrary::new().unwrap(); let library = VulkanLibrary::new().unwrap();
@ -111,7 +112,7 @@ pub fn setup() -> (
let queue = queues.next().unwrap(); 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( pub fn create_swapchain(

View file

@ -47,11 +47,11 @@ fn main() -> ImageResult<()> {
// let s = BasicScene::new(); // let s = BasicScene::new();
let s = basic_cornell(); let s = basic_cornell();
let c = BasicCamera::new( let c = BasicCamera::from_look_at(
400, 400,
400, 400,
s.camera_pos, s.camera_pos,
s.camera_dir, s.camera_look_at,
s.camera_up, s.camera_up,
s.horizontal_fov, s.horizontal_fov,
); );

View file

@ -10,7 +10,7 @@ use std::fmt::Debug;
pub struct ExampleScene<R: Rng> { pub struct ExampleScene<R: Rng> {
pub scene: fn() -> Box<dyn Scene<R> + Sync>, pub scene: fn() -> Box<dyn Scene<R> + Sync>,
pub camera_pos: Pos3, pub camera_pos: Pos3,
pub camera_dir: Dir3, pub camera_look_at: Pos3,
pub camera_up: Dir3, pub camera_up: Dir3,
pub horizontal_fov: Float, pub horizontal_fov: Float,
} }
@ -57,7 +57,7 @@ pub fn cornell2<R: Rng + Debug + 'static>() -> ExampleScene<R> {
ExampleScene { ExampleScene {
scene: f, scene: f,
camera_pos: Pos3::new(275.0, 275.0, -800.0), 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(), camera_up: Dir3::up(),
horizontal_fov: 90.0_f32.to_radians(), horizontal_fov: 90.0_f32.to_radians(),
} }
@ -137,7 +137,7 @@ pub fn basic_cornell<R: Rng + 'static>() -> ExampleScene<R> {
ExampleScene { ExampleScene {
scene: f, scene: f,
camera_pos: Pos3::new(-6.0, 0.0, 0.0), 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(), camera_up: Dir3::up(),
horizontal_fov: Float::to_radians(90.0), horizontal_fov: Float::to_radians(90.0),
} }

View file

@ -99,11 +99,11 @@ fn main() {
let f = map.get(scene).unwrap(); let f = map.get(scene).unwrap();
let e = f(); let e = f();
let c = BasicCamera::new( let c = BasicCamera::from_look_at(
args.width, args.width,
args.height, args.height,
e.camera_pos, e.camera_pos,
e.camera_dir, e.camera_look_at,
e.camera_up, e.camera_up,
e.horizontal_fov, e.horizontal_fov,
); );