Usage of ZBuffer instead of stencil buffer.

Hey guys,

I’m writing this 3D engine in opengl, and decided I wanted to use stencil buffering. But sb doesn’t use hardware acceleration.
I figured I could just as well use the zbuffers for these, but I dunno how I can just fill the ZBuffer, say, set it to 0.0f, and filling the polygon position to 1.0f.

Any suggestions ?

Depending on your graphics board (NVIDIA based, for example) you might want to switch to 32 bits color. Then you can get pixelformats with 24 bits depth and 8 bits stencil. 16 bits color depth offer 16 bits Z and no stencil.
And the stencil buffer can do many more tricks than the z-buffer.

Nevertheless, the commands you need to know are glEnable(GL_DEPTH_TEST), glDepthRange(), glClearDepth(), glClear(GL_DEPTH_BUFFER_BIT), glColorMask() for not writing into the color buffer, glDepthMask() for enabling or disabling depth writes, maybe glScissor() and some good basics on projection and modelview matrices.

The default is that OpenGL maps the zNear and zFar values in glOrtho, glFrustum, or gluPerspective to window-z coordinates from 0.0 to 1.0 respectively.
With this it should be no problem to position visible or invisible polygons in the scene which lie at the desired depth.

Beware of rounding effects due to different numbers of depth buffer bits.

Ditto, glDepthFunc() too, and in case you can’t figure it out, you will need to use glDepthRange(…) and set the near and far depth ranges to the exact same value. This is the value that will then get written to the Z-buffer irregardless of any pixel’s actual depth. So be sure you also have your primitives depth sorted before you draw them.

Thanx, I got it to work !