moderngl 5.12.0


pip install moderngl

  Latest version

Released: Oct 17, 2024


Meta
Author: Szabolcs Dombi
Requires Python: >=3.7

Classifiers

Development Status
  • 5 - Production/Stable

License
  • OSI Approved
  • OSI Approved :: MIT License

Operating System
  • OS Independent

Topic
  • Games/Entertainment
  • Multimedia :: Graphics
  • Multimedia :: Graphics :: 3D Rendering
  • Scientific/Engineering :: Visualization

Programming Language
  • Python :: 3 :: Only

preview

ModernGL

ModernGL is a Python wrapper over OpenGL Core. ModernGL simplifies the creation of graphics applications like scientific simulations, games or user interfaces. Usually, acquiring in-depth knowledge of OpenGL requires a steep learning curve. In contrast, ModernGL is easy to learn and use. ModernGL is capable of rendering with high performance and quality, with less code written.

pip install moderngl

Extras

glcontext - Headless Context Creation

pip install moderngl[headless]

moderngl-window - Window Creation and Resource Loading

pip install moderngl-window

Features

  • GPU accelerated high quality graphics
  • Rendering modern OpenGL scenes with less headache
  • Simpler and faster than PyOpenGL
  • Can render without a window
  • 100% Pythonic

Sample usage

>>> import moderngl
>>> ctx = moderngl.get_context()
>>> buf = ctx.buffer(b"Hello World!")  # allocated on the GPU
>>> buf.read()
b'Hello World!'

For complete examples please visit the Examples.

Easy to use with Pillow

>>> img = Image.open("texture.jpg").convert("RGB")
>>> ctx.texture(img.size, 3, img.tobytes())
<Texture: 1>

Easy to use with Numpy

>>> ctx.buffer(np.array([0.0, 0.0, 1.0, 1.0], dtype="f4"))
<Buffer: 1>

Compared to PyOpenGL

With PyOpenGL, using the original OpenGL API, you have to write three lines to achieve a simple task like binding a VBO:

vbo1 = GL.glGenBuffers(1)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo1)
GL.glBufferData(GL.GL_ARRAY_BUFFER, b"Hello World!", GL.GL_STATIC_DRAW)

vbo2 = GL.glGenBuffers(1)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo2)
GL.glBufferData(GL.GL_ARRAY_BUFFER, None, GL.GL_DYNAMIC_DRAW)

With ModernGL you need just one simple line per VBO to achieve the same results:

vbo1 = ctx.buffer(b"Hello World!")
vbo2 = ctx.buffer(reserve=1024, dynamic=True)

Development

git clone git@github.com:moderngl/moderngl.git
cd moderngl
python -m pip install -e .

Using GLSL in ModernGL

GLSL (OpenGL Shading Language) is integral for shader programming using the ModernGL library. It allows developers to execute code on the GPU, enhancing graphics rendering performance. This concise introduction covers the core concepts necessary for starting with GLSL in ModernGL.

Data Types

GLSL offers standard data types similar to C, including int, float, double, and bool, as well as graphics-specific types like vectors (vec2, vec3, vec4) and matrices (mat2, mat3, mat4) for efficient graphics computations. Inputs and Outputs

  Attributes: Per-vertex data passed to the vertex shader. Commonly used for positions, normals, and texture coordinates.
  Varyings: Interpolated data passed from the vertex to the fragment shader, such as colors or texture coordinates.
  Output: Fragment shader's output, typically the color of the pixel (fragColor).

Uniforms

Uniforms are global variables declared in shaders, constant for all vertices or fragments for a single draw call. They're ideal for passing data from your application, like transformation matrices or material properties, to the shader. Shader Types

  Vertex Shader: Processes each vertex's attributes. It's the first stage in the shader pipeline, used for transformations and passing data to the fragment shader.
  Fragment Shader: Calculates the color of each pixel. It uses data interpolated from the vertex shader to apply textures, lighting, and color.
  Geometry Shader: Processes primitives (points, lines, triangles) formed by vertices from the vertex shader. It can add or remove vertices from the primitive.
  Tessellation Shaders: Control the tessellation of patches, allowing for smoother geometries at varying distances.
  Compute Shader: Handles general-purpose computing tasks not directly related to rendering images, like physics simulations or post-processing effects.

