ortho_rh_zo.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_rh_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_rh_zo(vec3 box[2], mat4 dest)
  14. CGLM_INLINE void glm_ortho_aabb_p_rh_zo(vec3 box[2],
  15. float padding,
  16. mat4 dest)
  17. CGLM_INLINE void glm_ortho_aabb_pz_rh_zo(vec3 box[2],
  18. float padding,
  19. mat4 dest)
  20. CGLM_INLINE void glm_ortho_default_rh_zo(float aspect,
  21. mat4 dest)
  22. CGLM_INLINE void glm_ortho_default_s_rh_zo(float aspect,
  23. float size,
  24. mat4 dest)
  25. */
  26. #ifndef cglm_ortho_rh_zo_h
  27. #define cglm_ortho_rh_zo_h
  28. #include "../common.h"
  29. #include "../plane.h"
  30. #include "../mat4.h"
  31. /*!
  32. * @brief set up orthographic projection matrix with a right-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_rh_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 right-hand coordinate system and a clip-space with depth
  65. * values from zero to one.
  66. *
  67. * bounding box (AABB) must be in view space
  68. *
  69. * @param[in] box AABB
  70. * @param[out] dest result matrix
  71. */
  72. CGLM_INLINE
  73. void
  74. glm_ortho_aabb_rh_zo(vec3 box[2], mat4 dest) {
  75. glm_ortho_rh_zo(box[0][0], box[1][0],
  76. box[0][1], box[1][1],
  77. -box[1][2], -box[0][2],
  78. dest);
  79. }
  80. /*!
  81. * @brief set up orthographic projection matrix using bounding box
  82. * with a right-hand coordinate system and a clip-space with depth
  83. * values from zero to one.
  84. *
  85. * bounding box (AABB) must be in view space
  86. *
  87. * @param[in] box AABB
  88. * @param[in] padding padding
  89. * @param[out] dest result matrix
  90. */
  91. CGLM_INLINE
  92. void
  93. glm_ortho_aabb_p_rh_zo(vec3 box[2], float padding, mat4 dest) {
  94. glm_ortho_rh_zo(box[0][0] - padding, box[1][0] + padding,
  95. box[0][1] - padding, box[1][1] + padding,
  96. -(box[1][2] + padding), -(box[0][2] - padding),
  97. dest);
  98. }
  99. /*!
  100. * @brief set up orthographic projection matrix using bounding box
  101. * with a right-hand coordinate system and a clip-space with depth
  102. * values from zero to one.
  103. *
  104. * bounding box (AABB) must be in view space
  105. *
  106. * @param[in] box AABB
  107. * @param[in] padding padding for near and far
  108. * @param[out] dest result matrix
  109. */
  110. CGLM_INLINE
  111. void
  112. glm_ortho_aabb_pz_rh_zo(vec3 box[2], float padding, mat4 dest) {
  113. glm_ortho_rh_zo(box[0][0], box[1][0],
  114. box[0][1], box[1][1],
  115. -(box[1][2] + padding), -(box[0][2] - padding),
  116. dest);
  117. }
  118. /*!
  119. * @brief set up unit orthographic projection matrix with a right-hand
  120. * coordinate system and a clip-space of [0, 1].
  121. *
  122. * @param[in] aspect aspect ration ( width / height )
  123. * @param[out] dest result matrix
  124. */
  125. CGLM_INLINE
  126. void
  127. glm_ortho_default_rh_zo(float aspect, mat4 dest) {
  128. if (aspect >= 1.0f) {
  129. glm_ortho_rh_zo(-aspect, aspect, -1.0f, 1.0f, -100.0f, 100.0f, dest);
  130. return;
  131. }
  132. aspect = 1.0f / aspect;
  133. glm_ortho_rh_zo(-1.0f, 1.0f, -aspect, aspect, -100.0f, 100.0f, dest);
  134. }
  135. /*!
  136. * @brief set up orthographic projection matrix with given CUBE size
  137. * with a right-hand coordinate system and a clip-space with depth
  138. * values from zero to one.
  139. *
  140. * @param[in] aspect aspect ratio ( width / height )
  141. * @param[in] size cube size
  142. * @param[out] dest result matrix
  143. */
  144. CGLM_INLINE
  145. void
  146. glm_ortho_default_s_rh_zo(float aspect, float size, mat4 dest) {
  147. if (aspect >= 1.0f) {
  148. glm_ortho_rh_zo(-size * aspect,
  149. size * aspect,
  150. -size,
  151. size,
  152. -size - 100.0f,
  153. size + 100.0f,
  154. dest);
  155. return;
  156. }
  157. glm_ortho_rh_zo(-size,
  158. size,
  159. -size / aspect,
  160. size / aspect,
  161. -size - 100.0f,
  162. size + 100.0f,
  163. dest);
  164. }
  165. #endif /*cglm_ortho_rh_zo_h*/