PDA

View Full Version : z-axis ordering



elimin8tor
02-12-2002, 02:45 AM
Hi I have two squares mapped along the y axis which basically are supposed to be mapped above each other. The 1 one has a land texture mapped to it and the second one a house on and an alpha value where you are supposed to look through and see the land.
Now how do I order them, because I only see the quad drawn first. I came up with translating the second one a very little bit in front of the first one, but I wondered if there might be a better solution.

nexusone
02-12-2002, 06:14 AM
I had the same type problem the objects in back being drawn on top of the front objects.
Try reversing the order in which you draw your objects.


Originally posted by elimin8tor:
Hi I have two squares mapped along the y axis which basically are supposed to be mapped above each other. The 1 one has a land texture mapped to it and the second one a house on and an alpha value where you are supposed to look through and see the land.
Now how do I order them, because I only see the quad drawn first. I came up with translating the second one a very little bit in front of the first one, but I wondered if there might be a better solution.

marcus256
02-12-2002, 06:40 AM
I'm not sure if I understand your problem correctly, but I think you are suffering from two problems (of which only one is visible):

1) Transparent objects should always be drawn last. If you have several (semi)transparent objects that need to be depth sorted, you need to use alpha-testing (enable depth buffering, but don't draw parts where alpha=0).

2) "Z-fighting"! (search for the term in the forums). If you draw two objects which lie in (more or less) the same plane and have depth testing enabled, the rounding errors in the z-buffer will make it more or less random which object is actually drawn, due to limited z-buffer resolution. This can even appear as "stripes" and flickering when the planes are tilted compared to the viewing plane. See this link: http://www.opengl.org/developers/code/features/StencilTalk/sld045.htm

I suppose that number 2 is the reason that you see an improvement when you render the second (transparent?) object with a small offset. There is actually a special function in OpenGL that deals with this problem (used e.g. in games for classic blood-stains-on-the-wall effects), and it is called Polygon Offset. For a description of the function, go to http://hem.passagen.se/opengl/glfw/glman.html and click on glPolygonOffset.

What the function does is to (for each vertex) calculate an offset which is proportional to the z-buffer resolution at that point (the effective z-buffer resolution varies with the distance: close points have better z-resolution, distant points have worse z-resolution), otherwise it is very similar to your method.