{"id":6004,"library":"moderngl","title":"ModernGL","description":"ModernGL is a high-performance Python wrapper over OpenGL Core, simplifying the creation of graphics applications like scientific simulations, games, or user interfaces. It aims to provide a more Pythonic and less boilerplate-heavy API compared to direct OpenGL bindings like PyOpenGL. The current stable version is 5.12.0, with releases occurring periodically, often including breaking changes.","status":"active","version":"5.12.0","language":"en","source_language":"en","source_url":"https://github.com/moderngl/moderngl/","tags":["graphics","opengl","rendering","gpu","visualization","shader","glsl","3d"],"install":[{"cmd":"pip install moderngl","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"moderngl","correct":"import moderngl"},{"symbol":"create_context","correct":"from moderngl import create_context"},{"symbol":"get_context","correct":"from moderngl import get_context"},{"note":"Constants like BLEND, DEPTH_TEST, etc., were moved from the top-level module to the Context object in version 5.9.0.","wrong":"moderngl.BLEND","symbol":"BLEND","correct":"ctx.BLEND"}],"quickstart":{"code":"import moderngl\n\n# Create a headless context or attach to an existing one\n# For a windowed application, ctx = moderngl.create_context()\n# after a window is created by a library like Pygame, GLFW, etc.\nctx = moderngl.create_context(standalone=True)\n\n# Create a buffer on the GPU\nbuf = ctx.buffer(b\"Hello ModernGL World!\")\n\n# Read data back from the buffer\nprint(buf.read())\n\n# Release resources (important for standalone contexts)\n# For windowed contexts, resource management is often handled by the windowing library.\nctx.release()","lang":"python","description":"This quickstart demonstrates creating a ModernGL context (headless in this example), allocating a buffer on the GPU, writing data to it, and reading it back. For graphical applications, `moderngl.create_context()` would typically be called after setting up a window with an OpenGL-capable library."},"warnings":[{"fix":"Upgrade Python to version 3.8 or higher.","message":"Python 3.7 support was removed in ModernGL 5.9.0. Ensure your environment uses Python 3.8 or newer.","severity":"breaking","affected_versions":">=5.9.0"},{"fix":"Access these constants directly from the `Context` instance, e.g., `ctx.BLEND` instead of `moderngl.BLEND`.","message":"Global constants (e.g., `moderngl.BLEND`, `moderngl.DEPTH_TEST`, `moderngl.CULL_FACE`) were moved to the `Context` object in version 5.9.0. Direct access to these from the `moderngl` module will raise an `AttributeError`.","severity":"breaking","affected_versions":">=5.9.0"},{"fix":"If integrating with a window, ensure `create_context()` is called after the window's OpenGL context is active. For headless use, always pass `standalone=True`. Consider `moderngl-window` for simplified window management.","message":"ModernGL context creation (`moderngl.create_context()`) requires an existing OpenGL context (e.g., from a windowing library) or the `standalone=True` flag for headless rendering. Incorrect setup is a common source of errors.","severity":"gotcha","affected_versions":"All"},{"fix":"When creating a context, explicitly require the necessary OpenGL version, e.g., `ctx = moderngl.create_context(standalone=True, require=430)` for compute shaders. Check shader source for syntax errors.","message":"Shader compilation errors, especially with compute shaders, can sometimes lead to silent crashes without clear Python exceptions. This can be due to insufficient OpenGL version support or driver issues.","severity":"gotcha","affected_versions":"All"},{"fix":"Allocate all ModernGL objects at program startup or when resources are first needed, and store references for reuse. Release them when no longer required or at program termination.","message":"OpenGL resources (buffers, textures, programs) should be created once during initialization and reused. Creating them repeatedly (e.g., in a rendering loop) leads to severe performance degradation and memory leaks.","severity":"gotcha","affected_versions":"All"},{"fix":"Sort transparent objects by depth before rendering. Consider alternative blending techniques if sorting is not feasible for complex scenes.","message":"When rendering transparent objects with depth testing and blending enabled, objects must be drawn in a specific order (typically back-to-front relative to the camera) to ensure correct blending results. Incorrect order leads to visual artifacts.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}