1use super::matrix::Mat4;
2use super::scalar::{Float, radians, valid_tolerance};
3use super::vector::{Vec2, Vec3};
4
5#[repr(C)]
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct Viewport<T: Float = f32> {
9 pub x: T,
10 pub y: T,
11 pub width: T,
12 pub height: T,
13 pub min_depth: T,
14 pub max_depth: T,
15}
16
17pub type DViewport = Viewport<f64>;
18
19impl<T: Float> Default for Viewport<T> {
20 fn default() -> Self {
21 Self {
22 x: T::ZERO,
23 y: T::ZERO,
24 width: T::ONE,
25 height: T::ONE,
26 min_depth: T::ZERO,
27 max_depth: T::ONE,
28 }
29 }
30}
31
32impl<T: Float> Viewport<T> {
33 pub fn is_valid(self) -> bool {
34 self.x.is_finite()
35 && self.y.is_finite()
36 && self.width.is_finite()
37 && self.height.is_finite()
38 && self.min_depth.is_finite()
39 && self.max_depth.is_finite()
40 && self.width > T::ZERO
41 && self.height > T::ZERO
42 && self.min_depth >= T::ZERO
43 && self.max_depth <= T::ONE
44 && self.min_depth < self.max_depth
45 }
46
47 pub fn try_project(
48 self,
49 point: Vec3<T>,
50 world_view_projection: Mat4<T>,
51 tolerance: T,
52 ) -> Option<Vec3<T>> {
53 if !self.is_valid() || !valid_tolerance(tolerance) {
54 return None;
55 }
56 let ndc = world_view_projection.try_project_point(point, tolerance)?;
57 let result = Vec3 {
58 x: self.x + (ndc.x + T::ONE) * T::HALF * self.width,
59 y: self.y + (T::ONE - ndc.y) * T::HALF * self.height,
60 z: self.min_depth + ndc.z * (self.max_depth - self.min_depth),
61 };
62 result.is_finite().then_some(result)
63 }
64
65 pub fn try_unproject(
66 self,
67 point: Vec3<T>,
68 world_view_projection: Mat4<T>,
69 tolerance: T,
70 ) -> Option<Vec3<T>> {
71 if !self.is_valid() || !point.is_finite() || !valid_tolerance(tolerance) {
72 return None;
73 }
74 let ndc = Vec3 {
75 x: T::TWO * (point.x - self.x) / self.width - T::ONE,
76 y: T::ONE - T::TWO * (point.y - self.y) / self.height,
77 z: (point.z - self.min_depth) / (self.max_depth - self.min_depth),
78 };
79 world_view_projection
80 .try_inverse(tolerance)?
81 .try_project_point(ndc, tolerance)
82 }
83}
84
85impl<T: Float> Mat4<T> {
86 pub fn try_perspective_off_center(
87 left: T,
88 right: T,
89 bottom: T,
90 top: T,
91 near: T,
92 far: T,
93 ) -> Option<Self> {
94 if !left.is_finite()
95 || !right.is_finite()
96 || !bottom.is_finite()
97 || !top.is_finite()
98 || !near.is_finite()
99 || !far.is_finite()
100 || right <= left
101 || top <= bottom
102 || near <= T::ZERO
103 || far <= near
104 {
105 return None;
106 }
107 let width = right - left;
108 let height = top - bottom;
109 let depth = near - far;
110 if !width.is_finite() || !height.is_finite() || !depth.is_finite() {
111 return None;
112 }
113 let mut result = Self::default();
114 result.m[0][0] = T::TWO * near / width;
115 result.m[1][1] = T::TWO * near / height;
116 result.m[2][0] = (right + left) / width;
117 result.m[2][1] = (top + bottom) / height;
118 result.m[2][2] = far / depth;
119 result.m[2][3] = -T::ONE;
120 result.m[3][2] = far * near / depth;
121 result.is_finite().then_some(result)
122 }
123
124 pub fn try_perspective(fov_y_degrees: T, aspect: T, near: T, far: T) -> Option<Self> {
125 if !fov_y_degrees.is_finite()
126 || !aspect.is_finite()
127 || !near.is_finite()
128 || !far.is_finite()
129 || fov_y_degrees <= T::ZERO
130 || fov_y_degrees >= T::from_f64(180.0)
131 || aspect <= T::ZERO
132 || near <= T::ZERO
133 || far <= near
134 {
135 return None;
136 }
137 let half_height = near * (radians(fov_y_degrees) * T::HALF).tan();
138 if !half_height.is_finite() || half_height <= T::ZERO {
139 return None;
140 }
141 let half_width = half_height * aspect;
142 Self::try_perspective_off_center(
143 -half_width,
144 half_width,
145 -half_height,
146 half_height,
147 near,
148 far,
149 )
150 }
151
152 pub fn try_perspective_shifted(
153 fov_y_degrees: T,
154 aspect: T,
155 near: T,
156 far: T,
157 ndc_offset: Vec2<T>,
158 ) -> Option<Self> {
159 if !ndc_offset.is_finite() {
160 return None;
161 }
162 let mut result = Self::try_perspective(fov_y_degrees, aspect, near, far)?;
163 result.m[2][0] = -ndc_offset.x;
164 result.m[2][1] = -ndc_offset.y;
165 Some(result)
166 }
167
168 pub fn try_orthographic_off_center(
169 left: T,
170 right: T,
171 bottom: T,
172 top: T,
173 near: T,
174 far: T,
175 ) -> Option<Self> {
176 if !left.is_finite()
177 || !right.is_finite()
178 || !bottom.is_finite()
179 || !top.is_finite()
180 || !near.is_finite()
181 || !far.is_finite()
182 || right <= left
183 || top <= bottom
184 || far <= near
185 {
186 return None;
187 }
188 let width = right - left;
189 let height = top - bottom;
190 let depth = far - near;
191 if !width.is_finite() || !height.is_finite() || !depth.is_finite() {
192 return None;
193 }
194 let mut result = Self::identity();
195 result.m[0][0] = T::TWO / width;
196 result.m[1][1] = T::TWO / height;
197 result.m[2][2] = -T::ONE / depth;
198 result.m[3][0] = -(right + left) / width;
199 result.m[3][1] = -(top + bottom) / height;
200 result.m[3][2] = -near / depth;
201 result.is_finite().then_some(result)
202 }
203
204 pub fn try_orthographic(width: T, height: T, near: T, far: T, zoom: T) -> Option<Self> {
205 if !width.is_finite()
206 || !height.is_finite()
207 || !zoom.is_finite()
208 || width <= T::ZERO
209 || height <= T::ZERO
210 || zoom <= T::ZERO
211 {
212 return None;
213 }
214 let half_width = width * T::HALF / zoom;
215 let half_height = height * T::HALF / zoom;
216 Self::try_orthographic_off_center(
217 -half_width,
218 half_width,
219 -half_height,
220 half_height,
221 near,
222 far,
223 )
224 }
225
226 pub fn try_orthographic_shifted(
227 width: T,
228 height: T,
229 near: T,
230 far: T,
231 zoom: T,
232 ndc_offset: Vec2<T>,
233 ) -> Option<Self> {
234 if !ndc_offset.is_finite() {
235 return None;
236 }
237 let mut result = Self::try_orthographic(width, height, near, far, zoom)?;
238 result.m[3][0] = -ndc_offset.x;
239 result.m[3][1] = -ndc_offset.y;
240 Some(result)
241 }
242
243 pub fn try_perspective_reverse_z(fov_y_degrees: T, aspect: T, near: T, far: T) -> Option<Self> {
244 if !fov_y_degrees.is_finite()
245 || !aspect.is_finite()
246 || !near.is_finite()
247 || !far.is_finite()
248 || fov_y_degrees <= T::ZERO
249 || fov_y_degrees >= T::from_f64(180.0)
250 || aspect <= T::ZERO
251 || near <= T::ZERO
252 || far <= near
253 {
254 return None;
255 }
256 let tangent = (radians(fov_y_degrees) * T::HALF).tan();
257 if !tangent.is_finite() || tangent <= T::ZERO {
258 return None;
259 }
260 let mut result = Self::default();
261 result.m[0][0] = T::ONE / (aspect * tangent);
262 result.m[1][1] = T::ONE / tangent;
263 result.m[2][2] = near / (far - near);
264 result.m[2][3] = -T::ONE;
265 result.m[3][2] = far * near / (far - near);
266 result.is_finite().then_some(result)
267 }
268
269 pub fn try_perspective_reverse_z_infinite(fov_y_degrees: T, aspect: T, near: T) -> Option<Self> {
270 if !fov_y_degrees.is_finite()
271 || !aspect.is_finite()
272 || !near.is_finite()
273 || fov_y_degrees <= T::ZERO
274 || fov_y_degrees >= T::from_f64(180.0)
275 || aspect <= T::ZERO
276 || near <= T::ZERO
277 {
278 return None;
279 }
280 let tangent = (radians(fov_y_degrees) * T::HALF).tan();
281 if !tangent.is_finite() || tangent <= T::ZERO {
282 return None;
283 }
284 let mut result = Self::default();
285 result.m[0][0] = T::ONE / (aspect * tangent);
286 result.m[1][1] = T::ONE / tangent;
287 result.m[2][3] = -T::ONE;
288 result.m[3][2] = near;
289 result.is_finite().then_some(result)
290 }
291}