ft: quad support

This commit is contained in:
2026-05-02 04:46:15 +02:00
parent 27bdce5882
commit f1ac226dbb
7 changed files with 193 additions and 42 deletions

View File

@@ -5,7 +5,7 @@ use crate::{camera::Camera, vec3::Vec3};
#[derive(Deserialize)]
pub struct RawCamera {
// output
image_width: u32,
image_width: u32,
image_height: u32,
// raytracing

View File

@@ -1,5 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug;
use std::sync::Arc;
use log::warn;
use serde::Deserialize;
@@ -12,6 +12,7 @@ use crate::{
lambertian::{Lambertian, Metal},
traits::Material,
},
quad::Quad,
sphere::Sphere,
traits::Hittable,
triangle::Triangle,
@@ -28,7 +29,11 @@ pub struct Scene {
impl Debug for Scene {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Scene").field("camera", &self.camera).field("materials", &self.materials).field("objects", &self.objects).finish()
f.debug_struct("Scene")
.field("camera", &self.camera)
.field("materials", &self.materials)
.field("objects", &self.objects)
.finish()
}
}
@@ -100,11 +105,21 @@ struct RawTriangle {
pub material: u32,
}
#[derive(Deserialize)]
struct RawQuad {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub p4: Vec3,
pub material: u32,
}
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
enum HittableDef {
Sphere(RawSphere),
Triangle(RawTriangle),
Quad(RawQuad),
}
impl HittableDef {
@@ -140,6 +155,22 @@ impl HittableDef {
materials.get(t.material as usize).unwrap().clone(),
)))
}
HittableDef::Quad(q) => {
if q.material as usize >= materials.len() {
warn!(
"Quad specified nonexistent material {}; skipping...",
q.material
);
return None;
}
Some(Arc::new(Quad::new(
q.p1,
q.p2,
q.p3,
q.p4,
materials.get(q.material as usize).unwrap().clone(),
)))
}
}
}
}