Graphics Reference
In-Depth Information
Table 10.2 Implementation outline of a concave object collision test using OpenGL.
Step
Code
// Disable color buffer writes
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// Clear depth and stencil buffers, initializing depth buffer to far Z (1.0)
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Enable depth buffer testing
glEnable(GL_DEPTH_TEST);
0
// Enable depth buffer updates. Set all pixels to pass always
glDepthMask(GL_TRUE);
glDepthFunc(GL_ALWAYS);
// Draw edges of object B
glPushAttrib(GL_POLYGON_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
DrawObject(B);
glPopAttrib();
1
// Disable depth buffer updates. Pass pixels if nearer than stored depth value
glDepthMask(GL_FALSE);
glDepthFunc(GL_LESS);
// Increment stencil buffer for object A frontfaces
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glCullFace(GL_BACK);
DrawObject(A);
// Decrement stencil buffer for object A backfaces
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
glCullFace(GL_FRONT);
DrawObject(A);
// Read back stencil buffer. Nonzero stencil values implies collision
...
2
than just one) solidly. In order to know which two objects intersect when a collision
is detected, object (and edge and face) IDs can be encoded in bits of the color buffer
during rendering of both wireframe edges and solid faces. Where nonzero stencil
buffer values indicate an object collision, the bits at the corresponding position in
the color buffer now identify the objects (and edges and faces) in collision. Due to
hardware rasterization issues, lines do not rasterize in the same way as polygons.
Specifically, the edges of a polygon, rasterized as lines, do not necessarily occupy
the same pixels at the same depths as the polygon edge pixels. Thus, when several
 
Search WWH ::




Custom Search