运行你的代码给了我一些
"Invalid Operation Error:1282"对消息的
glReadPixels电话。相反,这是我刚刚编写的一个简单演示,演示了如何从OpenGL获取渲染三角形的颜色和深度缓冲区。我在这里所做的是使用所需的纹理附件(用于接收颜色和深度数据)将FBO(帧缓冲区对象)绑定到屏幕。然后,我使用读取GPU中的数据
glGetTexImage。使用纹理可能不是最快的方法,但这很简单,应该可以很好地工作。让我知道是否有任何不清楚的地方,我将对其进行详细说明。
from OpenGL.GL import *from OpenGL.GLUT import *import numpy as npimport sysdef draw_scene(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBegin(GL_TRIANGLES) glColor3f(1, 0, 0) glVertex3f(-1, -1, 0) glColor3f(0, 1, 0) glVertex3f(0, 1, 0) glColor3f(0, 0, 1) glVertex3f(1, -1, 0) glEnd()def draw_texture(): global color_texture glColor3f(1, 1, 1) glBindTexture(GL_TEXTURE_2D, color_texture) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex3f(-1, -1, 0) glTexCoord2f(0, 1) glVertex3f(-1, 1, 0) glTexCoord2f(1, 1) glVertex3f(1, 1, 0) glTexCoord2f(1, 0) glVertex3f(1, -1, 0) glEnd() glBindTexture(GL_TEXTURE_2D, 0)def update_display(): global fbo, color_texture, depth_texture #Render the scene to an offscreen FBO glBindframebuffer(GL_frameBUFFER, fbo) draw_scene() glBindframebuffer(GL_frameBUFFER, 0) #Then render the results of the color texture attached to the FBO to the screen draw_texture() #Obtain the color data in a numpy array glBindTexture(GL_TEXTURE_2D, color_texture) color_str = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE) glBindTexture(GL_TEXTURE_2D, 0) color_data = np.fromstring(color_str, dtype=np.uint8) #Obtain the depth data in a numpy array glBindTexture(GL_TEXTURE_2D, depth_texture) depth_str = glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT) glBindTexture(GL_TEXTURE_2D, 0) depth_data = np.fromstring(depth_str, dtype=np.float32) print(np.min(depth_data), np.max(depth_data))#This is just to show the normalized range of depth values obtained glutSwapBuffers()width, height = 800, 600fbo = Nonecolor_texture = Nonedepth_texture = Noneif __name__ == '__main__': glutInit([]) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE) glutInitWindowSize(width, height) glutInitWindowPosition(0, 0) glutCreateWindow("Triangle Test") glEnable(GL_TEXTURE_2D)#not needed if using shaders... glEnable(GL_DEPTH_TEST) color_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, color_texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, None) glBindTexture(GL_TEXTURE_2D, 0) depth_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, depth_texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None) glBindTexture(GL_TEXTURE_2D, 0) fbo = glGenframebuffers(1) glBindframebuffer(GL_frameBUFFER, fbo) glframebufferTexture2D(GL_frameBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_texture, 0) glframebufferTexture2D(GL_frameBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture, 0) glBindframebuffer(GL_frameBUFFER, 0) glutDisplayFunc(update_display) glutIdleFunc(glutPostRedisplay) glutMainLoop()


