Spurious crashes - Switching to enums

Segfaults and stack overflows. I get overflows when running in either
debug or release. Segmentation faults when hooked up to GDB. I think it
may be related to the copying of uninitialized trait objects, but I
don't know.

I've decided the trait-based interface just isn't worth this. I'll
rearrange the materials into an Enum and a big match block.
This commit is contained in:
2023-06-04 09:19:25 -05:00
parent 5cc0b49cd9
commit d1bde8a1a8
4 changed files with 16 additions and 17 deletions

View File

@@ -2,16 +2,17 @@
use crate::vec3::Vec3; use crate::vec3::Vec3;
use crate::ray::Ray; use crate::ray::Ray;
use crate::material::Material; use crate::material::Material;
use std::rc::Rc;
pub struct HitRecord<'a>{ pub struct HitRecord{
pub p: Vec3, pub p: Vec3,
pub normal: Vec3, pub normal: Vec3,
pub material: Option<&'a Material>, pub material: Option<Rc<dyn Material>>,
pub t: f32, pub t: f32,
pub front_face: bool, pub front_face: bool,
} }
impl<'a> HitRecord<'a>{ impl HitRecord{
pub fn set_face_normal(&mut self, r: Ray, outward_normal: Vec3) -> (){ pub fn set_face_normal(&mut self, r: Ray, outward_normal: Vec3) -> (){
self.front_face = Vec3::dot(r.dir, outward_normal) < 0.0; self.front_face = Vec3::dot(r.dir, outward_normal) < 0.0;
self.normal = if self.front_face { outward_normal } else { -outward_normal }; self.normal = if self.front_face { outward_normal } else { -outward_normal };

View File

@@ -28,7 +28,7 @@ fn main() {
(400.0 / aspect_ratio) as i32 (400.0 / aspect_ratio) as i32
); );
let samples_per_pixel = 100; let samples_per_pixel = 100;
let max_depth = 50; let max_depth = 100;
// world // world
@@ -37,7 +37,7 @@ fn main() {
Box::new( Box::new(
Sphere{ Sphere{
center: Vec3{ x: 0.0, y: 0.0, z: -1.0}, center: Vec3{ x: 0.0, y: 0.0, z: -1.0},
radius: 0.5 radius: 0.5,
material: None, material: None,
} }
) )
@@ -75,7 +75,7 @@ fn main() {
eprintln!("Done!"); eprintln!("Done!");
} }
fn ray_color(r: Ray, world: &HittableList, depth: u32, srng: &mut SmallRng, distrib: Uniform<f32> ) -> Vec3 { fn ray_color(r: Ray, world: &dyn Hittable, depth: u32, srng: &mut SmallRng, distrib: Uniform<f32> ) -> Vec3 {
// recursion depth guard // recursion depth guard
if depth == 0 { if depth == 0 {
return Vec3::new(0.0, 0.0, 0.0); return Vec3::new(0.0, 0.0, 0.0);

View File

@@ -3,11 +3,9 @@ use crate::ray::Ray;
use crate::hittable::HitRecord; use crate::hittable::HitRecord;
use crate::Vec3; use crate::Vec3;
pub struct Material; pub trait Material {
pub Scatter {
fn scatter( fn scatter(
ray_in: Ray, rec: HitRecord, attenuation: Vec3, scattered: Ray &self, ray_in: Ray, rec: HitRecord, attenuation: Vec3, scattered: Ray
) -> bool; ) -> bool;
} }

View File

@@ -5,16 +5,17 @@ use crate::hittable::{
HitRecord, HitRecord,
}; };
use crate::material::Material; use crate::material::Material;
use crate::ray::Ray; use crate::ray::Ray;
pub struct Sphere<'a>{ use std::rc::Rc;
pub struct Sphere{
pub center: Vec3, pub center: Vec3,
pub radius: f32, pub radius: f32,
pub material: &'a Material, pub material: Option<Rc<dyn Material>>,
} }
impl<'a> Hittable for Sphere<'a> { impl Hittable for Sphere {
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord>{ fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord>{
let oc = r.orig - self.center; let oc = r.orig - self.center;
let a = r.dir.length_squared(); let a = r.dir.length_squared();
@@ -35,11 +36,10 @@ impl<'a> Hittable for Sphere<'a> {
return None; return None;
} }
} }
let mut record = HitRecord{ let mut record = HitRecord{
p: r.at(root), p: r.at(root),
normal: (r.at(root) - self.center) / self.radius, normal: (r.at(root) - self.center) / self.radius,
material: Some(self.material), material: self.material.clone(),
t: root, t: root,
front_face: false, front_face: false,
}; };