- class QOpenGLFramebufferObject¶
The
QOpenGLFramebufferObject
class encapsulates an OpenGL framebuffer object. More…Synopsis¶
Methods¶
def
__init__()
def
attachment()
def
bind()
def
format()
def
handle()
def
height()
def
isBound()
def
isValid()
def
release()
def
setAttachment()
def
size()
def
sizes()
def
takeTexture()
def
texture()
def
textures()
def
toImage()
def
width()
Static functions¶
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
The
QOpenGLFramebufferObject
class encapsulates an OpenGL framebuffer object, defined by theGL_EXT_framebuffer_object
extension. It provides a rendering surface that can be painted on with a QPainter with the help ofQOpenGLPaintDevice
, or rendered to using native OpenGL calls. This surface can be bound and used as a regular texture in your own OpenGL drawing code. By default, theQOpenGLFramebufferObject
class generates a 2D OpenGL texture (using theGL_TEXTURE_2D
target), which is used as the internal rendering target.It is important to have a current OpenGL context when creating a
QOpenGLFramebufferObject
, otherwise initialization will fail.Create the QOpenGLFrameBufferObject instance with the
CombinedDepthStencil
attachment if you want QPainter to render correctly. Note that you need to create aQOpenGLFramebufferObject
with more than one sample per pixel for primitives to be antialiased when drawing using a QPainter. To create a multisample framebuffer object you should use one of the constructors that take aQOpenGLFramebufferObjectFormat
parameter, and set thesamples()
property to a non-zero value.For multisample framebuffer objects a color render buffer is created, otherwise a texture with the specified texture target is created. The color render buffer or texture will have the specified internal format, and will be bound to the
GL_COLOR_ATTACHMENT0
attachment in the framebuffer object.Multiple render targets are also supported, in case the OpenGL implementation supports this. Here there will be multiple textures (or, in case of multisampling, renderbuffers) present and each of them will get attached to
GL_COLOR_ATTACHMENT0
,1
,2
, …If you want to use a framebuffer object with multisampling enabled as a texture, you first need to copy from it to a regular framebuffer object using QOpenGLContext::blitFramebuffer().
It is possible to draw into a
QOpenGLFramebufferObject
using QPainter andQOpenGLPaintDevice
in a separate thread.- class Attachment¶
This enum type is used to configure the depth and stencil buffers attached to the framebuffer object when it is created.
Constant
Description
QOpenGLFramebufferObject.NoAttachment
No attachment is added to the framebuffer object. Note that the OpenGL depth and stencil tests won’t work when rendering to a framebuffer object without any depth or stencil buffers. This is the default value.
QOpenGLFramebufferObject.CombinedDepthStencil
If the
GL_EXT_packed_depth_stencil
extension is present, a combined depth and stencil buffer is attached. If the extension is not present, only a depth buffer is attached.QOpenGLFramebufferObject.Depth
A depth buffer is attached to the framebuffer object.
See also
- class FramebufferRestorePolicy¶
This enum type is used to configure the behavior related to restoring framebuffer bindings when calling blitFramebuffer().
Constant
Description
QOpenGLFramebufferObject.DontRestoreFramebufferBinding
Do not restore the previous framebuffer binding. The caller is responsible for tracking and setting the framebuffer binding as needed.
QOpenGLFramebufferObject.RestoreFramebufferBindingToDefault
After the blit operation, bind the default framebuffer.
QOpenGLFramebufferObject.RestoreFrameBufferBinding
Restore the previously bound framebuffer. This is potentially expensive because of the need to query the currently bound framebuffer.
See also
- __init__(size, format)¶
- Parameters:
size –
QSize
format –
QOpenGLFramebufferObjectFormat
- __init__(size[, target=GL_TEXTURE_2D])
- Parameters:
size –
QSize
target – int
- __init__(width, height, format)
- Parameters:
width – int
height – int
format –
QOpenGLFramebufferObjectFormat
- __init__(width, height[, target=GL_TEXTURE_2D])
- Parameters:
width – int
height – int
target – int
- __init__(size, attachment[, target=GL_TEXTURE_2D[, internalFormat=0]])
- Parameters:
size –
QSize
attachment –
Attachment
target – int
internalFormat – int
- __init__(width, height, attachment[, target=GL_TEXTURE_2D[, internalFormat=0]])
- Parameters:
width – int
height – int
attachment –
Attachment
target – int
internalFormat – int
- addColorAttachment(width, height[, internalFormat=0])
- Parameters:
width – int
height – int
internalFormat – int
- attachment()¶
- Return type:
- bind()¶
- Return type:
bool
Switches rendering from the default, windowing system provided framebuffer to this framebuffer object. Returns
true
upon success, false otherwise.Note
If
takeTexture()
was called, a new texture is created and associated with the framebuffer object. This is potentially expensive and changes the context state (the currently bound texture).See also
- static bindDefault()¶
- Return type:
bool
Switches rendering back to the default, windowing system provided framebuffer. Returns
true
upon success, false otherwise.- static blitFramebuffer(target, source[, buffers=GL_COLOR_BUFFER_BIT[, filter=GL_NEAREST]])¶
- Parameters:
target –
QOpenGLFramebufferObject
source –
QOpenGLFramebufferObject
buffers – int
filter – int
- static blitFramebuffer(target, targetRect, source, sourceRect[, buffers=GL_COLOR_BUFFER_BIT[, filter=GL_NEAREST]])
- Parameters:
target –
QOpenGLFramebufferObject
targetRect –
QRect
source –
QOpenGLFramebufferObject
sourceRect –
QRect
buffers – int
filter – int
- static blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, readColorAttachmentIndex, drawColorAttachmentIndex)
- Parameters:
target –
QOpenGLFramebufferObject
targetRect –
QRect
source –
QOpenGLFramebufferObject
sourceRect –
QRect
buffers – int
filter – int
readColorAttachmentIndex – int
drawColorAttachmentIndex – int
- static blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, readColorAttachmentIndex, drawColorAttachmentIndex, restorePolicy)
- Parameters:
target –
QOpenGLFramebufferObject
targetRect –
QRect
source –
QOpenGLFramebufferObject
sourceRect –
QRect
buffers – int
filter – int
readColorAttachmentIndex – int
drawColorAttachmentIndex – int
restorePolicy –
FramebufferRestorePolicy
- format()¶
- Return type:
- handle()¶
- Return type:
int
Returns the OpenGL framebuffer object handle for this framebuffer object (returned by the
glGenFrameBuffersEXT()
function). This handle can be used to attach new images or buffers to the framebuffer. The user is responsible for cleaning up and destroying these objects.- static hasOpenGLFramebufferBlit()¶
- Return type:
bool
Returns
true
if the OpenGLGL_EXT_framebuffer_blit
extension is present on this system; otherwise returnsfalse
.See also
- static hasOpenGLFramebufferObjects()¶
- Return type:
bool
Returns
true
if the OpenGLGL_EXT_framebuffer_object
extension is present on this system; otherwise returnsfalse
.- height()¶
- Return type:
int
Returns the height of the framebuffer object attachments.
- isBound()¶
- Return type:
bool
- isValid()¶
- Return type:
bool
Returns
true
if the framebuffer object is valid.The framebuffer can become invalid if the initialization process fails, the user attaches an invalid buffer to the framebuffer object, or a non-power of two width/height is specified as the texture size if the texture target is
GL_TEXTURE_2D
. The non-power of two limitation does not apply if the OpenGL version is 2.0 or higher, or if the GL_ARB_texture_non_power_of_two extension is present.The framebuffer can also become invalid if the QOpenGLContext that the framebuffer was created within is destroyed and there are no other shared contexts that can take over ownership of the framebuffer.
- release()¶
- Return type:
bool
Switches rendering back to the default, windowing system provided framebuffer. Returns
true
upon success, false otherwise.See also
- setAttachment(attachment)¶
- Parameters:
attachment –
Attachment
- takeTexture()¶
- Return type:
int
Returns the texture id for the texture attached to this framebuffer object. The ownership of the texture is transferred to the caller.
If the framebuffer object is currently bound, an implicit
release()
will be done. During the next call tobind()
a new texture will be created.If a multisample framebuffer object is used, then there is no texture and the return value from this function will be invalid. Similarly, incomplete framebuffer objects will also return 0.
- takeTexture(colorAttachmentIndex)
- Parameters:
colorAttachmentIndex – int
- Return type:
int
- texture()¶
- Return type:
int
Returns the texture id for the texture attached as the default rendering target in this framebuffer object. This texture id can be bound as a normal texture in your own OpenGL code.
If a multisample framebuffer object is used then the value returned from this function will be invalid.
When multiple textures are attached, the return value is the ID of the first one.
See also
- textures()¶
- Return type:
.list of unsigned int
Returns the contents of this framebuffer object as a QImage.
If
flipped
is true the image is flipped from OpenGL coordinates to raster coordinates. If used together withQOpenGLPaintDevice
,flipped
should be the opposite of the value ofpaintFlipped()
.The returned image has a format of premultiplied ARGB32 or RGB32. The latter is used only when internalTextureFormat() is set to
GL_RGB
. Since Qt 5.2 the function will fall back to premultiplied RGBA8888 or RGBx8888 when reading to (A)RGB32 is not supported, and this includes OpenGL ES. Since Qt 5.4 an A2BGR30 image is returned if the internal format is RGB10_A2, and since Qt 5.12 a RGBA64 image is return if the internal format is RGBA16.If the rendering in the framebuffer was not done with premultiplied alpha in mind, create a wrapper QImage with a non-premultiplied format. This is necessary before performing operations like QImage::save() because otherwise the image data would get unpremultiplied, even though it was not premultiplied in the first place. To create such a wrapper without performing a copy of the pixel data, do the following:
QImage fboImage(fbo.toImage()); QImage image(fboImage.constBits(), fboImage.width(), fboImage.height(), QImage::Format_ARGB32);
For multisampled framebuffer objects the samples are resolved using the
GL_EXT_framebuffer_blit
extension. If the extension is not available, the contents of the returned image is undefined.For singlesampled framebuffers the contents is retrieved via
glReadPixels
. This is a potentially expensive and inefficient operation. Therefore it is recommended that this function is used as seldom as possible.See also
- toImage(flipped, colorAttachmentIndex)
- Parameters:
flipped – bool
colorAttachmentIndex – int
- Return type:
- width()¶
- Return type:
int
Returns the width of the framebuffer object attachments.