views:

553

answers:

3

I'm trying to implement an old-school technique where a rendered background image AND preset depth information is used to occlude other objects in the scene.

So for instance if you have a picture of a room with some wires hanging from the ceiling in the foreground, these are given a shallow depth value in the depthmap, and when rendered correctly, allows the character to walk "behind" the wires but in front of other objects in the room.

So far I've tried creating a depth texture using:

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, Image.GetWidth(), Image.GetHeight(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, pixels);

Then just binding it to a quad and rendering that over the screen, but it doesnt write the depth values from the texture.

I've also tried:

glDrawPixels(Image.GetWidth(), Image.GetHeight(), GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, pixels);

But this slows down my framerate to about 0.25 fps...

I know that you can do this in a pixelshader by setting the gl_fragDepth to a value from the texture, but I wanted to know if I could achieve this with non-pixelshader enabled hardware?

A: 

Disable writing to the color buffer (glColorMask(GL_FALSE, GL_FLASE, GL_FALSE, GL_FALSE), then render polygons representing your "in-front" image with an appropriate Z-coordinate.

genpfault
A: 

After trying to get a depth texture to work, I then tried the ARB_fragment_program extension to see if I could write a very simple (and therefore widely compatible with old hardware) GPU assembly fragment shader to do the trick, but I eventually just decided to use GLSL as the ARB_fragment_program extensions seem to be deprecated or at least their usage is frowned upon nowadays.

Mikepote
A: 

You can try using frame buffer objects to do what you want.

Create fbo1 and attach a color and depth buffer. Render or set the scene for the background.

Create fbo2 and attach a color and depth buffer.

Use frame buffer blit at each update to blit the color and depth buffers from fbo1 to fbo2.

Render the rest into fbo2

Do a full screen blit of fbo2 color buffer to the main display buffer.

Jose