【问题标题】:How to scale pyopengl texture size to pygame window size?如何将 pyopengl 纹理大小缩放到 pygame 窗口大小?
【发布时间】:2020-08-17 16:31:13
【问题描述】:

窗口尺寸:640 * 480

纹理大小:1280 * 720

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

【问题讨论】:

  • 请在问题中包含您的代码,而不仅仅是图片。在许多设备上以及对于有无障碍问题的人来说,您的图片是不可读的。此外,如果有人想重现您的问题,他们将不得不重新输入您的代码。请让其他人轻松帮助您。

标签: opengl pygame textures pyopengl opengl-compat


【解决方案1】:

您已设置正交投影:

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

如果你想让纹理填满整个窗口,那么你必须绘制一个正交投影对应的四边形,纹理坐标从(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()

【讨论】:

  • 我用 glfw 替换了 pygame。但视频显示是静态的。
  • @seeu 抱歉,这是一个新问题。请Ask a public question 并提供新代码。但是请务必使用代码的屏幕截图,您必须将代码粘贴到新问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
相关资源
最近更新 更多