In openGL, how can you get items to draw back to front


By default it seems that objects are drawn front to back. I am drawing a 2-D UI object and would like to create it back to front. For example I could create a white square first then create a slightly smaller black square on top of it thus creating a black pane with a white border. This post had some discussion on it and described this order as the "Painter's Algorithm" but ultimately the example they gave simply rendered the objects in reverse order to get the desired effect. I figure back to front (first objects go in back, subsequent objects get draw on top) rendering can be achieved via some transformation (gOrtho?) ?

I will also mention that I am not interested in a solution using a wrapper library such as GLUT.

I have also found that the default behavior on the Mac using the Cocoa NSOpenGLView appears to draw back to front, where as in windows I cannot get this behavior. The setup code in windows I am using is this:

glViewport (0, 0, wd, ht);
glOrtho (0.0f, wd, ht, 0.0f, -1.0f, 1.0f);

Best Solution

The following call will turn off depth testing causing objects to be drawn in the order created. This will in effect cause objects to draw back to front.

glDepthFunc(GL_NEVER);      // Ignore depth values (Z) to cause drawing bottom to top

Be sure you do not call this:

glEnable (GL_DEPTH_TEST);   // Enables Depth Testing
Related Question