Debian Codegraph @

debian/missing-sources/glam-0.18.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 glam_assert!(z_near > T::ZERO);
87 let f = T::ONE / (T::HALF * fov_y_radians).tan();
88 Self::from_cols(
89 V4::new(f / aspect_ratio, T::ZERO, T::ZERO, T::ZERO),
90 V4::new(T::ZERO, f, T::ZERO, T::ZERO),
91 V4::new(T::ZERO, T::ZERO, -T::ONE, -T::ONE),
92 V4::new(T::ZERO, T::ZERO, -z_near, T::ZERO),
93 )
94 }
95
96 /// Creates an infinite reverse right-handed perspective projection matrix
97 /// with [0,1] depth range.
98 fn perspective_infinite_reverse_rh(fov_y_radians: T, aspect_ratio: T, z_near: T) -> Self {
99 glam_assert!(z_near > T::ZERO);
100 let f = T::ONE / (T::HALF * fov_y_radians).tan();
101 Self::from_cols(
102 V4::new(f / aspect_ratio, T::ZERO, T::ZERO, T::ZERO),
103 V4::new(T::ZERO, f, T::ZERO, T::ZERO),
104 V4::new(T::ZERO, T::ZERO, T::ZERO, -T::ONE),
105 V4::new(T::ZERO, T::ZERO, z_near, T::ZERO),
106 )
107 }
108
109 /// Creates a right-handed orthographic projection matrix with [-1,1] depth
110 /// range. This is the same as the OpenGL `glOrtho` function in OpenGL.
111 /// See
112 /// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml
113 fn orthographic_rh_gl(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
114 let a = T::TWO / (right - left);
115 let b = T::TWO / (top - bottom);
116 let c = -T::TWO / (far - near);
117 let tx = -(right + left) / (right - left);
118 let ty = -(top + bottom) / (top - bottom);
119 let tz = -(far + near) / (far - near);
120
121 Self::from_cols(
122 V4::new(a, T::ZERO, T::ZERO, T::ZERO),
123 V4::new(T::ZERO, b, T::ZERO, T::ZERO),
124 V4::new(T::ZERO, T::ZERO, c, T::ZERO),
125 V4::new(tx, ty, tz, T::ONE),
126 )
127 }
128
129 /// Creates a left-handed orthographic projection matrix with [0,1] depth range.
130 fn orthographic_lh(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
131 let rcp_width = T::ONE / (right - left);
132 let rcp_height = T::ONE / (top - bottom);
133 let r = T::ONE / (far - near);
134 Self::from_cols(
135 V4::new(rcp_width + rcp_width, T::ZERO, T::ZERO, T::ZERO),
136 V4::new(T::ZERO, rcp_height + rcp_height, T::ZERO, T::ZERO),
137 V4::new(T::ZERO, T::ZERO, r, T::ZERO),
138 V4::new(
139 -(left + right) * rcp_width,
140 -(top + bottom) * rcp_height,
141 -r * near,
142 T::ONE,
143 ),
144 )
145 }
146
147 /// Creates a right-handed orthographic projection matrix with [0,1] depth range.
148 fn orthographic_rh(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Self {
149 let rcp_width = T::ONE / (right - left);
150 let rcp_height = T::ONE / (top - bottom);
151 let r = T::ONE / (near - far);
152 Self::from_cols(
153 V4::new(rcp_width + rcp_width, T::ZERO, T::ZERO, T::ZERO),
154 V4::new(T::ZERO, rcp_height + rcp_height, T::ZERO, T::ZERO),
155 V4::new(T::ZERO, T::ZERO, r, T::ZERO),
156 V4::new(
157 -(left + right) * rcp_width,
158 -(top + bottom) * rcp_height,
159 r * near,
160 T::ONE,
161 ),
162 )
163 }
164 }