From eeae0572042a04462fd413062e335749873ddb72 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sun, 14 Sep 2025 22:41:52 +0200 Subject: [PATCH] Rendering lto orb barely --- ray-tracing-pbrt-scene/src/lib.rs | 28 +++-- ray-tracing-pbrt-scene/src/material.rs | 103 ++++++++++-------- ray-tracing-pbrt-scene/src/shape.rs | 5 - .../src/texture/imagemap.rs | 25 ++++- 4 files changed, 97 insertions(+), 64 deletions(-) diff --git a/ray-tracing-pbrt-scene/src/lib.rs b/ray-tracing-pbrt-scene/src/lib.rs index 5891908..49e7c75 100644 --- a/ray-tracing-pbrt-scene/src/lib.rs +++ b/ray-tracing-pbrt-scene/src/lib.rs @@ -248,12 +248,12 @@ fn parse_shape(iter: &mut Tokenizer) -> Result> { "loopsubdiv" => { let t = parse_dict!(iter => levels, u32, 3; - indices, Vec, Vec::new(); + indices, Vec<[usize;3]>, Vec::new(); p, Vec, Vec::new(); alpha, ShapeAlpha, ShapeAlpha::None => levels, "integer levels", iter.parse_parameter()?; - indices, "integer indices", iter.parse_list()?; + indices, "integer indices", iter.parse_list_3(|a, b, c|[a, b, c])?; p, "point3 P", iter.parse_list_3(Pos3::new)?; alpha, "float alpha", ShapeAlpha::Value(iter.parse_parameter()?); alpha, "texture alpha", ShapeAlpha::Texture(iter.parse_parameter()?) @@ -263,16 +263,26 @@ fn parse_shape(iter: &mut Tokenizer) -> Result> { bail!("indices are a required field") } - if t.p.is_empty() { - bail!("p is a required field") + if t.p.len() < 3 { + bail!("at least 3 vertices required") } + eprintln!( + "WARNING: loopsubdiv is bodged. No subdivisions. levels: {}", + t.levels + ); + Ok(Statement::Shape( - ShapeType::LoopSubDiv { - levels: t.levels, - indices: t.indices, - p: t.p, - }, + ShapeType::TriangleMesh(Bvh::new( + TriangleMesh { + indices: t.indices, + p: t.p, + n: Vec::new(), + s: Vec::new(), + uv: Vec::new(), + }, + 8, + )), t.alpha, )) } diff --git a/ray-tracing-pbrt-scene/src/material.rs b/ray-tracing-pbrt-scene/src/material.rs index f9902fc..b711311 100644 --- a/ray-tracing-pbrt-scene/src/material.rs +++ b/ray-tracing-pbrt-scene/src/material.rs @@ -70,11 +70,15 @@ struct PbrtCoatedDiffuseMaterial { impl PbrtMaterial for PbrtCoatedDiffuseMaterial { fn eval(&self, u: Float, v: Float, w_in: Dir3, w_out: Dir3, rng: &mut R) -> Color { let _ = rng; - let _ = w_out; let _ = w_in; - let _ = u; - let _ = v; - Color::black() + if w_out.y() >= 0.0 { + match &self.reflectance { + Either::A(v) => *v, + Either::B(t) => t.get(u, v), + } + } else { + Color::black() + } } } @@ -202,50 +206,53 @@ pub fn parse_material( "texture reflectance", Either::B(parse_2d_spectrum_texture(input, context)?) ] ))), - "coateddiffuse" => Ok(Arc::new(parse_dict2!(input, PbrtCoatedDiffuseMaterial; - albedo, Either::A(Color::black()), [ - "rgb albedo", Either::A(texture::parse_rgb(input)?); - "spectrum albedo", Either::A(texture::parse_spectrum(input)?); - "texture albedo", Either::B(parse_2d_spectrum_texture(input, context)?) - ]; - g, Either::A(0.0), [ - "float g", Either::A(input.parse_parameter()?); - "texture g", Either::B(parse_2d_float_texture(input, context)?) - ]; - maxdepth, 10, ["integer maxdepth", input.parse_parameter()?]; - nsamples, 1, ["integer nsamples", input.parse_parameter()?]; - thickness, 0.01, ["float thickness", input.parse_parameter()?]; - roughness, Either::A(0.0), [ - "float roughness", Either::A(input.parse_parameter()?); - "texture roughness", Either::B(parse_2d_float_texture(input, context)?) - ]; - uroughness, Either::A(0.0), [ - "float uroughness", Either::A(input.parse_parameter()?); - "texture uroughness", Either::B(parse_2d_float_texture(input, context)?) - ]; - vroughness, Either::A(0.0), [ - "float vroughness", Either::A(input.parse_parameter()?); - "texture vroughness", Either::B(parse_2d_float_texture(input, context)?) - ]; - reflectance, Either::A(Color::black()), [ - "rgb reflectance", Either::A(texture::parse_rgb(input)?); - "spectrum reflectance", Either::A(texture::parse_spectrum(input)?); - "texture reflectance", Either::B(parse_2d_spectrum_texture(input, context)?) - ]; - eta, Either::A(Either::A(1.5)), [ - "float eta", Either::A(Either::A(input.parse_parameter()?)); - "rgb eta", Either::B(Either::A(parse_rgb(input)?)); - "spectrum eta", Either::B(Either::A(texture::parse_spectrum(input)?)); - "texture eta", { - let n = input.parse_parameter()?; - let t = context.get_texture(&n).ok_or_else(|| miette!("Unknown texture"))?; - match Arc::clone(t).get_2d_spectrum_texture() { - Ok(t) => Either::B(Either::B(t)), - Err(_) => Either::A(Either::B(Arc::clone(t).get_2d_float_texture()?)) - } - } - ] - ))), + "coateddiffuse" => Ok({ + eprintln!("WARNING: coateddiffuse has no coating"); + Arc::new(parse_dict2!(input, PbrtCoatedDiffuseMaterial; + albedo, Either::A(Color::black()), [ + "rgb albedo", Either::A(texture::parse_rgb(input)?); + "spectrum albedo", Either::A(texture::parse_spectrum(input)?); + "texture albedo", Either::B(parse_2d_spectrum_texture(input, context)?) + ]; + g, Either::A(0.0), [ + "float g", Either::A(input.parse_parameter()?); + "texture g", Either::B(parse_2d_float_texture(input, context)?) + ]; + maxdepth, 10, ["integer maxdepth", input.parse_parameter()?]; + nsamples, 1, ["integer nsamples", input.parse_parameter()?]; + thickness, 0.01, ["float thickness", input.parse_parameter()?]; + roughness, Either::A(0.0), [ + "float roughness", Either::A(input.parse_parameter()?); + "texture roughness", Either::B(parse_2d_float_texture(input, context)?) + ]; + uroughness, Either::A(0.0), [ + "float uroughness", Either::A(input.parse_parameter()?); + "texture uroughness", Either::B(parse_2d_float_texture(input, context)?) + ]; + vroughness, Either::A(0.0), [ + "float vroughness", Either::A(input.parse_parameter()?); + "texture vroughness", Either::B(parse_2d_float_texture(input, context)?) + ]; + reflectance, Either::A(Color::black()), [ + "rgb reflectance", Either::A(texture::parse_rgb(input)?); + "spectrum reflectance", Either::A(texture::parse_spectrum(input)?); + "texture reflectance", Either::B(parse_2d_spectrum_texture(input, context)?) + ]; + eta, Either::A(Either::A(1.5)), [ + "float eta", Either::A(Either::A(input.parse_parameter()?)); + "rgb eta", Either::B(Either::A(parse_rgb(input)?)); + "spectrum eta", Either::B(Either::A(texture::parse_spectrum(input)?)); + "texture eta", { + let n = input.parse_parameter()?; + let t = context.get_texture(&n).ok_or_else(|| miette!("Unknown texture"))?; + match Arc::clone(t).get_2d_spectrum_texture() { + Ok(t) => Either::B(Either::B(t)), + Err(_) => Either::A(Either::B(Arc::clone(t).get_2d_float_texture()?)) + } + } + ] + )) + }), "dielectric" => Ok(Arc::new(parse_dict2!(input, PbrtDielectricMaterial; eta, Either::A(Either::A(1.5)), [ "float eta", Either::A(Either::A(input.parse_parameter()?)); diff --git a/ray-tracing-pbrt-scene/src/shape.rs b/ray-tracing-pbrt-scene/src/shape.rs index c2aff45..902751a 100644 --- a/ray-tracing-pbrt-scene/src/shape.rs +++ b/ray-tracing-pbrt-scene/src/shape.rs @@ -159,11 +159,6 @@ pub(crate) enum ShapeType { n: Vec, uv: Vec<[Float; 2]>, }, - LoopSubDiv { - levels: u32, - indices: Vec, - p: Vec, - }, Disk { height: Float, radius: Float, diff --git a/ray-tracing-pbrt-scene/src/texture/imagemap.rs b/ray-tracing-pbrt-scene/src/texture/imagemap.rs index 1e40957..492a6ac 100644 --- a/ray-tracing-pbrt-scene/src/texture/imagemap.rs +++ b/ray-tracing-pbrt-scene/src/texture/imagemap.rs @@ -121,8 +121,29 @@ impl Pbrt2dSpectrumTexture for SpectrumImageMapTexture { let (w, h) = self.image.dimensions(); - dbg!(u, v, w, h); - todo!() + let (u, v) = match self.wrap { + ImageMapWrap::Repeat => ( + if u < 0.0 { 1.0 + u.fract() } else { u.fract() }, + if v < 0.0 { 1.0 + v.fract() } else { v.fract() }, + ), + ImageMapWrap::Black => { + if !(0.0..=1.0).contains(&u) || !(0.0..=1.0).contains(&v) { + return Color::black(); + } + + (u, v) + } + ImageMapWrap::Clamp => (u.clamp(0.0, 1.0), v.clamp(0.0, 1.0)), + }; + + let x = u * w as Float; + let y = v * h as Float; + + let c = self + .image + .get_pixel((x as u32).clamp(0, w - 1), (y as u32).clamp(0, h - 1)); + + Color::new(c.0[0], c.0[1], c.0[2]) } }