我正在尝试对四边形进行纹理处理并了解这个小示例的工作原理。我的代码不是原创的,它是由各种示例混合而成的。
纹理: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 Specification和 Shader最先进的渲染方式。
When I change
GL_TEXTURE_MIN_FILTER
toGL_TEXTURE_MAG_FILTER
inglTexParameteri
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
norLINEAR
), and the texture is not mipmap complete.
When I change
GL_LINEAR
toGL_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 themakeQuad()
function?
OpenGL 是一个状态引擎。每个状态都会保留,直到您再次更改它,甚至超出帧。由于您已经在 loadImage
glBindTexture(GL_TEXTURE_2D, bgImgGL)
当前命名的纹理对象,绑定(bind)到纹理单元0是bgImgGL
。该纹理用于绘图。
关于python - Pygame 和 PyOpenGL 四元纹理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53400377/