使用 PyOpenGL 渲染纹理矩形

发布于 2024-11-05 13:26:16 字数 2313 浏览 1 评论 0原文

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

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 ?是否可以进行某种类似于纹理生成的顶点生成或其他操作?

I'm working on a project using PyOpenGL, and I'm currently attempting to get OpenGL to render a kind of splash screen. My decided solution to this is to draw a textured 2D rectangle. Unfortunately, it appears that no matter what I do, nothing is ever drawn, I just get a black screen (So I guess something is drawn, otherwise it would be a transparent window, but it's definitely not what I want). Here is the pertinent code for my class:

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

As I said, just that nets me with a totally black screen. It feels like I may be missing some kind of initialization or enabling, but I don't know what else I would need to enable.

As an aside, apparently glVertexPointer is deprecated, how would I run glDrawElements without it? Is there some kind of vertex generation you can do similar to texture generation or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-11-12 13:26:16

难道是因为你调用了glGenTextures(1)两次?您将其分配给 TexFromPNG 中的纹理,并在运行中将其分配给 self.MainTex。

Could it be because you called glGenTextures(1) twice? You assigned it to texture in TexFromPNG and assigned it to self.MainTex in run.

甩你一脸翔 2024-11-12 13:26:16

您还需要提供纹理坐标,否则 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()

You need to supply texture coordinates as well, otherwise OpenGL doesn't know how to map the texture to your quad — you also could use automatic texture coordinate generation, but in your case just specifying coordinates is simpler. In addition you also need to specify viewport and projection

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文