1

我正在开发一款 2D 游戏,并决定从 SDL 切换到 OpenGL。我把 rabbyt 作为一个 opengl 包装器来渲染我的精灵,并使用 pymunk(花栗鼠)来做我的物理。我使用 pygame 创建窗口,使用 rabbyt 在屏幕上绘制精灵。

我发现使用 pygame+rabbyt 时,(0,0) 坐标位于屏幕中间。我喜欢这个事实,因为物理引擎中的坐标表示与我的图形引擎中的相同(渲染精灵时我不必重新计算坐标)。

然后我切换到 pyglet,因为我想用 OpenGL 画线 - 突然发现 (0,0) 坐标位于屏幕的左下角。

我怀疑这与 glViewport 函数有关,但只有 rabbyt 执行该函数,pyglet 仅在调整窗口大小时才会触及它。

如何在屏幕中间设置 (0,0) 坐标?

我对OpenGL不是很熟悉,经过几个小时的谷歌搜索和试错后找不到任何东西......我希望有人能帮助我:)

编辑:一些附加信息:)

这是我的 pyglet 屏幕初始化代码:

self.window = Window(width=800, height=600)
rabbyt.set_viewport((800,600))
rabbyt.set_default_attribs()

这是我的 pygame 屏幕初始化代码:

display = pygame.display.set_mode((800,600), \
  pygame.OPENGL | pygame.DOUBLEBUF)
rabbyt.set_viewport((800, 600))
rabbyt.set_default_attribs()

编辑 2:我查看了 pyglet 和 pygame 的来源,并没有在屏幕初始化代码中发现与 OpenGL 视口有关的任何内容......这是两个 rabbyt 函数的来源:

def set_viewport(viewport, projection=None):
    """
    ``set_viewport(viewport, [projection])``

    Sets how coordinates map to the screen.

    ``viewport`` gives the screen coordinates that will be drawn to.  It
    should be in either the form ``(width, height)`` or
    ``(left, top, right, bottom)``

    ``projection`` gives the sprite coordinates that will be mapped to the
    screen coordinates given by ``viewport``.  It too should be in one of the
    two forms accepted by ``viewport``.  If ``projection`` is not given, it
    will default to the width and height of ``viewport``.  If only the width
    and height are given, ``(0, 0)`` will be the center point.
    """
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    if len(viewport) == 4:
        l, t, r, b = viewport
    else:
        l, t = 0, 0
        r, b = viewport
    for i in (l,t,r,b):
        if i < 0:
            raise ValueError("Viewport values cannot be negative")
    glViewport(l, t, r-l, b-t)

    if projection is not None:
        if len(projection) == 4:
            l, t, r, b = projection
        else:
            w,h = projection
            l, r, t, b = -w/2, w/2, -h/2, h/2
    else:
        w,h = r-l, b-t
        l, r, b, t = -w/2, w/2, -h/2, h/2
    glOrtho(l, r, b, t, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

def set_default_attribs():
    """
    ``set_default_attribs()``

    Sets a few of the OpenGL attributes that sprites expect.

    Unless you know what you are doing, you should call this at least once
    before rendering any sprites.  (It is called automatically in
    ``rabbyt.init_display()``)
    """
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    glEnable(GL_BLEND)
    #glEnable(GL_POLYGON_SMOOTH)

谢谢,史蒂芬

4

1 回答 1

0

正如 l33tnerd 建议的那样,可以使用 glTranslatef 将原点放置在中心...我在屏幕初始化代码下方添加了以下内容:

pyglet.gl.glTranslatef(width/2, height/2, 0)

谢谢!

于 2010-11-25T17:57:24.670 回答