Code: Select all
qboolean GL_Occlusion(GLfloat width, GLfloat height)
{
float w = 640.0f / (float)width;
float h = 480.0f / (float)height;
float cornerFactor = 2.0f;
double corner1 = realtime*2;
double corner2 = realtime*3;
double corner3 = realtime*4;
double corner4 = realtime*5;
qboolean Occluded;
GLuint occQuery;
GLuint occSamples = 0;
GLuint occAvailable = 0;
if(GL_ExtensionBits & HAS_OCCLUSION)
{
// start up queries for occlusion.
glGenQueries(1, &occQuery);
// take down color and depthmask, we do not want to draw anything.
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
// Load queries for Occlusion.
glBeginQuery(GL_SAMPLES_PASSED, occQuery);
// do a full scree quad to get the data it needs,
// do not render it, hence taking down color and depthmask above.
glBegin(GL_QUADS);
glVertex2f(-(w * 0.5f) + (sinf(corner1) * cornerFactor), -(h * 0.5f) + (cosf(corner1) * cornerFactor));
glVertex2f(-(w * 0.5f) + (sinf(corner2) * cornerFactor), (h * 0.5f) + (cosf(corner2) * cornerFactor));
glVertex2f((w * 0.5f) + (sinf(corner3) * cornerFactor), (h * 0.5f) + (cosf(corner3) * cornerFactor));
glVertex2f((w * 0.5f) + (sinf(corner4) * cornerFactor), -(h * 0.5f) + (cosf(corner4) * cornerFactor));
glEnd();
// Occlusion test done
glEndQuery(GL_SAMPLES_PASSED);
// flush queries
glFlush();
do
{
// Run queries until pipeline get's availiable.
glGetQueryObjectiv(occQuery, GL_QUERY_RESULT_AVAILABLE, &occAvailable);
} while(!occAvailable);
// go back to normal rendering.
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// fresh in from above,
// and tested in a way that does not stall the pipeline.
if (occAvailable > 0)
{
// Test again and output samples that passed.
glGetQueryObjectiv(occQuery, GL_QUERY_RESULT, &occSamples);
// get occlusion state (false = visible - true = occluded).
Occluded = (occSamples > 0) ? false : true;
}
}
else
{
// if we have an ancient card let thing's pass.
Occluded = false;
}
return Occluded;
}
It does work and is even resonably fast, but it does tend to get a little to effective on smaller screen space objects.
The word is that it is as it is, so to get to some of the benefits you need to use it in huge complex scenes.