Graphics Reference
In-Depth Information
Table 10.1 Continued.
Pass
Code
// If occlusion test indicates no samples rendered, exit with no collision
glGetQueryObjectuiv(query[0], GL_QUERY_RESULT, &numSamplesRendered);
if (numSamplesRendered == 0) return NO_COLLISION;
// Set pixels to always write depth
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_TRUE);
// Draw front faces of object B
glCullFace(GL_BACK);
RenderObject(B);
// Pass pixels if depth is greater than current depth value
glDepthFunc(GL_GREATER);
// Disable depth buffer updates
glDepthMask(GL_FALSE);
// Render back faces of A with occlusion testing enabled
glBeginQuery(GL_SAMPLES_PASSED, query[0]);
glCullFace(GL_FRONT);
RenderObject(A);
glEndQuery(GL_SAMPLES_PASSED);
// If occlusion test indicates no pixels rendered, exit with no collision
glGetQueryObjectuiv(query[0], GL_QUERY_RESULT, &numSamplesRendered);
if (numSamplesRendered == 0) return NO_COLLISION;
// Objects A and B must be intersecting
return COLLISION;
2
Better collision results than a simple Boolean result could be obtained through
enhanced hardware occlusion query functionality. For example, in addition to the
number of samples passed, hardware returning a screen-space bounding box and
the closest and farthest z value of samples passed during the occlusion query would
provide further information about where a collision occurs and the level of object
penetration.
It is worth noting that the presented test also serves as a conservative test for concave
objects A and B . If the test reports A and B as not intersecting, they are indeed not
intersecting (within the accuracy of the rasterized approximation). However, if the
test reports the concave objects as intersecting, they may or may not actually be
intersecting in reality. Accurately dealing with concave geometry requires a different
approach, as described in the next section.
 
Search WWH ::




Custom Search