4

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??
Mike Manh
  • 333
  • 2
  • 11

2 Answers2

5

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')
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
-1

Use glGetTexImage (or preferably glGetTextureImage) to copy the data into a buffer (from the texture you are using for your colour data).

https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetTexImage.xhtml

glGetTextureImage(textureToReadFrom, 0, GL_RGBA, GL_FLOAT, bufferSize, bufferPointer);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
robthebloke
  • 9,331
  • 9
  • 12
  • The question is about the OpenGL API, and my answer is correct wrt the usage of the GL api. The first parameter of glGetTextureImage *is* the texture object, and not the target as you claim. You are getting confused with the older glGetTexImage method. – robthebloke Jul 11 '19 at 05:24
  • 1
    The question is how to read it back into a numpy array. This is python question and not C++. The title of the question is *"... back into a numpy array?"* – Rabbid76 Jul 11 '19 at 05:25