栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用PyOpenGL读取深度缓冲区

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用PyOpenGL读取深度缓冲区

运行你的代码给了我一些

"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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611523.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号