Pygame 和 PyOpenGL 四边形纹理问题

Pygame and PyOpenGL quad texturing problem

我正在尝试对四边形进行纹理处理并了解这个小示例的工作原理。我的代码不是原创的,它是从各种例子中混合而来的。

纹理:https://jamesmwake.files.wordpress.com/2015/10/uv_texture_map.jpg?w=660

我的问题:

代码:

    import pygame
    from pygame.locals import *

    from OpenGL.GL import *
    from OpenGL.GLU import *

    def loadImage():
        img = pygame.image.load("checker_texture_downsized.jpg")
        textureData = pygame.image.tostring(img, "RGB", 1)
        width = img.get_width()
        height = img.get_height()
        bgImgGL = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, bgImgGL)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData)
        glEnable(GL_TEXTURE_2D)

    def makeQuad():
        glBegin(GL_QUADS)
        glTexCoord2f(0, 0)
        glVertex2f(25, 25)
        glTexCoord2f(0, 1)
        glVertex2f(25, 775)
        glTexCoord2f(1, 1)
        glVertex2f(775, 775)
        glTexCoord2f(1, 0)
        glVertex2f(775, 25)
        glEnd()

    def main():
        pygame.init()
        display = (1280,800)
        pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
        gluOrtho2D(0, 1280, 0, 800)
        loadImage()

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
            makeQuad()
            pygame.display.flip()

    main()

请注意,通过 glBegin/glEnd 序列绘制、固定功能管线矩阵堆栈和每个顶点光模型的固定功能管线已被弃用数十年。 阅读 Fixed Function Pipeline and see Vertex Specification and Shader 了解最先进的渲染方式。


When I change GL_TEXTURE_MIN_FILTER to GL_TEXTURE_MAG_FILTER in glTexParameteri the texture disappears. Why?

GL_TEXTURE_MIN_FILTER的初始值为GL_NEAREST_MIPMAP_LINEAR。如果您不更改它并且不创建 mipmap,则纹理不是 "complete",也不会是 "shown"。参见 glTexParameter

参见 OpenGL 4.6 API Compatibility Profile Specification; 8.17 Texture Completeness; page 306

A texture is said to be complete if all the texture images and texture parameters required to utilize the texture for texture application are consistently defined.

... a texture is complete unless any of the following conditions hold true:

  • The minification filter requires a mipmap (is neither NEAREST nor LINEAR), and the texture is not mipmap complete.

When I change GL_LINEAR to GL_NEAREST, nothing happens. The used texture's resolution changed to 300x300px. Why is that?

如果纹理小于纹理包裹的区域,缩小滤镜没有效果,但放大会有效果。如果将值 GL_NEAREST 设置为 GL_TEXTURE_MAG_FILTER,则不会再对纹素进行插值。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

How can I make mipmaps and then using them?

可以通过 glGenerateMipmap:

生成 Mipmap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData)
glGenerateMipmap(GL_TEXTURE_2D)

The loadImage() function make a texture. How knows PyOpenGL which texture should be used in the makeQuad() function?

OpenGL 是一个状态引擎。每个状态都会保留,直到您再次更改它,甚至超出帧。由于您已在 loadImage

中绑定纹理
glBindTexture(GL_TEXTURE_2D, bgImgGL)

绑定到纹理单元 0 的当前命名纹理对象是 bgImgGL。此纹理用于绘图。