tags:

views:

64

answers:

3

How exactly can I do a Z buffer prepass with openGL.

I'v tried this:

glcolormask(0,0,0,0); //disable color buffer

//draw scene

glcolormask(1,1,1,1); //reenable color buffer

//draw scene

//flip buffers

But it doesn't work. after doing this I do not see anything. What is the better way to do this?

Thanks

A: 

If i get you right, you are trying to disable the depth-test performed by OpenGL to determine culling. You are using color functions here, which does not make sense to me. I think you are trying to do the following:

glDisable(GL_DEPTH_TEST); // disable z-buffer

// draw scene

glEnable(GL_DEPTH_TEST); // enable z-buffer

// draw scene

// flip buffers

Do not forget to clear the depth buffer at the beginning of each pass.

elusive
@elusive well basically I'm trying to do a Z prepass to try to avoid drawing certain things.
Milo
Actually that's exactly the opposite of what he's asking. A Z prepass is used to *only* test depth without computing any lighting. The idea is that you can reject a lot of occluded objects early using a cheap depth test and save the expensive lighting calculations for only geometry that will be seen.
Bob Somers
So how do I do that then :p
Milo
Well, I've never written a Z prepass myself, so I'll defer to someone who has. But I would imagine it would be a lot easier to do in the shader pipeline where you have more control over what exactly is rendered where.
Bob Somers
Sorry, i misunderstood that.
elusive
+1  A: 

You're doing the right thing with glColorMask.

However, if you're not seeing anything, it's likely because you're using the wrong depth test function. You need GL_LEQUAL, not GL_LESS (which happens to be the default).

glDepthFunc(GL_LEQUAL);
Bahbar
+3  A: 
// clear everything
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// z-prepass
glEnable(GL_DEPTH_TEST);  // We want depth test !
glDepthFunc(GL_LESS);     // We want to get the nearest pixels
glcolormask(0,0,0,0);     // Disable color, it's useless, we only want depth.
glDepthMask(GL_TRUE);     // Ask z writing

draw()

// real render
glEnable(GL_DEPTH_TEST);  // We still want depth test
glDepthFunc(GL_LEQUAL);   // EQUAL should work, too. (Only draw pixels if they are the closest ones)
glcolormask(1,1,1,1);     // We want color this time
glDepthMask(GL_FALSE);    // Writing the z component is useless now, we already have it

draw();
Calvin1602