OpenGL FrameBuffer Objects weird behavior

My algorithm is this:

Render the scene to a FBO with shadow mapping from multiple locations Render the scene to the screen with shadow mapping ...black magic that I still have to imlement... Combine the samples from step 1 with the image from step 2

I'm trying to debug steps 1 and 2 and am coming across STRANGE behavior. My algorithm for each shadow mapped pass is: render the scene to a FBO connected to a depth array texture from the POV of each light render the scene from the viewpoint and use vertex/frag shaders to compare the depths

When I run my algorithm this way:

render from point to FBO render from point to screen glutSwapBuffers()

The normal vectors in the screen pass appear to be incorrect (inverted possibly). I'm pretty sure that's the issue because my diffuse lighting calculation is incorrect, but the material colors are correct, and the shadows appear in the correct places. So, it seems like the only thing that could be the culprit is the normals.

However if I do

render from point to FBO render from point to Screen glutSwapBuffers() //wrong here render from point to Screen glutSwapBuffers()

the second pass is correct. I assume there's a problem with my framebuffer calls. Can anyone see what the problem is from the log below? Its from a bugle trace grepped for 'buffer' with a few edits to make it a little more clear.

[INFO] trace.call: glGenFramebuffersEXT(1, 0xdfeb90 -> { 1 })
[INFO] trace.call: glGenFramebuffersEXT(1, 0xdfebac -> { 2 })
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 1)
[INFO] trace.call: glDrawBuffer(GL_NONE)
[INFO] trace.call: glReadBuffer(GL_NONE)
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 0)
//start render to FBO
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 2)
[INFO] trace.call: glReadBuffer(GL_NONE)
[INFO] trace.call: glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 2, 0)
[INFO] trace.call: glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 3, 0)
[INFO] trace.call: glDrawBuffer(GL_COLOR_ATTACHMENT0)
//bind to the FBO attached to a depth tex array for shadows
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 1)
[INFO] trace.call: glFramebufferTextureLayerARB(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 1, 0, 0)
[INFO] trace.call: glClear(GL_DEPTH_BUFFER_BIT)
//draw geometry
//bind to the FBO I want the shadow mapped image rendered to
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 2)
[INFO] trace.call: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
//draw geometry

//draw to screen pass
//again shadow mapping FBO
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 1)
[INFO] trace.call: glFramebufferTextureLayerARB(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 1, 0, 0)
[INFO] trace.call: glClear(GL_DEPTH_BUFFER_BIT)
//draw geometry
//bind to the screen
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 0)
[INFO] trace.call: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
//finished, swap buffers
[INFO] trace.call: glXSwapBuffers(0xd5fc10, 0x05800002)
//INCORRECT OUTPUT

//second try at render to screen:
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 1)
[INFO] trace.call: glFramebufferTextureLayerARB(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 1, 0, 0)
[INFO] trace.call: glClear(GL_DEPTH_BUFFER_BIT)
//draw geometry
[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 0)
[INFO] trace.call: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
draw geometry
[INFO] trace.call: glXSwapBuffers(0xd5fc10, 0x05800002)
//correct output

There is nothing wrong with your FBO code. But you are not telling us everything. If this was really what you were doing:

[INFO] trace.call: glBindFramebufferEXT(GL_FRAMEBUFFER, 0)
[INFO] trace.call: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
//finished, swap buffers
[INFO] trace.call: glXSwapBuffers(0xd5fc10, 0x05800002)
//INCORRECT OUTPUT

Then the incorrect output would be black screen (because you bind the default framebuffer, clear and swap buffers). There must be some rendering going on between the glClear() and glXSwapBuffers(). You should post complete source code instead of this grep-ped trace.

Secondly, your issue doesn't have anything to do with the FBOs. They do not affect lighting in any way, you're mostl likely not setting up matrices correctly (it is easy to mess up when mixing lookat from light and lookat from camera). Your answer is a bit messy, so it is likely your code is a bit messy as well. Perhaps you should try looking in there ...

链接地址: http://www.djcxy.com/p/34050.html

上一篇: 渲染到纹理OpenGL ES 2.0

下一篇: OpenGL FrameBuffer对象怪异的行为