如何将 pyopengl 纹理大小缩放到 pygame window 大小?

How to scale pyopengl texture size to pygame window size?

Windows 尺寸:640 * 480

纹理大小:1280 * 720

您好,如何将纹理缩放到 pygame window?

您已设置正交投影:

glOrtho(0, self.windowWidth, self.windowHeight, 0, -1, 1)

如果想让贴图布满整个window,那么就要画一个正投影对应的四边形,贴图坐标形式为(0, 0)到(1, 1):

glEnable(GL_TEXTURE_2D)

glBegin(GL_QUAD)

glTexCoord2f(0, 0)
glVertex2f(0, 0)

glTexCoord2f(1, 0)
glVertex2f(self.windowWidth, 0)

glTexCoord2f(1, 1)
glVertex2f(self.windowWidth, self.windowHeight)

glTexCoord2f(0, 1)
glVertex2f(0, self.windowHeight)

glEnd()

如果你想保持纹理的纵横比,那么你必须缩放四边形的大小。例如:

left = 0
top = 0
width = self.windowWidth
height = self.windowHeight

sx = self.windowWidth / textureWidth 
sy = self.windowHeight / textureHeight 
if sx > sy:
    width *= sy / sx
    left = (self.windowWidth - width) / 2
else:
    height *= sx / sy  
    top = (self.windowHeight - height) / 2

glEnable(GL_TEXTURE_2D)

glBegin(GL_QUAD)
glTexCoord2f(0, 0)
glVertex2f(left, top)
glTexCoord2f(1, 0)
glVertex2f(left + width, top)
glTexCoord2f(1, 1)
glVertex2f(left + width, top + height)
glTexCoord2f(0, 1)
glVertex2f(left, top + height)
glEnd()