如何将现代OpenGL的帧缓冲对象(FBO)读取到一个NumPy数组中?

4

我有两个 FBO 中的一个,用于在 glsl 中进行一些计算,并且需要将纹理数据(dtype='f4')读取回 numpy 数组以进行进一步的计算。我没有在文档中找到任何说明如何执行此操作的内容。请帮忙提供帮助。

我使用以下代码创建纹理:

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

我会按照以下方式处理它们:

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??
2个回答

5
您可以使用 fbo.read 读取帧缓冲区的内容。

您可以使用np.frombuffer将缓冲区转换为numpy数组。

示例:

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

-1

这个问题涉及OpenGL API,我的答案在GL api的使用方面是正确的。glGetTextureImage的第一个参数确实是纹理对象,而不是你所声称的目标。你可能会混淆旧的glGetTexImage方法。 - robthebloke
1
问题是如何将其读回到numpy数组中。这是一个Python问题,而不是C ++。问题的标题是“...回到numpy数组中?” - Rabbid76

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接