feat(raytracer): add minimal phong lighting demo
This commit is contained in:
@@ -1,70 +1,102 @@
|
||||
use super::{
|
||||
scene::Scene,
|
||||
types::{Point3, Ray, Scalar, Vector3},
|
||||
types::{Color, Point3, Ray, Scalar, Vector3},
|
||||
};
|
||||
// use image::Rgb32FImage;
|
||||
use image::RgbImage;
|
||||
|
||||
/// A camera that can render a scene.
|
||||
pub struct Camera {
|
||||
/// Position of the camera's eye.
|
||||
position: Point3,
|
||||
up: Vector3,
|
||||
right: Vector3,
|
||||
upper_left: Point3,
|
||||
/// The lower left point of the image plane.
|
||||
lower_left: Point3,
|
||||
/// The direction of the x-axis on the image plane. (length is equal to the image width)
|
||||
x_dir: Vector3,
|
||||
/// The direction of the y-axis on the image plane. (length is equal to the image height)
|
||||
y_dir: Vector3,
|
||||
/// The width of the image. [px]
|
||||
width: usize,
|
||||
/// The height of the image. [px]
|
||||
height: usize,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
/// Create a new camera at `position` looking at `center` with `up` as the up vector.
|
||||
/// The camera has a field of view of `fovy` degrees and an image size of `width` x `height`.
|
||||
pub fn new(
|
||||
position: Point3,
|
||||
direction: Vector3,
|
||||
center: Point3,
|
||||
up: Vector3,
|
||||
fovy: Scalar,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Camera {
|
||||
let aspect_ratio = width as Scalar / height as Scalar;
|
||||
let fovx = fovy * aspect_ratio;
|
||||
let right = direction.cross(&up).normalize();
|
||||
let x_dir = right * (fovx / 2.0).tan();
|
||||
let y_dir = -up * (fovy / 2.0).tan();
|
||||
let upper_left = position + direction - x_dir + y_dir;
|
||||
let view = (center - position).normalize();
|
||||
let dist = (center - position).norm();
|
||||
let aspect = width as Scalar / height as Scalar;
|
||||
|
||||
let im_height = 2.0 * dist * (fovy.to_radians() / 2.0).tan();
|
||||
let im_width = aspect * im_height;
|
||||
|
||||
let x_dir = view.cross(&up).normalize() * im_width;
|
||||
let y_dir = x_dir.cross(&view).normalize() * im_height;
|
||||
|
||||
let lower_left = center - 0.5 * x_dir - 0.5 * y_dir;
|
||||
|
||||
Camera {
|
||||
position,
|
||||
up,
|
||||
right,
|
||||
upper_left,
|
||||
lower_left,
|
||||
x_dir,
|
||||
y_dir,
|
||||
width,
|
||||
height,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
/// Get a ray pointing from the camera to a relative position on the image plane.
|
||||
/// `x` and `y` are expected to be in the range `[0, 1]`.
|
||||
pub fn ray_at_relative(&self, x: Scalar, y: Scalar) -> Ray {
|
||||
let x_dir = self.x_dir * x;
|
||||
let y_dir = self.y_dir * y;
|
||||
Ray::new(
|
||||
self.position,
|
||||
(self.upper_left + x_dir - y_dir - self.position).normalize(),
|
||||
(self.lower_left + x_dir + y_dir - self.position).normalize(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Get a ray pointing from the camera to a pixel on the image plane.
|
||||
/// `x` and `y` are expected to be in the range `[0, width-1]` and `[0, height-1]` respectively.
|
||||
pub fn ray_at(&self, x: usize, y: usize) -> Ray {
|
||||
let x = x as Scalar / self.width as Scalar;
|
||||
let y = y as Scalar / self.height as Scalar;
|
||||
self.ray_at_relative(x, y)
|
||||
self.ray_at_relative(x, 1.0 - y)
|
||||
}
|
||||
|
||||
// pub fn render(&self, scene: &Scene, depth: u32) -> Rgb32FImage {
|
||||
// Rgb32FImage::from_fn(self.width, self.height, |x, y| {
|
||||
// let ray = self.ray_at(x, y);
|
||||
// let color = scene.trace(&ray, depth);
|
||||
// color.into()
|
||||
// })
|
||||
// }
|
||||
/// Render the scene from the camera's perspective.
|
||||
/// - `depth` is the maximum number of reflections to calculate.
|
||||
/// - `subp` is the number of subpixels to use for antialiasing.
|
||||
pub fn render(&self, scene: &Scene, depth: u32, subp: u32) -> RgbImage {
|
||||
let dx = 1.0 / self.width as Scalar;
|
||||
let dy = 1.0 / self.width as Scalar;
|
||||
let dsx = dx / subp as Scalar;
|
||||
let dsy = dy / subp as Scalar;
|
||||
RgbImage::from_fn(self.width as u32, self.height as u32, |x, y| {
|
||||
let x = x as Scalar * dx;
|
||||
let y = y as Scalar * dy;
|
||||
let mut color = Color::new(0.0, 0.0, 0.0);
|
||||
for sx in 0..subp {
|
||||
for sy in 0..subp {
|
||||
color += scene.trace(
|
||||
&self.ray_at_relative(
|
||||
x + sx as Scalar * dsx,
|
||||
1.0 - (y + sy as Scalar * dsy),
|
||||
),
|
||||
depth,
|
||||
);
|
||||
}
|
||||
}
|
||||
color *= 255.0 / (subp * subp) as Scalar;
|
||||
[color.x as u8, color.y as u8, color.z as u8].into()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user