PyOpenGl or pyglet? [closed]

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?


Asked by: Cadie915 | Posted: 05-10-2021






Answer 1

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.

Answered by: Tess822 | Posted: 06-11-2021



Answer 2

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.

Answered by: Melanie559 | Posted: 06-11-2021



Answer 3

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.

Answered by: Edward654 | Posted: 06-11-2021



Answer 4

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

Answered by: Fiona188 | Posted: 06-11-2021



Answer 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!

Answered by: Caroline470 | Posted: 06-11-2021



Answer 6

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?

Answered by: Tess540 | Posted: 06-11-2021



Answer 7

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

Answered by: Grace247 | Posted: 06-11-2021



Answer 8

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.

Answered by: Alberta882 | Posted: 06-11-2021



Answer 9

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.

Answered by: Melissa255 | Posted: 06-11-2021



Similar questions

python - Trying to Graph a Simple Square in pyOpenGL

I'm trying to teach myself OpenGL using pyopengl and I'm struck on trying to render a simple 2D square centered at the origin. Whenever I set an array value greater than or equal to 1, the shape takes up the entire screen as if I am only viewing a small section of the axis. I have tried to base it off the NeHe tutorials rewritten in pyopengl but I can't find what I'm doing wrong. from OpenGL.GL import * fro...


python - pyopengl glut input

After the window containing GLUT graphics appears, I would like to enter input in the terminal: user@computer: python woop.py # Now displaying a beautiful landscape (cmd): season winter # Now changing season to winter (cmd): event meteor # Now meteoring otherwise peaceful landscape (cmd): season summer # Now changing season to summer (cmd): exit #bye ^_^ user@computer: Ideally I would like...


python - pyOpenGL VBOs with index

I want to draw a Rectangle in pyOpenGL using VBOs with indices. I am using the glDrawRangeElements() function for that but I always get the same mistake in the line glDrawRangeElements: WindowsError: exception: access violation reading 0x00000000 I tried a lot of things and was looking on the internet for the solution and was studying code examples all day so now I really do not know how to go on. It would ...


python - Can normal algos run on PyOpenGL?

I want to write an algorithm that would benefit from the GPU's superior hashing capability over the CPU. Is PyOpenGL the answer? I don't want to use drawing tools, but simply run a "vanilla" python script ported to the GPU. I have an ATI/AMD GPU if that means anything.


python - How to enable vsync in PyOpengl?

As the title says really. I'd like to turn on vertical sync in PyOpenGL, but how can I do it? A fairly exhaustive web search didn't turn up anything, but maybe someone has a clever solution? I'm on OS X and I don't mind which package is used to create the window and the application loop. However, I'd rather stay away from developing a full-blown Cocoa app, for reasons discussed below. I looked into using pyglet rat...


python - Can not import PyOpenGL

I am trying to install the PyOpenGL module on my mac. But when I run pip install PyOpenGL PyOpenGL_accelerate I get: Requirement already satisfied (use --upgrade to upgrade): PyOpenGL in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyOpenGL-3.1.0a3-py2.7.egg Requirement already satisfied (use --upgrade to upgrade): PyOpenGL-accelerate in /Library/Frame...


python - 64-bit PyOpenGL not able to use single buffer?

I recently upgraded to a 64-bit machine, and installed PyOpenGL 64 bit. I'm using the 3.1.0a1 version of PyOpenGL, with Python 2.7.6. My old code has stopped working in single buffer mode. I've also tried PyOpenGL 3.0.2, and it doesn't make a difference. So this code here: from OpenGL.GL import * from OpenGL.GLUT import * def display(): glClearColor(1.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_B...


python - Can't pass data in vertex shader using pyopengl

I have a problem with passing data to vertex shader. When I try to pass data to vertex shader it's simply not passed at all. My PC spec needed: OS: Windows 8 64-bit Python: 2.7.6 64-bit PyOpenGL: 3.0.2 64-bit NumPy: 1.8.0 64-bit unofficial (but not the source of the problem) Graphics Card: Radeon HD 8330 Code: import sys import os import numpy from mat...


python - py2exe and PyOpenGL

