z-buffer algorithm

Hey all,

I am pretty new to openGL and graphics in general, and I was wondering if anyone can help give me some insight into the z-buffer algorithm (painter’s algorithm). I’ve looked online and the algorithm itself makes sense, with objects being painted back-to-front.

What I do not understand is how to assign a z-value to each polygon and then to display it pixel by pixel. I have a class that holds all the polygons of an object (with normals, vertices etc), but I am unsure as to how to display pixels, or on how to assign these generic values to the polygons.

If anyone can give me a little guidance, it would go a long way. And of course, if you need more information on my issue, please let me know.

Thanks!

Edited: Grammar (yikes)

Well, here’s the first insight: z-buffering is not the painter’s algorithm. Painter’s is just ordering the triangles from back to front. Z-buffering is about giving each pixel a z value and only rendering a pixel if the z value of the incoming fragment is closer than the one in the pixel. If you use z-buffering, you don’t have to order anything to achieve correct results.

Are you trying to do depth buffering manually? Because otherwise, the depth is just computed from the interpolated positions of the vertices.

Ah, I must have been confused, I always saw the Z-buffer algorithm and painters in the same setting.

The only thing I am trying to do is implement the z-buffer algorithm and to color each polygon differently. I am trying to have to different objects interpenetrate so that it can show that my algorithm is working. Since I do not need to order anything, should I just store every polygon in a list, give each pixel a z-value (kinda unsure as to how to give these values), and then color each polygon depending on the value given?

I’m guessing z-value has to do with the depth of the object, but I am confused as to how to find this value.

Thanks again!

Edit: I am not trying to do depth buffering manually, do I just take the (x,y,z) position of each of the vertices, store that information and assign a depth based on location?

If you’re polygon vertices are defined in 3-space, i.e. (x,y,z) coordinates, then z-buffering is automatically done for you by OpenGL. All you have to do is enable it.