How to write to the OpenGL Depth Buffer
- by Mikepote
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?