View Full Version : Always on top (in front) sphere

05-08-2008, 05:45 AM
Hi All,

Suppose to have a scene of multiple 3D objects. I would like to draw a sphere staying always on top (in front) of the others while rotating the scene.

I thought about drawing first all the other 3D objects then disabling the DEPTH_TEST and drawing the sphere but the result is: an always on top sphere, but with its triangles messed up as well.

What is the best approach for this problem?



05-08-2008, 06:25 AM
Instead of disabling the depth test you can try to simply clear the depth buffer.

[ www.trenki.net (http://www.trenki.net) | vector_math (3d math library) (http://www.trenki.net/content/view/16/36/) | software renderer (http://www.trenki.net/content/view/18/38/) ]

05-08-2008, 06:37 AM
Because a sphere is a convex object you can also simply switch off depth test and enable back face culling while rendering the sphere.
If it is transparent, render it twice, first with front face culling, then with back face culling.

05-08-2008, 06:42 AM
OpenGL renders triangles in the order your applications specifies them. The depth test prevents fragments (aka pixels) to be drawn, if there is already a pixel at the given position which is - according to it's depth - supposed to be in front.

If you disable depth testing, your triangles will be draw in the order you specify them. Because that order will most likely not be sorted according to the depth for your viewpoint, frontside and backside of the sphere will be mixed up.

Two possible solutions:
As long as you only want to draw convex objects on top of your scene, enabling backface culling is sufficient (that's in most cases a good idea anyway).

If you want to draw concave objects (or even intersecting triangles), you need some sort of depth sorting. Instead of disabling the depth test, just clear the depth buffer before you draw your in-front-objects.

Edit: too late ;)

05-08-2008, 06:53 AM
Wonderful, clearing the depth buffer works like a charm.

Thanks a lot to you all!