Add another test case for smooth rendering

This commit is contained in:
Michael Zhang 2023-03-03 02:14:29 -06:00
parent d1da20a85f
commit 9dafc764c3
2 changed files with 38 additions and 5 deletions

View file

@ -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

View file

@ -11,6 +11,7 @@ use super::Scene;
#[derive(Debug)]
pub struct Triangle {
/// Indexes into the scene's vertex list
pub vertices: Vector3<usize>,
/// 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,