1

私は2Dゲームに取り組んでおり、SDLからOpenGLに切り替えることにしました。スプライトをレンダリングし、物理学にpymunk(シマリス)を使用するためのopenglラッパーとしてrabbytを使用しました。ウィンドウの作成にはpygameを使用し、画面にスプライトを描画するためにrabbytを使用しました。

pygame + rabbytを使用すると、(0,0)座標が画面の中央にあることがわかりました。物理エンジンの座標表現はグラフィックエンジンの座標表現と同じだったので、その事実が気に入りました(スプライトをレンダリングするときに座標を再計算する必要はありません)。

次に、OpenGLで線を描きたかったので、ピグレットに切り替えました。突然、(0,0)座標が画面の左下にあることがわかりました。

それはglViewport関数と関係があるのではないかと思いましたが、rabbytだけがその関数を実行し、ウィンドウのサイズが変更されたときにのみpygletがそれに触れます。

画面中央に(0,0)座標を設定するにはどうすればよいですか?

私はOpenGLにあまり精通しておらず、数時間のグーグルと試行錯誤の後で何も見つかりませんでした...誰かが私を助けてくれることを願っています:)

編集:いくつかの追加情報:)

これは私のピグレット画面の初期化コードです:

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ビューポートと関係のある画面初期化コードには何も見つかりませんでした...2つの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 に答える