HTML and CSS Reference
renderer diffuse value is that, modified for each cube, there are 200 uniform calls. This is almost 100 more than the
best case scenario, in which the cubes are rendered by color, resulting in 3 calls to set the diffuse color and 100 calls
setting the matrix. So, even though the underlying renderer is smart enough to prevent redundant calls, there can still
be bottlenecks because of how the renderer is being fed the scene.
When optimizing, it's important to think about the how the application works and to choose algorithms that
fit the specific use. Generalization is usually in opposition to execution speed, so really think about how the scene
should be rendered, and adjust accordingly. Additionally, profiling should be added from the start, as there is no way
to prove that one algorithm is more effective than another without having any metrics to back that up. Also, remember
that some optimizations may have a bigger effect on different hardware, so make sure to profile on a good sampling of
devices that may run the program.
With graphics programming there are multitudes of published algorithms available to help speed the rendering.
For this reason, it's impossible to cover them all. However, there are a few optimization techniques that should be
present in a software engineer's toolbox.
Sometimes, the entire scene is not completely within the view of the user. In this case, it's important to submit
only geometry that can affect the final rendering. To determine what is visible, the bounds of the geometry can be
compared with the viewing volume, and if the geometry is contained within the extents, it can be passed to the
The viewing volume (also called the viewing frustum) can be represented as a set of six planes: near, far, left,
right, top, and bottom (see Figure 9-3 ). The bounding volume is then compared against those planes to determine
whether it is contained within the frustum.
Figure 9-3. Frustum culling
The algorithm depends on the bounding volume used. The tighter the bounding volume, the higher the
computational cost. The simplest is the bounding sphere (see Listing 9-7 for a simple implementation), which can
easily be compared against the plane but which is highly unlikely to provide a snug fit around the geometry. An
axis-aligned bounding box (AABB) is more likely to have a close fit than a sphere, resulting in less geometry's being
inadvertently set to the renderer, but the computation cost is greater. There are other potential bounding volumes—
cylinder, capsule, object-oriented (OO) bounding box—but the sphere or AABB are the ones most likely to be
encountered when working on the graphics side.