10.5.A Scene with Metal Spheres
This commit is contained in:
parent
11245af112
commit
bceb4ae244
|
@ -7,6 +7,7 @@ use crate::ppm_writer::PPMWriter;
|
||||||
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3};
|
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3};
|
||||||
use rand::rngs::ThreadRng;
|
use rand::rngs::ThreadRng;
|
||||||
use rand::{Rng, thread_rng};
|
use rand::{Rng, thread_rng};
|
||||||
|
use crate::material::{Material, MaterialKind};
|
||||||
|
|
||||||
impl<'a> Camera<'a> {
|
impl<'a> Camera<'a> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
@ -61,9 +62,9 @@ impl<'a> Camera<'a> {
|
||||||
// let mut img_content = format!("P3\n{} {}\n255\n", self.image_width, self.image_height);
|
// let mut img_content = format!("P3\n{} {}\n255\n", self.image_width, self.image_height);
|
||||||
|
|
||||||
for j in 0..self.image_height {
|
for j in 0..self.image_height {
|
||||||
if j % 10 == 0 {
|
// if j % 10 == 0 {
|
||||||
println!("scan line {}/{} ", j + 1, self.image_height);
|
println!("scan line {}/{} ", j + 1, self.image_height);
|
||||||
}
|
// }
|
||||||
|
|
||||||
for i in 0..self.image_width {
|
for i in 0..self.image_width {
|
||||||
// color
|
// color
|
||||||
|
@ -105,17 +106,65 @@ impl<'a> Camera<'a> {
|
||||||
// 限制出射光线的角度(0.001经验值)
|
// 限制出射光线的角度(0.001经验值)
|
||||||
let hit_record = &mut HitRecord{
|
let hit_record = &mut HitRecord{
|
||||||
t: 0.0,
|
t: 0.0,
|
||||||
|
p: Vec3::new(0.0, 0.0, 0.0),
|
||||||
normal: Vec3::new(0.0, 0.0, 0.0),
|
normal: Vec3::new(0.0, 0.0, 0.0),
|
||||||
front_face: false,
|
front_face: false,
|
||||||
|
material: None,
|
||||||
};
|
};
|
||||||
let hit = world.hit(&ray, 0.001, f32::MAX, hit_record);
|
let hit = world.hit(&ray, 0.001, f32::MAX, hit_record);
|
||||||
if hit_record.t >= 0.0 {
|
if hit {
|
||||||
// diffuse normal vec
|
let scatted = &mut Ray::new(Point::new(0.0, 0.0, 0.0), Vec3::random());
|
||||||
let diffuse_vec = Vec3::random_unit();
|
let attenuation = &mut Color::new(1.0, 1.0, 1.0);
|
||||||
let lambertian_vec = diffuse_vec + hit_record.normal;
|
let hit_m = &hit_record.material;
|
||||||
// 每次反射按0.5计算颜色(反射率 )
|
let hc = &mut HitRecord{
|
||||||
return 0.5 * self.ray_color(&Ray::new(ray.point, lambertian_vec), depth - 1, world);
|
t: hit_record.t,
|
||||||
|
p: hit_record.p,
|
||||||
|
normal: hit_record.normal,
|
||||||
|
front_face: hit_record.front_face,
|
||||||
|
material: None,
|
||||||
|
};
|
||||||
|
let hit_c = match hit_m {
|
||||||
|
Some(mk) => {
|
||||||
|
let mc = match mk {
|
||||||
|
MaterialKind::Lambertian(l) => {
|
||||||
|
if l.scatter(ray, hc, attenuation, scatted) {
|
||||||
|
let r_c = self.ray_color(scatted, depth - 1, world);
|
||||||
|
let sc_color = Vec3::new(attenuation.x * r_c.x, attenuation.y * r_c.y, attenuation.z * r_c.z);
|
||||||
|
// *attenuation * ;
|
||||||
|
Color::new(sc_color.x, sc_color.y, sc_color.z)
|
||||||
|
} else {
|
||||||
|
Color::new(0.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
MaterialKind::Metal(m) => {
|
||||||
|
if m.scatter(ray, hc, attenuation, scatted) {
|
||||||
|
let r_c = self.ray_color(scatted, depth - 1, world);
|
||||||
|
let sc_color = Vec3::new(attenuation.x * r_c.x, attenuation.y * r_c.y, attenuation.z * r_c.z);
|
||||||
|
// *attenuation * ;
|
||||||
|
|
||||||
|
Color::new(sc_color.x, sc_color.y, sc_color.z)
|
||||||
|
} else {
|
||||||
|
Color::new(0.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return mc
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
// if hit_record.t >= 0.0 {
|
||||||
|
// // diffuse normal vec
|
||||||
|
// let diffuse_vec = Vec3::random_unit();
|
||||||
|
// let lambertian_vec = diffuse_vec + hit_record.normal;
|
||||||
|
// // 每次反射按0.5计算颜色(反射率 )
|
||||||
|
// return 0.5 * self.ray_color(&Ray::new(ray.point, lambertian_vec), depth - 1, world);
|
||||||
|
// }
|
||||||
|
Color::new(0.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return hit_c;
|
||||||
}
|
}
|
||||||
|
|
||||||
// v / |v|
|
// v / |v|
|
||||||
let unit_direction = ray.direction / ray.direction.length();
|
let unit_direction = ray.direction / ray.direction.length();
|
||||||
let a = 0.5 * (unit_direction.y + 1.0);
|
let a = 0.5 * (unit_direction.y + 1.0);
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
|
use crate::material::MaterialKind;
|
||||||
use crate::types_defined::{HitRecord, Ray, Vec3};
|
use crate::types_defined::{HitRecord, Ray, Vec3};
|
||||||
|
|
||||||
pub trait Hittable {
|
pub trait Hittable {
|
||||||
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool;
|
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct HittableList {
|
pub struct HittableList {
|
||||||
pub objects: Vec<Box<dyn Hittable>>
|
pub objects: Vec<Box<dyn Hittable>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl HittableList {
|
impl HittableList {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
HittableList { objects: vec![] }
|
HittableList { objects: vec![] }
|
||||||
|
@ -20,14 +19,15 @@ impl HittableList {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool {
|
pub fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool {
|
||||||
|
|
||||||
let mut hits = false;
|
let mut hits = false;
|
||||||
let mut closest_so_far = t_max;
|
let mut closest_so_far = t_max;
|
||||||
|
|
||||||
let temp_hit_record = &mut HitRecord{
|
let temp_hit_record = &mut HitRecord {
|
||||||
t: 0.0,
|
t: 0.0,
|
||||||
normal: Vec3::new(0.0, 0.0, 0.0),
|
normal: Vec3::new(0.0, 0.0, 0.0),
|
||||||
|
p: Vec3::new(0.0, 0.0, 0.0),
|
||||||
front_face: false,
|
front_face: false,
|
||||||
|
material: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
for object in &self.objects {
|
for object in &self.objects {
|
||||||
|
@ -35,10 +35,18 @@ impl HittableList {
|
||||||
if hit {
|
if hit {
|
||||||
hits = true;
|
hits = true;
|
||||||
closest_so_far = temp_hit_record.t;
|
closest_so_far = temp_hit_record.t;
|
||||||
|
|
||||||
hit_record.t = temp_hit_record.t;
|
hit_record.t = temp_hit_record.t;
|
||||||
|
hit_record.p = temp_hit_record.p;
|
||||||
hit_record.front_face = temp_hit_record.front_face;
|
hit_record.front_face = temp_hit_record.front_face;
|
||||||
hit_record.normal = temp_hit_record.normal;
|
hit_record.normal = temp_hit_record.normal;
|
||||||
|
hit_record.material = if let Some(ref m) = temp_hit_record.material {
|
||||||
|
match m {
|
||||||
|
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
|
||||||
|
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
if temp_hit_record.t >= 0.0 {
|
if temp_hit_record.t >= 0.0 {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -48,4 +56,3 @@ impl HittableList {
|
||||||
hits
|
hits
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -2,8 +2,9 @@ use std::fs::File;
|
||||||
use std::io::BufWriter;
|
use std::io::BufWriter;
|
||||||
|
|
||||||
use crate::hittable::HittableList;
|
use crate::hittable::HittableList;
|
||||||
|
use crate::material::{Lambertian, MaterialKind, Metal};
|
||||||
use crate::ppm_writer::PPMWriter;
|
use crate::ppm_writer::PPMWriter;
|
||||||
use crate::types_defined::{Camera, Point, Sphere, Vec3};
|
use crate::types_defined::{Camera, Color, Point, Sphere, Vec3};
|
||||||
mod camera;
|
mod camera;
|
||||||
mod color;
|
mod color;
|
||||||
mod hittable;
|
mod hittable;
|
||||||
|
@ -14,6 +15,7 @@ mod vec3;
|
||||||
mod write_file_util;
|
mod write_file_util;
|
||||||
mod math_utils;
|
mod math_utils;
|
||||||
mod ppm_writer;
|
mod ppm_writer;
|
||||||
|
mod material;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
camera_render();
|
camera_render();
|
||||||
|
@ -37,20 +39,26 @@ fn camera_render() {
|
||||||
width,
|
width,
|
||||||
16.0 / 9.0,
|
16.0 / 9.0,
|
||||||
2.0,
|
2.0,
|
||||||
Point::new(0.0, 0.0, 1.0),
|
Point::new(0.0, 0.0, 0.0),
|
||||||
Vec3::new(0.0, 0.0, -1.0),
|
Vec3::new(0.0, 0.0, -1.0),
|
||||||
50,
|
100,
|
||||||
50,
|
50,
|
||||||
pw
|
pw
|
||||||
);
|
);
|
||||||
// world
|
// world
|
||||||
// world objects(spheres)
|
// world objects(spheres)
|
||||||
let mut world = HittableList::new();
|
let mut world = HittableList::new();
|
||||||
world.put(Box::new(Sphere::new(Point::new(0.0, 0.0, -1.0), 0.5)));
|
|
||||||
world.put(Box::new(Sphere::new(Point::new(0.3, 0.1, -1.0), 0.1)));
|
let plane_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.3, 0.2, 0.5)}));
|
||||||
world.put(Box::new(Sphere::new(Point::new(-0.5, 0.0, -1.0), 0.3)));
|
let plane2_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.3, 0.2, 0.5)}));
|
||||||
world.put(Box::new(Sphere::new(Point::new(0.0, -1000.0, -1.0), 1000.0)));
|
let center_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.1, 0.2, 0.5)}));
|
||||||
world.put(Box::new(Sphere::new(Point::new(-0.5, 0.2, -0.5), 0.4)));
|
let left_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.8, 0.8, 0.8)}));
|
||||||
|
let right_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.8, 0.6, 0.2)}));
|
||||||
|
|
||||||
|
world.put(Box::new(Sphere::new(Point::new(0.0, -100.5, -1.2), 100.0, plane2_m)));
|
||||||
|
world.put(Box::new(Sphere::new(Point::new(0.0, 0.0, -1.2), 0.5, center_m)));
|
||||||
|
world.put(Box::new(Sphere::new(Point::new(-1.0, 0.0, -1.0), 0.5, left_m)));
|
||||||
|
world.put(Box::new(Sphere::new(Point::new(1.0, 0.0, -1.0), 0.5, right_m)));
|
||||||
|
|
||||||
camera.render(&world);
|
camera.render(&world);
|
||||||
// write_image(
|
// write_image(
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
use crate::math_utils::{near_zero, reflect};
|
||||||
|
use crate::types_defined::{Color, HitRecord, Ray, Vec3};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MaterialKind {
|
||||||
|
Lambertian(Lambertian),
|
||||||
|
Metal(Metal),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Material {
|
||||||
|
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, ray: &mut Ray) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Lambertian {
|
||||||
|
pub albedo: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Lambertian {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Lambertian {
|
||||||
|
albedo: self.albedo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Material for Lambertian {
|
||||||
|
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
|
||||||
|
|
||||||
|
let scatter_direction = hit_record.normal + Vec3::random_unit();
|
||||||
|
// let scatter_direction = reflect(r_in.direction, hit_record.normal);
|
||||||
|
|
||||||
|
if near_zero(scatter_direction) {
|
||||||
|
scattered.direction = hit_record.normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
*scattered = Ray::new(hit_record.p, scatter_direction);
|
||||||
|
*attenuation = self.albedo.clone();
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Metal {
|
||||||
|
pub albedo: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Metal {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Metal {
|
||||||
|
albedo: self.albedo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Material for Metal {
|
||||||
|
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
|
||||||
|
/*
|
||||||
|
vec3 reflected = reflect(r_in.direction(), rec.normal);
|
||||||
|
scattered = ray(rec.p, reflected);
|
||||||
|
attenuation = albedo;
|
||||||
|
return true;
|
||||||
|
*/
|
||||||
|
let reflected = reflect(r_in.direction, hit_record.normal);
|
||||||
|
*scattered = Ray::new(hit_record.p, reflected);
|
||||||
|
*attenuation = self.albedo.clone();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::types_defined::Vec3;
|
||||||
|
|
||||||
pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
|
pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
|
||||||
if value < low {
|
if value < low {
|
||||||
|
@ -8,3 +9,13 @@ pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn near_zero(v: Vec3) -> bool {
|
||||||
|
const EPSILON: f32 = 1e-8;
|
||||||
|
|
||||||
|
v.x.abs() < EPSILON && v.y.abs() < EPSILON && v.z.abs() < EPSILON
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
|
||||||
|
v - ((2.0 * v.dot(n)) * n)
|
||||||
|
}
|
|
@ -1,11 +1,13 @@
|
||||||
use crate::hittable;
|
use crate::hittable;
|
||||||
use crate::types_defined::{HitRecord, Point, Ray, Sphere};
|
use crate::material::{Lambertian, Material, MaterialKind, Metal};
|
||||||
|
use crate::types_defined::{Color, HitRecord, Point, Ray, Sphere};
|
||||||
|
|
||||||
impl Sphere {
|
impl Sphere {
|
||||||
pub fn new(c: Point, r: f32) -> Self {
|
pub fn new(c: Point, r: f32, m: Option<MaterialKind>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
center: c,
|
center: c,
|
||||||
radius: r,
|
radius: r,
|
||||||
|
material: m,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +21,10 @@ impl hittable::Hittable for Sphere {
|
||||||
|
|
||||||
let discriminant = h * h - a * c;
|
let discriminant = h * h - a * c;
|
||||||
|
|
||||||
|
if discriminant < 0.0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// // 两个交点
|
// // 两个交点
|
||||||
let disc_sqrt = discriminant.sqrt();
|
let disc_sqrt = discriminant.sqrt();
|
||||||
let near = (h - disc_sqrt) / a;
|
let near = (h - disc_sqrt) / a;
|
||||||
|
@ -37,8 +43,17 @@ impl hittable::Hittable for Sphere {
|
||||||
let normal = (p - self.center) / self.radius;
|
let normal = (p - self.center) / self.radius;
|
||||||
|
|
||||||
hit_record.t = root;
|
hit_record.t = root;
|
||||||
|
hit_record.p = p;
|
||||||
hit_record.normal = normal;
|
hit_record.normal = normal;
|
||||||
hit_record.front_face = r.direction.dot(normal) < 0.0;
|
hit_record.front_face = r.direction.dot(normal) < 0.0;
|
||||||
|
hit_record.material = if let Some(ref m) = self.material {
|
||||||
|
match m {
|
||||||
|
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
|
||||||
|
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{fs::File, io::{BufWriter, Write}};
|
use std::{fs::File, io::{BufWriter, Write}};
|
||||||
|
use rand::distributions::Open01;
|
||||||
|
use crate::material::{Material, MaterialKind};
|
||||||
use crate::ppm_writer::PPMWriter;
|
use crate::ppm_writer::PPMWriter;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -25,9 +26,10 @@ pub struct Ray {
|
||||||
|
|
||||||
pub struct HitRecord {
|
pub struct HitRecord {
|
||||||
pub t: f32,
|
pub t: f32,
|
||||||
// pub p: Vec3,
|
pub p: Vec3,
|
||||||
pub normal: Vec3,
|
pub normal: Vec3,
|
||||||
pub front_face: bool,
|
pub front_face: bool,
|
||||||
|
pub material: Option<MaterialKind>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Camera<'a> {
|
pub struct Camera<'a> {
|
||||||
|
@ -60,4 +62,5 @@ pub struct Camera<'a> {
|
||||||
pub struct Sphere {
|
pub struct Sphere {
|
||||||
pub center: Point,
|
pub center: Point,
|
||||||
pub radius: f32,
|
pub radius: f32,
|
||||||
|
pub material: Option<MaterialKind>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,15 @@ impl Div<f32> for Vec3 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Vec * Vec
|
||||||
|
impl Mul<Vec3> for Vec3 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, rhs: Vec3) -> Self::Output {
|
||||||
|
Vec3::new(self.x * rhs.x, self.y * rhs.y, self.z * rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 加法赋值: Vec3 += Vec3
|
// 加法赋值: Vec3 += Vec3
|
||||||
impl AddAssign for Vec3 {
|
impl AddAssign for Vec3 {
|
||||||
fn add_assign(&mut self, other: Self) {
|
fn add_assign(&mut self, other: Self) {
|
||||||
|
|
Loading…
Reference in New Issue