Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre Z buffer pass with OpenGL?

Tags:

c++

c

opengl

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

like image 335
jmasterx Avatar asked Sep 13 '10 17:09

jmasterx


2 Answers

// 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();
like image 96
Calvin1602 Avatar answered Nov 19 '22 13:11

Calvin1602


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);
like image 37
Bahbar Avatar answered Nov 19 '22 13:11

Bahbar