Alex Rivera | Logout

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

Asked 2008-10-09T00:57:58.393
15

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);
glMatrixMode(GL_PROJECTION);           
glLoadIdentity();                      
glOrtho (0.0f, wd, ht, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);             
glLoadIdentity();                       
Edit
Report

2 Answers

27

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
answered 2008-10-10T02:28:34.640
1

Your stuff will be drawn in the exact order you call the glBegin/glEnd functions in. You can get depth-buffering using the z-buffer, and if your 2d objects have different z values, you can get the effect you want that way. The only way you are seeing the behavior you describe on the Mac is if the program is drawing stuff in back-to-front order manually or using the z-buffer to accomplish this. OpenGL otherwise does not have any functionality automatically as you describe.

answered 2008-10-10T02:40:12.963

Your Answer