-
Notifications
You must be signed in to change notification settings - Fork 1
/
quad.rs
108 lines (95 loc) · 2.5 KB
/
quad.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::{f64::consts::PI, ops::Range};
use glam::DVec3;
use crate::{
hittable::{HitRecord, Hittable},
material::Material,
ray::Ray,
textures::Texture,
};
pub struct Quad {
Q: DVec3,
u: DVec3,
v: DVec3,
material: Material,
normal: DVec3,
D: f64,
w: DVec3,
}
impl Quad {
pub fn new(
Q: DVec3,
u: DVec3,
v: DVec3,
material: Material,
) -> Self {
let n = u.cross(v);
let normal = n.normalize();
let D = normal.dot(Q);
let w = n / n.dot(n);
Self {
Q,
u,
v,
material,
normal,
D,
w,
}
}
fn is_interior(a: f64, b: f64) -> Option<(f64, f64)> {
// Given the hit point in plane coordinates, return false if it is outside the
// primitive, otherwise set the hit record UV coordinates and return true.
if (a < 0.) || (1. < a) || (b < 0.) || (1. < b) {
return None;
}
// a,b == u,v
Some((a, b))
}
}
impl Hittable for Quad {
fn hit(
&self,
ray: &Ray,
interval: Range<f64>,
) -> Option<HitRecord> {
let denom = self.normal.dot(ray.direction);
// No hit if the ray is parallel to the plane.
if denom.abs() < 1e-8 {
return None;
}
// Return false if the hit point parameter t is outside the ray interval.
let t =
(self.D - self.normal.dot(ray.origin)) / denom;
if !interval.contains(&t) {
return None;
}
// Determine the hit point lies within the planar shape using its plane coordinates.
let intersection = ray.at(t);
let planar_hitpt_vector: DVec3 =
intersection - self.Q;
let alpha =
self.w.dot(planar_hitpt_vector.cross(self.v));
let beta =
self.w.dot(self.u.cross(planar_hitpt_vector));
let Some((u, v)) = Quad::is_interior(alpha, beta)
else {
return None;
};
// Ray hits the 2D shape; set the rest of the hit record and return true.
// rec.t = t;
// rec.p = intersection;
// rec.mat = mat;
// rec.set_face_normal(r, normal);
// return true;
let rec = HitRecord::with_face_normal(
self.material.clone(),
intersection,
self.normal,
t,
ray,
u,
v,
);
Some(rec)
}
}