persp_lh_zo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Functions:
  9. CGLM_INLINE void glm_frustum_lh_zo(float left, float right,
  10. float bottom, float top,
  11. float nearZ, float farZ,
  12. mat4 dest)
  13. CGLM_INLINE void glm_perspective_lh_zo(float fovy,
  14. float aspect,
  15. float nearZ,
  16. float farZ,
  17. mat4 dest)
  18. CGLM_INLINE void glm_perspective_default_lh_zo(float aspect, mat4 dest)
  19. CGLM_INLINE void glm_perspective_resize_lh_zo(float aspect, mat4 proj)
  20. CGLM_INLINE void glm_persp_move_far_lh_zo(mat4 proj,
  21. float deltaFar)
  22. CGLM_INLINE void glm_persp_decomp_lh_zo(mat4 proj,
  23. float * __restrict nearZ,
  24. float * __restrict farZ,
  25. float * __restrict top,
  26. float * __restrict bottom,
  27. float * __restrict left,
  28. float * __restrict right)
  29. CGLM_INLINE void glm_persp_decompv_lh_zo(mat4 proj,
  30. float dest[6])
  31. CGLM_INLINE void glm_persp_decomp_x_lh_zo(mat4 proj,
  32. float * __restrict left,
  33. float * __restrict right)
  34. CGLM_INLINE void glm_persp_decomp_y_lh_zo(mat4 proj,
  35. float * __restrict top,
  36. float * __restrict bottom)
  37. CGLM_INLINE void glm_persp_decomp_z_lh_zo(mat4 proj,
  38. float * __restrict nearZ,
  39. float * __restrict farZ)
  40. CGLM_INLINE void glm_persp_decomp_far_lh_zo(mat4 proj, float * __restrict farZ)
  41. CGLM_INLINE void glm_persp_decomp_near_lh_zo(mat4 proj, float * __restrict nearZ)
  42. CGLM_INLINE void glm_persp_sizes_lh_zo(mat4 proj, float fovy, vec4 dest)
  43. */
  44. #ifndef cglm_persp_lh_zo_h
  45. #define cglm_persp_lh_zo_h
  46. #include "../common.h"
  47. #include "persp.h"
  48. /*!
  49. * @brief set up perspective peprojection matrix with a left-hand coordinate
  50. * system and a clip-space of [0, 1].
  51. *
  52. * @param[in] left viewport.left
  53. * @param[in] right viewport.right
  54. * @param[in] bottom viewport.bottom
  55. * @param[in] top viewport.top
  56. * @param[in] nearZ near clipping plane
  57. * @param[in] farZ far clipping plane
  58. * @param[out] dest result matrix
  59. */
  60. CGLM_INLINE
  61. void
  62. glm_frustum_lh_zo(float left, float right,
  63. float bottom, float top,
  64. float nearZ, float farZ,
  65. mat4 dest) {
  66. float rl, tb, fn, nv;
  67. glm_mat4_zero(dest);
  68. rl = 1.0f / (right - left);
  69. tb = 1.0f / (top - bottom);
  70. fn =-1.0f / (farZ - nearZ);
  71. nv = 2.0f * nearZ;
  72. dest[0][0] = nv * rl;
  73. dest[1][1] = nv * tb;
  74. dest[2][0] = (right + left) * rl;
  75. dest[2][1] = (top + bottom) * tb;
  76. dest[2][2] =-farZ * fn;
  77. dest[2][3] = 1.0f;
  78. dest[3][2] = farZ * nearZ * fn;
  79. }
  80. /*!
  81. * @brief set up perspective projection matrix with a left-hand coordinate
  82. * system and a clip-space of [0, 1].
  83. *
  84. * @param[in] fovy field of view angle
  85. * @param[in] aspect aspect ratio ( width / height )
  86. * @param[in] nearZ near clipping plane
  87. * @param[in] farZ far clipping planes
  88. * @param[out] dest result matrix
  89. */
  90. CGLM_INLINE
  91. void
  92. glm_perspective_lh_zo(float fovy,
  93. float aspect,
  94. float nearZ,
  95. float farZ,
  96. mat4 dest) {
  97. float f, fn;
  98. glm_mat4_zero(dest);
  99. f = 1.0f / tanf(fovy * 0.5f);
  100. fn = 1.0f / (nearZ - farZ);
  101. dest[0][0] = f / aspect;
  102. dest[1][1] = f;
  103. dest[2][2] =-farZ * fn;
  104. dest[2][3] = 1.0f;
  105. dest[3][2] = nearZ * farZ * fn;
  106. }
  107. /*!
  108. * @brief extend perspective projection matrix's far distance with a
  109. * left-hand coordinate system and a clip-space with depth values
  110. * from zero to one.
  111. *
  112. * this function does not guarantee far >= near, be aware of that!
  113. *
  114. * @param[in, out] proj projection matrix to extend
  115. * @param[in] deltaFar distance from existing far (negative to shink)
  116. */
  117. CGLM_INLINE
  118. void
  119. glm_persp_move_far_lh_zo(mat4 proj, float deltaFar) {
  120. float fn, farZ, nearZ, p22, p32;
  121. p22 = -proj[2][2];
  122. p32 = proj[3][2];
  123. nearZ = p32 / p22;
  124. farZ = p32 / (p22 + 1.0f) + deltaFar;
  125. fn = 1.0f / (nearZ - farZ);
  126. proj[2][2] = -farZ * fn;
  127. proj[3][2] = nearZ * farZ * fn;
  128. }
  129. /*!
  130. * @brief set up perspective projection matrix with default near/far
  131. * and angle values with a left-hand coordinate system and a
  132. * clip-space of [0, 1].
  133. *
  134. * @param[in] aspect aspect ratio ( width / height )
  135. * @param[out] dest result matrix
  136. */
  137. CGLM_INLINE
  138. void
  139. glm_perspective_default_lh_zo(float aspect, mat4 dest) {
  140. glm_perspective_lh_zo(GLM_PI_4f, aspect, 0.01f, 100.0f, dest);
  141. }
  142. /*!
  143. * @brief resize perspective matrix by aspect ratio ( width / height )
  144. * this makes very easy to resize proj matrix when window /viewport
  145. * reized
  146. *
  147. * @param[in] aspect aspect ratio ( width / height )
  148. * @param[in, out] proj perspective projection matrix
  149. */
  150. CGLM_INLINE
  151. void
  152. glm_perspective_resize_lh_zo(float aspect, mat4 proj) {
  153. if (proj[0][0] == 0.0f)
  154. return;
  155. proj[0][0] = proj[1][1] / aspect;
  156. }
  157. /*!
  158. * @brief decomposes frustum values of perspective projection
  159. * with angle values with a left-hand coordinate system and a
  160. * clip-space of [0, 1].
  161. *
  162. * @param[in] proj perspective projection matrix
  163. * @param[out] nearZ near
  164. * @param[out] farZ far
  165. * @param[out] top top
  166. * @param[out] bottom bottom
  167. * @param[out] left left
  168. * @param[out] right right
  169. */
  170. CGLM_INLINE
  171. void
  172. glm_persp_decomp_lh_zo(mat4 proj,
  173. float * __restrict nearZ, float * __restrict farZ,
  174. float * __restrict top, float * __restrict bottom,
  175. float * __restrict left, float * __restrict right) {
  176. float m00, m11, m20, m21, m22, m32, n, f;
  177. float n_m11, n_m00;
  178. m00 = proj[0][0];
  179. m11 = proj[1][1];
  180. m20 = proj[2][0];
  181. m21 = proj[2][1];
  182. m22 =-proj[2][2];
  183. m32 = proj[3][2];
  184. n = m32 / m22;
  185. f = m32 / (m22 + 1.0f);
  186. n_m11 = n / m11;
  187. n_m00 = n / m00;
  188. *nearZ = n;
  189. *farZ = f;
  190. *bottom = n_m11 * (m21 - 1.0f);
  191. *top = n_m11 * (m21 + 1.0f);
  192. *left = n_m00 * (m20 - 1.0f);
  193. *right = n_m00 * (m20 + 1.0f);
  194. }
  195. /*!
  196. * @brief decomposes frustum values of perspective projection
  197. * with angle values with a left-hand coordinate system and a
  198. * clip-space of [0, 1].
  199. * this makes easy to get all values at once
  200. *
  201. * @param[in] proj perspective projection matrix
  202. * @param[out] dest array
  203. */
  204. CGLM_INLINE
  205. void
  206. glm_persp_decompv_lh_zo(mat4 proj, float dest[6]) {
  207. glm_persp_decomp_lh_zo(proj, &dest[0], &dest[1], &dest[2],
  208. &dest[3], &dest[4], &dest[5]);
  209. }
  210. /*!
  211. * @brief decomposes left and right values of perspective projection (ZO).
  212. * x stands for x axis (left / right axis)
  213. *
  214. * @param[in] proj perspective projection matrix
  215. * @param[out] left left
  216. * @param[out] right right
  217. */
  218. CGLM_INLINE
  219. void
  220. glm_persp_decomp_x_lh_zo(mat4 proj,
  221. float * __restrict left,
  222. float * __restrict right) {
  223. float nearZ, m20, m00;
  224. m00 = proj[0][0];
  225. m20 = proj[2][0];
  226. nearZ = proj[3][2] / (proj[3][3]);
  227. *left = nearZ * (m20 - 1.0f) / m00;
  228. *right = nearZ * (m20 + 1.0f) / m00;
  229. }
  230. /*!
  231. * @brief decomposes top and bottom values of perspective projection
  232. * with angle values with a left-hand coordinate system and a
  233. * clip-space of [0, 1].
  234. * y stands for y axis (top / bottom axis)
  235. *
  236. * @param[in] proj perspective projection matrix
  237. * @param[out] top top
  238. * @param[out] bottom bottom
  239. */
  240. CGLM_INLINE
  241. void
  242. glm_persp_decomp_y_lh_zo(mat4 proj,
  243. float * __restrict top,
  244. float * __restrict bottom) {
  245. float nearZ, m21, m11;
  246. m21 = proj[2][1];
  247. m11 = proj[1][1];
  248. nearZ = proj[3][2] / (proj[3][3]);
  249. *bottom = nearZ * (m21 - 1) / m11;
  250. *top = nearZ * (m21 + 1) / m11;
  251. }
  252. /*!
  253. * @brief decomposes near and far values of perspective projection
  254. * with angle values with a left-hand coordinate system and a
  255. * clip-space of [0, 1].
  256. * z stands for z axis (near / far axis)
  257. *
  258. * @param[in] proj perspective projection matrix
  259. * @param[out] nearZ near
  260. * @param[out] farZ far
  261. */
  262. CGLM_INLINE
  263. void
  264. glm_persp_decomp_z_lh_zo(mat4 proj,
  265. float * __restrict nearZ,
  266. float * __restrict farZ) {
  267. float m32, m22;
  268. m32 = proj[3][2];
  269. m22 = -proj[2][2];
  270. *nearZ = m32 / m22;
  271. *farZ = m32 / (m22 + 1.0f);
  272. }
  273. /*!
  274. * @brief decomposes far value of perspective projection
  275. * with angle values with a left-hand coordinate system and a
  276. * clip-space of [0, 1].
  277. *
  278. * @param[in] proj perspective projection matrix
  279. * @param[out] farZ far
  280. */
  281. CGLM_INLINE
  282. void
  283. glm_persp_decomp_far_lh_zo(mat4 proj, float * __restrict farZ) {
  284. *farZ = proj[3][2] / (-proj[2][2] + 1.0f);
  285. }
  286. /*!
  287. * @brief decomposes near value of perspective projection
  288. * with angle values with a left-hand coordinate system and a
  289. * clip-space of [0, 1].
  290. *
  291. * @param[in] proj perspective projection matrix
  292. * @param[out] nearZ near
  293. */
  294. CGLM_INLINE
  295. void
  296. glm_persp_decomp_near_lh_zo(mat4 proj, float * __restrict nearZ) {
  297. *nearZ = proj[3][2] / -proj[2][2];
  298. }
  299. /*!
  300. * @brief returns sizes of near and far planes of perspective projection
  301. * with a left-hand coordinate system and a
  302. * clip-space of [0, 1].
  303. *
  304. * @param[in] proj perspective projection matrix
  305. * @param[in] fovy fovy (see brief)
  306. * @param[out] dest sizes order: [Wnear, Hnear, Wfar, Hfar]
  307. */
  308. CGLM_INLINE
  309. void
  310. glm_persp_sizes_lh_zo(mat4 proj, float fovy, vec4 dest) {
  311. float t, a, nearZ, farZ;
  312. t = 2.0f * tanf(fovy * 0.5f);
  313. a = glm_persp_aspect(proj);
  314. glm_persp_decomp_z_lh_zo(proj, &nearZ, &farZ);
  315. dest[1] = t * nearZ;
  316. dest[3] = t * farZ;
  317. dest[0] = a * dest[1];
  318. dest[2] = a * dest[3];
  319. }
  320. /*!
  321. * @brief returns field of view angle along the Y-axis (in radians)
  322. * with a left-hand coordinate system and a clip-space of [0, 1].
  323. *
  324. * if you need to degrees, use glm_deg to convert it or use this:
  325. * fovy_deg = glm_deg(glm_persp_fovy(projMatrix))
  326. *
  327. * @param[in] proj perspective projection matrix
  328. */
  329. CGLM_INLINE
  330. float
  331. glm_persp_fovy_lh_zo(mat4 proj) {
  332. return glm_persp_fovy(proj);
  333. }
  334. /*!
  335. * @brief returns aspect ratio of perspective projection
  336. * with a left-hand coordinate system and a clip-space of [0, 1].
  337. *
  338. * @param[in] proj perspective projection matrix
  339. */
  340. CGLM_INLINE
  341. float
  342. glm_persp_aspect_lh_zo(mat4 proj) {
  343. return glm_persp_aspect(proj);
  344. }
  345. #endif /*cglm_persp_lh_zo_h*/