Search code examples
pythonnumpyopenglpython-moderngl

How do I read a moderngl fbo(frame buffer object) back into a numpy array?


I have one of 2 FBO's i've been using to ping pong some calculations in glsl, and I need to read the texture data (of dtype='f4') back into a numpy array for further calculations. I haven't found anything in the documentation that explains how to do this. Any help?

I create the textures with this

self.texturePing = self.ctx.texture( (width, height), 4, dtype='f4')
self.texturePong = self.ctx.texture( (width, height), 4, dtype='f4')

And I process them like this:

def render(self, time, frame_time):
        self.line_texture.use(0)
        self.transform['lineImg'].value = 0
        for _ in range (2):
            self.fbo2.use()
            self.texturePing.use(1)
            self.transform['prevData'].value = 1

            self.process_vao.render(moderngl.TRIANGLE_STRIP)

            #this rendered to texturePong 
            self.fbo1.use() #texture Ping


            self.texturePong.use(1)
            self.transform['prevData'].value = 1                
            self.process_vao.render(moderngl.TRIANGLE_STRIP)

        #stop drawing to the fbo and draw to the screen
        self.ctx.screen.use()
        self.ctx.clear(1.0, 1.0, 1.0, 0.0) #might be unnecessary   
        #tell the canvas to use this as the final texture 
        self.texturePing.use(3)
        self.canvas_prog['Texture'].value = 3
        #bind the ping texture as the Texture in the shader
        self.canvas_vao.render(moderngl.TRIANGLE_STRIP)

        # this looks good but how do I read texturePong back into a numpy array??

Solution

  • You can read the framebuffer's content with fbo.read.

    You can turn the buffer into a numpy array with np.frombuffer

    Example:

    raw = self.fbo1.read(components=4, dtype='f4') # RGBA, floats
    buf = np.frombuffer(raw, dtype='f4')