Using depth map for occlusion

Hi,

I’m trying to create a program that displays a photo on the screen with 3d objects overlayed on top of it. I want the 3d objects to be occluded by certain objects in the actual photo.

I got this working by modeling the objects in the photo and drawing them with the color buffer disabled. Here’s the basic outline of my program right now:

  • Draw photo on the screen
  • Disable color buffer
  • Draw static objects in photo
  • Enable color buffer
  • Draw dynamic objects which can be occluded by photo

It works fine now, but instead of modelling each object in the photo I would like to create a greyscale version of the photo that represents the depth at each pixel.

I tried doing this by copying the depth image to the depth buffer using glDrawPixels. However, glDrawPixels is too slow.

Does anybody know of another way to do this? I was thinking of drawing the depth image to the screen and using a vertex program to convert the texture value to the depth value, but from my understanding, you can’t perform texture fetches inside a vertex program.

Any comment is greatly appreciated.

-Farshid

I tried doing this by copying the depth image to the depth buffer using glDrawPixels. However, glDrawPixels is too slow.

Use glTexSubImage2D and a fullscreen quad instead.

I was thinking of drawing the depth image to the screen and using a vertex program to convert the texture value to the depth value, but from my understanding, you can’t perform texture fetches inside a vertex program.

You can do this with GeForce 6 series of hardware currently.

-SirKnight

Hi :wink:
If you own 3D Studio MAX 6.0, you can render a scene as a ZBuffer depth bitmap.

Might be helpfull somehow :stuck_out_tongue:

Hi,

Thanks for the advice. Unfortunately, I don’t have a GeForce 6.

Also, I might have been a little unclear. I already have the depth map image. My problem is copying the data from the depth map texture to the zbuffer.

I looked at the glTexSubImage2D command but it seems as though this is for copying data into a texture. Did I misinterpret the use of this command?

Again, thanks for the help

-Farshid

The parameter in glTexSubImage2D called “format” which is the 7th parameter has the same meaning as the “format” parameter in glDrawPixels which is the 5th parameter. So you can pass into glTexSubImage2D the constant GL_DEPTH_COMPONENT just like you did with glDrawPixels to write the height data into the depth buffer.

–EDIT–

BTW, Make sure when you do this operation to disable color writes, glColorMask( 0, 0, 0, 0 ), then after the depth buffer has been filled turn color writes back on, glColorMask( 1, 1, 1, 1 ).

-SirKnight

Thanks for the tip SirKnight. The documentation I was looking at must be outdated. It said the following about the glTexSubImage2D command:

“In general, texture images can be represented by the same data formats as the pixels in a glDrawPixels command, except that GL_STENCIL_INDEX and GL_DEPTH_COMPONENT cannot be used.”

I’ve also considered using a fragment program and writing the texture value to result.depth. However I can’t find any documentation on this. Is result.depth a linear mapping from [0,1]?

  • Farshid