def _platform_free(self): """Frees resources associated with this context.""" if self._context and self._context == osmesa.OSMesaGetCurrentContext(): osmesa.OSMesaMakeCurrent(None, None, GL.GL_FLOAT, 0, 0) osmesa.OSMesaDestroyContext(self._context) self._buffer = None self._context = None
def __init__(self, width=1024, height=1024, **kwargs): super(OSMesaRenderingContext, self).__init__(width, height, **kwargs) self.osmesa = osmesa # Now we create our necessary bits. config_attribs = np.array( [ osmesa.OSMESA_DEPTH_BITS, 24, osmesa.OSMESA_STENCIL_BITS, 8, osmesa.OSMESA_FORMAT, osmesa.OSMESA_RGBA, osmesa.OSMESA_PROFILE, osmesa.OSMESA_CORE_PROFILE, 0, ], dtype="i4", ) self.context = osmesa.OSMesaCreateContextAttribs(config_attribs, None) self._buffer = np.zeros((self.height, self.width, 4), dtype="u1") osmesa.OSMesaMakeCurrent(self.context, self._buffer, GL.GL_UNSIGNED_BYTE, self.height, self.width) GL.glClearColor(0.0, 0.0, 0.0, 0.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
def _platform_make_current(self): if self._context: success = osmesa.OSMesaMakeCurrent(self._context, self._buffer, GL.GL_FLOAT, self._width, self._height) if not success: raise RuntimeError('Failed to make OSMesa context current.')
def runglmesa(draw=draw): global ctx if ctx is None: ctx = osmesa.OSMesaCreateContext(osmesa.OSMESA_RGBA, None) buf = arrays.GLubyteArray.zeros((h, w, 4)) assert (osmesa.OSMesaMakeCurrent(ctx, buf, GL_UNSIGNED_BYTE, w, h)) assert (osmesa.OSMesaGetCurrentContext()) initGL() reshape(w, h) display(draw) data = glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE) m = np.frombuffer(data, np.uint8) m = m.reshape(w, h, 4) m = np.flip(m, [0, 1]) return m