ft: added cube

This commit is contained in:
2026-05-02 12:57:45 +02:00
parent e7018a84ed
commit c3d37f4758
6 changed files with 99 additions and 25 deletions

View File

@@ -18,7 +18,6 @@ use crate::ray::Ray;
use crate::scenes::scene::Scene;
use crate::vec3::Vec3;
use dotenv::dotenv;
use log::warn;
use rand::RngExt;
// TODO: implement scene serialization

View File

@@ -3,4 +3,5 @@ pub mod materials;
pub mod sphere;
pub mod triangle;
pub mod quad;
pub mod cube;
pub mod traits;

49
src/objects/cube.rs Normal file
View File

@@ -0,0 +1,49 @@
use std::sync::Arc;
use crate::{
objects::{materials::traits::Material, quad::Quad, traits::Hittable},
ray::Ray,
vec3::Vec3,
};
use super::hit::Hit;
#[derive(Debug)]
pub struct Cube {
faces: Vec<Arc<dyn Hittable>>,
material: Arc<dyn Material>,
}
impl Cube {
pub fn new(
p1: Vec3,
p2: Vec3,
p3: Vec3,
p4: Vec3,
p5: Vec3,
p6: Vec3,
p7: Vec3,
p8: Vec3,
material: Arc<dyn Material>,
) -> Self {
let faces: Vec<Arc<dyn Hittable>> = vec![
Arc::new(Quad::new(p1, p2, p3, p4, material.clone())),
Arc::new(Quad::new(p1, p5, p6, p2, material.clone())),
Arc::new(Quad::new(p2, p6, p7, p3, material.clone())),
Arc::new(Quad::new(p5, p8, p4, p1, material.clone())),
Arc::new(Quad::new(p5, p6, p7, p8, material.clone())),
Arc::new(Quad::new(p4, p3, p7, p8, material.clone())),
];
Self { faces, material }
}
}
impl Hittable for Cube {
fn hit(&self, r: &Ray) -> Option<Hit> {
Hit::hit_list(&self.faces, r)
}
fn normal_at(&self, p: &Vec3) -> Vec3 {
todo!()
}
}

View File

@@ -35,7 +35,23 @@ impl Quad {
out.push(Sphere::new(self.p3, 1., self.material.clone()));
out.push(Sphere::new(self.p4, 1., self.material.clone()));
return out;
}
pub fn hit(
p1: Vec3,
p2: Vec3,
p3: Vec3,
p4: Vec3,
material: Arc<dyn Material>,
normal: Vec3,
r: &Ray,
) -> Option<Hit> {
let isct1 = Triangle::hit(p1, p2, p4, material.clone(), normal, r);
let isct2 = Triangle::hit(p2, p3, p4, material.clone(), normal, r);
if isct1.is_some() {
return isct1;
}
return isct2;
}
}
@@ -53,28 +69,15 @@ impl Debug for Quad {
impl Hittable for Quad {
fn hit(&self, r: &Ray) -> Option<Hit> {
let isct1 = Triangle::hit(
Quad::hit(
self.p1,
self.p2,
self.p4,
self.material.clone(),
self.normal,
r,
);
let isct2 = Triangle::hit(
self.p2,
self.p3,
self.p4,
self.material.clone(),
self.normal,
r,
);
if isct1.is_some() {
return isct1;
}
if isct2.is_some() {
}
return isct2;
)
}
fn normal_at(&self, _p: &Vec3) -> Vec3 {

View File

@@ -1,13 +1,13 @@
use std::fmt::Debug;
use std::sync::Arc;
use serde_json::error::Error;
use log::warn;
use serde::Deserialize;
use crate::{
camera::Camera,
objects::{
cube::Cube,
materials::{
dielectric::Dielectric,
lambertian::{Lambertian, Metal},
@@ -113,8 +113,6 @@ impl RawMaterial {
}
}
#[derive(Deserialize)]
struct RawSphere {
pub center: Vec3,
@@ -139,12 +137,26 @@ struct RawQuad {
pub material: RawMaterial,
}
#[derive(Deserialize)]
struct RawCube {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub p4: Vec3,
pub p5: Vec3,
pub p6: Vec3,
pub p7: Vec3,
pub p8: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
enum HittableDef {
Sphere(RawSphere),
Triangle(RawTriangle),
Quad(RawQuad),
Cube(RawCube),
}
impl HittableDef {
@@ -175,6 +187,16 @@ impl HittableDef {
None
}
}
HittableDef::Cube(c) => {
let material = c.material.into_arc(materials);
if let Some(m) = material {
Some(Arc::new(Cube::new(
c.p1, c.p2, c.p3, c.p4, c.p5, c.p6, c.p7, c.p8, m,
)))
} else {
None
}
}
}
}
}