pip install 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)
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.
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)
vbo1 = ctx.buffer(b'Hello World!')
vbo2 = ctx.buffer(reserve=1024, dynamic=True)
# 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')
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.
Most of the calls only require OpenGL 3.3 but Subroutines and Compute Shaders require OpenGL 4.0 and OpenGL 4.3
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)
size = (256, 256)
fbo = ctx.framebuffer(ctx.renderbuffer(size))
fbo.use()
# Render scene
Image.frombytes('RGB', size, fbo.read(components=3))
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))
Just change the size to (4096, 4096)
This will require much more video ram.
# 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.
Take a look at the examples and docs.
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.
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.
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.
- Windows
- Linux
- Mac
apt-get install python3-dev libgl1-mesa-dev libx11-dev
python3 setup.py install
pip install -r docs/requirements.txt
python setup.py build_sphinx
pytest
Some of the tests may be skipped when the supported OpenGL version is below the requirements of the given test.
Code is tested with pep8, flake8, prospector and pylint
and many others
Thank You!
Contributions are welcome. (Please add yourself to the list)