OpenGL quad layering

So. if i’m rendering textured quads, and want to have them layered, what would be the best way to do this? currently I just render them in the order they’re to be layered, but this seems like a bad way to do it.
basically right now it’s just

GL11.glBegin(GL_QUADS);
//4 corner coords go here
GL11.glEnd();

4 times in the order I want to layer them. Should I be using 3D coords and using the Z coordinate, or does this not work?
like:

GL11.glVertext3f(0, 0, 1);
GL11.glVertex3f(1,0,1);
GL11.glVertext3f(0, 1, 1);
GL11.glVertex3f(1,1,1);

then change to:

GL11.glVertext3f(0, 0, 2);
GL11.glVertex3f(1,0,2);
GL11.glVertext3f(0, 1, 2);
GL11.glVertex3f(1,1,2);

for a closer/further layer?

Using the z-buffer for hardware depth-testing isn’t always a good idea if you’re working with 2D. It doesn’t work so well with semi-transparent sprites, and the performance difference will most likely be negligible. A better and easier alternative is just to order your sprites from back to front, and use blending:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

One change you should consider is to “batch” your vertices. You can specify as many vertices as you want within glBegin and glEnd. So, instead of using glBegin/glEnd for each sprite, you would use them once, and place your sprites vertices within glBegin/glEnd. Note: you can’t adjust the transform or view matrices within glBegin/glEnd, or change blend func, or what have you. Just set vertices and their colors/texcoords/etc.

If you really want to know the “best” way to do things with OpenGL, then you should drop immediate mode altogether. It’s old and deprecated, and also really slow. Instead, you should look into batching your sprites with VBOs or vertex arrays. You can also make use of GLSL and other aspects of “modern GL” rather than relying on fixed-function.

My suggestion would be to use a library like LibGDX or lwjgl-basics to cover the boilerplate for you.

Some reading suggestions:


http://arcsynthesis.org/gltut/ [url=https://bitbucket.org/ra4king/lwjgl-shader-tutorials]and the Java ports[url]
http://open.gl/
http://www.opengl-tutorial.org/
http://tomdalling.com/blog/category/modern-opengl/

Oh, and don’t forget to disable depth-testing.