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?
Instead of disabling the depth test you can try to simply clear the depth buffer.
[ www.trenki.net | vector_math (3d math library) | software renderer ]
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.
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
Wonderful, clearing the depth buffer works like a charm.
Thanks a lot to you all!