环境
- Time 2022-11-16
- WSL-Ubuntu 22.04
- Rust 1.65.0
前言
说明
参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html
目标
重构相机部分逻辑,将其单独提到一个类中,保持逻辑不变。
camera.rs
use super::ray::Ray;
use super::vector3::{Point3, Vector3};
pub struct Camera {
origin: Point3,
corner: Point3,
horizontal: Vector3,
vertical: Vector3,
}
pub const RATIO: f64 = 16.0 / 9.0;
const HEIGHT: f64 = 2.0;
const WIDTH: f64 = RATIO * HEIGHT;
const FOCAL: f64 = 1.0;
impl Camera {
pub fn new() -> Camera {
let origin = Point3::new(0.0, 0.0, 0.0);
let horizontal = Vector3::new(WIDTH, 0.0, 0.0);
let vertical = Vector3::new(0.0, HEIGHT, 0.0);
let center = origin - horizontal / 2.0 - vertical / 2.0;
let corner = center - Vector3::new(0.0, 0.0, FOCAL);
Camera {
origin,
horizontal,
vertical,
corner,
}
}
pub fn get_ray(&self, u: f64, v: f64) -> Ray {
let vector3 = self.corner + u * self.horizontal + v * self.vertical;
Ray {
origin: self.origin,
direction: vector3 - self.origin,
}
}
}
main.rs
use camera::Camera;
use hittable::{Hit, World};
use ray::Ray;
use sphere::Sphere;
use vector3::{Color, Point3};
mod camera;
mod hittable;
mod ray;
mod sphere;
mod vector3;
fn main() {
// 图片的比例,和宽高
const WIDTH: u64 = 400;
const HEIGHT: u64 = (WIDTH as f64 / camera::RATIO) as u64;
// 相机
let camera = Camera::new();
// 输出图片,第一行输出 P3,表示像素图
let mut content = String::from("P3");
// 输出宽和高,和最大颜色值
content.push_str(&format!("\n{WIDTH} {HEIGHT}\n255\n"));
let world: World = vec![
Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)),
Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)),
];
for j in (0..HEIGHT).rev() {
// 进度
eprintln!("Scan lines remaining: {j}");
for i in 0..WIDTH {
let u = i as f64 / (WIDTH - 1) as f64;
let v = j as f64 / (HEIGHT - 1) as f64;
let color = ray_color(&camera.get_ray(u, v), &world);
content.push_str(&color.format_str());
}
}
println!("{}", content);
eprintln!("Done.");
}
// 光线的颜色计算
fn ray_color(ray: &Ray, hittable: &dyn Hit) -> Color {
let option = hittable.hit(ray, 0.0, f64::INFINITY);
if let Some(record) = option {
return 0.5 * (record.normal + Color::new(1.0, 1.0, 1.0));
}
// 射线的单位向量
let unit = ray.direction().unit();
// 因为需要得到上下渐变的背景图,所以需要对 y 进行插值。
let t = 0.5 * (unit.y + 1.0);
// 线性插值,根据不同的光线得到在下面这个范围里的不同的颜色,并且是渐变色。
(1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0)
}
总结
将相机的逻辑独立到一个单独的文件中,减少了主函数的逻辑。