使用PyOpenGL渲染带纹理的矩形

时间:2011-05-06 06:29:11

标签: python opengl glut

我正在使用PyOpenGL开发一个项目,我正在尝试让OpenGL呈现一种启动画面。我决定的解决方案是绘制纹理2D矩形。不幸的是,似乎无论我做什么,都没有画出任何东西,我只是得到一个黑色的屏幕(所以我想有些东西被绘制,否则它将是一个透明的窗口,但它绝对不是我想要的)。这是我班级的相关代码:

class ClassThing:
    def __init__(self):
        self.Splash = True
        glutInit(sys.argv)

    def TexFromPNG(self, filename):
        img = Image.open(filename)
        img_data = numpy.array(list(img.getdata()), numpy.uint8)

        texture = glGenTextures(1)
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        glBindTexture(GL_TEXTURE_2D, texture)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
        return texture

    def run(self):
        glutInitDisplayMode(GLUT_RGBA)

        glutInitWindowSize(256,224)
        self.window = glutCreateWindow("GL")
        glutDisplayFunc(self.draw)

        glClearColor(0,0,0,0)
        glEnable(GL_TEXTURE_2D)
        glEnable(GL_VERTEX_ARRAY)

        self.MainTex = glGenTextures(1)
        self.SplashTex = self.TexFromPNG("Resources/Splash.png")

        glutMainLoop()

    def draw(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        if self.Splash:
            glBindTexture(GL_TEXTURE_2D, self.SplashTex)
        else:
            glBindTexture(GL_TEXTURE_2D, self.MainTex)

        glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)
        Varray = numpy.array([[0,0],[0,256],[224,256],[224,0]],numpy.uint16)
        glVertexPointer(2,GL_SHORT,0,Varray)
        indices = [0,1,2,3]
        glDrawElements(GL_QUADS,1,GL_UNSIGNED_SHORT,indices)

        glFlush()
thing = ClassThing()
thing.run()

正如我所说,只是用一个完全黑屏的网络给我打电话。感觉我可能缺少某种初始化或启用,但我不知道还需要启用什么。

顺便说一下,显然glVertexPointer已被弃用,如果没有它,我将如何运行glDrawElements?是否有某种顶点生成,你可以做类似于纹理生成或什么?

2 个答案:

答案 0 :(得分:4)

可能是因为您两次致电glGenTextures(1)了吗?您已将其分配给TexFromPNG中的纹理,并在运行中将其分配给self.MainTex。

答案 1 :(得分:0)

你也需要提供纹理坐标,否则OpenGL不知道如何将纹理映射到四边形 - 你也可以使用自动纹理坐标生成,但在你的情况下,只是指定坐标更简单。此外,您还需要指定视口和投影

class ClassThing:
    def __init__(self):
        self.Splash = True

    def TexFromPNG(self, filename):
        img = Image.open(filename)
        img_data = numpy.array(list(img.getdata()), numpy.uint8)

        texture = glGenTextures(1)
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        glBindTexture(GL_TEXTURE_2D, texture)

        # Texture parameters are part of the texture object, so you need to 
        # specify them only once for a given texture object.
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
        return texture

    def run(self):
        glutInitDisplayMode(GLUT_RGBA)

        glutInitWindowSize(256,224)
        self.window = glutCreateWindow("GL")
        glutReshapeFunc(self.reshape)
        glutDisplayFunc(self.draw)

        self.MainTex = glGenTextures(1)
        self.SplashTex = self.TexFromPNG("Resources/Splash.png")

        glutMainLoop()

    def reshape(self, width, height):
        self.width = width
        self.height = height
        glutPostRedisplay();

    def draw(self):
        glViewport(0, 0, self.width, self.height)

        glClearDepth(1) # just for completeness
        glClearColor(0,0,0,0)
        glClear(GL_COLOR_BUFFER_BIT)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, 1, 0, 1, -1, 1)

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity()

        if self.Splash:
            glBindTexture(GL_TEXTURE_2D, self.SplashTex)
        else:
            glBindTexture(GL_TEXTURE_2D, self.MainTex)

        # it's a good idea to enable state right before you need it
        # there's no such thing like global state intialization in
        # OpenGL
        glEnable(GL_TEXTURE_2D)
        # vertex arrays must be enabled using glEnableClientState
        glEnableClientState(GL_VERTEX_ARRAY)
        glEnableClientState(GL_TEXTURE_COORD_ARRAY)

        Varray = numpy.array([[0,0],[0,1],[1,1],[1,0]],numpy.float)
        glVertexPointer(2,GL_FLOAT,0,Varray)
        glTexCoordPointer(2,GL_FLOAT,0,Varray)
        indices = [0,1,2,3]
        glDrawElements(GL_QUADS,1,GL_UNSIGNED_SHORT,indices)

        # This implies a glFinish, which includes a glFlush
        glutSwapBuffers() 

# GLUT initialization in program global, so initialize it on
# the process level. It might be 
glutInit(sys.argv)

thing = ClassThing()
thing.run()