From 9dafc764c34992fea7bd3b69e2d143ed36a8e0d0 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 3 Mar 2023 02:14:29 -0600 Subject: [PATCH] Add another test case for smooth rendering --- assignment-1c/examples/smooth.txt | 27 +++++++++++++++++++++++++++ assignment-1c/src/scene/triangle.rs | 16 +++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 assignment-1c/examples/smooth.txt diff --git a/assignment-1c/examples/smooth.txt b/assignment-1c/examples/smooth.txt new file mode 100644 index 000000000..a93fc94 --- /dev/null +++ b/assignment-1c/examples/smooth.txt @@ -0,0 +1,27 @@ +eye 0 0 4 +viewdir 0 0 -1 +updir 0 1 0 +hfov 60 +imsize 640 480 + +bkgcolor 0.1 0.1 0.1 +light -2 1 0 1 1 1 1 +mtlcolor 1 0.6 1 1 1 1 0.1 0.4 0.4 20 + + +v 0 0 0 +v 1 0 -1 +v 0 1 -1 +v -1 0 -1 +v 0 -1 -1 + +vn 0 0 1 +vn 1 0 1 +vn 0 1 1 +vn -1 0 1 +vn 0 -1 1 + +f 1//1 2//2 3//3 +f 1//1 3//3 4//4 +f 1//1 4//4 5//5 +f 1//1 5//5 2//2 diff --git a/assignment-1c/src/scene/triangle.rs b/assignment-1c/src/scene/triangle.rs index d7eecc5..8da1c2f 100644 --- a/assignment-1c/src/scene/triangle.rs +++ b/assignment-1c/src/scene/triangle.rs @@ -11,6 +11,7 @@ use super::Scene; #[derive(Debug)] pub struct Triangle { + /// Indexes into the scene's vertex list pub vertices: Vector3, /// Indexes into the scene's normal coordinates list @@ -48,11 +49,13 @@ impl Triangle { ([x0, y0, z0], [xd, yd, zd]) => (x0, y0, z0, xd, yd, zd), _ => unreachable!("lol rip no tuple interface"), }; + let denom = a * xd + b * yd + c * zd; if denom == 0.0 { // The ray is parallel to the plane, so there is no intersection point. return Ok(None); }; + -(a * x0 + b * y0 + c * z0 + d) / denom }; @@ -74,13 +77,13 @@ impl Triangle { return Ok(None); } - // If surface normals are provided, then interpolate the normals to do - // smooth shading let normal = match self.normals { + // If surface normals are provided, then interpolate the normals to do + // smooth shading Some(normals) => { - let n0 = scene.vertex_normals[normals[self.vertices.x]]; - let n1 = scene.vertex_normals[normals[self.vertices.y]]; - let n2 = scene.vertex_normals[normals[self.vertices.z]]; + let n0 = scene.vertex_normals[normals.x]; + let n1 = scene.vertex_normals[normals.y]; + let n2 = scene.vertex_normals[normals.z]; (alpha * n0 + beta * n1 + gamma * n2).normalize() } @@ -94,6 +97,8 @@ impl Triangle { })) } + /// Get the (u, v) texture coordinates corresponding to the point provided in + /// the intersection context pub fn get_texture_coord( &self, scene: &Scene, @@ -146,6 +151,7 @@ impl Triangle { (p0, e1, e2) } + /// Compute barycentric coordinates fn compute_barycentric_coordinates( &self, scene: &Scene,