persp_rh_zo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_rh_zo(float left, float right,
  10. float bottom, float top,
  11. float nearZ, float farZ,
  12. mat4 dest)
  13. CGLM_INLINE void glm_perspective_rh_zo(float fovy,
  14. float aspect,
  15. float nearZ,
  16. float farZ,
  17. mat4 dest)
  18. CGLM_INLINE void glm_perspective_default_rh_zo(float aspect, mat4 dest)
  19. CGLM_INLINE void glm_perspective_resize_rh_zo(float aspect, mat4 proj)
  20. CGLM_INLINE void glm_persp_move_far_rh_zo(mat4 proj,
  21. float deltaFar)
  22. CGLM_INLINE void glm_persp_decomp_rh_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_rh_zo(mat4 proj,
  30. float dest[6])
  31. CGLM_INLINE void glm_persp_decomp_x_rh_zo(mat4 proj,
  32. float * __restrict left,
  33. float * __restrict right)
  34. CGLM_INLINE void glm_persp_decomp_y_rh_zo(mat4 proj,
  35. float * __restrict top,
  36. float * __restrict bottom)
  37. CGLM_INLINE void glm_persp_decomp_z_rh_zo(mat4 proj,
  38. float * __restrict nearZ,
  39. float * __restrict farZ)
  40. CGLM_INLINE void glm_persp_decomp_far_rh_zo(mat4 proj, float * __restrict farZ)
  41. CGLM_INLINE void glm_persp_decomp_near_rh_zo(mat4 proj, float * __restrict nearZ)
  42. CGLM_INLINE void glm_persp_sizes_rh_zo(mat4 proj, float fovy, vec4 dest)
  43. */
  44. #ifndef cglm_persp_rh_zo_h
  45. #define cglm_persp_rh_zo_h
  46. #include "../common.h"
  47. #include "persp.h"
  48. /*!
  49. * @brief set up perspective peprojection matrix with a right-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_rh_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 right-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_rh_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 set up perspective projection matrix with default near/far
  109. * and angle values with a right-hand coordinate system and a
  110. * clip-space of [0, 1].
  111. *
  112. * @param[in] aspect aspect ratio ( width / height )
  113. * @param[out] dest result matrix
  114. */
  115. CGLM_INLINE
  116. void
  117. glm_perspective_default_rh_zo(float aspect, mat4 dest) {
  118. glm_perspective_rh_zo(GLM_PI_4f, aspect, 0.01f, 100.0f, dest);
  119. }
  120. /*!
  121. * @brief resize perspective matrix by aspect ratio ( width / height )
  122. * this makes very easy to resize proj matrix when window /viewport
  123. * resized with a right-hand coordinate system and a clip-space of
  124. * [0, 1].
  125. *
  126. * @param[in] aspect aspect ratio ( width / height )
  127. * @param[in, out] proj perspective projection matrix
  128. */
  129. CGLM_INLINE
  130. void
  131. glm_perspective_resize_rh_zo(float aspect, mat4 proj) {
  132. if (proj[0][0] == 0.0f)
  133. return;
  134. proj[0][0] = proj[1][1] / aspect;
  135. }
  136. /*!
  137. * @brief extend perspective projection matrix's far distance with a
  138. * right-hand coordinate system and a clip-space of [0, 1].
  139. *
  140. * this function does not guarantee far >= near, be aware of that!
  141. *
  142. * @param[in, out] proj projection matrix to extend
  143. * @param[in] deltaFar distance from existing far (negative to shink)
  144. */
  145. CGLM_INLINE
  146. void
  147. glm_persp_move_far_rh_zo(mat4 proj, float deltaFar) {
  148. float fn, farZ, nearZ, p22, p32;
  149. p22 = proj[2][2];
  150. p32 = proj[3][2];
  151. nearZ = p32 / p22;
  152. farZ = p32 / (p22 + 1.0f) + deltaFar;
  153. fn = 1.0f / (nearZ - farZ);
  154. proj[2][2] = farZ * fn;
  155. proj[3][2] = nearZ * farZ * fn;
  156. }
  157. /*!
  158. * @brief decomposes frustum values of perspective projection
  159. * with angle values with a right-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_rh_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 right-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_rh_zo(mat4 proj, float dest[6]) {
  207. glm_persp_decomp_rh_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_rh_zo(mat4 proj,
  221. float * __restrict left,
  222. float * __restrict right) {
  223. float nearZ, m20, m00, m22;
  224. m00 = proj[0][0];
  225. m20 = proj[2][0];
  226. m22 = proj[2][2];
  227. nearZ = proj[3][2] / m22;
  228. *left = nearZ * (m20 - 1.0f) / m00;
  229. *right = nearZ * (m20 + 1.0f) / m00;
  230. }
  231. /*!
  232. * @brief decomposes top and bottom values of perspective projection
  233. * with angle values with a right-hand coordinate system and a
  234. * clip-space of [0, 1].
  235. * y stands for y axis (top / bottom axis)
  236. *
  237. * @param[in] proj perspective projection matrix
  238. * @param[out] top top
  239. * @param[out] bottom bottom
  240. */
  241. CGLM_INLINE
  242. void
  243. glm_persp_decomp_y_rh_zo(mat4 proj,
  244. float * __restrict top,
  245. float * __restrict bottom) {
  246. float nearZ, m21, m11, m22;
  247. m21 = proj[2][1];
  248. m11 = proj[1][1];
  249. m22 = proj[2][2];
  250. nearZ = proj[3][2] / m22;
  251. *bottom = nearZ * (m21 - 1) / m11;
  252. *top = nearZ * (m21 + 1) / m11;
  253. }
  254. /*!
  255. * @brief decomposes near and far values of perspective projection
  256. * with angle values with a right-hand coordinate system and a
  257. * clip-space of [0, 1].
  258. * z stands for z axis (near / far axis)
  259. *
  260. * @param[in] proj perspective projection matrix
  261. * @param[out] nearZ near
  262. * @param[out] farZ far
  263. */
  264. CGLM_INLINE
  265. void
  266. glm_persp_decomp_z_rh_zo(mat4 proj,
  267. float * __restrict nearZ,
  268. float * __restrict farZ) {
  269. float m32, m22;
  270. m32 = proj[3][2];
  271. m22 = proj[2][2];
  272. *nearZ = m32 / m22;
  273. *farZ = m32 / (m22 + 1.0f);
  274. }
  275. /*!
  276. * @brief decomposes far value of perspective projection
  277. * with angle values with a right-hand coordinate system and a
  278. * clip-space of [0, 1].
  279. *
  280. * @param[in] proj perspective projection matrix
  281. * @param[out] farZ far
  282. */
  283. CGLM_INLINE
  284. void
  285. glm_persp_decomp_far_rh_zo(mat4 proj, float * __restrict farZ) {
  286. *farZ = proj[3][2] / (proj[2][2] + 1.0f);
  287. }
  288. /*!
  289. * @brief decomposes near value of perspective projection
  290. * with angle values with a right-hand coordinate system and a
  291. * clip-space of [0, 1].
  292. *
  293. * @param[in] proj perspective projection matrix
  294. * @param[out] nearZ near
  295. */
  296. CGLM_INLINE
  297. void
  298. glm_persp_decomp_near_rh_zo(mat4 proj, float * __restrict nearZ) {
  299. *nearZ = proj[3][2] / proj[2][2];
  300. }
  301. /*!
  302. * @brief returns sizes of near and far planes of perspective projection
  303. * with a right-hand coordinate system and a
  304. * clip-space of [0, 1].
  305. *
  306. * @param[in] proj perspective projection matrix
  307. * @param[in] fovy fovy (see brief)
  308. * @param[out] dest sizes order: [Wnear, Hnear, Wfar, Hfar]
  309. */
  310. CGLM_INLINE
  311. void
  312. glm_persp_sizes_rh_zo(mat4 proj, float fovy, vec4 dest) {
  313. float t, a, nearZ, farZ;
  314. t = 2.0f * tanf(fovy * 0.5f);
  315. a = glm_persp_aspect(proj);
  316. glm_persp_decomp_z_rh_zo(proj, &nearZ, &farZ);
  317. dest[1] = t * nearZ;
  318. dest[3] = t * farZ;
  319. dest[0] = a * dest[1];
  320. dest[2] = a * dest[3];
  321. }
  322. /*!
  323. * @brief returns field of view angle along the Y-axis (in radians)
  324. * with a right-hand coordinate system and a clip-space of [0, 1].
  325. *
  326. * if you need to degrees, use glm_deg to convert it or use this:
  327. * fovy_deg = glm_deg(glm_persp_fovy(projMatrix))
  328. *
  329. * @param[in] proj perspective projection matrix
  330. */
  331. CGLM_INLINE
  332. float
  333. glm_persp_fovy_rh_zo(mat4 proj) {
  334. return glm_persp_fovy(proj);
  335. }
  336. /*!
  337. * @brief returns aspect ratio of perspective projection
  338. * with a right-hand coordinate system and a clip-space of [0, 1].
  339. *
  340. * @param[in] proj perspective projection matrix
  341. */
  342. CGLM_INLINE
  343. float
  344. glm_persp_aspect_rh_zo(mat4 proj) {
  345. return glm_persp_aspect(proj);
  346. }
  347. #endif /*cglm_persp_rh_zo_h*/