Graphics Reference
In-Depth Information
1. Render edges:
Enable depth buffer updates.
Set all pixels to pass always.
Draw all edges of object B , writing their depth into the depth buffer.
2. Render faces:
Disable depth buffer updates.
Set pixels to pass if they are nearer than the stored depth value.
Enable the stencil buffer test.
Render front faces of object A . Increment stencil buffer for pixels that pass.
Render back faces of object A . Decrement stencil buffer for pixels that pass.
After all three steps have been executed, the stencil buffer contains nonzero values
if objects A and B are in collision. A sample OpenGL implementation of this collision
test is provided in Table 10.2.
Rather than actually reading back the stencil buffer, nonzero values in the stencil
buffer can be detected using an occlusion query for the screen-space bounding box
of the smaller of the two objects. By passing query pixels that have a nonzero stencil
value, the objects are intersecting if and only if the occlusion query reports pixels
passing the stencil test.
It is important to note that the method described in this section is not without
problems and may fail in certain situations. As an example, consider the case of
Figure 10.5, in which two AABBs, A and B , are intersecting. Because those edges of A
in intersection with object B are aimed straight into the screen, they are not rasterized.
Thus, the only depth values written by A are those for the edges of the frontmost
or backmost face. In that none of these edges interpenetrate B , and because no
edges of B interpenetrate A , the result is that collision is not detected. Fortunately, in
practice this situation can be made unlikely to happen by rendering the objects using
perspective rather than orthographic projection. Although similar configurations can
be constructed for perspective projection, the odds of shapes aligning into a similar
situation in perspective projection are low. When alignment is still a concern, testing
collision from multiple viewpoints would further reduce the odds of this type of
failure.
The current test is not limited to detecting the intersection of only two objects. The
intersections of multiple objects can be tested by changing the algorithm to first render
all objects (rather than just one) in wireframe, followed by rendering all objects (rather
Search WWH ::




Custom Search