Alternatives to glPushMatrix() & co. in Pyglet 2.0?

166 Views Asked by At

I am using Pyglet 2.0

I've found that Pyglet 2.0 has removed a lot of OpenGL functions such as glPushMatrix(). What should we be doing instead in this new version? What is the alternative technique for managing rotations, translations, scaling, etc?

2

There are 2 best solutions below

0
janneb On

Disclaimer: I have never used pyglet myself.

It seems that pyglet has removed functionality for the old school 'immediate mode' OpenGL 1.x, and expects the user to instead use what is commonly known as 'modern OpenGL'. In modern OpenGL you indeed don't have the matrix helper functions with the matrix state handled by the driver, instead you are expected to use some other library for setting up and wrangling matrices on the CPU. For matrix handling on the GPU you use GLSL shaders.

If nothing else, python at least has numpy which is perfectly capable of matrix math, though it's more a general purpose tool and not specific to 3D graphics. There also seems to be https://pypi.org/project/PyGLM/ , a binding to the popular GLM library which is commonly used together with modern OpenGL when using C++ (or cglm when using plain C).

I recommend that you check out some tutorial on modern OpenGL programming, and then try to recreate the concepts there with pyglet.

0
Charlie On

With Pyglet 2.0 (OpenGL 3.3+) you should be using Matrixes. Pyglet offers them built in now via pyglet.math.Mat4. The window has these matrices accessible via window.view and window.projection

For example:

Translation : glTranslatef(x, y, z) becomes window.view = window.view.translate((x, y, z))

Scaling : glScalef(zoom, zoom, 1) becomes window.view = window.view.scale((zoom, zoom, 1))

Orth Projection:

gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glOrtho(0, width, 0, height, -255, 255)
gl.glMatrixMode(gl.GL_MODELVIEW)

Becomes:

window.projection = pyglet.math.Mat4.orthogonal_projection(
    0, width, 0, height, -255, 255
)