ft: quad support
This commit is contained in:
84
src/objects/quad.rs
Normal file
84
src/objects/quad.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use crate::objects::hit::Hit;
|
||||
use crate::objects::sphere::Sphere;
|
||||
use crate::objects::traits::Hittable;
|
||||
use crate::objects::triangle::Triangle;
|
||||
use crate::ray::Ray;
|
||||
use crate::{objects::materials::traits::Material, vec3::Vec3};
|
||||
use log::info;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Quad {
|
||||
p1: Vec3,
|
||||
p2: Vec3,
|
||||
p3: Vec3,
|
||||
p4: Vec3,
|
||||
material: Arc<dyn Material>,
|
||||
normal: Vec3,
|
||||
}
|
||||
|
||||
impl Quad {
|
||||
pub fn new(p1: Vec3, p2: Vec3, p3: Vec3, p4: Vec3, material: Arc<dyn Material>) -> Self {
|
||||
Self {
|
||||
p1,
|
||||
p2,
|
||||
p3,
|
||||
p4,
|
||||
material,
|
||||
normal: (p2 - p1).cross(&(p4 - p1)).get_unit(),
|
||||
}
|
||||
}
|
||||
pub fn corner_spheres(&self) -> Vec<Sphere> {
|
||||
let mut out: Vec<Sphere> = vec![];
|
||||
out.push(Sphere::new(self.p1, 1., self.material.clone()));
|
||||
out.push(Sphere::new(self.p2, 1., self.material.clone()));
|
||||
out.push(Sphere::new(self.p3, 1., self.material.clone()));
|
||||
out.push(Sphere::new(self.p4, 1., self.material.clone()));
|
||||
return out;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Quad {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Triangle")
|
||||
.field("p1", &self.p1)
|
||||
.field("p2", &self.p2)
|
||||
.field("p3", &self.p3)
|
||||
.field("p4", &self.p4)
|
||||
.field("material", &self.material)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Hittable for Quad {
|
||||
fn hit(&self, r: &Ray) -> Option<Hit> {
|
||||
let isct1 = Triangle::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 {
|
||||
// FIXME: might cause ownership issues
|
||||
return self.normal;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user