SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLFrustum.cpp
Go to the documentation of this file.
1 /**
2  * \file SLFrustum.cpp
3  * \authors Luc Girod, Marcus Hudritsch
4  * \date Summer 2021
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <vector>
11 #include <SLPlane.h>
12 #include <SLMat4.h>
13 #include <SLFrustum.h>
14 
15 //-----------------------------------------------------------------------------
16 /*!
17  * Calculates the coefficients of the 6 frustum planes from the passed
18  * projection and view matrices. See the paper from Gribb and Hartmann:
19  * https://www.gamedevs.org/uploads/fast-extraction-viewing-frustum-planes-from-world-view-projection-matrix.pdf
20  * \param planes Pointer to an array of 6 SLPlanes (L R T B N F)
21  * \param projectionMat 4x4 projection matrix
22  * \param viewMat 4x4 view matrix
23  */
25  const SLMat4f& projectionMat,
26  const SLMat4f& viewMat)
27 {
28  // Combine view and projection matrix
29  SLMat4f A = projectionMat * viewMat;
30 
31  // Order is L R T B N F
32  viewToFrustumPlanes(planes, A);
33 }
34 //-----------------------------------------------------------------------------
35 /*!
36  * Calculates the coefficients of the 6 frustum planes from the passed
37  * matrix A. See the paper from Gribb and Hartmann:
38  * https://www.gamedevs.org/uploads/fast-extraction-viewing-frustum-planes-from-world-view-projection-matrix.pdf
39  * \param planes Pointer to an array of 6 SLPlanes (L R T B N F)
40  * \param A The projection matrix
41  */
43 {
44  // set the A,B,C & D coefficient for each plane
45  // Order is L R T B N F
46  planes[0].setCoefficients(A.m(0) + A.m(3),
47  A.m(4) + A.m(7),
48  A.m(8) + A.m(11),
49  A.m(12) + A.m(15));
50  planes[1].setCoefficients(-A.m(0) + A.m(3),
51  -A.m(4) + A.m(7),
52  -A.m(8) + A.m(11),
53  -A.m(12) + A.m(15));
54  planes[2].setCoefficients(-A.m(1) + A.m(3),
55  -A.m(5) + A.m(7),
56  -A.m(9) + A.m(11),
57  -A.m(13) + A.m(15));
58  planes[3].setCoefficients(A.m(1) + A.m(3),
59  A.m(5) + A.m(7),
60  A.m(9) + A.m(11),
61  A.m(13) + A.m(15));
62  planes[4].setCoefficients(A.m(2) + A.m(3),
63  A.m(6) + A.m(7),
64  A.m(10) + A.m(11),
65  A.m(14) + A.m(15));
66  planes[5].setCoefficients(-A.m(2) + A.m(3),
67  -A.m(6) + A.m(7),
68  -A.m(10) + A.m(11),
69  -A.m(14) + A.m(15));
70 }
71 //-----------------------------------------------------------------------------
72 //! Returns frustum points in view space
74  float fovV,
75  float ratio,
76  float clipNear,
77  float clipFar)
78 {
79  SLfloat tanFovV = tan(Utils::DEG2RAD * fovV * 0.5f);
80 
81  // Calculate the 4 points on the near plane
82  SLfloat t = tanFovV * clipNear; // top
83  SLfloat b = -t; // bottom
84  SLfloat r = ratio * t; // right
85  SLfloat l = -r; // left
86  points[0] = (SLVec3f(r, t, -clipNear));
87  points[1] = (SLVec3f(r, b, -clipNear));
88  points[2] = (SLVec3f(l, t, -clipNear));
89  points[3] = (SLVec3f(l, b, -clipNear));
90 
91  // Calculate the 4 points on the far plane
92  t = tanFovV * clipFar; // top
93  b = -t; // bottom
94  r = ratio * t; // right
95  l = -r; // left
96 
97  points[4] = (SLVec3f(r, t, -clipFar));
98  points[5] = (SLVec3f(r, b, -clipFar));
99  points[6] = (SLVec3f(l, t, -clipFar));
100  points[7] = (SLVec3f(l, b, -clipFar));
101 }
102 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
static void viewToFrustumPlanes(SLPlane *planes, const SLMat4f &projectionMat, const SLMat4f &viewMat)
Definition: SLFrustum.cpp:24
static void getPointsInViewSpace(SLVec3f *points, float fovV, float ratio, float clipNear, float clipFar)
Returns frustum points in view space.
Definition: SLFrustum.cpp:73
void m(int i, T val)
Definition: SLMat4.h:93
Defines a plane in 3D space with the equation ax + by + cy + d = 0.
Definition: SLPlane.h:25
void setCoefficients(const SLfloat A, const SLfloat B, const SLfloat C, const SLfloat D)
Definition: SLPlane.cpp:50
static const float DEG2RAD
Definition: Utils.h:239