2010-11-25 112 views
1

我正在開發2D遊戲並決定從SDL切換到OpenGL。我把rabbyt作爲opengl包裝來渲染我的精靈,並使用pymunk(花栗鼠)來做我的物理。我使用pygame創建窗口和rabbyt在屏幕上繪製小精靈。當從pygame + rabbyt遷移到pyglet + rabbyt時,座標發生了變化

我發現用pygame + rabbyt(0,0)座標位於屏幕中間。我喜歡這個事實,因爲物理引擎中的座標表示與我的圖形引擎中的相同(在渲染精靈時,我不必重新計算座標)。

然後,我切換到pyglet,因爲我想用OpenGL畫線 - 並發現突然(0,0)座標在屏幕的左下角。

我懷疑這與glViewport函數有關,但只有rabbyt才能執行該函數,pyglet只有在調整窗口大小時纔會觸及它。

如何在屏幕中間設置座標(0,0)?

我不是很熟悉OpenGL和找不到幾個小時google搜索後任何審判&錯誤...我希望有人能幫助我:)

編輯:一些額外的信息:)

這是我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) 

感謝, 斯特芬

+0

在渲染前嘗試`glTranslate2f(window.width/2,window.height/2)`或類似的東西`這應該將原點設置在中心。 – 2010-11-25 17:37:47

回答

0

由於l33tnerd建議的起源可以放置在與的glTranslatef中心...... 我說我下面的屏幕初始化代碼如下:

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

謝謝!

相關問題