latest update

This commit is contained in:
2023-01-30 17:18:16 +01:00
parent 899e8b7228
commit b764470c77
4 changed files with 34 additions and 12 deletions

File diff suppressed because one or more lines are too long

View File

@@ -43,7 +43,7 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
fn main() { fn main() {
// Image // Image
let aspect_ratio: f64 = 1.0 / 1.0; let aspect_ratio: f64 = 1.0 / 1.0;
let image_width: i32 = 1024; let image_width: i32 = 512;
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32; let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
let samples_per_pixel: i32 = 100; let samples_per_pixel: i32 = 100;
let max_depth: i32 = 4; let max_depth: i32 = 4;

View File

@@ -1,13 +1,30 @@
use crate::hittable::HitRecord; use crate::hittable::HitRecord;
use crate::ray::Ray; use crate::ray::{Ray, self};
use crate::rtweekend::*; use crate::vec3::{Color, random_unit_vector};
use crate::hittable_list::*;
use crate::vec3::Color;
pub struct Material { pub struct Material {
} }
trait Scatter { trait Scatter {
fn scatter(r_in: &Ray, rec: HitRecord, attenuation: &Color, scattered: &Ray) -> bool; fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
}
pub struct Lambertian {
albedo: Color,
}
impl Lambertian {
pub fn new(a: &Color) -> Self {
Lambertian { albedo: *a }
}
}
impl Scatter 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();
scattered = &mut Ray::new(rec.p, scatter_direction);
attenuation = &mut self.albedo;
return true;
}
} }

View File

@@ -1,14 +1,18 @@
use crate::hittable::*; use std::rc::Rc;
use crate::{hittable::*, material};
use crate::material::Material;
use crate::vec3::*; use crate::vec3::*;
pub struct Sphere { pub struct Sphere {
center: Point3, center: Point3,
radius: f64 radius: f64,
mat_ptr: Rc<Material>,
} }
impl Sphere { impl Sphere {
pub fn new(center: Point3, radius: f64) -> Self { pub fn new(center: Point3, radius: f64, mat_ptr: Rc<Material>) -> Self {
Sphere {center, radius} Sphere {center, radius, mat_ptr}
} }
} }
@@ -36,6 +40,7 @@ impl Hittable for Sphere {
rec.p = r.at(rec.t); rec.p = r.at(rec.t);
let outward_normal = (rec.p - self.center) / self.radius; let outward_normal = (rec.p - self.center) / self.radius;
rec.set_face_normal(r, &outward_normal); rec.set_face_normal(r, &outward_normal);
rec.mat_ptr = self.mat_ptr;
return true; return true;
} }