为什么 pygame 和 pyglet 使用相同的矩阵在屏幕上显示不同的结果?

Why do pygame and pyglet show different results on the screen with the SAME matrices?

当我 运行 此代码时 use_pygletTrueFalse 相比,为什么我看到不同的结果?

两种情况下的矩阵和视口都一样,所以我真的很困惑。

import ctypes
import numpy

use_pyglet = False   # change this to True to see the difference
if use_pyglet:
    import pyglet
    from pyglet.gl import *
    window = pyglet.window.Window(resizable=True, config=pyglet.gl.Config(double_buffer=True))
else:
    import pygame, pygame.locals
    from pyglet.gl import *
    pygame.init()
    pygame.display.set_mode((640, 480), pygame.locals.DOUBLEBUF | pygame.locals.OPENGL)
a = (ctypes.c_int   *  4)(); glGetIntegerv(GL_VIEWPORT, a); print numpy.asarray(a)
a = (ctypes.c_float * 16)(); glGetFloatv(GL_PROJECTION_MATRIX, a); print numpy.asarray(a).reshape((4, 4)).T
a = (ctypes.c_float * 16)(); glGetFloatv(GL_MODELVIEW_MATRIX, a); print numpy.asarray(a).reshape((4, 4)).T
def on_draw():
    glClearColor(1, 1, 1, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glColor4d(0, 0, 0, 1)
    glBegin(GL_LINE_STRIP)
    glVertex2d(0, 0)
    glVertex2d(100, 100)
    glEnd()
if use_pyglet:
    on_draw = window.event(on_draw)
    pyglet.app.run()
else:
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                break
        on_draw()
        pygame.display.flip()
        pygame.time.wait(20)

PyGame:

小狗:

The matrices and viewport are the same in both cases, so I'm really confused.

他们实际上不是。问题是在你检查它的时候它们还没有被改变。如果您改为将支票移至 on_draw。然后你会注意到 Pyglet 的 GL_PROJECTION_MATRIX 会输出:

[[ 0.003125    0.          0.         -1.        ]
 [ 0.          0.00416667  0.         -1.        ]
 [ 0.          0.         -1.         -0.        ]
 [ 0.          0.          0.          1.        ]]

而对于 Pygame 它将输出:

[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

解决方案是自己设置投影矩阵。从而确保它永远是一样的。

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

当然,您希望如何设置项目矩阵取决于所需的结果。