Debian Codegraph @

debian/missing-sources/glam-0.16.0/src/core/traits/projection.rs

raw symbols SCIP
1 use crate::core::traits::{
2 matrix::FloatMatrix4x4, quaternion::Quaternion, scalar::FloatEx, vector::*,
3 };
4
5 pub trait ProjectionMatrix<T: FloatEx, V4: FloatVector4<T> + Quaternion<T>>:
6 FloatMatrix4x4<T, V4>
7 {
8 /// Creates a right-handed perspective projection matrix with [-1,1] depth range.
9 /// This is the same as the OpenGL `gluPerspective` function.
10 /// See https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml
11 fn perspective_rh_gl(fov_y_radians: T, aspect_ratio: T, z_near: T, z_far: T) -> Self {
12 let inv_length = T::ONE / (z_near - z_far);
13 let f = T::ONE / (T::HALF * fov_y_radians).tan();
14 let a = f / aspect_ratio;
15 let b = (z_near + z_far) * inv_length;
16 let c = (T::TWO * z_near * z_far) * inv_length;
17 Self::from_cols(
18 V4::new(a, T::ZERO, T::ZERO, T::ZERO),
19 V4::new(T::ZERO, f, T::ZERO, T::ZERO),
20 V4::new(T::ZERO, T::ZERO, b, -T::ONE),
21 V4::new(T::ZERO, T::ZERO, c, T::ZERO),
22 )
23 }
24
25 /// Creates a left-handed perspective projection matrix with [0,1] depth range.
26 fn perspective_lh(fov_y_radians: T, aspect_ratio: T, z_near: T, z_far: T) -> Self {
27 glam_assert!(z_near > T::ZERO && z_far > T::ZERO);
28 let (sin_fov, cos_fov) = (T::HALF * fov_y_radians).sin_cos();
29 let h = cos_fov / sin_fov;
30 let w = h / aspect_ratio;
31 let r = z_far / (z_far - z_near);
32 Self::from_cols(
33 V4::new(w, T::ZERO, T::ZERO, T::ZERO),
34 V4::new(T::ZERO, h, T::ZERO, T::ZERO),
35 V4::new(T::ZERO, T::ZERO, r, T::ONE),
36 V4::new(T::ZERO, T::ZERO, -r * z_near, T::ZERO),
37 )
38 }
39
40 /// Creates a right-handed perspective projection matrix with [0,1] depth range.
41 fn perspective_rh(fov_y_radians: T, aspect_ratio: T, z_near: T, z_far: T) -> Self {
42 glam_assert!(z_near > T::ZERO && z_far > T::ZERO);
43 let (sin_fov, cos_fov) = (T::HALF * fov_y_radians).sin_cos();
44 let h = cos_fov / sin_fov;
45 let w = h / aspect_ratio;
46 let r = z_far / (z_near - z_far);
47 Self::from_cols(
48 V4::new(w, T::ZERO, T::ZERO, T::ZERO),
49 V4::new(T::ZERO, h, T::ZERO, T::ZERO),
50 V4::new(T::ZERO, T::ZERO, r, -T::ONE),
51 V4::new(T::ZERO, T::ZERO, r * z_near, T::ZERO),
52 )
53 }
54
55 /// Creates an infinite left-handed perspective projection matrix with [0,1] depth range.
56 fn perspective_infinite_lh(fov_y_radians: T, aspect_ratio: T, z_near: T) -> Self {
57 glam_assert!(z_near > T::ZERO);
58 let (sin_fov, cos_fov) = (T::HALF * fov_y_radians).sin_cos();
59 let h = cos_fov / sin_fov;
60 let w = h / aspect_ratio;
61 Self::from_cols(
62 V4::new(w, T::ZERO, T::ZERO, T::ZERO),
63 V4::new(T::ZERO, h, T::ZERO, T::ZERO),
64 V4::new(T::ZERO, T::ZERO, T::ONE, T::ONE),
65 V4::new(T::ZERO, T::ZERO, -z_near, T::ZERO),
66 )
67 }
68
69 /// Creates an infinite left-handed perspective projection matrix with [0,1] depth range.
70 fn perspective_infinite_reverse_lh(fov_y_radians: T, aspect_ratio: T, z_near: T) -> Self {
71 glam_assert!(z_near > T::ZERO);
72 let (sin_fov, cos_fov) = (T::HALF * fov_y_radians).sin_cos();
73 let h = cos_fov / sin_fov;
74 let w = h / aspect_ratio;
75 Self::from_cols(
76 V4::new(w, T::ZERO, T::ZERO, T::ZERO),
77 V4::new(T::ZERO, h, T::ZERO, T::ZERO),
78 V4::new(T::ZERO, T::ZERO, T::ZERO, T::ONE),
79 V4::new(T::ZERO, T::ZERO, z_near, T::ZERO),
80 )
81 }
82
83 /// Creates an infinite right-handed perspective projection matrix with
84 /// [0,1] depth range.
85 fn perspective_infinite_rh(fov_y_radians: T, aspect_ratio: T, z_near: T) -> Self {
86 let f = T::ONE / (T::HALF * fov_y_radians).tan();
87 Self::from_cols(
88 V4::new(f / aspect_ratio, T::ZERO, T::ZERO, T::ZERO),
89 V4::new(T::ZERO, f, T::ZERO, T::ZERO),
90 V4::new(T::ZERO, T::ZERO, -T::ONE, -T::ONE),
91 V4::new(T::ZERO, T::ZERO, -z_near, T::ZERO),
92 )
93 }
94
95 /// Creates an infinite reverse right-handed perspective projection matrix
96 /// with [0,1] depth range.
97 fn perspective_infinite_reverse_rh(fov_y_radians: T, aspect_ratio: T, z_near: T) -> Self {
98 let f = T::ONE / (T::HALF * fov_y_radians).tan();
99 Self::from_cols(
100 V4::new(f / aspect_ratio, T::ZERO, T::ZERO, T::ZERO),
101 V4::new(T::ZERO, f, T::ZERO, T::ZERO),
102 V4::new(T::ZERO, T::ZERO, T::ZERO, -T::ONE),
103 V4::new(T::ZERO, T::ZERO, z_near, T::ZERO),
104 )
105 }
106
107 /// Creates a right-handed orthographic projection matrix with [-1,1] depth
108 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
109 /// See
110 /// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml
111 fn orthographic_rh_gl(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
112 let a = T::TWO / (right - left);
113 let b = T::TWO / (top - bottom);
114 let c = -T::TWO / (far - near);
115 let tx = -(right + left) / (right - left);
116 let ty = -(top + bottom) / (top - bottom);
117 let tz = -(far + near) / (far - near);
118
119 Self::from_cols(
120 V4::new(a, T::ZERO, T::ZERO, T::ZERO),
121 V4::new(T::ZERO, b, T::ZERO, T::ZERO),
122 V4::new(T::ZERO, T::ZERO, c, T::ZERO),
123 V4::new(tx, ty, tz, T::ONE),
124 )
125 }
126
127 /// Creates a left-handed orthographic projection matrix with [0,1] depth range.
128 fn orthographic_lh(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
129 let rcp_width = T::ONE / (right - left);
130 let rcp_height = T::ONE / (top - bottom);
131 let r = T::ONE / (far - near);
132 Self::from_cols(
133 V4::new(rcp_width + rcp_width, T::ZERO, T::ZERO, T::ZERO),
134 V4::new(T::ZERO, rcp_height + rcp_height, T::ZERO, T::ZERO),
135 V4::new(T::ZERO, T::ZERO, r, T::ZERO),
136 V4::new(
137 -(left + right) * rcp_width,
138 -(top + bottom) * rcp_height,
139 -r * near,
140 T::ONE,
141 ),
142 )
143 }
144
145 /// Creates a right-handed orthographic projection matrix with [0,1] depth range.
146 fn orthographic_rh(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
147 let rcp_width = T::ONE / (right - left);
148 let rcp_height = T::ONE / (top - bottom);
149 let r = T::ONE / (near - far);
150 Self::from_cols(
151 V4::new(rcp_width + rcp_width, T::ZERO, T::ZERO, T::ZERO),
152 V4::new(T::ZERO, rcp_height + rcp_height, T::ZERO, T::ZERO),
153 V4::new(T::ZERO, T::ZERO, r, T::ZERO),
154 V4::new(
155 -(left + right) * rcp_width,
156 -(top + bottom) * rcp_height,
157 r * near,
158 T::ONE,
159 ),
160 )
161 }
162 }