ft (wip): deserialization

This commit is contained in:
2026-04-25 14:46:03 +02:00
parent ef8da70436
commit 430bdf63bc
15 changed files with 421 additions and 158 deletions

View File

@@ -45,7 +45,7 @@ impl Hit {
self.front_face
}
pub fn hit_list<T: Hittable>(hittables: &Vec<T>, r: &Ray) -> Option<Hit> {
pub fn hit_list(hittables: &Vec<Arc<dyn Hittable>>, r: &Ray) -> Option<Hit> {
let mut closest: Option<Hit> = None;
for hittable in hittables {
if let Some(hit) = hittable.hit(r) {

View File

@@ -1,6 +1,7 @@
use core::f32::math::sqrt;
use rand::RngExt;
use serde::Deserialize;
use crate::{
objects::{hit::Hit, materials::traits::Material},
@@ -8,6 +9,7 @@ use crate::{
vec3::{Colour, Vec3},
};
#[derive(Debug, Deserialize)]
pub struct Dielectric {
refraction_index: f32,
}

View File

@@ -1,4 +1,5 @@
use rand::RngExt;
use serde::Deserialize;
use crate::{
objects::{hit::Hit, materials::traits::Material},
@@ -6,6 +7,7 @@ use crate::{
vec3::{Colour, Vec3},
};
#[derive(Debug, Deserialize)]
pub struct Lambertian {
albedo: Colour,
prob: f32,
@@ -42,6 +44,7 @@ impl Material for Lambertian {
}
}
#[derive(Debug, Deserialize)]
pub struct Metal {
albedo: Colour,
prob: f32,

View File

@@ -1,5 +1,6 @@
use crate::{objects::hit::Hit, ray::Ray, vec3::Colour};
use std::fmt::Debug;
pub trait Material {
pub trait Material: Debug {
fn scatter(&self, hit: &Hit, ray: &Ray) -> Option<(Ray, Colour)>;
}

View File

@@ -1,6 +1,9 @@
use core::f32::math::sqrt;
use std::fmt::{self, Debug};
use std::sync::Arc;
use serde::Deserialize;
use crate::objects::hit::Hit;
use crate::objects::materials::traits::Material;
use crate::objects::traits::Hittable;
@@ -13,6 +16,16 @@ pub struct Sphere {
material: Arc<dyn Material>,
}
impl Debug for Sphere {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sphere")
.field("center", &self.center)
.field("radius", &self.radius)
.field("material", &self.material)
.finish()
}
}
impl Sphere {
pub fn new(center: Vec3, r: f32, mat: Arc<dyn Material>) -> Self {
Self {

View File

@@ -1,8 +1,10 @@
use std::fmt::Debug;
use crate::objects::hit::Hit;
use crate::Ray;
use crate::Vec3;
pub trait Hittable {
pub trait Hittable: Debug {
fn hit(&self, r: &Ray) -> Option<Hit>;
fn normal_at(&self, p: &Vec3) -> Vec3;
// fn intersect<H: Hittable>(&self, o: &H) -> bool;