Smooth shading

This commit is contained in:
Michael Zhang 2023-02-25 16:45:53 -06:00
parent 40c66154fc
commit 26ddd8d236
4 changed files with 35 additions and 28 deletions

View file

@ -20,7 +20,7 @@ Due: Friday March 3rd
correctly evaluating the Phong illumination equation using the unit length correctly evaluating the Phong illumination equation using the unit length
normal of the plane in which the triangle lies. (10 pts) normal of the plane in which the triangle lies. (10 pts)
- [ ] The program is capable of rendering triangles using smooth shading, in - [x] The program is capable of rendering triangles using smooth shading, in
which every pixel within a triangle is assigned a unique color, obtained which every pixel within a triangle is assigned a unique color, obtained
by evaluating the Phong illumination equation using a unit length normal by evaluating the Phong illumination equation using a unit length normal
direction correctly interpolated from the three normal directions defined direction correctly interpolated from the three normal directions defined

View file

@ -207,7 +207,7 @@ impl Scene {
"v" => scene.triangle_vertices.push(r!(Vector)), "v" => scene.triangle_vertices.push(r!(Vector)),
// vn nx ny nz // vn nx ny nz
"vn" => scene.normals.push(r!(Vector)), "vn" => scene.vertex_normals.push(r!(Vector)),
// f v1 v2 v3 // f v1 v2 v3
// f v1//n1 v2//n2 v3//n3 // f v1//n1 v2//n2 v3//n3

View file

@ -7,7 +7,7 @@ pub mod sphere;
pub mod texture; pub mod texture;
pub mod triangle; pub mod triangle;
use crate::image::{Color, Image}; use crate::image::Color;
use crate::{Point, Point2, Vector}; use crate::{Point, Point2, Vector};
use self::data::{Attenuation, DepthCueing, Light, Material}; use self::data::{Attenuation, DepthCueing, Light, Material};
@ -44,7 +44,5 @@ pub struct Scene {
/// Triangle vertices /// Triangle vertices
pub triangle_vertices: Vec<Point>, pub triangle_vertices: Vec<Point>,
pub vertex_normals: Vec<Vector>,
/// Normal vectors
pub normals: Vec<Vector>,
} }

View file

@ -5,7 +5,6 @@ use ordered_float::NotNan;
use crate::ray::Ray; use crate::ray::Ray;
use crate::utils::{cross, dot}; use crate::utils::{cross, dot};
use super::illumination::IntersectionContext; use super::illumination::IntersectionContext;
use super::Scene; use super::Scene;
@ -61,7 +60,6 @@ impl Triangle {
// Use barycentric coordinates to determine if the point is inside of the // Use barycentric coordinates to determine if the point is inside of the
// triangle // triangle
{
// p = p0 + beta * e1 + gamma * e2 // p = p0 + beta * e1 + gamma * e2
// Using the whack linear algebra approach derived on slide 57 // Using the whack linear algebra approach derived on slide 57
let ep = point - p0; let ep = point - p0;
@ -85,8 +83,19 @@ impl Triangle {
if ![alpha, beta, gamma].into_iter().all(|v| 0.0 < v && v < 1.0) { if ![alpha, beta, gamma].into_iter().all(|v| 0.0 < v && v < 1.0) {
return Ok(None); return Ok(None);
} }
// If surface normals are provided, then interpolate the normals to do
// smooth shading
let normal = match self.normals {
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]];
(alpha * n0 + beta * n1 + gamma * n2).normalize()
} }
let normal = n.normalize(); None => n.normalize(),
};
Ok(Some(IntersectionContext { Ok(Some(IntersectionContext {
time, time,