ModernGL
pip install ModernGL
Backward compatibility
Install ModernGL 4.x
pip install ModernGL<5.0.0
Please write an issue if your build is still broken.
Pythonic OpenGL and GLSL support
Installation
pip install ModernGL
Description
Why ModernGL?
If you prefer GPU accelerated high quality graphics then you should develop your applications using ModernGL. It is much simpler than PyOpenGL and capable of rendering with the same quality and performace.
- Full linting support - (using vscode and pylint)
- Create GLSL shaders with only a few lines of code
- Create framebuffers and validate them with a single call
- Access cool OpenGL features by writing clean and self-explaining code
- vscode snippets for fast prototyping
- Render to pillow image - (no window required)
Why is it simpler?
With the original OpenGL API you have to write a couple of lines to achieve a simple task like compiling a shader or running a computation on the GPU. With ModernGL you will need just a few lines to achieve the same result.
Using PyOpenGL
vbo1 = glGenBuffers(1)
GL.glBindBuffer(GL_ARRAY_BUFFER, vbo1)
GL.glBufferData(GL_ARRAY_BUFFER, b'Hello World!', GL_STATIC_DRAW)
vbo2 = glGenBuffers(1)
GL.glBindBuffer(GL_ARRAY_BUFFER, vbo2)
GL.glBufferData(GL_ARRAY_BUFFER, b'\x00' * 1024, GL_DYNAMIC_DRAW)
Using ModernGL
vbo1 = ctx.buffer(b'Hello World!')
vbo2 = ctx.buffer(reserve=1024, dynamic=True)
Some cool features
# Read the content
>>> vbo1.read()
b'Hello World!'
# Copy between buffers
>>> ctx.copy_buffer(vbo2, vbo1)
>>> vbo2.read(5)
b'Hello'
# Buffer re-specification
>>> vbo2.orphan()
>>> vbo2.write(b'Some other data')
Is ModernGL faster than PyOpenGL?
In some cases yes, the core functions of ModernGL are written in C++, OpenGL functions are called in quick succession so these calls together count as a single python function call.
What version of OpenGL is used?
Most of the calls only require OpenGL 3.3 but Subroutines and Compute Shaders require OpenGL 4.0 and OpenGL 4.3
Is my old PC supported?
OpenGL 3.3 came out in February 2010. With up to date drivers you will be able to use the most of the ModernGL functions, even on integrated graphics cards. (No, Compute Shaders won't work)
Render to pillow image
size = (256, 256)
fbo = ctx.framebuffer(ctx.renderbuffer(size))
fbo.use()
# Render scene
Image.frombytes('RGB', size, fbo.read(components=3))
Render to pillow image (multisample)
size = (256, 256)
fbo1 = ctx.framebuffer(ctx.renderbuffer(size, samples=8))
fbo2 = ctx.framebuffer(ctx.renderbuffer(size))
fbo1.use()
# Render scene
ctx.copy_framebuffer(fbo2, fbo1)
Image.frombytes('RGB', size, fbo1.read(components=3))
Render to pillow image (larger than screen)
Just change the size to (4096, 4096)
This will require much more video ram.
Working with uniforms and attributes
# The declaration of the program is omitted.
>>> prog.uniforms
{
'Matrix': <Uniform: 0>,
'Lights': <Uniform: 1>,
'Color': <Uniform: 2>,
}
>>> prog.uniforms['Lights'].value
[(0.0, 0.0, 100.0), [(10.0, 10.0, 90.0)]
>>> prog.attributes
{
'vertex': <Attribute: 0>,
'normal': <Attribute: 1>,
'texcoord': <Attribute: 2>,
}
>>> ctx.detect_format(prog, ['vertex', 'normal'])
'4f3f'
>>> color_uniform = prog.uniforms['Color']
>>> color_uniform.value = (0.0, 1.0, 0.0)
Don't read uniform values too often they force some GPUs to sync.
How can I start using ModernGL?
Take a look at the examples and docs.
Where can I use ModernGL?
Anywhere where OpenGL is supported. ModernGL is capable of rendering using a standalone_context as well. Rendering to a window only requires a valid OpenGL context.
Can ModernGL create a Window?
NO, Window creation is up to you. You can choose from a large variety of modules that can create a window: PyQt5, pyglet, pygame, GLUT and many others.
Limitations using ModernGL over PyOpenGL?
All the neccessary calls are (or can be) implemented in ModernGL. However you can interract with the ModernGL objects from PyOpenGL. If something is missing write an issue or raise a PR.
Supported platforms
- Windows
- Linux
- Mac
Installing from source
Installing on Ubuntu from source
apt-get install python3-dev libgl1-mesa-dev libx11-dev
python3 setup.py install
Building the sphinx documentation
pip install -r docs/requirements.txt
python setup.py build_sphinx
Running tests
pytest
Some of the tests may be skipped when the supported OpenGL version is below the requirements of the given test.
Headless rendering
apt-get install xvfb
alias xpy='xvfb-run -s "-screen 0 1x1x24" python3'
xpy -m ModernGL
Code quality
Code is tested with pep8, flake8, prospector and pylint
Community
Contributors
and many others
Thank You!
Contributions are welcome. (Please add yourself to the list)