تم تغيير الإحداثيات عند الهجرة من Pygame+Rabbyt إلى Pyglet+Rabbyt

StackOverflow https://stackoverflow.com/questions/4279516

  •  28-09-2019
  •  | 
  •  

سؤال

أنا أعمل على لعبة ثنائية الأبعاد وقررت التبديل من SDL إلى OpenGL. أخذت RABBYT كركب OpenGL لتقديم العفاريت الخاصة بي واستخدام pymunk (Chipmunk) لفيزيائي. لقد استخدمت Pygame لإنشاء النافذة و Rabbyt لرسم العفاريت على الشاشة.

اكتشفت أنه مع Pygame+Rabbyt الإحداثي (0،0) في منتصف الشاشة. أعجبتني هذه الحقيقة ، لأن تمثيل الإحداثيات في محرك الفيزياء كان هو نفسه كما في محرك الرسومات الخاص بي (لا يجب أن أعيد حساب الإحداثيات عند تقديم العفاريت).

ثم انتقلت إلى Pyglet لأنني أردت رسم خطوط باستخدام OpenGL - واكتشفت أن الإحداثيات (0،0) فجأة كانت في أسفل اليسار من الشاشة.

كنت أظن أن هذا له علاقة بوظيفة Glviewport ، ولكن Rabbyt فقط ينفذ هذه الوظيفة ، فإن Pyglet يمسها فقط عند تغيير حجم النافذة.

كيف يمكنني ضبط الإحداثي (0،0) في منتصف الشاشة؟

لست على دراية بـ OpenGL ولم أتمكن من العثور على أي شيء بعد عدة ساعات من googling والتجربة والخطأ ... آمل أن يتمكن شخص ما من مساعدتي :)

تحرير: بعض المعلومات الإضافية :)

هذا هو رمز تهيئة شاشة 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 Viewport ... إليك مصدر وظيفتي 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)

شكرا ، ستيفن

هل كانت مفيدة؟

المحلول

كما اقترح L33Tnerd أن الأصل يمكن وضعه في المركز مع Gltranslatef ... لقد أضفت ما يلي أدناه رمز تهيئة الشاشة الخاص بي:

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

شكرًا!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top