Search code examples
c#opengldepth-testing

Understanding Depth_Test opengl


I am newbie to opengl,and i have difficulties to learn depth test in opengl..Can someone explain me what is DEPTH_TEST?I am reading my literature where it says:

"It enables hiding objects which are covered by some other object(hiding by depth).If it isn't enabled,the object that is earlier painted,is covered."

I don't understand first sentence at all.What for example,if I had to put some text in front of some quad,which has greater area than that text,how could i do that?If DEPTH_TEST is not enabled,i would just draw first that quad,and then text,and then text would be in front of that quad..But how when DEPTH_TEST is enabled?i don't know what is there covered,i don't know if order of showing matters...Is that maybe only about 3d objects which have back sides,and that those backsides can be covered?When should I use DEPTH_TEST anyway?
Thanks in advance!:)


Solution

  • I don't know what literature you are using, but

    It enables hiding objects which are covered by some other object(hiding by depth).If it isn't enabled,the object that is earlier painted,is covered.

    is a very poor explanation. The depth test is not about objects at all. It is about deciding the visibility separately for each screen pixel separately.

    The basic premise is: if you deal with opaque objects in a 3D space, the surfaces closer to the camera will occlude the ones which are farther away. Since we do transform the objects into some coordinate frame relative to the camera, we typically interpret the resulting Z value as the depth value. (X and Y are horizontal and vertical axis, and Z points "into your monitor").

    The depth test works by storing a depth value for each screen pixel (in the so-called depth buffer or Z buffer), and before drawing to a new pixel, first checking if it's associated Z value is lower than the one already in the buffer. If not, the fragment is discarded without being written to the frame buffer. If yes, the color and the depth buffer will be updated with that fragment's data (overwriting what was drawn before - at this particular pixel). The algorithm is usually known as Z-Buffering.

    Is that maybe only about 3d objects which have back sides,and that those backsides can be covered?

    No, backfaces have not much to do with this. (Although the depth test will also deal with cases where back faces are drawn before the front faces, but enabling backface culling is even better for such scenarios).

    In the GL, every fragment will have a Z value. If you work with 2D things like text, you just work in some z=const plane. But you can chose any plane you like.

    When should I use DEPTH_TEST anyway?

    Whenever you want to draw primitives where you can't or don't want to sort all primitives in your scene in back-to-front order, i.e a 3D world where camera and/or objects are moving. Also, the DEPTH_TEST can be used to improve performance (fragments which lie behind already drawn surfaces don't need to be shaded).

    Also note that you can switch the test on an off at any time, so you can draw some 3D scene with Z-Buffering, and disable it to draw some text overlay without having to care about its Z coordinates.