Compare commits

..

No commits in common. "ba56d1a2dd582d8a80ee504134e7fbd42dca962d" and "33b370706449de4786d3bfaf76653a807f4316bc" have entirely different histories.

8 changed files with 64 additions and 240 deletions

View File

@ -2,12 +2,11 @@ use std::fs::File;
use std::io::{BufWriter}; use std::io::{BufWriter};
use crate::hittable::HittableList; use crate::hittable::HittableList;
use crate::math_utils::clamp; use crate::math_utils::{clamp, linear_to_gamma};
use crate::ppm_writer::PPMWriter; use crate::ppm_writer::PPMWriter;
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3}; use crate::types_defined::{Camera, Color, 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(
@ -62,9 +61,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
@ -84,12 +83,17 @@ impl<'a> Camera<'a> {
// clamp color rgb // clamp color rgb
let color_clamped = Color::new( let color_clamped = Color::new(
clamp(color.x, 0.0, 1.0), clamp(linear_to_gamma(color.x), 0.0, 1.0),
clamp(color.y, 0.0, 1.0), clamp(linear_to_gamma(color.y), 0.0, 1.0),
clamp(color.z, 0.0, 1.0), clamp(linear_to_gamma(color.z), 0.0, 1.0),
); );
self.ppm_file_writer.write(color.to_color()); let _ = self.ppm_file_writer.write(color_clamped.to_color());
// content
// img_content.push_str(&color_clamped.to_color_str().as_str());
// img_content.push('\n');
} }
} }
@ -104,67 +108,14 @@ impl<'a> Camera<'a> {
} }
// 限制出射光线的角度0.001经验值) // 限制出射光线的角度0.001经验值)
let hit_record = &mut HitRecord{ let hr = world.hit(&ray, 0.001, f32::MAX);
t: 0.0, if hr.t >= 0.0 {
p: Vec3::new(0.0, 0.0, 0.0), // diffuse normal vec
normal: Vec3::new(0.0, 0.0, 0.0), let diffuse_vec = Vec3::random_unit();
front_face: false, let lambertian_vec = diffuse_vec + hr.normal;
material: None, // 每次反射按0.5计算颜色(反射率
}; return 0.5 * self.ray_color(&Ray::new(ray.point, lambertian_vec), depth - 1, world);
let hit = world.hit(&ray, 0.001, f32::MAX, hit_record);
if hit {
let scatted = &mut Ray::new(Point::new(0.0, 0.0, 0.0), Vec3::random());
let attenuation = &mut Color::new(1.0, 1.0, 1.0);
let hit_m = &hit_record.material;
let hc = &mut HitRecord{
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);

View File

@ -1,14 +1,15 @@
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) -> HitRecord;
} }
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![] }
@ -18,41 +19,22 @@ impl HittableList {
let _ = &self.objects.push(object); let _ = &self.objects.push(object);
} }
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) -> HitRecord {
let mut hits = false;
let mut closest_so_far = t_max;
let temp_hit_record = &mut HitRecord {
t: 0.0,
normal: Vec3::new(0.0, 0.0, 0.0),
p: Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: None,
};
for object in &self.objects { for object in &self.objects {
let hit = object.hit(r, t_min, closest_so_far, temp_hit_record); let temp = object.hit(r, t_min, t_max);
if hit { if temp.t >= 0.0 {
hits = true; return temp
closest_so_far = 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.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 {
return true;
}
} }
} }
HitRecord {
hits t: -1.0,
normal: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
front_face: false,
}
} }
} }

View File

@ -2,9 +2,8 @@ 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, Color, Point, Sphere, Vec3}; use crate::types_defined::{Camera, Point, Sphere, Vec3};
mod camera; mod camera;
mod color; mod color;
mod hittable; mod hittable;
@ -15,7 +14,6 @@ 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();
@ -41,24 +39,18 @@ fn camera_render() {
2.0, 2.0,
Point::new(0.0, 0.0, 0.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),
100, 50,
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)));
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.3, 0.1, -1.0), 0.1)));
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.5, 0.0, -1.0), 0.3)));
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.0, -1000.0, -1.0), 1000.0)));
let left_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.8, 0.8, 0.8)})); world.put(Box::new(Sphere::new(Point::new(-0.5, 0.2, -0.5), 0.4)));
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(

View File

@ -1,71 +0,0 @@
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
}
}

View File

@ -1,4 +1,3 @@
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 {
@ -10,12 +9,10 @@ pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
return value; return value;
} }
pub fn near_zero(v: Vec3) -> bool { pub fn linear_to_gamma(value: f32) -> f32 {
const EPSILON: f32 = 1e-8; if value > 0.0 {
value.sqrt()
v.x.abs() < EPSILON && v.y.abs() < EPSILON && v.z.abs() < EPSILON } else {
} 0.0
}
pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
v - ((2.0 * v.dot(n)) * n)
} }

View File

@ -1,19 +1,17 @@
use crate::hittable; use crate::hittable;
use crate::material::{Lambertian, Material, MaterialKind, Metal}; use crate::types_defined::{HitRecord, Point, Ray, Sphere};
use crate::types_defined::{Color, HitRecord, Point, Ray, Sphere};
impl Sphere { impl Sphere {
pub fn new(c: Point, r: f32, m: Option<MaterialKind>) -> Self { pub fn new(c: Point, r: f32) -> Self {
Self { Self {
center: c, center: c,
radius: r, radius: r,
material: m,
} }
} }
} }
impl hittable::Hittable for Sphere { impl hittable::Hittable for Sphere {
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) -> HitRecord {
let a: f32 = r.direction.length_squared(); let a: f32 = r.direction.length_squared();
let h = r.direction.dot(self.center - r.point); let h = r.direction.dot(self.center - r.point);
// let b = -2.0 * r.direction.dot(sphere_center - r.point); // let b = -2.0 * r.direction.dot(sphere_center - r.point);
@ -21,10 +19,6 @@ 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;
@ -34,28 +28,19 @@ impl hittable::Hittable for Sphere {
if root <= t_min || root >= t_max { if root <= t_min || root >= t_max {
root = far; root = far;
if root <= t_min || root >= t_max { if root <= t_min || root >= t_max {
// hit_record.t = -1.0; root = -1.0;
return false
} }
} }
let p = r.at(root); let p = r.at(root);
let normal = (p - self.center) / self.radius; let normal = (p - self.center) / self.radius;
HitRecord {
hit_record.t = root; t: root,
hit_record.p = p; // p,
hit_record.normal = normal; normal,
hit_record.front_face = r.direction.dot(normal) < 0.0; // check if r.p inside(>=0) the sphere.
hit_record.material = if let Some(ref m) = self.material { front_face: r.direction.dot(normal) < 0.0,
match m { }
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
}
} else {
None
};
true
} }
} }

View File

@ -1,6 +1,5 @@
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;
/* /*
@ -26,10 +25,9 @@ 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> {
@ -62,5 +60,4 @@ 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>,
} }

View File

@ -125,15 +125,6 @@ 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) {