I’m writing an application where I need to draw 2D sprites (textured quads) on a 3D scene such that each sprite has a depth buffer value so that 2D sprites will be occluded by 3D objects in front of them.
My method is:
- Setup 3D Projection. (gluLookAt(…))
- Setup Modelview + draw 3D objects.
- Run gluProject(…) to get the screen coordinates for where the sprites should be. (i.e., take 3D world coords, get X & Y for screen coords and a Z value for the buffer).
- Setup Ortho Project.
- Draw Sprites.
However, I’m having problems setting the correct depth for the 2D drawing section.
gluProject gives me XY coordinates for the screen and a Z value which is between 0 and 1.
I thought it would be as simple as calling:
glVertex3d(x, y, z);
in the Ortho section and have Depth Testing do the hard work.
Is the Z value I’ve got back from gluProject the actual value that would be tested on the depth buffer?
Could I disable depth testing and just test this Z value on the existing depth buffer to see whether I should draw the sprite or not?
I’m having a hard time describing this (and I’m sure reading it is no fun either) but any help would be gratefully received.