class FrameBufferObject: def __init__(self, width, height): super().__init__() buffer_format = QOpenGLFramebufferObjectFormat() buffer_format.setAttachment(QOpenGLFramebufferObject.Depth) self._fbo = QOpenGLFramebufferObject(width, height, buffer_format) self._contents = None ## Get the texture ID of the texture target of this FBO. def getTextureId(self): return self._fbo.texture() ## Bind the FBO so it can be rendered to. def bind(self): self._contents = None self._fbo.bind() ## Release the FBO so it will no longer be rendered to. def release(self): self._fbo.release() ## Get the contents of the FBO as an image data object. def getContents(self): if not self._contents: self._contents = self._fbo.toImage() return self._contents
class QtFrameBufferObject(FrameBufferObject): def __init__(self, width, height): super().__init__() buffer_format = QOpenGLFramebufferObjectFormat() buffer_format.setAttachment(QOpenGLFramebufferObject.Depth) self._fbo = QOpenGLFramebufferObject(width, height, buffer_format) self._contents = None def bind(self): self._contents = None self._fbo.bind() def release(self): self._fbo.release() def getTextureId(self): return self._fbo.texture() def getContents(self): if not self._contents: self._contents = self._fbo.toImage() return self._contents
class FrameBufferObject: """An interface for OpenGL FrameBuffer Objects. This class describes a minimal interface that is expected of FrameBuffer Object classes. """ def __init__(self, width: int, height: int) -> None: super().__init__() buffer_format = QOpenGLFramebufferObjectFormat() buffer_format.setAttachment(QOpenGLFramebufferObject.Depth) self._fbo = QOpenGLFramebufferObject(width, height, buffer_format) self._contents = None def getTextureId(self) -> int: """Get the texture ID of the texture target of this FBO.""" return self._fbo.texture() def bind(self) -> None: """Bind the FBO so it can be rendered to.""" self._contents = None self._fbo.bind() def release(self) -> None: """Release the FBO so it will no longer be rendered to.""" self._fbo.release() def getContents(self) -> QImage: """Get the contents of the FBO as an image data object.""" if not self._contents: self._contents = self._fbo.toImage() return self._contents
def createFramebuffer(self, gl, dim, depth=False, filter=None, internalFormat=None, format=None, type=None): ''' Creates framebuffer object with required parameters ''' if filter is None: filter = gl.GL_LINEAR framebuffer = QOpenGLFramebufferObject(dim, dim) if depth: framebuffer.setAttachment(QOpenGLFramebufferObject.Depth) textureId = framebuffer.texture() assert textureId >= 0 gl.glBindTexture(gl.GL_TEXTURE_2D, textureId) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, filter) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, filter) if internalFormat and format and type: gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, internalFormat, dim, dim, 0, format, type, None) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) return framebuffer