Sunday, October 25, 2009

Rendering to a depth texture doesn't work

Annoying problem: I want to capture the depth for each fragment and use it for some computation. So, I set up the FBO with a depth texture attached to the depth attachment point. Then I bind the FBO and render the scene. Now the moment of truth: draw a quad that is textured with the depth info.

The quad is completely white. Ok, maybe I'm setting up the texture wrong. Check some examples, tweak things a bit, check some of my old code, everything looks good for another try.

Now the depth texture looks like its full of random stuff. It never gets updated, but the actual scene is updating quite nicely.

What am I doing wrong? *bangs head against wall for an hour or two*

I just needed to to:

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

before rendering the depth. That's all I was missing.

1 comment:

  1. I don't think it matters what the clear color is. Either way, the depth buffer is cleared. But what if I want to clear it to zero and not infinity? Hmmm.

    ReplyDelete