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??
0

2 Answers 2

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')
Sign up to request clarification or add additional context in comments.

Comments

-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);

2 Comments

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.
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?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.