1 /*
2  * Copyright (c), Recep Aslantas.
3  *
4  * MIT License (MIT), htt../opensource.org/licenses/MIT
5  * Full license can be found in the LICENSE file
6  */
7 
8 /*
9  Functions:
10    CGLM_INLINE mat4s glms_frustum_lh_no(float left,    float right,
11                                         float bottom,  float top,
12                                         float nearZ,   float farZ)
13    CGLM_INLINE mat4s glms_perspective_lh_no(float fovy,
14                                             float aspect,
15                                             float nearZ,
16                                             float farZ)
17    CGLM_INLINE void  glms_persp_move_far_lh_no(mat4s proj, float deltaFar)
18    CGLM_INLINE mat4s glms_perspective_default_lh_no(float aspect)
19    CGLM_INLINE void  glms_perspective_resize_lh_no(mat4s proj, float aspect)
20    CGLM_INLINE void  glms_persp_decomp_lh_no(mat4s  proj,
21                                              float *nearv, float *farv,
22                                              float *top,   float *bottom,
23                                              float *left,  float *right)
24    CGLM_INLINE void  glms_persp_decompv_lh_no(mat4s proj, float dest[6])
25    CGLM_INLINE void  glms_persp_decomp_x_lh_no(mat4s proj, float *left, float *right)
26    CGLM_INLINE void  glms_persp_decomp_y_lh_no(mat4s proj, float *top, float *bottom)
27    CGLM_INLINE void  glms_persp_decomp_z_lh_no(mat4s proj, float *nearv, float *farv)
28    CGLM_INLINE void  glms_persp_decomp_far_lh_no(mat4s proj, float *farZ)
29    CGLM_INLINE void  glms_persp_decomp_near_lh_no(mat4s proj, float *nearZ)
30    CGLM_INLINE float glms_persp_fovy_lh_no(mat4s proj)
31    CGLM_INLINE float glms_persp_aspect_lh_no(mat4s proj)
32    CGLM_INLINE vec4s glms_persp_sizes_lh_no(mat4s proj, float fovy)
33  */
34 
35 #ifndef cglms_persp_lh_no_h
36 #define cglms_persp_lh_no_h
37 
38 #include "../../common.h"
39 #include "../../types-struct.h"
40 #include "../../plane.h"
41 #include "../../cam.h"
42 
43 /*!
44  * @brief set up perspective peprojection matrix
45  *        with a left-hand coordinate system and a
46  *        clip-space of [-1, 1].
47  *
48  * @param[in]  left    viewport.left
49  * @param[in]  right   viewport.right
50  * @param[in]  bottom  viewport.bottom
51  * @param[in]  top     viewport.top
52  * @param[in]  nearZ   near clipping plane
53  * @param[in]  farZ    far clipping plane
54  * @returns    result matrix
55  */
56 CGLM_INLINE
57 mat4s
glms_frustum_lh_no(float left,float right,float bottom,float top,float nearZ,float farZ)58 glms_frustum_lh_no(float left,   float right,
59                    float bottom, float top,
60                    float nearZ,  float farZ) {
61   mat4s dest;
62   glm_frustum_lh_no(left, right, bottom, top, nearZ, farZ, dest.raw);
63   return dest;
64 }
65 
66 /*!
67  * @brief set up perspective projection matrix
68  *        with a left-hand coordinate system and a
69  *        clip-space of [-1, 1].
70  *
71  * @param[in]  fovy    field of view angle
72  * @param[in]  aspect  aspect ratio ( width / height )
73  * @param[in]  nearZ   near clipping plane
74  * @param[in]  farZ    far clipping planes
75  * @returns    result matrix
76  */
77 CGLM_INLINE
78 mat4s
glms_perspective_lh_no(float fovy,float aspect,float nearZ,float farZ)79 glms_perspective_lh_no(float fovy, float aspect, float nearZ, float farZ) {
80   mat4s dest;
81   glm_perspective_lh_no(fovy, aspect, nearZ, farZ, dest.raw);
82   return dest;
83 }
84 
85 /*!
86  * @brief extend perspective projection matrix's far distance
87  *        with a left-hand coordinate system and a
88  *        clip-space of [-1, 1].
89  *
90  * NOTE: if you dodn't want to create new matrix then use array api on struct.raw
91  *       like glms_persp_move_far_lh_no(prooj.raw, deltaFar) to avoid create new mat4
92  *       each time
93  *
94  * this function does not guarantee far >= near, be aware of that!
95  *
96  * @param[in, out] proj      projection matrix to extend
97  * @param[in]      deltaFar  distance from existing far (negative to shink)
98  */
99 CGLM_INLINE
100 mat4s
glms_persp_move_far_lh_no(mat4s proj,float deltaFar)101 glms_persp_move_far_lh_no(mat4s proj, float deltaFar) {
102   mat4s dest;
103   dest = proj;
104   glm_persp_move_far_lh_no(dest.raw, deltaFar);
105   return dest;
106 }
107 
108 /*!
109  * @brief set up perspective projection matrix with default near/far
110  *        and angle values with a left-hand coordinate system and a
111  *        clip-space of [-1, 1].
112  *
113  * @param[in]  aspect aspect ratio ( width / height )
114  * @returns    result matrix
115  */
116 CGLM_INLINE
117 mat4s
glms_perspective_default_lh_no(float aspect)118 glms_perspective_default_lh_no(float aspect) {
119   mat4s dest;
120   glm_perspective_default_lh_no(aspect, dest.raw);
121   return dest;
122 }
123 
124 /*!
125  * @brief resize perspective matrix by aspect ratio ( width / height )
126  *        this makes very easy to resize proj matrix when window /viewport
127  *        reized with a left-hand coordinate system and a
128  *        clip-space of [-1, 1].
129  *
130  * NOTE: if you dodn't want to create new matrix then use array api on struct.raw
131  *       like glm_perspective_resize_lh_no(proj.raw, aspect) to avoid create new mat4
132  *       each time
133  *
134  * @param[in, out] proj   perspective projection matrix
135  * @param[in]      aspect aspect ratio ( width / height )
136  */
137 CGLM_INLINE
138 mat4s
glms_perspective_resize_lh_no(mat4s proj,float aspect)139 glms_perspective_resize_lh_no(mat4s proj, float aspect) {
140   mat4s dest;
141   dest = proj;
142   glm_perspective_resize_lh_no(aspect, dest.raw);
143   return dest;
144 }
145 
146 /*!
147  * @brief decomposes frustum values of perspective projection.
148  *        with a left-hand coordinate system and a
149  *        clip-space of [-1, 1].
150  *
151  * @param[in]  proj    perspective projection matrix
152  * @param[out] nearZ   near
153  * @param[out] farZ    far
154  * @param[out] top     top
155  * @param[out] bottom  bottom
156  * @param[out] left    left
157  * @param[out] right   right
158  */
159 CGLM_INLINE
160 void
glms_persp_decomp_lh_no(mat4s proj,float * __restrict nearZ,float * __restrict farZ,float * __restrict top,float * __restrict bottom,float * __restrict left,float * __restrict right)161 glms_persp_decomp_lh_no(mat4s proj,
162                         float * __restrict nearZ, float * __restrict farZ,
163                         float * __restrict top,   float * __restrict bottom,
164                         float * __restrict left,  float * __restrict right) {
165   glm_persp_decomp_lh_no(proj.raw, nearZ, farZ, top, bottom, left, right);
166 }
167 
168 /*!
169  * @brief decomposes frustum values of perspective projection.
170  *        this makes easy to get all values at once
171  *        with a left-hand coordinate system and a
172  *        clip-space of [-1, 1].
173  *
174  * @param[in]  proj   perspective projection matrix
175  * @param[out] dest   array
176  */
177 CGLM_INLINE
178 void
glms_persp_decompv_lh_no(mat4s proj,float dest[6])179 glms_persp_decompv_lh_no(mat4s proj, float dest[6]) {
180   glm_persp_decompv_lh_no(proj.raw, dest);
181 }
182 
183 /*!
184  * @brief decomposes left and right values of perspective projection
185  *        with a left-hand coordinate system and a
186  *        clip-space of [-1, 1].
187  *        x stands for x axis (left / right axis)
188  *
189  * @param[in]  proj  perspective projection matrix
190  * @param[out] left  left
191  * @param[out] right right
192  */
193 CGLM_INLINE
194 void
glms_persp_decomp_x_lh_no(mat4s proj,float * __restrict left,float * __restrict right)195 glms_persp_decomp_x_lh_no(mat4s proj,
196                           float * __restrict left,
197                           float * __restrict right) {
198   glm_persp_decomp_x_lh_no(proj.raw, left, right);
199 }
200 
201 /*!
202  * @brief decomposes top and bottom values of perspective projection
203  *        with a left-hand coordinate system and a
204  *        clip-space of [-1, 1].
205  *        y stands for y axis (top / botom axis)
206  *
207  * @param[in]  proj   perspective projection matrix
208  * @param[out] top    top
209  * @param[out] bottom bottom
210  */
211 CGLM_INLINE
212 void
glms_persp_decomp_y_lh_no(mat4s proj,float * __restrict top,float * __restrict bottom)213 glms_persp_decomp_y_lh_no(mat4s proj,
214                           float * __restrict top,
215                           float * __restrict bottom) {
216   glm_persp_decomp_y_lh_no(proj.raw, top, bottom);
217 }
218 
219 /*!
220  * @brief decomposes near and far values of perspective projection
221  *        with a left-hand coordinate system and a
222  *        clip-space of [-1, 1].
223  *        z stands for z axis (near / far axis)
224  *
225  * @param[in]  proj    perspective projection matrix
226  * @param[out] nearZ   near
227  * @param[out] farZ    far
228  */
229 CGLM_INLINE
230 void
glms_persp_decomp_z_lh_no(mat4s proj,float * __restrict nearZ,float * __restrict farZ)231 glms_persp_decomp_z_lh_no(mat4s proj,
232                           float * __restrict nearZ,
233                           float * __restrict farZ) {
234   glm_persp_decomp_z_lh_no(proj.raw, nearZ, farZ);
235 }
236 
237 /*!
238  * @brief decomposes far value of perspective projection
239  *        with a left-hand coordinate system and a
240  *        clip-space of [-1, 1].
241  *
242  * @param[in]  proj   perspective projection matrix
243  * @param[out] farZ   far
244  */
245 CGLM_INLINE
246 void
glms_persp_decomp_far_lh_no(mat4s proj,float * __restrict farZ)247 glms_persp_decomp_far_lh_no(mat4s proj, float * __restrict farZ) {
248   glm_persp_decomp_far_lh_no(proj.raw, farZ);
249 }
250 
251 /*!
252  * @brief decomposes near value of perspective projection
253  *        with a left-hand coordinate system and a
254  *        clip-space of [-1, 1].
255  *
256  * @param[in]  proj  perspective projection matrix
257  * @param[out] nearZ near
258  */
259 CGLM_INLINE
260 void
glms_persp_decomp_near_lh_no(mat4s proj,float * __restrict nearZ)261 glms_persp_decomp_near_lh_no(mat4s proj, float * __restrict nearZ) {
262   glm_persp_decomp_near_lh_no(proj.raw, nearZ);
263 }
264 
265 /*!
266  * @brief returns field of view angle along the Y-axis (in radians)
267  *        with a left-hand coordinate system and a
268  *        clip-space of [-1, 1].
269  *
270  * if you need to degrees, use glm_deg to convert it or use this:
271  * fovy_deg = glm_deg(glm_persp_fovy(projMatrix))
272  *
273  * @param[in] proj perspective projection matrix
274  */
275 CGLM_INLINE
276 float
glms_persp_fovy_lh_no(mat4s proj)277 glms_persp_fovy_lh_no(mat4s proj) {
278   return glm_persp_fovy_lh_no(proj.raw);
279 }
280 
281 /*!
282  * @brief returns aspect ratio of perspective projection
283  *        with a left-hand coordinate system and a
284  *        clip-space of [-1, 1].
285  *
286  * @param[in] proj perspective projection matrix
287  */
288 CGLM_INLINE
289 float
glms_persp_aspect_lh_no(mat4s proj)290 glms_persp_aspect_lh_no(mat4s proj) {
291   return glm_persp_aspect_lh_no(proj.raw);
292 }
293 
294 /*!
295  * @brief returns sizes of near and far planes of perspective projection
296  *        with a left-hand coordinate system and a
297  *        clip-space of [-1, 1].
298  *
299  * @param[in]  proj perspective projection matrix
300  * @param[in]  fovy fovy (see brief)
301  * @returns    sizes as vector, sizes order: [Wnear, Hnear, Wfar, Hfar]
302  */
303 CGLM_INLINE
304 vec4s
glms_persp_sizes_lh_no(mat4s proj,float fovy)305 glms_persp_sizes_lh_no(mat4s proj, float fovy) {
306   vec4s dest;
307   glm_persp_sizes_lh_no(proj.raw, fovy, dest.raw);
308   return dest;
309 }
310 
311 #endif /* cglms_persp_lh_no_h */
312