2010-11-25 13 views
1

私は2Dゲームで作業しており、SDLからOpenGLに切り替えることにしました。私は私のスプライトを描画し、私の物理学のためにpymunk(chipmunk)を使うために、ラビットをOpenGLラッパーとして取った。私は画面をスプライトを描画するためにウインドウとウサギを作成するためにパイゲームを使用しました。pygame + rabbytからpyglet + rabbytへの移行時に座標が変更されました

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

次に、私はOpenGLで線を描きたいのでpygletに切り替えました。突然(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ビューポートと何か関係があるスクリーン初期化コードで何も発見しませんでした...ここに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) 

おかげで、 ステファン

+0

レンダリングする前に 'glTranslate2f(window.width/2、window.height/2)'などの文字を試してみてください。原点を中心に設定する必要があります。 –

+0

ありがとうございます!できます :) – stefreak

答えて

0

をl33tnerdが原点はglTranslatefで中央に配置することが可能で示唆したように... 私は画面の初期化コードの下に次を追加しました:

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

ありがとうございました!

関連する問題