Replace arc
This commit is contained in:
parent
5857244ca8
commit
8df48d3824
3 changed files with 13 additions and 15 deletions
|
|
@ -9,7 +9,6 @@ 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() {
|
||||||
|
|
@ -19,16 +18,16 @@ fn main() {
|
||||||
let viewport_height = 2.0;
|
let viewport_height = 2.0;
|
||||||
|
|
||||||
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 world_m: Arc <dyn Material> = Arc::new(m);
|
let world_m: Box <dyn Material> = Box::new(m);
|
||||||
|
|
||||||
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 center_m: Arc <dyn Material> = Arc::new(m);
|
let center_m: Box <dyn Material> = Box::new(m);
|
||||||
|
|
||||||
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 left_m: Arc <dyn Material> = Arc::new(m);
|
let left_m: Box <dyn Material> = Box::new(m);
|
||||||
|
|
||||||
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 right_m: Arc <dyn Material> = Arc::new(m);
|
let right_m: Box <dyn Material> = Box::new(m);
|
||||||
|
|
||||||
let mut camera = Camera::new(aspect_ratio,image_width,focal_length,Point3::new(-1.0,1.0,1.0),Point3::new(0.0,0.0,-1.0),90.0);
|
let mut camera = Camera::new(aspect_ratio,image_width,focal_length,Point3::new(-1.0,1.0,1.0),Point3::new(0.0,0.0,-1.0),90.0);
|
||||||
let mut world = World::new();
|
let mut world = World::new();
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,21 @@ 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: Send + Sync{
|
pub trait Hittable: Send + Sync{
|
||||||
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord>;
|
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord<'_>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct HitRecord {
|
pub struct HitRecord<'a> {
|
||||||
point: Point3,
|
point: Point3,
|
||||||
normal: Vec3,
|
normal: Vec3,
|
||||||
root: f64,
|
root: f64,
|
||||||
front_face: bool,
|
front_face: bool,
|
||||||
material: Arc<dyn Material>,
|
material: &'a dyn Material,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HitRecord {
|
impl HitRecord <'_> {
|
||||||
pub fn normal(&self) -> Vec3 {
|
pub fn normal(&self) -> Vec3 {
|
||||||
self.normal
|
self.normal
|
||||||
}
|
}
|
||||||
|
|
@ -44,17 +43,17 @@ impl HitRecord {
|
||||||
pub struct Sphere {
|
pub struct Sphere {
|
||||||
radius: f64,
|
radius: f64,
|
||||||
center: Point3,
|
center: Point3,
|
||||||
material: Arc<dyn Material>
|
material: Box<dyn Material>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sphere {
|
impl Sphere {
|
||||||
pub fn new(radius: f64, center: Point3, material: Arc<dyn Material>) -> Self {
|
pub fn new(radius: f64, center: Point3, material: Box<dyn Material>) -> Self {
|
||||||
Sphere { radius, center , material}
|
Sphere { radius, center , material}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hittable for Sphere {
|
impl Hittable for Sphere {
|
||||||
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord> {
|
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord<'_>> {
|
||||||
let oc = self.center - ray.origin();
|
let oc = self.center - ray.origin();
|
||||||
let a = ray.direction().length_squared();
|
let a = ray.direction().length_squared();
|
||||||
let h = Vec3::dot(ray.direction(), oc.to_vec());
|
let h = Vec3::dot(ray.direction(), oc.to_vec());
|
||||||
|
|
@ -80,7 +79,7 @@ impl Hittable for Sphere {
|
||||||
point,
|
point,
|
||||||
normal,
|
normal,
|
||||||
front_face: false,
|
front_face: false,
|
||||||
material: self.material.clone()
|
material: self.material.as_ref(),
|
||||||
};
|
};
|
||||||
tmp.set_face_normal(ray, normal);
|
tmp.set_face_normal(ray, normal);
|
||||||
Some(tmp)
|
Some(tmp)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ impl World {
|
||||||
self.objects.push(Arc::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<'_>> {
|
||||||
let mut hit_anything = None;
|
let mut hit_anything = None;
|
||||||
let mut closest_so_far = interval.max();
|
let mut closest_so_far = interval.max();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue