ft: tri support

This commit is contained in:
2026-04-30 16:28:16 +02:00
parent 388fbcbb8a
commit 27bdce5882
7 changed files with 204 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug;
use log::warn;
use serde::Deserialize;
@@ -13,6 +14,7 @@ use crate::{
},
sphere::Sphere,
traits::Hittable,
triangle::Triangle,
},
scenes::raw_camera::RawCamera,
vec3::Vec3,
@@ -24,7 +26,29 @@ pub struct Scene {
pub objects: Vec<Arc<dyn Hittable>>,
}
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()
}
}
impl Scene {
pub fn new(image_width: u32, image_height: u32) -> Self {
Self {
camera: Camera::new(image_width, image_height),
materials: vec![],
objects: vec![],
}
}
pub fn camera(&mut self) -> &mut Camera {
&mut self.camera
}
pub fn add_hittable(&mut self, hittable: Arc<dyn Hittable>) {
self.objects.push(hittable);
}
pub fn render(&mut self) {
self.camera.render(&self.objects);
}
@@ -68,10 +92,19 @@ struct RawSphere {
pub material: u32,
}
#[derive(Deserialize)]
struct RawTriangle {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub material: u32,
}
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
enum HittableDef {
Sphere(RawSphere),
Triangle(RawTriangle),
}
impl HittableDef {
@@ -92,6 +125,21 @@ impl HittableDef {
materials.get(s.material as usize).unwrap().clone(),
)))
}
HittableDef::Triangle(t) => {
if t.material as usize >= materials.len() {
warn!(
"Triangle specified nonexistent material {}; skipping...",
t.material
);
return None;
}
Some(Arc::new(Triangle::new(
t.p1,
t.p2,
t.p3,
materials.get(t.material as usize).unwrap().clone(),
)))
}
}
}
}