PyOpenGl or pyglet? [closed] PyOpenGl or pyglet? [closed] python python

PyOpenGl or pyglet? [closed]


Start with pyglet. It contains the best high-level API, which contains all you need to get started, from opening a window to drawing sprites and OpenGL primitives using their friendly and powerful Sprite and Batch classes.

Later, you might also want to write your own lower-level code, that makes calls directly to OpenGL functions such as glDrawArrays, etc. You can do this using pyglet's OpenGL bindings, or using PyOpenGL's. The good news is that whichever you use, you can insert such calls right into the middle of your existing pyglet application, and they will 'just work'. Transitioning your code from Pyglet to PyOpenGL is fairly easy, so this is not a decision you need to worry about too much upfront. The trades-off between the two are:

PyOpenGL's bindings make the OpenGL interface more friendly and pythonic. For example, you can pass vertex arrays in many different forms, ctypes arrays, numpy arrays, plain lists, etc, and PyOpenGL will convert them into something OpenGL can use. Things like this make PyOpenGL really easy and convenient.

pyglet's OpenGL bindings are automatically generated, and are not as friendly to use as PyOpenGL. For example, sometimes you will have to manually create ctypes objects, in order to pass 'C pointer' kinds of args to OpenGL. This can be fiddly. The plus side though, is pyglet's bindings tends to be significantly faster.

This implies that there is an optimal middle ground: Use pyglet for windowing, mouse events, sound, etc. Then use PyOpenGL's friendly API when you want to make direct OpenGL function calls. Then when optimising, replace just the small percentage of performance-critical PyOpenGL calls that lie within your inner render loop with the pyglet equivalents. For me, this gives me between 2 and 4 times faster framerates, with PyOpenGL's convenience for 90% of my code.


As Tony said, this is really going to depend on your goals. If you're "tinkering" to try to learn about OpenGL or 3D rendering in general that I would dispense with all pleasantries and start working with PyOpenGL, which is as close are you're going to get to "raw" 3D programming using Python.

On the other hand, if you're "tinkering" by way of mocking up a game or multimedia application, or trying to learn about programming practices in general than Pyglet will save you lots of up-front development time by providing hooks for input events, sounds, text/billboarding, etc. Often, this up-front investment is what prevents people from completing their projects, so having it done for you is not something to be ignored. (It is also very Pythonic to avoid reinventing the wheel.)

If you are looking to do any sort of heavy-duty lifting (which normally falls outside my definition of "tinkering", but maybe not if you're tinkering with 3D engine design) then you might want to take a look at Python-Ogre, which wraps the very full-featured and robust OGRE 3D graphics engine.


Try ModernGL.

pip install ModernGL
  • PyOpenGL is an auto-generated version of the original OpenGL API (generated with SWIG). The original OpenGL API is not python friendly. Generated python bindings are hard to use.

  • pyglet is mostly for the window creation and event handling however you can you a PyOpenGL like API (for example pyglet.gl.glClearColor)

  • pygame provides a window where you can use PyOpenGL to do the rendering.

  • ModernGL is a great alternative to PyOpenGL, You can use the modern OpenGL API with less code written. ModernGL will not create a window by itself, but you can integrate it in pyglet, pygame, PyQt5 and even in GLUT. Edit You can use moderngl-window to create windows independently now.pip install moderngl-window

In ModernGL you can create a simple shader program with a single call:

prog = ctx.program(    vertex_shader='''        #version 330        in vec2 in_vert;        void main() {            gl_Position = vec4(in_vert, 0.0, 1.0);        }    ''',    fragment_shader='''        #version 330        out vec4 f_color;        void main() {            f_color = vec4(0.3, 0.5, 1.0, 1.0);        }    ''',)

With ModernGL you have full control over the OpenGL API.