tutorial mac español python opengl pygame pyglet

python - mac - pyopengl tutorial español



coordenadas cambiadas al migrar de pygame+rabbyt a pyglet+rabbyt (1)

Estoy trabajando en un juego en 2D y decidí cambiar de SDL a OpenGL. Tomé rabbyt como un contenedor abierto para renderizar mis sprites y usar pymunk (ardilla listada) para mi física. Usé pygame para crear la ventana y rabbyt para dibujar los sprites en la pantalla.

Descubrí que con pygame + rabbyt la coordenada (0,0) está en el medio de la pantalla. Me gustó ese hecho, porque la representación de coordenadas en el motor de física era la misma que en mi motor de gráficos (no tengo que volver a calcular las coordenadas al representar los sprites).

Luego cambié a pyglet porque quería dibujar líneas con OpenGL, y descubrí que de repente la coordenada (0,0) estaba en la parte inferior izquierda de la pantalla.

Sospeché que eso tiene algo que ver con la función glViewport, pero solo Rabbyt ejecuta esa función, pyglet la toca solo cuando la ventana cambia de tamaño.

¿Cómo puedo establecer la coordenada (0,0) en el medio de la pantalla?

No estoy muy familiarizado con OpenGL y no pude encontrar nada después de varias horas buscando en Google y prueba y error ... Espero que alguien me puede ayudar :)

Editar: alguna información adicional :)

Este es mi código de inicialización de pantalla de pyglet:

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

Este es mi código de inicialización de pantalla de pygame:

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

Editar 2: miré las fuentes de pyglet y pygame y no descubrí nada en el código de inicialización de pantalla que tenga algo que ver con la ventana OpenGL ... Aquí está la fuente de las dos funciones de 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)

Gracias, Steffen


Como l33tnerd sugirió que el origen se puede colocar en el centro con glTranslatef ... Agregué lo siguiente debajo del código de inicialización de mi pantalla:

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

¡Gracias!