The QOpenGLFramebufferObject class encapsulates an OpenGL framebuffer object. 更多...
头: | #include <QOpenGLFramebufferObject> |
CMake: |
find_package(Qt6 COMPONENTS OpenGL REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
qmake: | QT += opengl |
Since: | Qt 5.0 |
enum | Attachment { NoAttachment, CombinedDepthStencil, Depth } |
enum | FramebufferRestorePolicy { DontRestoreFramebufferBinding, RestoreFramebufferBindingToDefault, RestoreFrameBufferBinding } |
QOpenGLFramebufferObject (int width , int height , const QOpenGLFramebufferObjectFormat & format ) | |
QOpenGLFramebufferObject (const QSize & size , const QOpenGLFramebufferObjectFormat & format ) | |
QOpenGLFramebufferObject (int width , int height , QOpenGLFramebufferObject::Attachment attachment , GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0) | |
QOpenGLFramebufferObject (const QSize & size , QOpenGLFramebufferObject::Attachment attachment , GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0) | |
QOpenGLFramebufferObject (int width , int height , GLenum target = GL_TEXTURE_2D) | |
QOpenGLFramebufferObject (const QSize & size , GLenum target = GL_TEXTURE_2D) | |
virtual | ~QOpenGLFramebufferObject () |
void | addColorAttachment (const QSize & size , GLenum internalFormat = 0) |
void | addColorAttachment (int width , int height , GLenum internalFormat = 0) |
QOpenGLFramebufferObject::Attachment | attachment () const |
bool | bind () |
QOpenGLFramebufferObjectFormat | format () const |
GLuint | handle () const |
int | height () const |
bool | isBound () const |
bool | isValid () const |
bool | release () |
void | setAttachment (QOpenGLFramebufferObject::Attachment attachment ) |
QSize | size () const |
QList<QSize> | sizes () const |
GLuint | takeTexture () |
GLuint | takeTexture (int colorAttachmentIndex ) |
GLuint | texture () const |
QList<GLuint> | textures () const |
QImage | toImage (bool flipped = true) const |
QImage | toImage (bool flipped , int colorAttachmentIndex ) const |
int | width () const |
bool | bindDefault () |
void | blitFramebuffer (QOpenGLFramebufferObject * target , const QRect & targetRect , QOpenGLFramebufferObject * source , const QRect & sourceRect , GLbitfield buffers , GLenum filter , int readColorAttachmentIndex , int drawColorAttachmentIndex , QOpenGLFramebufferObject::FramebufferRestorePolicy restorePolicy ) |
void | blitFramebuffer (QOpenGLFramebufferObject * target , const QRect & targetRect , QOpenGLFramebufferObject * source , const QRect & sourceRect , GLbitfield buffers , GLenum filter , int readColorAttachmentIndex , int drawColorAttachmentIndex ) |
void | blitFramebuffer (QOpenGLFramebufferObject * target , const QRect & targetRect , QOpenGLFramebufferObject * source , const QRect & sourceRect , GLbitfield buffers = GL_COLOR_BUFFER_BIT, GLenum filter = GL_NEAREST) |
void | blitFramebuffer (QOpenGLFramebufferObject * target , QOpenGLFramebufferObject * source , GLbitfield buffers = GL_COLOR_BUFFER_BIT, GLenum filter = GL_NEAREST) |
bool | hasOpenGLFramebufferBlit () |
bool | hasOpenGLFramebufferObjects () |
The QOpenGLFramebufferObject class encapsulates an OpenGL framebuffer object, defined by the
GL_EXT_framebuffer_object
extension. It provides a rendering surface that can be painted on with a
QPainter
with the help of
QOpenGLPaintDevice
, 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, the QOpenGLFramebufferObject class generates a 2D OpenGL texture (using the
GL_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 a QOpenGLFramebufferObject 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 a QOpenGLFramebufferObjectFormat parameter, and set the QOpenGLFramebufferObjectFormat::samples () 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 and QOpenGLPaintDevice in a separate thread.
This enum type is used to configure the depth and stencil buffers attached to the framebuffer object when it is created.
常量 | 值 | 描述 |
---|---|---|
QOpenGLFramebufferObject::NoAttachment
|
0
|
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
|
1
|
若
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
|
2
|
A depth buffer is attached to the framebuffer object. |
另请参阅 attachment ().
[since 5.7]
enum QOpenGLFramebufferObject::
FramebufferRestorePolicy
This enum type is used to configure the behavior related to restoring framebuffer bindings when calling blitFramebuffer ().
常量 | 值 | 描述 |
---|---|---|
QOpenGLFramebufferObject::DontRestoreFramebufferBinding
|
0
|
Do not restore the previous framebuffer binding. The caller is responsible for tracking and setting the framebuffer binding as needed. |
QOpenGLFramebufferObject::RestoreFramebufferBindingToDefault
|
1
|
After the blit operation, bind the default framebuffer. |
QOpenGLFramebufferObject::RestoreFrameBufferBinding
|
2
|
Restore the previously bound framebuffer. This is potentially expensive because of the need to query the currently bound framebuffer. |
该枚举在 Qt 5.7 引入或被修改。
另请参阅 blitFramebuffer ().
Constructs an OpenGL framebuffer object of the given width and height based on the supplied format .
Constructs an OpenGL framebuffer object of the given size based on the supplied format .
Constructs an OpenGL framebuffer object and binds a texture to the buffer of the given width and height .
attachment
parameter describes the depth/stencil buffer configuration,
target
the texture target and
internalFormat
the internal texture format. The default texture target is
GL_TEXTURE_2D
, while the default internal format is
GL_RGBA8
for desktop OpenGL and
GL_RGBA
for OpenGL/ES.
另请参阅 size (), texture (),和 attachment ().
Constructs an OpenGL framebuffer object and binds a texture to the buffer of the given size .
attachment
parameter describes the depth/stencil buffer configuration,
target
the texture target and
internalFormat
the internal texture format. The default texture target is
GL_TEXTURE_2D
, while the default internal format is
GL_RGBA8
for desktop OpenGL and
GL_RGBA
for OpenGL/ES.
另请参阅 size (), texture (),和 attachment ().
Constructs an OpenGL framebuffer object and binds a 2D OpenGL texture to the buffer of the given width and height .
Constructs an OpenGL framebuffer object and binds a 2D OpenGL texture to the buffer of the size
size
. The texture is bound to the
GL_COLOR_ATTACHMENT0
target in the framebuffer object.
target
parameter is used to specify the OpenGL texture target. The default target is
GL_TEXTURE_2D
. Keep in mind that
GL_TEXTURE_2D
textures must have a power of 2 width and height (e.g. 256x512), unless you are using OpenGL 2.0 or higher.
By default, no depth and stencil buffers are attached. This behavior can be toggled using one of the overloaded constructors.
The default internal texture format is
GL_RGBA8
for desktop OpenGL, and
GL_RGBA
for OpenGL/ES.
It is important that you have a current OpenGL context set when creating the QOpenGLFramebufferObject, otherwise the initialization will fail.
另请参阅 size (), texture (),和 attachment ().
[virtual]
QOpenGLFramebufferObject::
~QOpenGLFramebufferObject
()
Destroys the framebuffer object and frees any allocated resources.
[since 5.6]
void
QOpenGLFramebufferObject::
addColorAttachment
(const
QSize
&
size
,
GLenum
internalFormat
= 0)
Creates and attaches an additional texture or renderbuffer of size width and height.
There is always an attachment at GL_COLOR_ATTACHMENT0. Call this function to set up additional attachments at GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, ...
当
internalFormat
不是
0
, it specifies the internal format of the texture or renderbuffer. Otherwise a default of GL_RGBA or GL_RGBA8 is used.
注意: This is only functional when multiple render targets are supported by the OpenGL implementation. When that is not the case, the function will not add any additional color attachments. Call QOpenGLFunctions::hasOpenGLFeature () 采用 QOpenGLFunctions::MultipleRenderTargets at runtime to check if MRT is supported.
注意: The internal format of the color attachments may differ but there may be limitations on the supported combinations, depending on the drivers.
注意: The size of the color attachments may differ but rendering is limited to the area that fits all the attachments, according to the OpenGL specification. Some drivers may not be fully conformant in this respect, however.
该函数在 Qt 5.6 引入。
[since 5.6]
void
QOpenGLFramebufferObject::
addColorAttachment
(
int
width
,
int
height
,
GLenum
internalFormat
= 0)
这是重载函数。
Creates and attaches an additional texture or renderbuffer of size width and height .
当
internalFormat
不是
0
, it specifies the internal format of the texture or renderbuffer. Otherwise a default of GL_RGBA or GL_RGBA8 is used.
该函数在 Qt 5.6 引入。
Returns the status of the depth and stencil buffers attached to this framebuffer object.
另请参阅 setAttachment ().
Switches rendering from the default, windowing system provided framebuffer to this framebuffer object. Returns
true
upon success, false otherwise.
注意: 若 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).
另请参阅 release ().
[static]
bool
QOpenGLFramebufferObject::
bindDefault
()
Switches rendering back to the default, windowing system provided framebuffer. Returns
true
upon success, false otherwise.
[static, since 5.7]
void
QOpenGLFramebufferObject::
blitFramebuffer
(
QOpenGLFramebufferObject
*
target
, const
QRect
&
targetRect
,
QOpenGLFramebufferObject
*
source
, const
QRect
&
sourceRect
,
GLbitfield
buffers
,
GLenum
filter
,
int
readColorAttachmentIndex
,
int
drawColorAttachmentIndex
,
QOpenGLFramebufferObject::FramebufferRestorePolicy
restorePolicy
)
Blits from the sourceRect rectangle in the source framebuffer object to the targetRect rectangle in the target framebuffer object.
若 source or target is 0, the default framebuffer will be used instead of a framebuffer object as source or target respectively.
This function will have no effect unless hasOpenGLFramebufferBlit () returns true.
buffers
parameter should be a mask consisting of any combination of
GL_COLOR_BUFFER_BIT
,
GL_DEPTH_BUFFER_BIT
,和
GL_STENCIL_BUFFER_BIT
. Any buffer type that is not present both in the source and target buffers is ignored.
sourceRect
and
targetRect
rectangles may have different sizes; in this case
buffers
should not contain
GL_DEPTH_BUFFER_BIT
or
GL_STENCIL_BUFFER_BIT
。
filter
parameter should be set to
GL_LINEAR
or
GL_NEAREST
, and specifies whether linear or nearest interpolation should be used when scaling is performed.
若 source 等于 target a copy is performed within the same buffer. Results are undefined if the source and target rectangles overlap and have different sizes. The sizes must also be the same if any of the framebuffer objects are multisample framebuffers.
注意: The scissor test will restrict the blit area if enabled.
When multiple render targets are in use, readColorAttachmentIndex and drawColorAttachmentIndex specify the index of the color attachments in the source and destination framebuffers.
restorePolicy
determines if the framebuffer that was bound prior to calling this function should be restored, or if the default framebuffer should be bound before returning, of if the caller is responsible for tracking and setting the bound framebuffer. Restoring the previous framebuffer can be relatively expensive due to the call to
glGetIntegerv
which on some OpenGL drivers may imply a pipeline stall.
该函数在 Qt 5.7 引入。
另请参阅 hasOpenGLFramebufferBlit ().
[static]
void
QOpenGLFramebufferObject::
blitFramebuffer
(
QOpenGLFramebufferObject
*
target
, const
QRect
&
targetRect
,
QOpenGLFramebufferObject
*
source
, const
QRect
&
sourceRect
,
GLbitfield
buffers
,
GLenum
filter
,
int
readColorAttachmentIndex
,
int
drawColorAttachmentIndex
)
这是重载函数。
Convenience overload to blit between two framebuffer objects and to restore the previous framebuffer binding. Equivalent to calling blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, readColorAttachmentIndex, drawColorAttachmentIndex, RestoreFrameBufferBinding ).
[static]
void
QOpenGLFramebufferObject::
blitFramebuffer
(
QOpenGLFramebufferObject
*
target
, const
QRect
&
targetRect
,
QOpenGLFramebufferObject
*
source
, const
QRect
&
sourceRect
,
GLbitfield
buffers
= GL_COLOR_BUFFER_BIT,
GLenum
filter
= GL_NEAREST)
这是重载函数。
* Convenience overload to blit between two framebuffer objects.
[static]
void
QOpenGLFramebufferObject::
blitFramebuffer
(
QOpenGLFramebufferObject
*
target
,
QOpenGLFramebufferObject
*
source
,
GLbitfield
buffers
= GL_COLOR_BUFFER_BIT,
GLenum
filter
= GL_NEAREST)
这是重载函数。
Convenience overload to blit between two framebuffer objects.
Returns the format of this framebuffer object.
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]
bool
QOpenGLFramebufferObject::
hasOpenGLFramebufferBlit
()
返回
true
if the OpenGL
GL_EXT_framebuffer_blit
extension is present on this system; otherwise returns
false
.
另请参阅 blitFramebuffer ().
[static]
bool
QOpenGLFramebufferObject::
hasOpenGLFramebufferObjects
()
返回
true
if the OpenGL
GL_EXT_framebuffer_object
extension is present on this system; otherwise returns
false
.
Returns the height of the framebuffer object attachments.
返回
true
if the framebuffer object is currently bound to the current context, otherwise false is returned.
返回
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.
Switches rendering back to the default, windowing system provided framebuffer. Returns
true
upon success, false otherwise.
另请参阅 bind ().
Sets the attachments of the framebuffer object to attachment .
This can be used to free or reattach the depth and stencil buffer attachments as needed.
注意: This function alters the current framebuffer binding.
另请参阅 attachment ().
Returns the size of the color and depth/stencil attachments attached to this framebuffer object.
[since 5.6]
QList
<
QSize
> QOpenGLFramebufferObject::
sizes
() const
Returns the sizes of all color attachments attached to this framebuffer object.
该函数在 Qt 5.6 引入。
[since 5.3]
GLuint
QOpenGLFramebufferObject::
takeTexture
()
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 to bind () 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.
该函数在 Qt 5.3 引入。
另请参阅 texture (), bind (),和 release ().
[since 5.6]
GLuint
QOpenGLFramebufferObject::
takeTexture
(
int
colorAttachmentIndex
)
这是重载函数。
Returns the texture id for the texture attached to the color attachment of index colorAttachmentIndex of this framebuffer object. The ownership of the texture is transferred to the caller.
当
colorAttachmentIndex
is
0
, the behavior is identical to the parameter-less variant of this function.
If the framebuffer object is currently bound, an implicit release () will be done. During the next call to bind () 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.
该函数在 Qt 5.6 引入。
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.
另请参阅 takeTexture () 和 textures ().
[since 5.6]
QList
<
GLuint
> QOpenGLFramebufferObject::
textures
() const
Returns the texture id for all attached textures.
If a multisample framebuffer object is used, then an empty vector is returned.
该函数在 Qt 5.6 引入。
另请参阅 takeTexture () 和 texture ().
Returns the contents of this framebuffer object as a QImage .
若 flipped is true the image is flipped from OpenGL coordinates to raster coordinates. If used together with QOpenGLPaintDevice , flipped should be the opposite of the value of QOpenGLPaintDevice::paintFlipped ().
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.
另请参阅 QOpenGLPaintDevice::paintFlipped ().
[since 5.6]
QImage
QOpenGLFramebufferObject::
toImage
(
bool
flipped
,
int
colorAttachmentIndex
) const
这是重载函数。
Returns the contents of the color attachment of index
colorAttachmentIndex
of this framebuffer object as a
QImage
. This method flips the image from OpenGL coordinates to raster coordinates when
flipped
被设为
true
.
注意: This overload is only fully functional when multiple render targets are supported by the OpenGL implementation. When that is not the case, only one color attachment will be set up.
该函数在 Qt 5.6 引入。
Returns the width of the framebuffer object attachments.