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.
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 theRGB
channel. – axel22