I have tried using py2exe to make an executable of my PyOpenGL project, and I did... The problem is, it is not running... The exe is not running and i am getting this log: Traceback (most recent call last): File "gl.py", line 1, in <module> ImportError: No module named OpenGL.GL setup.py looks like this: from distutils.core import setup import py2exe setup(windows=['...


python - Why I cannot rotate my cube in PyOpenGL?

I am trying to rotate my cube when I pushed the key 'r' (keyevent), I am using Pyopengl, but I don't know why this doesn't it. This is a part of my code. def init(): glMatrixMode (GL_PROJECTION) glClearColor(0.529, 0.529, 0.529, 0.0)#Definimos color de ventana glColor3f(0.0, 1.0, 1.0)#Definimos color inicial para el cubo def dibujarCubo(): glClear (GL_COLOR_BUFFER_BIT) glColor3f (1...


python - Render PyCairo onto PyOpenGL surface?

I've recently started playing with pycairo - is it easy enough to render this to an pyopengl surface (e.g. on the side of a cube?)... my opengl is really non-existant so I'm not sure the best way to go about this.


python - pyopengl display blocked when acting as a socket server at the same time

I am actually using a pyopengl program to act as a socket server. At the same time the server receives commands from clients and interprets these commands and does corresponding drawing at the same time. In case that the main thread got blocked when doing socket things I actually started a thread to do the socket server accept thing, and the updateserver socket method is called in the display loop. ...


python - problem with pyopengl in linux

I am a beginner. I want to use pyopengl in linux but I have a problem. I try to import pyopengl module as below : from OpenGl.GL import * but interpreter shows this error message : Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named OpenGl.Gl what should I do?


python - Trying to Graph a Simple Square in pyOpenGL

I'm trying to teach myself OpenGL using pyopengl and I'm struck on trying to render a simple 2D square centered at the origin. Whenever I set an array value greater than or equal to 1, the shape takes up the entire screen as if I am only viewing a small section of the axis. I have tried to base it off the NeHe tutorials rewritten in pyopengl but I can't find what I'm doing wrong. from OpenGL.GL import * fro...


python - Using offsets into the buffer in PyOpenGL calls

In OpenGL the indices parameter for glDrawElements has two different meanings, depending on if you are using GL_ELEMENT_ARRAY_BUFFER or not. If you have a VBO bound, then it is the offset to start in that buffer, rather than the buffer itself. When using Py...


python - How to PyOpengl or pyglet to draw on top of desktop?

I'm writing an application that is looking to draw basic polygons and ellipses on the Windows 7 desktop using OpenGL. According to this previous question, this is possibly by getting the window handle to the desktop, which I know how to do. Draw OpenGL on the windows desktop without a window However, I've g...


python - pyopengl glut input

After the window containing GLUT graphics appears, I would like to enter input in the terminal: user@computer: python woop.py # Now displaying a beautiful landscape (cmd): season winter # Now changing season to winter (cmd): event meteor # Now meteoring otherwise peaceful landscape (cmd): season summer # Now changing season to summer (cmd): exit #bye ^_^ user@computer: Ideally I would like...


python - pyOpenGL VBOs with index

I want to draw a Rectangle in pyOpenGL using VBOs with indices. I am using the glDrawRangeElements() function for that but I always get the same mistake in the line glDrawRangeElements: WindowsError: exception: access violation reading 0x00000000 I tried a lot of things and was looking on the internet for the solution and was studying code examples all day so now I really do not know how to go on. It would ...


python - pyopengl average two+ textures at runtime

I have a pyopengl program that blends a few images at runtime then applies them to some simple geometry, it works but is extremely slow, slower than i know it can be. The following code is what im using where image'n'_weight varies at runtime. img1 = PIL.Image.open(file_name1) image1 = numpy.array(list(img.getdata()), numpy.uint8) img2 = PIL.Image.open(file_name2) image2 = numpy.array(list...


python - why wont this code display my texture in pyopenGL

I'm trying to get multipass texturing work for me but right now i can get simple texturing to work, can anybody spor whats going wring with the following code. This examples just shows the poly as white, rather than textured. def loadTexture(name): img = PIL.Image.open(name) # .jpg, .bmp, etc. also work img_data = numpy.array(list(img.getdata()), numpy.int8) id = glGenTextures(1) glPixelS...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top