Example

glsl

// Vertex Shader
#version 330 core
in vec3 position;
uniform mat4 modelViewProjection;
void main() {
    gl_Position = modelViewProjection * vec4(position, 1.0);
}

// Fragment Shader
#version 330 core
out vec4 fragColor;
void main() {
    fragColor = vec4(1.0); // Set pixel color to white
}

This basic example demonstrates a vertex shader transforming vertex positions with a matrix and a fragment shader setting the color of each pixel to white.

Notes

ModernGL may be faster than other libraries providing direct OpenGL access. ModernGL is implemented in C++ and a single render call involving multiple OpenGL functions count as a single Python function call.

ModernGL require OpenGL 3.3. Compute Shaders require OpenGL 4.3. Some functionality relies on specific extensions.

ModernGL can be used anywhere where OpenGL is supported. ModernGL is also working in a headless environment.

ModernGL is responsible for calling the OpenGL API and providing a Pythonic user-friendly API instead. It is possible to integrate moderngl into any window libraries that support OpenGL. Consider moderngl-window which implements many of them, plus it also helps with resource loading.

ModernGL does not implement the full OpenGL feature set or extensions. You can interact with the ModernGL objects from OpenGL.

Citation

If you need to cite this repository in academic research:

@Online{Dombi2020,
  author = {Szabolcs Dombi},
  title = {ModernGL, high performance python bindings for OpenGL 3.3+},
  date = {2020-05-01},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/moderngl/moderngl}},
  commit = {<insert hash if needed>}
}

The commit hash can be found in the Releases.

Community

