Compare commits

...

2 commits

Author SHA1 Message Date
96e7085e37
Resolve all warnings. 2025-05-28 19:27:55 +02:00
86d009fb4c
Fix hopefully last nan for iridescent material 2025-05-28 19:08:38 +02:00
11 changed files with 68 additions and 42 deletions

View file

@ -23,6 +23,7 @@ use vulkano::{
type DynRenderer = type DynRenderer =
dyn ClassicalRenderer<SmallRng, Box<dyn Scene<SmallRng> + Sync>, BasicCamera> + Sync; dyn ClassicalRenderer<SmallRng, Box<dyn Scene<SmallRng> + Sync>, BasicCamera> + Sync;
#[allow(clippy::type_complexity)]
pub const RENDERER: [(&str, fn(u32, u32) -> Box<DynRenderer>); 5] = [ pub const RENDERER: [(&str, fn(u32, u32) -> Box<DynRenderer>); 5] = [
("Depth", |w, h| { ("Depth", |w, h| {
Box::new(DepthRenderer::new(w, h)) as Box<DynRenderer> Box::new(DepthRenderer::new(w, h)) as Box<DynRenderer>

View file

@ -47,7 +47,7 @@ impl Iridescent {
let imag = (num_imag * denom_real - num_real * denom_imag) let imag = (num_imag * denom_real - num_real * denom_imag)
/ (denom_real * denom_real + denom_imag * denom_imag); / (denom_real * denom_real + denom_imag * denom_imag);
// if !real.is_normal() || !imag.is_normal() { // if real.is_nan() || imag.is_nan() {
// dbg!( // dbg!(
// l, theta1, theta2, s_polaized, r12, phi, num_real, num_imag, denom_real, // l, theta1, theta2, s_polaized, r12, phi, num_real, num_imag, denom_real,
// denom_imag, real, imag // denom_imag, real, imag
@ -57,13 +57,13 @@ impl Iridescent {
real * real + imag * imag real * real + imag * imag
} }
pub fn abs_C_squared(&self, l: f32, theta1: f32, theta2: f32, s_polaized: bool) -> f32 { pub fn abs_c_squared(&self, l: f32, theta1: f32, theta2: f32, s_polaized: bool) -> f32 {
let local_abs_r_1_squared = self.abs_r_1_squared(l, theta1, theta2, s_polaized); let local_abs_r_1_squared = self.abs_r_1_squared(l, theta1, theta2, s_polaized);
local_abs_r_1_squared / (1.0 - local_abs_r_1_squared) local_abs_r_1_squared / (1.0 - local_abs_r_1_squared)
} }
fn cos_K_Delta(&self, l: f32, theta1: f32, theta2: f32, s_polaized: bool) -> f32 { fn cos_k_delta(&self, l: f32, theta1: f32, theta2: f32, s_polaized: bool) -> f32 {
let k1z = 2.0 * f32::consts::PI * self.n1 * theta1.cos() / l; let k1z = 2.0 * f32::consts::PI * self.n1 * theta1.cos() / l;
let k2z = 2.0 * f32::consts::PI * self.n2 * theta2.cos() / l; let k2z = 2.0 * f32::consts::PI * self.n2 * theta2.cos() / l;
@ -81,11 +81,11 @@ impl Iridescent {
fn reflectance(&self, l: f32, theta1: f32, s_polaized: bool) -> f32 { fn reflectance(&self, l: f32, theta1: f32, s_polaized: bool) -> f32 {
let theta2 = f32::asin(f32::sin(theta1) * self.n1 / self.n2); let theta2 = f32::asin(f32::sin(theta1) * self.n1 / self.n2);
let local_cos_k_delta = self.cos_K_Delta(l, theta1, theta2, s_polaized); let local_cos_k_delta = self.cos_k_delta(l, theta1, theta2, s_polaized);
let local_c_squred = self.abs_C_squared(l, theta1, theta2, s_polaized); let local_c_squred = self.abs_c_squared(l, theta1, theta2, s_polaized);
// if !local_c_squred.is_normal() || !local_cos_k_delta.is_normal() { // if local_c_squred.is_nan() || local_cos_k_delta.is_nan() {
// dbg!(( // dbg!((
// l, // l,
// theta1, // theta1,
@ -96,12 +96,14 @@ impl Iridescent {
// )); // ));
// } // }
if local_cos_k_delta.abs() < 1.0 { if local_c_squred.is_infinite() {
1.0
} else if local_cos_k_delta.abs() < 1.0 {
let k_delta = f32::acos(local_cos_k_delta); let k_delta = f32::acos(local_cos_k_delta);
let u = f32::sin(k_delta) / f32::sin(self.n * k_delta); let u = f32::sin(k_delta) / f32::sin(self.n * k_delta);
// if !k_delta.is_normal() || !u.is_normal() { // if k_delta.is_nan() || u.is_nan() || r.is_nan() {
// dbg!(( // dbg!((
// l, // l,
// theta1, // theta1,
@ -111,6 +113,7 @@ impl Iridescent {
// s_polaized, // s_polaized,
// k_delta, // k_delta,
// u, // u,
// r
// )); // ));
// } // }
@ -122,7 +125,7 @@ impl Iridescent {
let u = f32::sinh(imk_delta) / f32::sinh(self.n * imk_delta); let u = f32::sinh(imk_delta) / f32::sinh(self.n * imk_delta);
// if !imk_delta.is_normal() || !u.is_normal() { // if imk_delta.is_nan() || u.is_nan() || r.is_nan() {
// dbg!(( // dbg!((
// l, // l,
// theta1, // theta1,
@ -131,7 +134,8 @@ impl Iridescent {
// local_c_squred, // local_c_squred,
// s_polaized, // s_polaized,
// imk_delta, // imk_delta,
// u // u,
// r
// )); // ));
// } // }
@ -139,7 +143,7 @@ impl Iridescent {
} else { } else {
let u = 1.0 / self.n; let u = 1.0 / self.n;
// if !u.is_normal() { // if u.is_nan() || r.is_nan() {
// dbg!(( // dbg!((
// l, // l,
// theta1, // theta1,
@ -147,7 +151,8 @@ impl Iridescent {
// local_cos_k_delta, // local_cos_k_delta,
// local_c_squred, // local_c_squred,
// s_polaized, // s_polaized,
// u // u,
// r
// )); // ));
// } // }
@ -189,13 +194,13 @@ impl Iridescent {
let r = self.averaged_reflectance(l, theta1); let r = self.averaged_reflectance(l, theta1);
// if !r.is_normal() { // if r.is_nan() {
// dbg!((theta1, l, r)); // dbg!((theta1, l, r));
// } // }
let color = Self::color_matching_function(l); let color = Self::color_matching_function(l);
// if !color.r().is_normal() || !color.g().is_normal() || !color.b().is_normal() { // if color.r().is_nan() || color.g().is_nan() || color.b().is_nan() {
// dbg!(color); // dbg!(color);
// } // }

View file

@ -83,7 +83,7 @@ impl<A: AccelerationStructure<u32>, R: Rng> Scene<R> for AccelerationStructureSc
let material = &self.materials[i as usize]; let material = &self.materials[i as usize];
let light_pdf = if let Some(l) = &material.light { let light_pdf = if let Some(_l) = &material.light {
0.0 0.0
} else { } else {
0.0 0.0

View file

@ -19,6 +19,12 @@ impl<R: Rng> BasicCornell<R> {
} }
} }
impl<R: Rng> Default for BasicCornell<R> {
fn default() -> Self {
Self::new()
}
}
impl<R: Rng + 'static> ExampleScene<R> for BasicCornell<R> { impl<R: Rng + 'static> ExampleScene<R> for BasicCornell<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> { fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
let s = self.scene.get_or_init(|| { let s = self.scene.get_or_init(|| {

View file

@ -22,6 +22,12 @@ impl<R: Rng> Cornell2<R> {
} }
} }
impl<R: Rng> Default for Cornell2<R> {
fn default() -> Self {
Self::new()
}
}
impl<R: Rng + 'static> ExampleScene<R> for Cornell2<R> { impl<R: Rng + 'static> ExampleScene<R> for Cornell2<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> { fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
let s = self.scene.get_or_init(|| { let s = self.scene.get_or_init(|| {

View file

@ -1,15 +1,11 @@
use std::cell::OnceCell;
use crate::triangle_bvh::{BVHMaterial, Triangle, TriangleBVH};
use super::ExampleScene; use super::ExampleScene;
use crate::triangle_bvh::{BVHMaterial, Triangle, TriangleBVH};
use ray_tracing_core::{light::AreaLight, prelude::*, scene::Scene}; use ray_tracing_core::{light::AreaLight, prelude::*, scene::Scene};
use ray_tracing_material::{ use ray_tracing_material::{
diffuse::DiffuseMaterial,
microfacet::{BeckmannDistribution, Microfacet}, microfacet::{BeckmannDistribution, Microfacet},
mirror::Mirror, mirror::Mirror,
}; };
use std::cell::OnceCell;
pub struct MISTest<R: Rng> { pub struct MISTest<R: Rng> {
scene: OnceCell<TriangleBVH<R>>, scene: OnceCell<TriangleBVH<R>>,
@ -67,6 +63,12 @@ impl<R: Rng> MISTest<R> {
} }
} }
impl<R: Rng> Default for MISTest<R> {
fn default() -> Self {
Self::new()
}
}
impl<R: Rng + 'static> ExampleScene<R> for MISTest<R> { impl<R: Rng + 'static> ExampleScene<R> for MISTest<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> { fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
let s = self.scene.get_or_init(move || { let s = self.scene.get_or_init(move || {

View file

@ -33,6 +33,11 @@ pub fn example_scenes<R: Rng + Debug + 'static>() -> HashMap<&'static str, Box<d
"stanford_dragon_iridescent", "stanford_dragon_iridescent",
Box::new(stanford_dragon::StanfordDragon::new(material)), Box::new(stanford_dragon::StanfordDragon::new(material)),
); );
let material = Iridescent::new(0.9 * 988.0, 0.1 * 988.0, 1.0, 1.5, 20);
map.insert(
"stanford_dragon_iridescent2",
Box::new(stanford_dragon::StanfordDragon::new(material)),
);
let material = Mirror::new(Color::white()); let material = Mirror::new(Color::white());
map.insert( map.insert(
"stanford_dragon_mirror", "stanford_dragon_mirror",
@ -44,7 +49,8 @@ pub fn example_scenes<R: Rng + Debug + 'static>() -> HashMap<&'static str, Box<d
); );
map.insert("mis_test", Box::new(mis_test::MISTest::new())); map.insert("mis_test", Box::new(mis_test::MISTest::new()));
let material = Iridescent::new(250.0, 250.0, 1.0, 1.3, 20); // let material = Iridescent::new(250.0, 250.0, 1.0, 1.3, 20);
let material = Iridescent::new(0.9 * 988.0, 0.1 * 988.0, 1.0, 1.5, 20);
map.insert("sphere", Box::new(sphere::SphereScene::new(material))); map.insert("sphere", Box::new(sphere::SphereScene::new(material)));
map map
} }

View file

@ -1,13 +1,7 @@
use std::{
cell::{Cell, OnceCell},
marker::PhantomData,
};
use ray_tracing_core::prelude::*;
use crate::basic_scene::BasicScene;
use super::ExampleScene; use super::ExampleScene;
use crate::basic_scene::BasicScene;
use ray_tracing_core::prelude::*;
use std::cell::{Cell, OnceCell};
pub struct SphereScene<M> { pub struct SphereScene<M> {
scene: OnceCell<BasicScene<M>>, scene: OnceCell<BasicScene<M>>,

View file

@ -1,15 +1,11 @@
use rand::Rng;
use ray_tracing_core::{light::AreaLight, prelude::*, scene::Scene};
use ray_tracing_material::{
microfacet::{BeckmannDistribution, Microfacet},
oren_nayar::OrenNayar,
};
use std::cell::{Cell, OnceCell};
use crate::{ use crate::{
parse_obj::ObjData, parse_obj::ObjData,
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH}, triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
}; };
use rand::Rng;
use ray_tracing_core::{light::AreaLight, prelude::*, scene::Scene};
use ray_tracing_material::oren_nayar::OrenNayar;
use std::cell::{Cell, OnceCell};
use super::ExampleScene; use super::ExampleScene;

View file

@ -28,6 +28,12 @@ impl<R: Rng> StanfordDragon<R> {
} }
} }
impl<R: Rng> Default for StanfordDragon<R> {
fn default() -> Self {
Self::new()
}
}
impl<R: Rng + 'static> ExampleScene<R> for StanfordDragon<R> { impl<R: Rng + 'static> ExampleScene<R> for StanfordDragon<R> {
fn get_scene(&self) -> Box<dyn Scene<R> + Sync> { fn get_scene(&self) -> Box<dyn Scene<R> + Sync> {
let s = self.scene.get_or_init(|| { let s = self.scene.get_or_init(|| {

View file

@ -51,9 +51,13 @@ fn render_image<
for _ in 0..samples_per_pixel { for _ in 0..samples_per_pixel {
let r = let r =
renderer.render_pixel(scene, camera, x, y, &mut rng) / (samples_per_pixel as Float); renderer.render_pixel(scene, camera, x, y, &mut rng) / (samples_per_pixel as Float);
c[0] += r.r(); if r.r().is_nan() || r.g().is_nan() || r.b().is_nan() {
c[1] += r.g(); eprintln!("NAN x: {:?}, y: {:?}, r: {:?}", x, y, r);
c[2] += r.b(); } else {
c[0] += r.r();
c[1] += r.g();
c[2] += r.b();
}
} }
}); });
println!("Rendered \"{}\" in {:?}", name.as_ref(), start.elapsed()); println!("Rendered \"{}\" in {:?}", name.as_ref(), start.elapsed());