Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1275

Python • Pyglet + ModernGL + Raspberry Pi 5

$
0
0
I recently tried Raspberry Pi for the first time, and I tried to make a simple app using ModernGL as the rendering backend, and ModernGL Window to show the app display.
ModernGL Window uses Pyglet as default.

But I had some hard time getting it the code to run. No shader errors, some error messages from Pyglet saying that it had failed to create context. I came across this post, which was having the same errors as me. viewtopic.php?t=355857

I want to share a few things that helped me to get it working.
There are a lot of posts here that tells to use the raspi-config command to set the OpenGL configuration, but Raspberry Pi 5 does not have the menu that these posts mentioned. So that did not work for me.

So this is for people who are
- using Raspberry Pi 5
- using Pyglet for creating window
- using ModernGL as the rendering backend

First, this article has helped me a lot https://pyglet.readthedocs.io/en/develo ... ngles.html
If you install Pyglet on Raspberry Pi 5 and run below code, it will fail saying that it has failed to create context.

If you look at the code included in the article, there were two parts that were very relevant to my problem

Code:

pyglet.options.shadow_window = False

Code:

config.opengl_api = "gles"
In short, Raspberry Pi 5 does not seem to support shadow window, and it supports OpenGL ES, so these settings have to be explicitly set.

But if you look at the post link first mentioned, you can see that the writer actually put 'gles' as the config option for pyglet window, but still got the same error.

Code:

window = pyglet.window.Window(width=300, height=300, vsync=True, config=gl.Config(opengl_api='gles'))
The 'gl.Config' is 'pyglet.gl.Config', and this part caused context creation error for me too.
It seems that OpenGL ES configuration should be managed seperately from this module, and did not cause error when it was set like the code from pyglet docs like below. I changed the minor version number to 0 because that was the one that worked for me. Also, if you want to actually see the window, you should add the last line as well.

Code:

import pyglet# Disable shadow windowpyglet.options.shadow_window = False# Query the best config for the screendisplay = pyglet.display.get_display()screen = display.get_default_screen()config = screen.get_best_config()# Specify that we want to use OpenGL ES 3.1# This is very specific to the Raspberry Pi 4 and 5. Use 3.2 if you can.config.opengl_api = "gles"config.major_version = 3config.minor_version = 0 # changed# Create the windowwindow = pyglet.window.Window(config=config)pyglet.app.run() # If you want to actually see the window on your computer
In my case, I was trying to use moderngl window library to use moderngl with pyglet. You can see in the source code that the library does not have a way for a developer to set the "opengl_api" config as they please. It is also using

Code:

pyglet.gl.Config
to set the config for the window. When I changed the code so that it would use OpenGL ES, the app started working as intended.

Also, now that the code uses OpenGL ES 3.0, all shader code should have the following code at the top.

Code:

#version 300 esprecision highp float;

Statistics: Posted by hlp_pls — Wed Jan 22, 2025 8:16 am



Viewing all articles
Browse latest Browse all 1275

Trending Articles