Question

I am trying to render a simple checkerboard in a FBO and then do a glReadPixels().

When I do it without FBO, everything works fine. So I assume that my render function is ok and so is the glReadPixels(). With the FBO, all I get are the lines that I draw after the calls to FBO have been done.

Here is my code (Python, aiming cross platform):

def renderFBO():
    #WhyYouNoWorking(GL_FRAMEBUFFER) # degug function... error checking
    glBindFramebuffer( GL_DRAW_FRAMEBUFFER, framebuffer)

    glBindRenderbuffer( GL_RENDERBUFFER, renderbufferA)
    glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA, window.width, window.height)

    glBindRenderbuffer( GL_RENDERBUFFER, renderbufferB)
    glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, window.width, window.height)    

    glBindFramebuffer( GL_DRAW_FRAMEBUFFER, framebuffer)
    glFramebufferRenderbuffer( GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbufferA)
    glFramebufferRenderbuffer( GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbufferB)

    #WhyYouNoWorking(GL_FRAMEBUFFER)

    glDrawBuffer(GL_COLOR_ATTACHMENT0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glViewport( 0, 0, window.width, window.height)


    DrawChecker(Nbr = 16, Dark = 25.0/255, Light = 75.0/255)
    for i in range(len(labelSysInfo)):
        pyglet.text.Label(labelSysInfo[i], font_name='Times New Roman', font_size=26, x=(window.width*0.68), y= (window.height*0.04*i)+(window.height*2/3), anchor_x='left', anchor_y='center', color = (250, 250, 250, 150)).draw()

    glReadPixels(0, 0, window.width, window.height, GL_RGBA, GL_UNSIGNED_BYTE, a)
    glBindFramebuffer( GL_FRAMEBUFFER, 0)

My other function:

def on_draw(dt):   
    glDrawBuffer(GL_BACK)
    glClear(GL_COLOR_BUFFER_BIT)
    glClearColor( 0.0, 0.0, 0.0, 1.0)
    glLoadIdentity()
    glEnable(GL_TEXTURE_2D)


    glDisable(GL_TEXTURE_2D)
    BlueLine() # draw a simple line. works fine
    DropFrameTest()   # draw a simple line. works fine

In the main, the call to renderFBO() is done once, and then on_draw is called periodically.

dt = pyglet.clock.tick()
renderFBO()  
pyglet.clock.schedule_interval(on_draw, 0.007)
pyglet.app.run()
Was it helpful?

Solution

At a guess, you've bound the framebuffer to the GL_DRAW_FRAMEBUFFER only. Use

glBindFramebuffer(GL_FRAMEBUFFER, ...

and

glFramebufferRenderbuffer(GL_FRAMEBUFFER, ...

to both read and write with the same FBO.

I'm sure you already have but checking for framebuffer completeness (glCheckFramebufferStatus) and for GL errors (glGetError, or the new extension) is also very useful.

[EDIT]
(The shotgun problem solving tactics from the comments)

If you see an image on the first frame, but none on the next there must be something staying behind from the previous frame.

  • The most common problem is forgetting to clear the depth buffer - but you haven't.
  • Next up are stencil buffers and blending (neither look like they're enabled to begin with).
  • Maybe a new FBO handle is being generated each frame and you're running out?
  • Another common problem is accumulating matrix transforms, but you have glLoadIdentity so should be no issue there.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top