frustum.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .. default-domain:: C
  2. frustum
  3. =============
  4. Header: cglm/frustum.h
  5. cglm provides convenient functions to extract frustum planes, corners...
  6. All extracted corners are **vec4** so you must create array of **vec4**
  7. not **vec3**. If you want to store them to save space you msut convert them
  8. yourself.
  9. **vec4** is used to speed up functions need to corners. This is why frustum
  10. functions use *vec4* instead of *vec3*
  11. Currently related-functions use [-1, 1] clip space configuration to extract
  12. corners but you can override it by prodiving **GLM_CUSTOM_CLIPSPACE** macro.
  13. If you provide it then you have to all bottom macros as *vec4*
  14. Current configuration:
  15. .. code-block:: c
  16. /* near */
  17. GLM_CSCOORD_LBN {-1.0f, -1.0f, -1.0f, 1.0f}
  18. GLM_CSCOORD_LTN {-1.0f, 1.0f, -1.0f, 1.0f}
  19. GLM_CSCOORD_RTN { 1.0f, 1.0f, -1.0f, 1.0f}
  20. GLM_CSCOORD_RBN { 1.0f, -1.0f, -1.0f, 1.0f}
  21. /* far */
  22. GLM_CSCOORD_LBF {-1.0f, -1.0f, 1.0f, 1.0f}
  23. GLM_CSCOORD_LTF {-1.0f, 1.0f, 1.0f, 1.0f}
  24. GLM_CSCOORD_RTF { 1.0f, 1.0f, 1.0f, 1.0f}
  25. GLM_CSCOORD_RBF { 1.0f, -1.0f, 1.0f, 1.0f}
  26. Explain of short names:
  27. * **LBN**: left bottom near
  28. * **LTN**: left top near
  29. * **RTN**: right top near
  30. * **RBN**: right bottom near
  31. * **LBF**: left bottom far
  32. * **LTF**: left top far
  33. * **RTF**: right top far
  34. * **RBF**: right bottom far
  35. Table of contents (click to go):
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. Macros:
  38. .. code-block:: c
  39. GLM_LBN 0 /* left bottom near */
  40. GLM_LTN 1 /* left top near */
  41. GLM_RTN 2 /* right top near */
  42. GLM_RBN 3 /* right bottom near */
  43. GLM_LBF 4 /* left bottom far */
  44. GLM_LTF 5 /* left top far */
  45. GLM_RTF 6 /* right top far */
  46. GLM_RBF 7 /* right bottom far */
  47. GLM_LEFT 0
  48. GLM_RIGHT 1
  49. GLM_BOTTOM 2
  50. GLM_TOP 3
  51. GLM_NEAR 4
  52. GLM_FAR 5
  53. Functions:
  54. 1. :c:func:`glm_frustum_planes`
  55. #. :c:func:`glm_frustum_corners`
  56. #. :c:func:`glm_frustum_center`
  57. #. :c:func:`glm_frustum_box`
  58. #. :c:func:`glm_frustum_corners_at`
  59. Functions documentation
  60. ~~~~~~~~~~~~~~~~~~~~~~~
  61. .. c:function:: void glm_frustum_planes(mat4 m, vec4 dest[6])
  62. | extracts view frustum planes
  63. planes' space:
  64. - if m = proj: View Space
  65. - if m = viewProj: World Space
  66. - if m = MVP: Object Space
  67. You probably want to extract planes in world space so use viewProj as m
  68. Computing viewProj:
  69. .. code-block:: c
  70. glm_mat4_mul(proj, view, viewProj);
  71. Exracted planes order: [left, right, bottom, top, near, far]
  72. Parameters:
  73. | *[in]* **m** matrix
  74. | *[out]* **dest** exracted view frustum planes
  75. .. c:function:: void glm_frustum_corners(mat4 invMat, vec4 dest[8])
  76. | extracts view frustum corners using clip-space coordinates
  77. corners' space:
  78. - if m = invViewProj: World Space
  79. - if m = invMVP: Object Space
  80. You probably want to extract corners in world space so use **invViewProj**
  81. Computing invViewProj:
  82. .. code-block:: c
  83. glm_mat4_mul(proj, view, viewProj);
  84. ...
  85. glm_mat4_inv(viewProj, invViewProj);
  86. if you have a near coord at **i** index,
  87. you can get it's far coord by i + 4;
  88. follow example below to understand that
  89. For instance to find center coordinates between a near and its far coord:
  90. .. code-block:: c
  91. for (j = 0; j < 4; j++) {
  92. glm_vec3_center(corners[i], corners[i + 4], centerCorners[i]);
  93. }
  94. corners[i + 4] is far of corners[i] point.
  95. Parameters:
  96. | *[in]* **invMat** matrix
  97. | *[out]* **dest** exracted view frustum corners
  98. .. c:function:: void glm_frustum_center(vec4 corners[8], vec4 dest)
  99. | finds center of view frustum
  100. Parameters:
  101. | *[in]* **corners** view frustum corners
  102. | *[out]* **dest** view frustum center
  103. .. c:function:: void glm_frustum_box(vec4 corners[8], mat4 m, vec3 box[2])
  104. | finds bounding box of frustum relative to given matrix e.g. view mat
  105. Parameters:
  106. | *[in]* **corners** view frustum corners
  107. | *[in]* **m** matrix to convert existing conners
  108. | *[out]* **box** bounding box as array [min, max]
  109. .. c:function:: void glm_frustum_corners_at(vec4 corners[8], float splitDist, float farDist, vec4 planeCorners[4])
  110. | finds planes corners which is between near and far planes (parallel)
  111. this will be helpful if you want to split a frustum e.g. CSM/PSSM. This will
  112. find planes' corners but you will need to one more plane.
  113. Actually you have it, it is near, far or created previously with this func ;)
  114. Parameters:
  115. | *[in]* **corners** frustum corners
  116. | *[in]* **splitDist** split distance
  117. | *[in]* **farDist** far distance (zFar)
  118. | *[out]* **planeCorners** plane corners [LB, LT, RT, RB]