Graphics Reference
In-Depth Information
Z near
Z far
Z near
Z far
Z near
Z far
B
B
B
A
A
A
(a)
(b)
(c)
Figure 10.3 Occlusion queries can be used to determine if (convex) objects A and B are
intersecting.
Table 10.1 Implementation of a collision test for two convex objects using OpenGL occlusion queries.
Pass
Code
// Initialize depth buffer to far Z (1.0)
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
// Disable color buffer writes
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Initialize occlusion queries
Gluint query[1], numSamplesRendered;
glGenQueries(1, query);
0
// Set pixels to always write depth
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_TRUE);
// Draw front faces of object A
glCullFace(GL_BACK);
RenderObject(A);
// Pass pixels if depth is greater than current depth value
glDepthFunc(GL_GREATER);
// Disable depth buffer updates
glDepthMask(GL_FALSE);
// Render back faces of B with occlusion testing enabled
glBeginQuery(GL_SAMPLES_PASSED, query[0]);
glCullFace(GL_FRONT);
RenderObject(B);
glEndQuery(GL_SAMPLES_PASSED);
1
(continued)
Search WWH ::




Custom Search