latest
This commit is contained in:
@@ -30,8 +30,10 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if world.hit(r, 0.001, INFINITY, &mut rec) {
|
if world.hit(r, 0.001, INFINITY, &mut rec) {
|
||||||
let target = rec.p + random_in_hemisphere(&rec.normal);
|
let mut scattered = Ray::new_empty();
|
||||||
return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth - 1)
|
let mut attenuation = Color::new_empty();
|
||||||
|
|
||||||
|
if rec.mat_ptr
|
||||||
}
|
}
|
||||||
|
|
||||||
let unit_direction: Vec3 = r.direction().unit_vector();
|
let unit_direction: Vec3 = r.direction().unit_vector();
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
use crate::hittable::HitRecord;
|
use crate::hittable::HitRecord;
|
||||||
use crate::ray::{Ray, self};
|
use crate::ray::Ray;
|
||||||
use crate::vec3::{Color, random_unit_vector};
|
use crate::vec3::{Color, random_unit_vector, reflect};
|
||||||
|
|
||||||
pub struct Material {
|
pub struct Material {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Scatter {
|
pub trait Scatter: {
|
||||||
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
|
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,8 +23,33 @@ impl Lambertian {
|
|||||||
impl Scatter for Lambertian {
|
impl Scatter for Lambertian {
|
||||||
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
|
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
|
||||||
let scatter_direction = rec.normal + random_unit_vector();
|
let scatter_direction = rec.normal + random_unit_vector();
|
||||||
|
|
||||||
|
if scatter_direction.near_zero() {
|
||||||
|
scatter_direction = rec.normal;
|
||||||
|
}
|
||||||
|
|
||||||
scattered = &mut Ray::new(rec.p, scatter_direction);
|
scattered = &mut Ray::new(rec.p, scatter_direction);
|
||||||
attenuation = &mut self.albedo;
|
attenuation = &mut self.albedo;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Metal {
|
||||||
|
pub albedo: Color
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Metal {
|
||||||
|
pub fn new(a: &Color) -> Self {
|
||||||
|
Metal { albedo: *a }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scatter for Metal {
|
||||||
|
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
|
||||||
|
let reflected = reflect(&r_in.direction(), &rec.normal);
|
||||||
|
scattered = &mut Ray::new(rec.p, rec.normal);
|
||||||
|
attenuation = &mut self.albedo;
|
||||||
|
|
||||||
|
scattered.direction().dot(&rec.normal) > 0.0
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,10 @@ impl Ray {
|
|||||||
Ray { origin, direction }
|
Ray { origin, direction }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_empty() -> Self {
|
||||||
|
Ray { direction: Point3::new_empty(), origin: Vec3::new_empty() }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn origin(&self) -> Point3 {
|
pub fn origin(&self) -> Point3 {
|
||||||
self.origin
|
self.origin
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/vec3.rs
13
src/vec3.rs
@@ -12,6 +12,10 @@ impl Vec3 {
|
|||||||
Vec3 { elements: [x, y, z] }
|
Vec3 { elements: [x, y, z] }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_empty() -> Self {
|
||||||
|
Vec3::new(0.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn x(&self) -> f64 {
|
pub fn x(&self) -> f64 {
|
||||||
self.elements[0]
|
self.elements[0]
|
||||||
}
|
}
|
||||||
@@ -59,6 +63,11 @@ impl Vec3 {
|
|||||||
pub fn random_range(min: f64, max: f64) -> Self {
|
pub fn random_range(min: f64, max: f64) -> Self {
|
||||||
Vec3::new(random_range_f64(min, max), random_range_f64(min, max), random_range_f64(min, max))
|
Vec3::new(random_range_f64(min, max), random_range_f64(min, max), random_range_f64(min, max))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn near_zero(&self) -> bool {
|
||||||
|
let s = 1e-8;
|
||||||
|
(self.x().abs() < s) && (self.y().abs() < s) && (self.z().abs() < s)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add for Vec3 {
|
impl Add for Vec3 {
|
||||||
@@ -213,3 +222,7 @@ pub fn random_in_hemisphere(normal: &Vec3) -> Vec3 {
|
|||||||
return -in_unit_sphere;
|
return -in_unit_sphere;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reflect(v: &Vec3, n: &Vec3) -> Vec3 {
|
||||||
|
*v - 2.0 * v.dot(n) * *n
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user