Updated types to play nicer with threading

This commit is contained in:
k 2026-04-13 11:13:34 -04:00
parent 38fd5264cd
commit 993136ea9e
3 changed files with 12 additions and 14 deletions

View file

@ -9,7 +9,7 @@ use render::{World,Camera};
use vec3::{Point3,Vec3}; use vec3::{Point3,Vec3};
use material::{Metal,Lambertian,Material}; use material::{Metal,Lambertian,Material};
use std::sync::Arc;
fn main() { fn main() {
@ -21,21 +21,17 @@ fn main() {
let m = Lambertian::new(Vec3::new(0.5,0.5,0.5)); let m = Lambertian::new(Vec3::new(0.5,0.5,0.5));
let b: Box <dyn Material> = Box::new(m); let world_m: Arc <dyn Material> = Arc::new(m);
let world_m = std::rc::Rc::new(b);
let m = Lambertian::new(Vec3::new(0.1, 0.2, 0.5)); let m = Lambertian::new(Vec3::new(0.1, 0.2, 0.5));
let b: Box <dyn Material> = Box::new(m); let center_m: Arc <dyn Material> = Arc::new(m);
let center_m = std::rc::Rc::new(b);
let m = Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.3); let m = Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.3);
let b: Box <dyn Material> = Box::new(m); let left_m: Arc <dyn Material> = Arc::new(m);
let left_m = std::rc::Rc::new(b);
let m = Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.5); let m = Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.5);
let b: Box <dyn Material> = Box::new(m); let right_m: Arc <dyn Material> = Arc::new(m);
let right_m = std::rc::Rc::new(b);
let mut camera = Camera::new(aspect_ratio,image_width,focal_length,viewport_height,camera_center); let mut camera = Camera::new(aspect_ratio,image_width,focal_length,viewport_height,camera_center);
let mut world = World::new(); let mut world = World::new();

View file

@ -2,6 +2,7 @@ use crate::vec3::{Point3, Ray, Vec3, Interval};
use crate::material::{Material,Scatter}; use crate::material::{Material,Scatter};
use rand::prelude::*; use rand::prelude::*;
use std::sync::Arc;
pub trait Hittable { pub trait Hittable {
@ -13,7 +14,7 @@ pub struct HitRecord {
normal: Vec3, normal: Vec3,
root: f64, root: f64,
front_face: bool, front_face: bool,
material: std::rc::Rc<Box<dyn Material>>, material: Arc<dyn Material>,
} }
impl HitRecord { impl HitRecord {
@ -43,11 +44,11 @@ impl HitRecord {
pub struct Sphere { pub struct Sphere {
radius: f64, radius: f64,
center: Point3, center: Point3,
material: std::rc::Rc<Box<dyn Material>> material: Arc<dyn Material>
} }
impl Sphere { impl Sphere {
pub fn new(radius: f64, center: Point3, material:std::rc::Rc<Box<dyn Material>>) -> Self { pub fn new(radius: f64, center: Point3, material: Arc<dyn Material>) -> Self {
Sphere { radius, center , material} Sphere { radius, center , material}
} }
} }

View file

@ -1,11 +1,12 @@
use crate::object::{HitRecord, Hittable}; use crate::object::{HitRecord, Hittable};
use crate::vec3::{Interval, Ray, Vec3, Point3}; use crate::vec3::{Interval, Ray, Vec3, Point3};
use crate::image::Image; use crate::image::Image;
use std::sync::Arc;
use rand::prelude::*; use rand::prelude::*;
pub struct World { pub struct World {
pub objects: Vec<Box<dyn Hittable>>, pub objects: Vec<Arc<dyn Hittable>>,
} }
impl World { impl World {
@ -16,7 +17,7 @@ impl World {
} }
pub fn add(&mut self, object: impl Hittable + 'static) { pub fn add(&mut self, object: impl Hittable + 'static) {
self.objects.push(Box::new(object)); self.objects.push(Arc::new(object));
} }
pub fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord> { pub fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord> {