PDA

View Full Version : gluProject(..) + sprites with Z values.



Arkiruthis
06-22-2008, 04:14 PM
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:

1. Setup 3D Projection. (gluLookAt(...))
2. Setup Modelview + draw 3D objects.
3. 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).
4. Setup Ortho Project.
5. 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.

satan
06-22-2008, 05:52 PM
gluProject gives me XY coordinates for the screen and a Z value which is between 0 and 1.

These are normalized coordinates. A depth value of 0 means that the vertex is on the near clipping plane and a value of 1 that it is on the far clipping plane.



Is the Z value I've got back from gluProject the actual value that would be tested on the depth buffer?

AFAIK, yes.



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?

Here I cannot follow you. Do you want to read back the depth buffer and manually test the z-values?
Because if you disable the depth testing there obviously is no test.

I don't understand what exactly you are trying to achieve and why you need gluProject/screen coordinates.
If you use the same near and far values for your perspective and your orthographic projections, you should be able to use you original z value and not the projected one.

hih

Arkiruthis
06-23-2008, 05:13 AM
Thanks for the reply.

I've got it working now. Seems I needed to make the depth negative and add a little bit to 'push' the objects in front of the buffer just a little bit.

This is possibly because I've inverted something along the way.

glVertex3d(x, y, -(z-0.005));

Anyway, it's working fine now. :)

Basically, I'm working on a re-make of Powermonger but in OpenGL (and I'd like to emulate the sprites used in the original game, but have them occluded by buildings, terrain etc.):
http://www.flyingislands.co.uk/upload/powermonger_sprites.jpg