5.12.0 Oct 17, 2024
5.11.1 Aug 13, 2024
5.11.0 Aug 10, 2024
5.10.0 Jan 22, 2024
5.9.0 Nov 18, 2023
5.8.2 Mar 26, 2023
5.8.1 Feb 27, 2023
5.8.0 Feb 25, 2023
5.7.4 Dec 11, 2022
5.7.3 Nov 28, 2022
5.7.2 Nov 07, 2022
5.7.1 Nov 06, 2022
5.7.0 Oct 26, 2022
5.6.4 Feb 19, 2021
5.6.3 Jan 31, 2021
5.6.2 Sep 08, 2020
5.6.1 Jun 09, 2020
5.6.0 Feb 01, 2020
5.5.4 Nov 10, 2019
5.5.3 Aug 16, 2019
5.5.2 Jun 13, 2019
5.5.1 Jun 12, 2019
5.5.0 Jan 09, 2019
5.4.2 Aug 01, 2018
5.4.1 Jul 30, 2018
5.4.0 Jul 30, 2018
5.3.0 Jun 27, 2018
5.2.1 May 10, 2018
5.2.0 May 10, 2018
5.1.0 Apr 28, 2018
5.0.7 Apr 08, 2018
5.0.6 Mar 22, 2018
5.0.5 Mar 21, 2018
5.0.4 Mar 07, 2018
5.0.3 Feb 28, 2018
5.0.2 Feb 20, 2018
5.0.1 Feb 18, 2018
5.0.0 Feb 18, 2018
4.2.2 Jan 06, 2018
4.2.1 Nov 27, 2017
4.2.0 Oct 16, 2017
4.1.12 Aug 22, 2017
4.1.11 Jul 02, 2017
4.1.10 Jun 23, 2017
4.1.9 Jun 23, 2017
4.1.8 Jun 19, 2017
4.1.7 Jun 13, 2017
4.1.6 Jun 12, 2017
4.1.4 Jun 07, 2017
4.1.3 Jun 04, 2017
4.1.2 May 31, 2017
4.1.1 May 27, 2017
4.1.0 May 24, 2017
4.0.0 May 20, 2017
3.1.5 May 08, 2017
3.1.4 Apr 08, 2017
3.1.3 Apr 06, 2017
3.1.2 Mar 26, 2017
2.4.1 Aug 30, 2016
2.4.0 Aug 26, 2016
2.3.0 Aug 01, 2016
2.2.1 Jul 16, 2016
2.2.0 Jul 09, 2016
2.1.2 Jul 08, 2016
2.1.1 Jul 06, 2016
2.1.0 Jul 05, 2016
2.0.11 Jul 02, 2016
2.0.10 Jun 29, 2016
2.0.9 Jun 28, 2016
2.0.8 Jun 27, 2016
2.0.7 Jun 27, 2016
2.0.6 Jun 24, 2016
2.0.5 Jun 23, 2016
2.0.4 Jun 23, 2016
2.0.3 Jun 21, 2016
2.0.2 Jun 21, 2016
0.9.4 Jun 13, 2016
0.9.3 Jun 11, 2016
0.9.2 Jun 08, 2016
0.9.1 Jun 08, 2016
0.8.8 May 29, 2016
0.8.7 May 27, 2016
0.8.6 May 27, 2016
0.8.5 May 27, 2016
0.8.4 May 27, 2016
0.8.3 May 27, 2016
0.8.2 May 27, 2016
0.8.1 May 27, 2016
2.4.1.win32 Aug 30, 2016
2.4.1.win Aug 30, 2016
2.4.0.win32 Aug 26, 2016
2.4.0.win Aug 26, 2016
2.3.0.win32 Aug 01, 2016
2.3.0.win Aug 01, 2016
2.2.1.win32 Jul 16, 2016
2.2.1.win Jul 16, 2016
2.2.0.win32 Jul 09, 2016
2.2.0.win Jul 09, 2016
2.1.2.win32 Jul 08, 2016
2.1.2.win Jul 08, 2016
2.1.2.linux Jul 08, 2016
2.1.1.win32 Jul 06, 2016
2.1.1.win Jul 06, 2016
2.1.0.win32 Jul 05, 2016
2.1.0.win Jul 05, 2016
2.0.9.win32 Jun 28, 2016
2.0.9.win Jun 28, 2016
2.0.8.win32 Jun 27, 2016
2.0.8.win Jun 27, 2016
2.0.7.win32 Jun 27, 2016
2.0.7.win Jun 27, 2016
2.0.6.win32 Jun 24, 2016
2.0.6.win Jun 24, 2016
2.0.5.win32 Jun 23, 2016
2.0.5.win Jun 23, 2016
2.0.4.win32 Jun 23, 2016
2.0.4.win Jun 23, 2016
2.0.3.win32 Jun 21, 2016
2.0.3.win Jun 21, 2016
2.0.2.win32 Jun 21, 2016
2.0.2.win Jun 21, 2016
2.0.11.win32 Jul 02, 2016
2.0.11.win Jul 02, 2016
2.0.10.win32 Jun 29, 2016
2.0.10.win Jun 29, 2016
0.9.4.win32 Jun 13, 2016
0.9.4.win Jun 13, 2016
0.9.3.win32 Jun 11, 2016
0.9.3.win Jun 11, 2016
0.9.2.win32 Jun 08, 2016
0.9.2.win Jun 08, 2016
0.9.1.win32 Jun 08, 2016
0.9.1.win Jun 08, 2016

Wheel compatibility matrix

Platform CPython 3.8 CPython 3.9 CPython 3.10 CPython 3.11 CPython 3.12 CPython 3.13
macosx_10_13_x86_64
macosx_10_9_x86_64
macosx_11_0_arm64
manylinux1_i686
manylinux2014_i686
manylinux2014_x86_64
manylinux_2_17_i686
manylinux_2_17_x86_64
manylinux_2_5_i686
musllinux_1_2_i686
musllinux_1_2_x86_64
win32
win_amd64

Files in release

