Rendering sprites with depth information

Hi all!

I’m looking for advice on how to acheive a particular rendering technique with OpenGL. This is best illustrated by the first The Sims game.

In The Sims, the people themselves were rendered as polygon models whereas all the furniture used pre-rendered sprites. However, the 3d models still intersected the sprites as if they had volume in 3d space. Here the sims pop through the water of the hot tub even though it is a 2d sprite:

[ATTACH=CONFIG]672[/ATTACH]

And here the fire (which is a textured cylinder) intersects the 2d sprite of the cooker:

[ATTACH=CONFIG]673[/ATTACH]

Each furniture sprite in The Sims had an accompanying image containing the depth of each pixel; something like this:

[ATTACH=CONFIG]671[/ATTACH]

I’d like to know whether it’s possible to this with OpenGL, perhaps by drawing to the depth buffer somehow. I’m having trouble searching for information on this technique, mainly because I don’t even know what it’s called! I suppose it’s a kind of voxel rendering, but this term has many different meanings in different contexts.

Thanks in advance!

I’d say this is definitely possible when you write your own shader.
You can bind 2 textures, one with the color information, and one with the depth information.
In your vertex shader you take the X- and Y-coordinates from the client, and the Z-coordinate from the depth texture.
In your fragment shader you take RGBA from the color texture.

Yes you can directly write your depth value to gl_FragDepth in the fragment shader.
But that will disable HyperZ/Hierarchical Z-Buffering. That should not be a performance problem if you create a simple 2d game.

There also is ARB_conservative_depth. With this you can tell the driver that you only will increase/decrease the current depth value and may still get the HyperZ/Hierarchical Z-Buffering.
More about it in the wiki: Fragment Shader - OpenGL Wiki