如何在 opengl/pyglet 中使用 HUD 缩放相机?
How to zoom camera with HUD in opengl/pyglet?
我正在用 pyglet 制作 2d 策略游戏并使用 glTranslatef 函数实现相机移动:
def background_motion(dt):
if stars.left:
pyglet.gl.glTranslatef(15, 0, 0)
stars.translation[0] += 15
if stars.right:
pyglet.gl.glTranslatef(-15, 0, 0)
stars.translation[0] -= 15
if stars.up:
pyglet.gl.glTranslatef(0, -15, 0)
stars.translation[1] -= 15
if stars.down:
pyglet.gl.glTranslatef(0, 15, 0)
stars.translation[1] += 15
并让 HUD 保持在这样的位置:
def on_draw():
window.clear()
stars.image.draw()
for s in game.ships:
s.draw()
pyglet.gl.glTranslatef(-stars.translation[0], -stars.translation[1], 0)
#HUD Start
overlay.draw(stars.image.x,stars.image.y,game.ships,stars.scale,stars.image.width)
if game.pause:
pause_text.draw()
#HUD End
pyglet.gl.glTranslatef( stars.translation[0], stars.translation[1], 0)
我在缩放时尝试了类似的方法,当缩放有效时,HUD 也被缩放:
def on_mouse_scroll(x, y, scroll_x, scroll_y):
if scroll_y > 0:
stars.scale += 0.01
elif scroll_y < 0:
stars.scale -= 0.01
@window.event
def on_draw():
window.clear()
pyglet.gl.glScalef(stars.scale,stars.scale, 0, 1)
stars.image.draw()
for s in game.ships:
s.draw()
scale_reverse = 1 + (1 - stars.scale)
pyglet.gl.glScalef(scale_reverse, scale_reverse, 0, 1)
pyglet.gl.glTranslatef(-stars.translation[0], -stars.translation[1], 0)
#HUD Start
overlay.draw(stars.image.x,stars.image.y,game.ships,stars.scale,stars.image.width)
if game.pause:
pause_text.draw()
#HUD End
pyglet.gl.glTranslatef( stars.translation[0], stars.translation[1], 0)
pyglet.gl.glScalef(stars.scale, stars.scale, 0, 1)
stars.scale = 1
我该怎么做才能不缩放 HUD?
请注意,几十年来不推荐使用 glBegin
/glEnd
序列和固定函数矩阵堆栈和固定进行绘制。
阅读 Fixed Function Pipeline and see Vertex Specification and Shader 了解最先进的渲染方式。
无论如何,我建议使用 glPushMatrix
/glPopMatrix
,在绘制 HUD 之前存储矩阵,并在绘制之后恢复它。
所以你可以在绘制HUD之前通过glLoadIdentity
设置单位矩阵。
甚至有可能为 HUD 使用完全不同的(平移和缩放)矩阵:
#HUD Start
pyglet.gl.glPushMatrix()
pyglet.gl.glLoadIdentity()
overlay.draw(stars.image.x,stars.image.y,game.ships,stars.scale,stars.image.width)
if game.pause:
pause_text.draw()
pyglet.gl.glPopMatrix()
#HUD End
我正在用 pyglet 制作 2d 策略游戏并使用 glTranslatef 函数实现相机移动:
def background_motion(dt):
if stars.left:
pyglet.gl.glTranslatef(15, 0, 0)
stars.translation[0] += 15
if stars.right:
pyglet.gl.glTranslatef(-15, 0, 0)
stars.translation[0] -= 15
if stars.up:
pyglet.gl.glTranslatef(0, -15, 0)
stars.translation[1] -= 15
if stars.down:
pyglet.gl.glTranslatef(0, 15, 0)
stars.translation[1] += 15
并让 HUD 保持在这样的位置:
def on_draw():
window.clear()
stars.image.draw()
for s in game.ships:
s.draw()
pyglet.gl.glTranslatef(-stars.translation[0], -stars.translation[1], 0)
#HUD Start
overlay.draw(stars.image.x,stars.image.y,game.ships,stars.scale,stars.image.width)
if game.pause:
pause_text.draw()
#HUD End
pyglet.gl.glTranslatef( stars.translation[0], stars.translation[1], 0)
我在缩放时尝试了类似的方法,当缩放有效时,HUD 也被缩放:
def on_mouse_scroll(x, y, scroll_x, scroll_y):
if scroll_y > 0:
stars.scale += 0.01
elif scroll_y < 0:
stars.scale -= 0.01
@window.event
def on_draw():
window.clear()
pyglet.gl.glScalef(stars.scale,stars.scale, 0, 1)
stars.image.draw()
for s in game.ships:
s.draw()
scale_reverse = 1 + (1 - stars.scale)
pyglet.gl.glScalef(scale_reverse, scale_reverse, 0, 1)
pyglet.gl.glTranslatef(-stars.translation[0], -stars.translation[1], 0)
#HUD Start
overlay.draw(stars.image.x,stars.image.y,game.ships,stars.scale,stars.image.width)
if game.pause:
pause_text.draw()
#HUD End
pyglet.gl.glTranslatef( stars.translation[0], stars.translation[1], 0)
pyglet.gl.glScalef(stars.scale, stars.scale, 0, 1)
stars.scale = 1
我该怎么做才能不缩放 HUD?
请注意,几十年来不推荐使用 glBegin
/glEnd
序列和固定函数矩阵堆栈和固定进行绘制。
阅读 Fixed Function Pipeline and see Vertex Specification and Shader 了解最先进的渲染方式。
无论如何,我建议使用 glPushMatrix
/glPopMatrix
,在绘制 HUD 之前存储矩阵,并在绘制之后恢复它。
所以你可以在绘制HUD之前通过glLoadIdentity
设置单位矩阵。
甚至有可能为 HUD 使用完全不同的(平移和缩放)矩阵:
#HUD Start
pyglet.gl.glPushMatrix()
pyglet.gl.glLoadIdentity()
overlay.draw(stars.image.x,stars.image.y,game.ships,stars.scale,stars.image.width)
if game.pause:
pause_text.draw()
pyglet.gl.glPopMatrix()
#HUD End