python - Pygame 和 PyOpenGL 四元纹理问题

标签 python python-3.x opengl pygame pyopengl

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

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

我的问题:

  • 当我将 GL_TEXTURE_MIN_FILTER 更改为 GL_TEXTURE_MAG_FILTER 时 glTexParameteri 纹理消失。为什么?
  • 当我将 GL_LINEAR 更改为 GL_NEAREST 时,没有任何反应。使用过的 纹理的分辨率更改为 300x300px。这是为什么?
  • 如何制作 mipmap 并使用它们?
  • loadImage() 函数创建纹理。 PyOpenGL 如何知道哪个 纹理应该在 makeQuad() 函数中使用吗?

代码:

    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并参见Vertex SpecificationShader最先进的渲染方式。


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,则纹理不“完整”并且不会“显示”。请参阅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?

Mipmap 可以通过 glGenerateMipmap 生成:

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

中绑定(bind)了纹理
glBindTexture(GL_TEXTURE_2D, bgImgGL)

当前命名的纹理对象,绑定(bind)到纹理单元0是bgImgGL。该纹理用于绘图。

关于python - Pygame 和 PyOpenGL 四元纹理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53400377/

相关文章:

python - 如何获取 Google Drive 文件的 'fileId'?

python - 有多少 Actor 与其他不同 Actor / Actor 合作过

python - functools.partial 和 functools.reduce 之间的交互

OpenGL 线条画工件

python - 开发一个用于将数据信息输出到特定范围的 Excel 行和列的计数器?

python-3.x - 如何将映射函数应用于 tensorflow 中的张量

python-3.x - 如何更改 ttk.Entry 插入光标颜色?

opengl - glRotate(angle,x,y,z),在这种情况下x,y,z是多少?

c++ - 画出这个形状

python - 使用 pip 在 python 3.5 上安装 uwsgi 时出错