diff --git a/src/lib.rs b/src/lib.rs index f2f9c39..18937dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,13 @@ pub struct SphericalPosition { pub distance: f32, } +#[derive(Debug, Clone, Copy)] +#[repr(u8)] +pub enum UseNormal { + Light, + Camera, +} + #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct RedrawArgs { @@ -36,7 +43,8 @@ pub struct RedrawArgs { pub light_spread: f32, pub accum_sigma: f32, pub accum_scale: f32, - pub reflections: u32, + pub reflections: u8, + pub use_normal: UseNormal, pub show_axes: bool, pub show_shapes: bool, pub show_hit_emission: bool, @@ -305,12 +313,15 @@ impl Core { if d2 > 9. * sigma2 { continue; } - assert!(hit.normal.is_normalized()); + let normal = match args.use_normal { + UseNormal::Light => light_hit.normal, + UseNormal::Camera => hit.normal, + }; + assert!(normal.is_normalized()); assert!(hit.incident.dir.is_normalized()); let reflector = Lambertian; let in_lm = 1.0; - let out_cd = - in_lm * reflector.brdf(light_hit.normal, light_hit.incident.dir, -ray.dir); + let out_cd = in_lm * reflector.brdf(normal, light_hit.incident.dir, -ray.dir); let weight = accum_normalizator * (-0.5 * d2 / sigma2).exp(); total_cd += weight * out_cd; } diff --git a/src/main.rs b/src/main.rs index f08bf16..cb11049 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{f32::consts::PI, sync::Arc}; use glam::uvec2; -use photon_light::{Core, RedrawArgs, SphericalPosition, init_gpu_inner}; +use photon_light::{Core, RedrawArgs, SphericalPosition, UseNormal, init_gpu_inner}; use winit::{ application::ApplicationHandler, event::WindowEvent, @@ -53,6 +53,7 @@ impl MainWindow { accum_sigma: 0.025, accum_scale: 0.01, reflections: 2, + use_normal: UseNormal::Light, show_axes: true, show_shapes: true, show_hit_emission: false, diff --git a/ui/src/api.hxx b/ui/src/api.hxx index caf32cd..bc96bc8 100644 --- a/ui/src/api.hxx +++ b/ui/src/api.hxx @@ -13,6 +13,11 @@ struct SphericalPosition { float distance = 1.; }; +enum class UseNormal: std::uint8_t { + Light, + Camera, +}; + struct RedrawArgs { SphericalPosition camera_position; SphericalPosition light_position; @@ -20,7 +25,8 @@ struct RedrawArgs { float light_spread = 0.; float accum_sigma = 1.; float accum_scale = 1.; - std::uint32_t reflections = 0; + std::uint8_t reflections = 0; + UseNormal use_normal = UseNormal::Light; bool show_axes = true; bool show_shapes = true; bool show_hit_emission = true; @@ -31,6 +37,7 @@ struct RedrawArgs { }; } // namespace ffi +using ffi::UseNormal; using ffi::RedrawArgs; using ffi::SphericalPosition; diff --git a/ui/src/main_window.cxx b/ui/src/main_window.cxx index dff2f7d..11ae0f2 100644 --- a/ui/src/main_window.cxx +++ b/ui/src/main_window.cxx @@ -16,6 +16,11 @@ float deg_to_rad(float val) { } void PhotonLight::updateView() { + UseNormal use_normal = {}; + if (m_ui->normalFromLight->isChecked()) + use_normal = UseNormal::Light; + else if (m_ui->normalFromCamera->isChecked()) + use_normal = UseNormal::Camera; RedrawArgs args{ .camera_position = SphericalPosition{ .yaw = deg_to_rad(m_ui->cameraYaw->value()), @@ -31,7 +36,8 @@ void PhotonLight::updateView() { .light_spread = 0.125, .accum_sigma = exp10f(m_ui->accumSigma->value() / 25.0), .accum_scale = exp10f(m_ui->accumScale->value() / 25.0), - .reflections = std::uint32_t(m_ui->reflections->value()), + .reflections = std::uint8_t(m_ui->reflections->value()), + .use_normal = use_normal, .show_axes = m_ui->displayAxes->isChecked(), .show_shapes = m_ui->displayShapes->isChecked(), .show_hit_emission = m_ui->displayEmitted->isChecked(), @@ -51,4 +57,9 @@ void PhotonLight::updateView() { m_ui->viewport->setView(args); } +void PhotonLight::updateViewIf(bool update) { + if (update) + updateView(); +} + #include "moc_main_window.cpp" diff --git a/ui/src/main_window.hxx b/ui/src/main_window.hxx index 1ca73cc..27b63ff 100644 --- a/ui/src/main_window.hxx +++ b/ui/src/main_window.hxx @@ -16,6 +16,7 @@ public: public slots: void updateView(); + void updateViewIf(bool update); // for radio buttons private: const std::unique_ptr m_ui; diff --git a/ui/src/main_window.ui b/ui/src/main_window.ui index 697c566..5802a1d 100644 --- a/ui/src/main_window.ui +++ b/ui/src/main_window.ui @@ -298,6 +298,30 @@ + + + + Use normal at + + + + + + + Hit position + + + true + + + + + + + Show position + + + @@ -637,8 +661,41 @@ + + normalFromLight + toggled(bool) + MainWindow + updateViewIf(bool) + + + 1429 + 787 + + + 1381 + 783 + + + + + normalFromCamera + toggled(bool) + MainWindow + updateViewIf(bool) + + + 1932 + 816 + + + 1600 + 874 + + + updateView() + updateViewIf(bool)