I'm trying (and trying and trying) to use occlusion query with a terrain engine. For the sake of simplicity, i suppose the terrain is a 64x64 regular grid, and i have a simple quad that i use as an occluder: terrain quads behind the occluder quad, must not be rendered.
The problem is that when i move the occluder quad around, all the terrain quads are rendered. Even if the occluder quad covers all the screen, fps tell that the terrain is there ...
I'm showing the init() and render() routines, which are quite simple, i can't figure out why it doesn't work. Thanks for any help, i've been trying all the weekend to make it work and i didn't succeed !
Oh, BTW, occlusion query is supported from my vga card.
init()
{
#define MAX_QUERIES 32*32
GLuint QO_terrain[MAX_QUERIES];
glGenQueriesARB( MAX_QUERIES, QO_terrain );
}
render()
{
int MAP_SIZE = 32;
Draw_Occluder_Quad();
// run occlusion query
int qindex=0;
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
for (int i=-MAP_SIZE/2; i<MAP_SIZE/2; i++)
{
for (int j=-MAP_SIZE/2; j<MAP_SIZE/2; j++)
{
glBeginQueryARB( GL_SAMPLES_PASSED_ARB, QO_terrain[qindex]);
glBegin(GL_QUADS);
glVertex3f(j+0,i+0,0);
glVertex3f(j+1,i+0,0);
glVertex3f(j+1,i+1,0);
glVertex3f(j+0,i+1,0);
glEnd();
glEndQueryARB( GL_SAMPLES_PASSED_ARB );
qindex++;
}
}
// render visible terrain
qindex=0;
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
for (int i=-MAP_SIZE/2; i<MAP_SIZE/2; i++)
{
for (int j=-MAP_SIZE/2; j<MAP_SIZE/2; j++)
{
glGetQueryObjectuivARB( QO_terrain[qindex], GL_QUERY_RESULT_ARB, &terrainFragmentCount); // get occlusion query results
if (terrainFragmentCount>0) // if fragments are visible, render the terrain patch
{
glBegin(GL_QUADS);
glVertex3f(j+0,i+0,0);
glVertex3f(j+1,i+0,0);
glVertex3f(j+1,i+1,0);
glVertex3f(j+0,i+1,0);
glEnd();
}
qindex++;
}
}
}