Package: glcontext
glcontextglcontextis a library providing OpenGL implementation for ModernGL on multiple platforms.glcontext on githubglcontext on pypiModernGLBackendsA glcontext backend is either an extension or a submodule of the glcontext package.
The package itself does not import any of the backends.
Importing the base packageglcontextmust safe and lightweight.StructureEvery backend of glcontext must provide a factory function:defcreate_context(*args,**kwargs)->GLContext:passThe create_context method can take any number of positional and keyword arguments.
The factory function must return an object supporting the following methods:defload(self,name:str)->int:passThe load method takes an OpenGL function name as an input and returns a C/C++ function pointer as a python integer.
The return value must be 0 for not implemented functions.def__enter__(self,name:str):passThe enter method calls___MakeCurrentto make the GLContext the calling thread's current rendering context.___MakeCurrentstands forwglMakeCurrent,glxMakeCurrent, ...def__exit__(self,exc_type,exc_val,exc_tb):passThe exit method calls___MakeCurrentto make the GLContext no longer current.defrelease(self):passThe release method destroys the OpenGL context.Development GuideThere are "empty" example backends provided for developers to help adding new backends to the library.
There is a pure python example inempty.pyand an extension example inempty.cpp.
Besides their name match, they do not depend on each other, they are independent submodules of glcontext.An "portable" backend implementation must load its dependency at runtime.
This rule is for simplifying the build of the entire package.
If an implementation cannot provide a "portable" backend, it will not be added to this library.
Non "portable" backends are welcome as third party libraries.A backend must be lightweight, its size must fit within reasonable limits.To add support for new platforms one must edit thesetup.pytoo.
Platform specific dependencies are exceptions from the "portability" rule.Example for platform specific dependencies:gdi32.libon windowslibdl.aon linuxPlease note thatlibGL.sois loaded dinamically by the backends.Current backendsEach backend supports aglversionandmodeparameters as a minimum.
Theglversionis the minimum OpenGL version required whilemodedecides how the context is created.Modesdetect: Will detect an existing active OpenGL context.standalone: Crates a headless OpenGL contextshare: Creates a new context sharing objects with the currently active context (headless)wglParametersglversion(int): The minimum OpenGL version for the contextmode(str): Creation mode.detect|standalone|sharelibgl(str): Name of gl library to load (default:opengl32.dll)x11Iflibglis not passed in the backend will try to locate
the GL library usingctypes.utils.find_library.Parametersglversion(int): The minimum OpenGL version for the contextmode(str): Creation mode.detect|standalone|sharelibgl(str): Name of gl library to load (default:libGL.so)libx11(str): Name of x11 library to load (default:libX11.so)darwinWill create the the highest core context available.Parametersmode(str): Creation mode.detect|standaloneeglOnly supports standalone mode.Iflibgland/orlibeglis not passed in the backend will try to locate
GL and/or EGL library usingctypes.utils.find_library.Parametersglversion(int): The minimum OpenGL version for the contextmode(str): Creation mode.standalonelibgl(str): Name of gl library to load (default:libGL.so)libegl(str): Name of gl library to load (default:libEGL.so)device_index(int) The device index to use (default:0)Environment VariablesEnvironment variables can be set to configure backends.
These will get first priority if defined.# Override OpenGL version code. For example: 410 (for opengl 4.1)GLCONTEXT_GLVERSION# Override libgl on linux. For example: libGL.1.soGLCONTEXT_LINUX_LIBGL# Override libx11 on linux. For exampleØ libX11.x.soGLCONTEXT_LINUX_LIBX11# Override libegl on linux. For exampleØ libEGL.x.soGLCONTEXT_LINUX_LIBEGL# Override gl dll on windows. For example: opengl32_custom.dllGLCONTEXT_WIN_LIBGL# Override the device index (egl)GLCONTEXT_DEVICE_INDEXRunning testspip install -r tests/requirements.txt
pytest testsContributingContribution is welcome.Pull Requests will be merged if they match theDevelopment Guide.For prototypes, pure python implementations using ctypes are also welcome.
We will probably port it to a proper extension in the future.Please ask questionshere.