26

I am looking to do some tinkering with openGL and Python and haven't been able to find good reasons for using PyOpenGl versus pyglet

Which would you recommend and why?

Hortitude
  • 13,638
  • 16
  • 58
  • 72

9 Answers9

33

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.

imyxh
  • 164
  • 10
bouvard
  • 3,864
  • 25
  • 29
33

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.

Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
  • Pyglet's most recent versions (1.3, currently in beta) adds more support for making direct openGL calls, and specifying opengl vertices to pass to Pyglet's batches. – Jonathan Hartley Jan 03 '18 at 16:33
  • Let's say I was planning to integrate my opengl app into a toolkit like wxpython, would it still make sense to go for pyglet, or should I stick to pyopengl? – gmagno Feb 23 '19 at 15:25
  • @gmagno Worth a read : https://mail.python.org/pipermail/python-list/2008-December/468416.html – theGtknerd Feb 23 '19 at 23:48
16

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.

Excalibur
  • 33
  • 1
  • 7
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
  • 3
    Hi, if you're promoting your library, you should at least disclose that you're the author of that library. Considering that the OP is interested in "PyOpenGl or pyglet", this answer looks more like spam than answering the question. – Yakov Galka Dec 04 '20 at 04:33
7

I'd say that Pyglet is actually more evolved than PyOpenGL. It has a nice API of it's own, and it has a full wrapper around OpenGL accessed through the pyglet.gl module! PyOpenGL doesn't even wrap all the functions OpenGL has. Pyglet also has a great library for rendering 2D with hardware acceleration through OpenGL, and it's really well made.

If you want a powerful ready made 3D engine you've got Ogre and such

Zoomulator
  • 20,774
  • 7
  • 28
  • 32
5

Hmm, i would suggest pyglet, it really provides everything needed by a game or application.

Mind you, you can do a lot of things pyglet does with PyOpenGL, for example to create a window all you need to do is:
glutInitWindow(title)

Although i think glutInitDisplayMode has to be called before that.

Simple summary: if you don't wanna code until you cry, choose pyglet, but if you want to be a master, choose PyOpenGL. Goto http://pyopengl.sourceforge.net to read the docs on PyOpenGL and to http://pyglet.org to read the docs on pyglet. Hope this was helpful!

Nathan
  • 9,651
  • 4
  • 45
  • 65
Uchiha Madara
  • 101
  • 1
  • 5
4

pyglet's GL API is nowhere near as nice as PyOpenGL's - pyglet's is at the raw ctypes layer which means you'll need to learn ctypes as well. If you're planning on doing a bunch of OpenGL programming you'll want to use PyOpenGL.

The nice thing is you can mix the two just fine. Use pyglet to provide the GL context, sounds, events, image loading and texture management, text rendering, etc, etc. Use PyOpenGL for the actual OpenGL programming you need to do.

Richard Jones
  • 400
  • 1
  • 5
4

pyglet has a lot of nice extras included with it (like image loading and sound). If you're starting out, I'd try pyglet first, and then switch to PyOpenGL if you feel like you want to get closer to the metal.

The real important question though is: what are you trying to accomplish?

Tony Arkles
  • 1,946
  • 13
  • 14
  • 1
    Hey. Minor refinement: I think it's potentially misleading to describe PyOpenGL as "closer to the metal." It is true that PyOpenGL doesn't offer the same high-level stuff as Pyglet, but on the other hand, the OpenGL bindings provided by PyOpenGL are more friendly, pythonic, and slower than the OpenGL bindings in pyglet. – Jonathan Hartley Nov 24 '10 at 17:54
4

I promote pyglet because it has the nicest API I've yet seen on stuff like this.

Pyglet has opengl API as well. But it's often nicer to use the recently added vertex list support.

pyglet.gl

Cheery
  • 24,645
  • 16
  • 59
  • 83
3

I would recommend Pyglet because it is very easy to get started and have something basic running, and then you can add more advanced techniques at your own pace.

Kiv
  • 31,940
  • 6
  • 44
  • 59