HTML and CSS Reference
In-Depth Information
Listing 9-7. Naive Frustum Culling
/// Could be sped up by batching a number of spheres and removing branches.
Frustum.prototype.containsSphere = function(sphere) {
var plane, dist, radius = sphere.getRadius();
for (var i = 0; i < 6; ++i) {
plane = this.planes_[i];
dist =, plane.getNormal()) + plane.getDistance();
if (dist < -radius)
return Frustum.Out;
else if (Math.abs(dist) < radius)
return Frustum.Intersect;
return Frustum.In;
For a bounding sphere the distance from the center point to the plane is compared, along with the radius of the
sphere, to determine which side of the plane the sphere is on. If the sphere is outside any of the six planes making up
the bounding volume, its geometry can be discarded from the visible set.
Rendering Order
The order in which geometry is sent to the graphics card can have a direct impact on the performance of the
rendering. For example, the hardware will not run the fragment shader upon failure of the depth test. This is an
optimization known in OpenGL as the Early Depth Test. For applications with expensive fragment shaders, or
multiple passes over the same geometry, this test can have a positive effect on performance.
The OpenGL specification defines a pipeline in which the depth test occurs after the fragment shader is run
(see Figure 9-4 ). However, in certain scenarios it is possible to run the depth test before the fragment shader, which
meets this stipulation, as the depth test still functions as if it were being done after the fragment shader. Whether
this optimization is available depends on the underlying OpenGL implementation and the fragment shader being
used. The optimization is likely to be turned off if the source uses the discard keyword. Also, if the shader uses
gl_FragDepth , which isn't present in WebGL 1.x but which is likely to be present in WebGL 2.x, the technique
will not be available.
Search WWH ::

Custom Search