moderngl-5.12.0-cp310-cp310-macosx_10_9_x86_64.whl (109.2KiB)
moderngl-5.12.0-cp310-cp310-macosx_11_0_arm64.whl (106.7KiB)
moderngl-5.12.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (284.6KiB)
moderngl-5.12.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (259.2KiB)
moderngl-5.12.0-cp310-cp310-musllinux_1_2_i686.whl (1.3MiB)
moderngl-5.12.0-cp310-cp310-musllinux_1_2_x86_64.whl (1.2MiB)
moderngl-5.12.0-cp310-cp310-win32.whl (98.7KiB)
moderngl-5.12.0-cp310-cp310-win_amd64.whl (105.7KiB)
moderngl-5.12.0-cp311-cp311-macosx_10_9_x86_64.whl (109.2KiB)
moderngl-5.12.0-cp311-cp311-macosx_11_0_arm64.whl (106.7KiB)
moderngl-5.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (286.6KiB)
moderngl-5.12.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (261.1KiB)
moderngl-5.12.0-cp311-cp311-musllinux_1_2_i686.whl (1.3MiB)
moderngl-5.12.0-cp311-cp311-musllinux_1_2_x86_64.whl (1.2MiB)
moderngl-5.12.0-cp311-cp311-win32.whl (98.7KiB)
moderngl-5.12.0-cp311-cp311-win_amd64.whl (105.7KiB)
moderngl-5.12.0-cp312-cp312-macosx_10_13_x86_64.whl (109.5KiB)
moderngl-5.12.0-cp312-cp312-macosx_11_0_arm64.whl (106.8KiB)
moderngl-5.12.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (289.4KiB)
moderngl-5.12.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (264.2KiB)
moderngl-5.12.0-cp312-cp312-musllinux_1_2_i686.whl (1.3MiB)
moderngl-5.12.0-cp312-cp312-musllinux_1_2_x86_64.whl (1.2MiB)
moderngl-5.12.0-cp312-cp312-win32.whl (98.8KiB)
moderngl-5.12.0-cp312-cp312-win_amd64.whl (106.0KiB)
moderngl-5.12.0-cp313-cp313-macosx_10_13_x86_64.whl (109.5KiB)
moderngl-5.12.0-cp313-cp313-macosx_11_0_arm64.whl (106.8KiB)
moderngl-5.12.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (289.3KiB)
moderngl-5.12.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (264.1KiB)
moderngl-5.12.0-cp313-cp313-musllinux_1_2_i686.whl (1.3MiB)
moderngl-5.12.0-cp313-cp313-musllinux_1_2_x86_64.whl (1.2MiB)
moderngl-5.12.0-cp313-cp313-win32.whl (98.8KiB)
moderngl-5.12.0-cp313-cp313-win_amd64.whl (106.0KiB)
moderngl-5.12.0-cp38-cp38-macosx_10_9_x86_64.whl (109.0KiB)
moderngl-5.12.0-cp38-cp38-macosx_11_0_arm64.whl (106.5KiB)
moderngl-5.12.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (287.2KiB)
moderngl-5.12.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (262.4KiB)
moderngl-5.12.0-cp38-cp38-musllinux_1_2_i686.whl (1.3MiB)
moderngl-5.12.0-cp38-cp38-musllinux_1_2_x86_64.whl (1.2MiB)
moderngl-5.12.0-cp38-cp38-win32.whl (98.7KiB)
moderngl-5.12.0-cp38-cp38-win_amd64.whl (105.8KiB)
moderngl-5.12.0-cp39-cp39-macosx_10_9_x86_64.whl (109.2KiB)
moderngl-5.12.0-cp39-cp39-macosx_11_0_arm64.whl (106.7KiB)
moderngl-5.12.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (283.1KiB)
moderngl-5.12.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (258.1KiB)
moderngl-5.12.0-cp39-cp39-musllinux_1_2_i686.whl (1.3MiB)
moderngl-5.12.0-cp39-cp39-musllinux_1_2_x86_64.whl (1.2MiB)
moderngl-5.12.0-cp39-cp39-win32.whl (98.8KiB)
moderngl-5.12.0-cp39-cp39-win_amd64.whl (106.0KiB)
moderngl-5.12.0.tar.gz (188.7KiB)
Extras:
Dependencies:
glcontext (>=3.0.0)