From 344df2c3d93258416747d6bdba75b9faa2981d70 Mon Sep 17 00:00:00 2001 From: Daniel Oom Date: Sat, 26 Oct 2024 19:01:49 +0200 Subject: [PATCH] Implement sphere normal computation --- geometry/src/geometry.rs | 12 ++++++++---- geometry/src/sphere.rs | 10 +++++++++- material-tester-cli/src/main.rs | 6 +++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/geometry/src/geometry.rs b/geometry/src/geometry.rs index 52f6296..cb18ee0 100644 --- a/geometry/src/geometry.rs +++ b/geometry/src/geometry.rs @@ -83,7 +83,9 @@ pub enum GeometryProperties { texcoords: TriangleTexcoords, material: M, }, - Sphere(), + Sphere { + material: M, + }, } impl GeometryProperties { @@ -100,7 +102,7 @@ impl GeometryProperties { texcoords: _, material, } => material, - GeometryProperties::Sphere() => todo!(), + GeometryProperties::Sphere { material } => material, } } @@ -117,7 +119,9 @@ impl GeometryProperties { texcoords: _, material: _, } => normals.lerp(u, v), - GeometryProperties::Sphere() => todo!(), + GeometryProperties::Sphere { material: _ } => { + Vec3::new(u.sin() * v.cos(), u.sin() * v.sin(), u.cos()) + } } } @@ -134,7 +138,7 @@ impl GeometryProperties { texcoords, material: _, } => texcoords.lerp(u, v), - GeometryProperties::Sphere() => todo!(), + GeometryProperties::Sphere { material: _ } => Vec2::new(u, v), } } } diff --git a/geometry/src/sphere.rs b/geometry/src/sphere.rs index 760def2..595036d 100644 --- a/geometry/src/sphere.rs +++ b/geometry/src/sphere.rs @@ -46,7 +46,15 @@ impl Sphere { let t1 = (-b - discriminant.sqrt()) / (2.0 * a); let t2 = (-b + discriminant.sqrt()) / (2.0 * a); let t = if t1 <= t2 { t1 } else { t2 }; - Some(RayIntersection { t, u: 0.0, v: 0.0 }) + + let normal = ray.param(t) - self.center; + let theta = normal.x.atan2(normal.y); + let phi = (normal.z / self.radius).acos(); + Some(RayIntersection { + t, + u: theta, + v: phi, + }) } } diff --git a/material-tester-cli/src/main.rs b/material-tester-cli/src/main.rs index 0aaeafc..2a2dbe5 100644 --- a/material-tester-cli/src/main.rs +++ b/material-tester-cli/src/main.rs @@ -140,7 +140,7 @@ fn main() { ); let pinhole = Pinhole::new(camera, args.size.as_uvec2()); let geometries = vec![Sphere::new(Vec3::new(0.0, 0.0, 0.0), 1.0).into()]; - let properties = vec![GeometryProperties::Sphere()]; + let properties = vec![GeometryProperties::Sphere { material: 0 }]; let materials = vec![Material { diffuse_reflectance: [1.0, 0.0, 0.0].into(), diffuse_texture_reflectance: None, @@ -151,9 +151,9 @@ fn main() { transparency: 0.0, }]; let lights = vec![SphericalLight { - center: [0.0, 0.0, 0.0].into(), + center: [-2.0, -2.0, -2.0].into(), intensity: [1.0, 1.0, 1.0].into(), - radius: 1.0, + radius: 2.0, }]; let accelerator = NoAccelerator {}; let pathtracer = Pathtracer {