Render the depth buffer into a texture using a frame buffer

江枫思渺然 提交于 2019-12-22 13:01:03

问题


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:


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.



来源:https://stackoverflow.com/questions/12546368/render-the-depth-buffer-into-a-texture-using-a-frame-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!