Library / SDK
moderngl/moderngl avatar
moderngl/moderngl

ModernGL: a C++ OpenGL 3.3 core wrapper where one Python call replaces a bind-and-fill sequence

Modern OpenGL binding for Python

2,351 stars243 forksPythonMIT

At a glance

What is it?
moderngl/moderngl wraps OpenGL Core in a compiled extension and exposes a context object that creates buffers, textures and programs. This article covers the install paths, the version floor for compute shaders, the PyOpenGL comparison and what the library leaves unimplemented.
Who is it for?
ModernGL fits Python developers who want OpenGL 3.3 or higher with numpy or Pillow data and a short call per resource, including headless rendering. It does not fit anyone who needs the full OpenGL feature set or older GL versions.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 1 day ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • moderngl/moderngl2,351 ★ · +21
Star history
Repository2026-10-182026-11-25
moderngl/moderngl2.3k2.4k

21 Stars in the last 38 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

A context object turns the bind-and-fill sequence into one call

Everything starts from a context. In the README sample, `moderngl.get_context()` returns `ctx`, and `ctx.buffer(b"Hello World!")` allocates a buffer on the GPU that `buf.read()` returns unchanged. The README's PyOpenGL comparison is concrete: binding a vertex buffer takes `glGenBuffers`, `glBindBuffer` and `glBufferData`, three lines per VBO, while ModernGL uses:

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

The `reserve` argument allocates space without data, and `dynamic=True` replaces the `GL_DYNAMIC_DRAW` flag. The saving is real for resource creation. It says nothing about shader logic, which is still GLSL you write yourself.

The extension is compiled from one C++ file, which sets the install cost

The plain install is one line:

code
pip install moderngl

Behind it, `setup.py` builds an `Extension` named `moderngl.mgl` from `src/moderngl.cpp`, with `-fpermissive` on Windows, Linux, Cygwin and Android and `-Wno-deprecated-declarations` on macOS. The code even checks for `pydroid3` in the Python executable path to select the Android target. Building from source therefore needs a C++ compiler, and a development checkout is made with:

code
git clone [email protected]:moderngl/moderngl.git
cd moderngl
python -m pip install -e .

Setting the `MODERNGL_COVERAGE` environment variable switches the build to `-O0 --coverage`, which is for the test suite and not for use.

OpenGL 3.3 is the floor, and compute shaders need 4.3

The README notes say ModernGL requires OpenGL 3.3, compute shaders require OpenGL 4.3, and some functionality relies on specific extensions. A machine that tops out at OpenGL 3.2 cannot run the library at all, and one that stops at 4.2 can render but cannot use compute.

The repository holds an example called `mesh_shaders.py` and another named `layered_rendering.py`, but the README does not state their version requirements. ModernGL also does not implement the full OpenGL feature set or extensions. You can interact with the ModernGL objects from OpenGL when you need something the wrapper skips, which means mixing in raw GL calls is the escape hatch, not a built-in feature.

The practical check is cheap: create a context on the target machine and read what OpenGL version it reports before designing around any feature. A virtual machine or a remote server with only software rendering can fail that test even when the hardware underneath could pass it.

Windows come from moderngl-window and headless contexts from glcontext

ModernGL does not create windows. The README sends you to two extras: `pip install moderngl-window` for window creation and resource loading, and `pip install moderngl[headless]` for glcontext, which creates contexts without a window.

code
pip install moderngl[headless]

The library can be integrated into any window library that supports OpenGL, and moderngl-window implements many of them. `examples/moderngl_window_empty.py` and `examples/moderngl_window_fractal.py` show that path, and `examples/headless_moderngl_logo.py` shows rendering with no window. The README says ModernGL works in a headless environment and can be used anywhere OpenGL is supported, which is a claim about OpenGL support on the machine, not a tested platform list.

The split has a cost. A project that needs a window, input handling and a render loop pulls in a second package with its own releases, and the README leaves the pairing of versions between ModernGL and moderngl-window undocumented. Check the moderngl-window repository for its own requirements before you pin either one.

Pillow and numpy data go straight into textures and buffers

Two README snippets show the data path. A Pillow image converted to RGB feeds a texture with `ctx.texture(img.size, 3, img.tobytes())`, where the second argument is the component count. A numpy array of dtype `f4` goes into `ctx.buffer(np.array([0.0, 0.0, 1.0, 1.0], dtype="f4"))`.

In both cases the data is passed as bytes, so you control layout and dtype. A float64 array passed where the shader expects `f4` is your bug to find. The repository also has `examples/08_texture.py` and `examples/15_pillow_overlay.py` for the Pillow path. The `moderngl-stubs/` directory ships type stubs, so editors can check these calls.

One more item on the data path: `examples/external_buffer.py` and `examples/external_texture.py` exist for wrapping buffers and textures that were created outside ModernGL, which is how the library meets code that already holds raw OpenGL objects.

The performance claims are the author's and the README hedges them

The feature list says ModernGL is simpler and faster than PyOpenGL. The Notes section softens that: ModernGL may be faster than other libraries providing direct OpenGL access. The stated reason is the C++ implementation, where a single render call involving multiple OpenGL functions counts as a single Python function call.

No benchmark is published in the README, so the speed difference is an architectural argument and not a measured number. The README also calls itself high performance while describing the entire library as responsible for calling the OpenGL API and providing a Pythonic API instead. If a render loop makes thousands of tiny draw calls, profile it yourself, because the repository gives no figures for that case.

The README stops at GLSL basics, and the fifteen numbered examples carry the rest

The README includes a short GLSL primer: data types, attributes, varyings, uniforms, vertex and fragment shaders, and a `#version 330 core` pair that transforms vertices with `modelViewProjection` and fills pixels white. It does not teach buffer layout, vertex array objects or framebuffer use.

The numbered examples do that work, from `01_hello_world.py` through `02_shader.py`, `03_camera.py`, `05_uniforms.py`, `10_lighting.py` and `14_postprocessing.py` up to `15_pillow_overlay.py`. Release 5.13.0 landed on 7 October 2026, after 5.12.0 on 17 October 2024 and 5.11.1 on 13 August 2024, and the last push was on 21 October 2026. The package classifier is Production/Stable.

Editorial conclusion

ModernGL fits Python developers who want OpenGL 3.3 or higher with numpy or Pillow data and a short call per resource, including headless rendering. It does not fit anyone who needs the full OpenGL feature set or older GL versions. Before committing, confirm that your target machines expose OpenGL 3.3, and 4.3 if you plan to use compute shaders.

Frequently asked questions

How do I install ModernGL?

Run pip install moderngl. For windowless contexts use pip install moderngl[headless], and for window creation use pip install moderngl-window.

What is ModernGL?

ModernGL is a Python wrapper over OpenGL Core, implemented in C++, for building graphics applications such as scientific simulations, games or user interfaces. It requires OpenGL 3.3.

How does ModernGL compare with OpenGL through PyOpenGL?

The README shows PyOpenGL needing three lines per VBO, where ModernGL needs one ctx.buffer call. It also says ModernGL may be faster, without publishing benchmark numbers.

Official sources

  1. License: MIT
  2. moderngl/moderngl on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/moderngl-moderngl.svg)](https://hysenlabs.com/projects/moderngl-moderngl)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/moderngl-moderngl.svg)](https://hysenlabs.com/projects/moderngl-moderngl)

Was this useful?