latest
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::material::Material;
|
||||
use crate::ray::*;
|
||||
use crate::vec3::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HitRecord {
|
||||
pub struct HitRecord<'a> {
|
||||
pub p: Point3,
|
||||
pub normal: Vec3,
|
||||
pub mat_ptr: Rc<Material>,
|
||||
pub mat_ptr: &'a dyn Material,
|
||||
pub t: f64,
|
||||
pub front_face: bool,
|
||||
}
|
||||
|
||||
impl HitRecord {
|
||||
pub fn new_empty() -> Self {
|
||||
HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false, mat_ptr: Rc::new(Material { }) }
|
||||
impl HitRecord<'_> {
|
||||
pub fn new_empty<T: Material>(material: &T) -> Self {
|
||||
HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false, mat_ptr: material }
|
||||
}
|
||||
|
||||
pub fn set_face_normal(&mut self, r: &Ray, outward_normal: &Vec3) {
|
||||
|
||||
@@ -20,7 +20,7 @@ impl HittableList {
|
||||
|
||||
impl Hittable for HittableList {
|
||||
fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
|
||||
let mut temp_rec = HitRecord::new_empty();
|
||||
let mut temp_rec = HitRecord::new_empty(rec.mat_ptr.clone());
|
||||
let mut hit_anything = false;
|
||||
let mut closest_so_far = t_max;
|
||||
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -10,6 +10,8 @@ pub mod material;
|
||||
|
||||
use std::{io::{BufWriter, Write, stdout}, fs::File, time::Instant};
|
||||
|
||||
use material::Metal;
|
||||
|
||||
use crate::{
|
||||
color::*,
|
||||
sphere::*,
|
||||
@@ -22,7 +24,7 @@ use crate::{
|
||||
};
|
||||
|
||||
fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
||||
let mut rec = HitRecord::new_empty();
|
||||
let mut rec = HitRecord::new_empty(&Metal{ albedo: Color::new_empty() });
|
||||
|
||||
// Limit the bounces
|
||||
if depth <= 0 {
|
||||
@@ -33,7 +35,7 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
|
||||
let mut scattered = Ray::new_empty();
|
||||
let mut attenuation = Color::new_empty();
|
||||
|
||||
if rec.mat_ptr
|
||||
//if rec.mat_ptr
|
||||
}
|
||||
|
||||
let unit_direction: Vec3 = r.direction().unit_vector();
|
||||
@@ -52,8 +54,8 @@ fn main() {
|
||||
|
||||
// World
|
||||
let mut world = HittableList::new_empty();
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)));
|
||||
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
|
||||
//world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)));
|
||||
//world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
|
||||
|
||||
// Camera
|
||||
let cam = Camera::new();
|
||||
|
||||
@@ -2,11 +2,7 @@ use crate::hittable::HitRecord;
|
||||
use crate::ray::Ray;
|
||||
use crate::vec3::{Color, random_unit_vector, reflect};
|
||||
|
||||
pub struct Material {
|
||||
|
||||
}
|
||||
|
||||
pub trait Scatter: {
|
||||
pub trait Material: {
|
||||
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
|
||||
}
|
||||
|
||||
@@ -20,7 +16,7 @@ impl Lambertian {
|
||||
}
|
||||
}
|
||||
|
||||
impl Scatter for Lambertian {
|
||||
impl Material for Lambertian {
|
||||
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
|
||||
let scatter_direction = rec.normal + random_unit_vector();
|
||||
|
||||
@@ -44,7 +40,7 @@ impl Metal {
|
||||
}
|
||||
}
|
||||
|
||||
impl Scatter for Metal {
|
||||
impl Material 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);
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{hittable::*, material};
|
||||
use crate::hittable::*;
|
||||
use crate::material::Material;
|
||||
use crate::vec3::*;
|
||||
|
||||
pub struct Sphere {
|
||||
pub struct Sphere<'a> {
|
||||
center: Point3,
|
||||
radius: f64,
|
||||
mat_ptr: Rc<Material>,
|
||||
mat_ptr: &'a dyn Material,
|
||||
}
|
||||
|
||||
impl Sphere {
|
||||
pub fn new(center: Point3, radius: f64, mat_ptr: Rc<Material>) -> Self {
|
||||
impl Sphere<'_> {
|
||||
pub fn new<T: Material>(center: Point3, radius: f64, mat_ptr: &T) -> Self {
|
||||
Sphere {center, radius, mat_ptr}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hittable for Sphere {
|
||||
impl Hittable for Sphere<'_> {
|
||||
fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
|
||||
let oc = r.origin() - self.center;
|
||||
let a = r.direction().length_squared();
|
||||
|
||||
Reference in New Issue
Block a user