You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapter25/chapter25.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,11 @@ Thus, in order to perform frustum culling we need to:
16
16
17
17
* For every GameItem, check if its contained inside that view frustum, that is, comatined between the size frustum planes, and eliminate the ones that lie outside from the rendering process.
So let’s start by calculating the frustum planes. A plane, is defined by a point contained in it and a vector orthogonal to that plane, as shown in the next figure:
22
22
23
-

23
+

24
24
25
25
The equation of a plane is defined like this:
26
26
@@ -30,7 +30,7 @@ Hence, we need to calculate the six plane equations for the six sides of our vie
30
30
31
31
So let’s start coding. We will create a new class named `FrustumCullingFilter` which will perform, as its name states, filtering operations according to the view frustum.
32
32
33
-
```
33
+
```java
34
34
publicclassFrustumCullingFilter {
35
35
36
36
privatestaticfinalintNUM_PLANES=6;
@@ -60,7 +60,7 @@ public class FrustumCullingFilter {
60
60
61
61
The `FrustumCullingFilter`classwill also have a method to calculate the plane equations called `updateFrustum`which will be called before rendering. The method is defined like this:
62
62
63
-
```
63
+
```java
64
64
public void updateFrustum(Matrix4f projMatrix, Matrix4f viewMatrix) {
65
65
// Calculate projection view matrix
66
66
prjViewMatrix.set(projMatrix);
@@ -76,13 +76,13 @@ First, we store a copy of the projection matrix and multiply it by the view matr
76
76
77
77
Now that we have all the planes calculated we just need to check if the `GameItem`instances are inside the frustum or not. How can we do this ? Let’s first examine how we can check if a point is inside the frustum. We can achieve that by calculating the signed distance of the point to each of the planes. If the distance of the point to the plane is positive, this means that the point is in front of the plane \(according to its normal\). If it’s negative, this means that the point is behind the plane.
78
78
79
-

79
+

80
80
81
81
Therefore, a point will be inside the view frustum if the distance to all the planes of the frustum is positive. The distance of a point to the plane is defined like this:
82
82
83
-
$$dist=Ax0+By0+Cz0+D$$, where $$x0$$, $$y0$$ and $$z0$$ are the coordinates of the point.
83
+
$$dist=Ax_{0}+By_{0}+Cz_{0}+D$$, where $$x_{0}$$, $$y_{0}$$ and $$z_{0}$$ are the coordinates of the point.
84
84
85
-
So, a point is behind the plane if $$Ax0+By0+Cz0+D <= 0$$.
85
+
So, a point is behind the plane if $$Ax_{0}+By_{0}+Cz_{0}+D<=0$$.
86
86
87
87
But, we do not have points, we have complex meshes, we cannot just use a point to check if an object is inside a frustum or not. You may think in checking every vertex of the `GameItem`and see if it’s inside the frustum or not. If any of the points is inside, the GameItem should be drawn. Butthis what OpenGL does in fact when clipping, this is what we are tying to avoid. Remember that frustum culling benefits will be more noticeable the more complex the meshes to be rendered are.
88
88
@@ -94,11 +94,11 @@ We need to enclsoe evey `GameItem`into a simple volume that is easy to check. He
94
94
95
95
Inthiscase, we will use spheres, since is the most simple approach. We will enclose every `GameItems`into a sphere and will check if the sphere is inside the view frustum or not. In order to do that, we just need the center and the radius of the sphere. The checks are almost equal to the point case, except that we need to take the radius into consideration. A sphere will be outside the frustim if it the following condition is met: $$dist=Ax0+By0+Cz0<=-radius$$.
0 commit comments