3
votes

I am using JOGL, but this question applies to OpenGL in general. There seem to be similar questions lying around, but they are either directed at GLSL code, have to do with copying the contents of a frame buffer, or are a general advice - use frame buffer objects instead of glCopyTexSubImage2D.

Question

I am doing some shadow mapping. How do I render the depth channel directly to a texture using a frame buffer object?

Can you please post a block of code that initializes the texture and the frame buffer object, as well as the code that initializes everything before rendering the scene?

Currently, I use glCopyTexSubImage2D. I initialize the texture like this:

glGenTextures(1, &textureNo)
glBindTexture(GL_TEXTURE_2D, textureNo)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL)
glTexParameterf(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY)
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 2048, 2048, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, null)

and then I render like this:

glBindTexture(GL_TEXTURE_2D, textureNo)
glClear(GL_DEPTH_BUFFER_BIT)
drawScene()
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 2048, 2048)

and this works fine, the texture ends up holding the depth channel of the scene - this means my projection and view matrices are set correctly to view the scene from the light point of view. I'd like to use frame buffer objects instead.

What do I need to change above to render directly to the texture instead using a frame buffer object?

EDIT:

Here is what I tried. I first initialized the frame buffer object:

glGenFramebuffers(1, &fbo)

I then rendered like this:

glBindFramebuffer(GL_FRAMEBUFFER, fbo)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureNo, 0)
glBindTexture(GL_TEXTURE_2D, textureNo)
drawScene()

I have confirmed that the above works for textures of type GL_RGB and GL_COLOR_ATTACHMENT0, but it does not seem to work for GL_DEPTH_COMPONENT type textures and GL_DEPTH_ATTACHMENT - the depth channel does not get rendered into the texture textureNo above.

1
Have you tried something that isn't working, or are you just looking for someone to write your code for you so that you can copy-and-paste it into your application? FBOs are not really that hard to understand.Nicol Bolas
Actually, I have written something that isn't working. I was initializing a frame buffer object, then attaching the texture to it. However, this did not render the depth channel of the scene to the texture. Interestingly, when I used a frame buffer object to render the scene to a regular GL_RGB texture, it works fine. I was not able anything in the docs about how using frame buffer objects to render the depth channel differs from rendering the RGB channel.axel22
I am writing the code in a thin wrapper over JOGL for Scala (github.com/axel22/scalagl), which was designed to offer some oo-style resource management and more structured coding, so I had to adapt my code a little bit - I've edited the question to show what I have done.axel22

1 Answers

4
votes

Assuming that this is all of the FBO setup code, I don't see the part where you call glCheckFramebufferStatus. You know, the function that would have told you that your Framebuffer Object was incomplete, and thus rendering to it would fail.

Specifically, it would have given you GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER​, because you didn't set a valid draw buffer. If you don't use a color attachment, you need to use glDrawBuffer to set all of the color buffer writes to GL_NONE.

It's a common FBO gotcha; you won't be the first to not have seen it. But that's why it's vital to always check glCheckFramebufferStatus after you modify your FBO.