Skip to content

Commit

Permalink
Add rudimentary camera translation to GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Jun 15, 2024
1 parent cb97d1e commit b79f132
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pathtracer-gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ clap = { version = "4.5.4", features = ["derive"] }
eframe = { version = "0.27.2", default-features = false, features = ["default_fonts", "x11", "glow"] }
egui = "0.27.2"
kdtree = { version = "1.0.0", path = "../kdtree" }
nalgebra = { version = "0.32.5", default-features = false }
rand = { version = "0.8.5", default-features = false, features = ["std", "small_rng"] }
scene = { version = "1.0.0", path = "../scene" }
tracing = { version = "1.0.0", path = "../tracing" }
Expand Down
27 changes: 24 additions & 3 deletions pathtracer-gui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::Parser;
use eframe::egui;
use egui::Vec2;
use kdtree::{build::build_kdtree, build_sah::SahKdTreeBuilder};
use nalgebra::Vector3;
use scene::Scene;
use std::{str, sync::Arc};
use tracing::pathtracer::Pathtracer;
Expand All @@ -24,6 +26,7 @@ struct Args {
}

struct PathtracerGui {
size: Vec2,
last_meta: Option<RenderMeta>,
texture: Option<egui::TextureHandle>,
workers: Workers,
Expand All @@ -32,6 +35,7 @@ struct PathtracerGui {
impl PathtracerGui {
fn new(workers: Workers) -> Self {
Self {
size: Vec2::new(512.0, 512.0),
last_meta: None,
texture: None,
workers,
Expand All @@ -56,10 +60,27 @@ impl eframe::App for PathtracerGui {
}
});

let mut translation = Vector3::zeros();
if ui.input(|i| i.key_pressed(egui::Key::ArrowUp)) {
translation.y = 0.1;
}
if ui.input(|i| i.key_pressed(egui::Key::ArrowDown)) {
translation.y = -0.1;
}
if ui.input(|i| i.key_pressed(egui::Key::ArrowLeft)) {
translation.x = -0.1;
}
if ui.input(|i| i.key_pressed(egui::Key::ArrowRight)) {
translation.x = 0.1;
}

if resize {
let width = ui.available_size().x as u32;
let height = ui.available_size().y as u32;
self.workers.send(width, height);
self.size = ui.available_size();
}

if resize || translation != Vector3::zeros() {
self.workers
.send(self.size.x as u32, self.size.y as u32, translation);
}

if let Some(texture) = &self.texture {
Expand Down
4 changes: 3 additions & 1 deletion pathtracer-gui/src/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
time,
};

use nalgebra::Vector3;
use rand::{rngs::SmallRng, SeedableRng};
use scene::camera::{Camera, Pinhole};
use tracing::{image_buffer::ImageBuffer, pathtracer::Pathtracer, raylogger::RayLogger};
Expand Down Expand Up @@ -100,7 +101,8 @@ impl Workers {
}
}

pub fn send(&self, width: u32, height: u32) {
pub fn send(&mut self, width: u32, height: u32, translation: Vector3<f32>) {
self.camera = self.camera.translate(&translation);
self.settings
.as_ref()
.unwrap()
Expand Down
10 changes: 10 additions & 0 deletions scene/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ impl Camera {
fov_degrees,
}
}

pub fn translate(&self, v: &Vector3<f32>) -> Self {
Camera {
position: self.position + v,
direction: self.direction,
up: self.up,
right: self.right,
fov_degrees: self.fov_degrees,
}
}
}

#[derive(Clone, Debug)]
Expand Down

0 comments on commit b79f132

Please sign in to comment.