ortho_lh_zo.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_ortho_lh_zo(float left, float right,
  10. float bottom, float top,
  11. float nearZ, float farZ,
  12. mat4 dest)
  13. CGLM_INLINE void glm_ortho_aabb_lh_zo(vec3 box[2], mat4 dest)
  14. CGLM_INLINE void glm_ortho_aabb_p_lh_zo(vec3 box[2],
  15. float padding,
  16. mat4 dest)
  17. CGLM_INLINE void glm_ortho_aabb_pz_lh_zo(vec3 box[2],
  18. float padding,
  19. mat4 dest)
  20. CGLM_INLINE void glm_ortho_default_lh_zo(float aspect,
  21. mat4 dest)
  22. CGLM_INLINE void glm_ortho_default_s_lh_zo(float aspect,
  23. float size,
  24. mat4 dest)
  25. */
  26. #ifndef cglm_ortho_lh_zo_h
  27. #define cglm_ortho_lh_zo_h
  28. #include "../common.h"
  29. #include "../plane.h"
  30. #include "../mat4.h"
  31. /*!
  32. * @brief set up orthographic projection matrix with a left-hand coordinate
  33. * system and a clip-space of [0, 1].
  34. *
  35. * @param[in] left viewport.left
  36. * @param[in] right viewport.right
  37. * @param[in] bottom viewport.bottom
  38. * @param[in] top viewport.top
  39. * @param[in] nearZ near clipping plane
  40. * @param[in] farZ far clipping plane
  41. * @param[out] dest result matrix
  42. */
  43. CGLM_INLINE
  44. void
  45. glm_ortho_lh_zo(float left, float right,
  46. float bottom, float top,
  47. float nearZ, float farZ,
  48. mat4 dest) {
  49. float rl, tb, fn;
  50. glm_mat4_zero(dest);
  51. rl = 1.0f / (right - left);
  52. tb = 1.0f / (top - bottom);
  53. fn =-1.0f / (farZ - nearZ);
  54. dest[0][0] = 2.0f * rl;
  55. dest[1][1] = 2.0f * tb;
  56. dest[2][2] =-fn;
  57. dest[3][0] =-(right + left) * rl;
  58. dest[3][1] =-(top + bottom) * tb;
  59. dest[3][2] = nearZ * fn;
  60. dest[3][3] = 1.0f;
  61. }
  62. /*!
  63. * @brief set up orthographic projection matrix using bounding box
  64. * with a left-hand coordinate system and a clip-space of [0, 1].
  65. *
  66. * bounding box (AABB) must be in view space
  67. *
  68. * @param[in] box AABB
  69. * @param[out] dest result matrix
  70. */
  71. CGLM_INLINE
  72. void
  73. glm_ortho_aabb_lh_zo(vec3 box[2], mat4 dest) {
  74. glm_ortho_lh_zo(box[0][0], box[1][0],
  75. box[0][1], box[1][1],
  76. -box[1][2], -box[0][2],
  77. dest);
  78. }
  79. /*!
  80. * @brief set up orthographic projection matrix using bounding box
  81. * with a left-hand coordinate system and a clip-space of [0, 1].
  82. *
  83. * bounding box (AABB) must be in view space
  84. *
  85. * @param[in] box AABB
  86. * @param[in] padding padding
  87. * @param[out] dest result matrix
  88. */
  89. CGLM_INLINE
  90. void
  91. glm_ortho_aabb_p_lh_zo(vec3 box[2], float padding, mat4 dest) {
  92. glm_ortho_lh_zo(box[0][0] - padding, box[1][0] + padding,
  93. box[0][1] - padding, box[1][1] + padding,
  94. -(box[1][2] + padding), -(box[0][2] - padding),
  95. dest);
  96. }
  97. /*!
  98. * @brief set up orthographic projection matrix using bounding box
  99. * with a left-hand coordinate system and a clip-space of [0, 1].
  100. *
  101. * bounding box (AABB) must be in view space
  102. *
  103. * @param[in] box AABB
  104. * @param[in] padding padding for near and far
  105. * @param[out] dest result matrix
  106. */
  107. CGLM_INLINE
  108. void
  109. glm_ortho_aabb_pz_lh_zo(vec3 box[2], float padding, mat4 dest) {
  110. glm_ortho_lh_zo(box[0][0], box[1][0],
  111. box[0][1], box[1][1],
  112. -(box[1][2] + padding), -(box[0][2] - padding),
  113. dest);
  114. }
  115. /*!
  116. * @brief set up unit orthographic projection matrix
  117. * with a left-hand coordinate system and a clip-space of [0, 1].
  118. *
  119. * @param[in] aspect aspect ration ( width / height )
  120. * @param[out] dest result matrix
  121. */
  122. CGLM_INLINE
  123. void
  124. glm_ortho_default_lh_zo(float aspect, mat4 dest) {
  125. if (aspect >= 1.0f) {
  126. glm_ortho_lh_zo(-aspect, aspect, -1.0f, 1.0f, -100.0f, 100.0f, dest);
  127. return;
  128. }
  129. aspect = 1.0f / aspect;
  130. glm_ortho_lh_zo(-1.0f, 1.0f, -aspect, aspect, -100.0f, 100.0f, dest);
  131. }
  132. /*!
  133. * @brief set up orthographic projection matrix with given CUBE size
  134. * with a left-hand coordinate system and a clip-space of [0, 1].
  135. *
  136. * @param[in] aspect aspect ratio ( width / height )
  137. * @param[in] size cube size
  138. * @param[out] dest result matrix
  139. */
  140. CGLM_INLINE
  141. void
  142. glm_ortho_default_s_lh_zo(float aspect, float size, mat4 dest) {
  143. if (aspect >= 1.0f) {
  144. glm_ortho_lh_zo(-size * aspect,
  145. size * aspect,
  146. -size,
  147. size,
  148. -size - 100.0f,
  149. size + 100.0f,
  150. dest);
  151. return;
  152. }
  153. glm_ortho_lh_zo(-size,
  154. size,
  155. -size / aspect,
  156. size / aspect,
  157. -size - 100.0f,
  158. size + 100.0f,
  159. dest);
  160. }
  161. #endif /*cglm_ortho_lh_zo_h*/