gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > No Z-sorting with glOrtho

#149897 - simonjhall - Sat Jan 26, 2008 4:31 pm

Sup.
I seems to remember having this problem ages ago, but I don't think I ever found a solution to it.

I'm drawing lots of quads in 3D to form my HUD (and this is done with glOrtho) and some quads overlap others. The HUD is effectively drawn back-to-front, so anything drawn first should be drawn over anything drawn later.
Regardless of the Z that I use I always get the same result: no kind of depth sorting and quads that overlap appear as if the top part of the quad is drawn at a different Z to the bottom of the quad. I've checked again and again to see if I'm doing anything silly and I'm not. I then thought it could be a precision problem, so the first quad is drawn at 1.0, the next at 2.0 etc etc and I still get the exact same resut! Messing around with znear/zfar does nothing interesting.

For my swap buffers command I'm using '0' which apparenty means Z sorting.

Has anyone else had issues like this? What're the solutions?

Btw, here's the code for the orthographic projection matrix: (from libnds)
Code:
GL_STATIC_INL void glOrthof32(int32 left, int32 right, int32 bottom, int32 top, int32 zNear, int32 zFar) {
   MATRIX_MULT4x4 = divf32(inttof32(2), right - left);     
   MATRIX_MULT4x4 = 0; 
   MATRIX_MULT4x4 = 0;     
   MATRIX_MULT4x4 = 0;

   MATRIX_MULT4x4 = 0; 
   MATRIX_MULT4x4 = divf32(inttof32(2), top - bottom);     
   MATRIX_MULT4x4 = 0;   
   MATRIX_MULT4x4 = 0;

   MATRIX_MULT4x4 = 0; 
   MATRIX_MULT4x4 = 0; 
   MATRIX_MULT4x4 = divf32(inttof32(-2), zFar - zNear);     
   MATRIX_MULT4x4 = 0;

   MATRIX_MULT4x4 = -divf32(right + left, right - left);//0; 
   MATRIX_MULT4x4 = -divf32(top + bottom, top - bottom); //0; 
   MATRIX_MULT4x4 = -divf32(zFar + zNear, zFar - zNear);//0; 
   MATRIX_MULT4x4 = floattof32(1.0F);
}

_________________
Big thanks to everyone who donated for Quake2

#149915 - ingramb - Sun Jan 27, 2008 7:40 am

I use 2d quads for my entire application, and it works fine. You need to assign each overlapping quad a different z-value or else you'll get z fighting. I draw everything back to front, and just increment the z-value as I go. I use 0 for GFX_FLUSH, and the glOrthof32 from libnds. Hopefully this helps.

#149921 - M3d10n - Sun Jan 27, 2008 12:19 pm

Use plain integers for all of glOrthof32 parameters, not floattof32'ed or inttof32'ed numbers, same for your vertex coordinates. You should be able to be able to properly depth sort your quads that way.

I'm using:
Code:
glOrthof32(0, 256, 192, 0, 0, 10);

Which allows me to use 10 unique z values.
(That's with the libnds version which comes with devkitARM r20, I don't know if it changed with the one from r21)