简体   繁体   中英

OpenGL - Occlusion query depth buffer?

I've just started getting into the topic of occlusion queries in OpenGL, but I'm a bit confused about how they actually work.

In most examples I've found, the depth and color masks are deactivated before drawing with the occlusion query (Because we don't need to actually 'draw' anything), in essence somewhat like this:

glDepthMask(GL_FALSE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

glBeginQuery(GL_ANY_SAMPLES_PASSED,query1);
    // Draw Object 1
glEndQuery(GL_ANY_SAMPLES_PASSED);

glBeginQuery(GL_ANY_SAMPLES_PASSED,query2);
    // Draw Object 2
glEndQuery(GL_ANY_SAMPLES_PASSED);
// etc

glDepthMask(GL_TRUE);
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);

(It's assumed that objects are drawn front to back, so object 1 is in front of object 2. The above code is just pseudo code for the sake of this question. The result of the queries would be retrieved at a later time.)

Now, to know if object 2 is actually occluded by object 1, it would need to keep the fragment information from query 1 somehow (I'm assuming in some sort of depth buffer). But we've disabled drawing to the depth and color buffers, which means nothing is drawn, which means it shouldn't store anything anywhere?

Is there a special 'query' buffer? If so, is there a way to access it? Is it in any way connected to the currently bound texture or frame buffer? Do I need to clear it? Am I misunderstanding how occlusion queries actually work?

Now, to know if object 2 is actually occluded by object 1, it would need to keep the fragment information from query 1 somehow

Why would it? Occlusion queries store a counter of the number of samples that pass the depth test, which is a single integer.

Since you've disabled writing to the color and depth buffer, the only thing drawing the objects will do is increment the occlusion query counter*. Object 2 can't possibly occlude object 1 because drawing object 1 doesn't change the depth buffer.

* Unless you have a stencil buffer or are doing something like image load/store in shaders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM