Skip to content

Commit

Permalink
Implement sphere normal computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Oct 26, 2024
1 parent e76a8b6 commit 344df2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
12 changes: 8 additions & 4 deletions geometry/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ pub enum GeometryProperties<M> {
texcoords: TriangleTexcoords,
material: M,
},
Sphere(),
Sphere {
material: M,
},
}

impl<M> GeometryProperties<M> {
Expand All @@ -100,7 +102,7 @@ impl<M> GeometryProperties<M> {
texcoords: _,
material,
} => material,
GeometryProperties::Sphere() => todo!(),
GeometryProperties::Sphere { material } => material,
}
}

Expand All @@ -117,7 +119,9 @@ impl<M> GeometryProperties<M> {
texcoords: _,
material: _,
} => normals.lerp(u, v),
GeometryProperties::Sphere() => todo!(),
GeometryProperties::Sphere { material: _ } => {
Vec3::new(u.sin() * v.cos(), u.sin() * v.sin(), u.cos())
}
}
}

Expand All @@ -134,7 +138,7 @@ impl<M> GeometryProperties<M> {
texcoords,
material: _,
} => texcoords.lerp(u, v),
GeometryProperties::Sphere() => todo!(),
GeometryProperties::Sphere { material: _ } => Vec2::new(u, v),
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion geometry/src/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions material-tester-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 344df2c

Please sign in to comment.