QOpenGLFramebufferObject Class
The QOpenGLFramebufferObject class encapsulates an OpenGL framebuffer object. More…
| Header | #include <QOpenGLFramebufferObject> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::OpenGL) |
| Module | Nt OpenGL C++ Classes |
| Since | 6.0 |
List of all members, including inherited members
Public Types
| enum | Attachment |
| enum | FramebufferRestorePolicy |
Public Functions
| QOpenGLFramebufferObject(const QSize &size, GLenum target = GL_TEXTURE_2D) | |
| QOpenGLFramebufferObject(int width, int height, GLenum target = GL_TEXTURE_2D) | |
| QOpenGLFramebufferObject(const QSize &size, Attachment attachment, GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0) | |
| QOpenGLFramebufferObject(int width, int height, Attachment attachment, GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0) | |
| QOpenGLFramebufferObject(const QSize &size, const QOpenGLFramebufferObjectFormat &format) | |
| QOpenGLFramebufferObject(int width, int height, const QOpenGLFramebufferObjectFormat &format) | |
| virtual | ~QOpenGLFramebufferObject() |
| QOpenGLFramebufferObjectFormat | format() const |
| bool | isValid() const |
| bool | isBound() const |
| bool | bind() |
| bool | release() |
| int | width() const |
| int | height() const |
| QSize | size() const |
| GLuint | texture() const |
| GLuint | takeTexture() |
| QImage | toImage(bool flipped = true) const |
| Attachment | attachment() const |
| void | setAttachment(Attachment attachment) |
| GLuint | handle() const |
Static Public Members
| bool | bindDefault() |
| bool | hasOpenGLFramebufferObjects() |
| bool | hasOpenGLFramebufferBlit() |
| 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) |
| void | blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, QOpenGLFramebufferObject *source, const QRect &sourceRect, GLbitfield buffers, GLenum filter, int readColorAttachmentIndex, int drawColorAttachmentIndex, FramebufferRestorePolicy restorePolicy = RestoreFrameBufferBinding) |
Detailed Description
A framebuffer object is an offscreen target for OpenGL, with a texture or, when it is multisampled, a renderbuffer for its colors. It is created in the context that is current, and works in contexts that share with it.
Paint on one with QPainter through a QOpenGLPaintDevice, and read the result back with toImage():
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setSamples(4);
QOpenGLFramebufferObject fbo(QSize(400, 300), format);
fbo.bind();
QOpenGLPaintDevice device(fbo.size());
QPainter painter(&device);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(QRectF(10, 10, 380, 280));
painter.end();
QImage image = fbo.toImage();
QPainter needs a stencil buffer to fill shapes that are not convex, so give the object the CombinedDepthStencil attachment.
See also QOpenGLFramebufferObjectFormat, QOpenGLPaintDevice and QOpenGLContext.
Differences from Qt
There is one color attachment: addColorAttachment(), textures() and sizes() are missing, and the color attachment index of blitFramebuffer() must be 0. Rectangle textures and GL_TEXTURE_2D_MULTISAMPLE are not supported. The object must be destroyed while a context that shares with its context is current; otherwise its OpenGL objects are left to the context.
Member Type Documentation
enum QOpenGLFramebufferObject::Attachment
This enum describes the buffers attached to the framebuffer object besides its colors.
| Constant | Description |
|---|---|
QOpenGLFramebufferObject::NoAttachment | None. |
QOpenGLFramebufferObject::CombinedDepthStencil | A depth and a stencil buffer, in one renderbuffer. |
QOpenGLFramebufferObject::Depth | A depth buffer. |
enum QOpenGLFramebufferObject::FramebufferRestorePolicy
This enum describes which framebuffer blitFramebuffer() leaves bound.
| Constant | Description |
|---|---|
QOpenGLFramebufferObject::DontRestoreFramebufferBinding | The framebuffer that was drawn to stays bound. |
QOpenGLFramebufferObject::RestoreFramebufferBindingToDefault | The default framebuffer of the context is bound. |
QOpenGLFramebufferObject::RestoreFrameBufferBinding | The framebuffer that was bound before is bound again. |
Member Function Documentation
QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, GLenum target = GL_TEXTURE_2D)
Constructs a framebuffer object of size, with a texture of target and no depth or stencil buffer, in the current context.
QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, GLenum target = GL_TEXTURE_2D)
This is an overloaded function.
Constructs a framebuffer object of width by height, with a texture of target.
QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, Attachment attachment, GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0)
This is an overloaded function.
Constructs a framebuffer object of size with attachment and a texture of target in internalFormat (0 for the default).
QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, Attachment attachment, GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0)
This is an overloaded function.
Constructs a framebuffer object of width by height with attachment and a texture of target in internalFormat.
QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, const QOpenGLFramebufferObjectFormat &format)
This is an overloaded function.
Constructs a framebuffer object of size with format.
QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, const QOpenGLFramebufferObjectFormat &format)
This is an overloaded function.
Constructs a framebuffer object of width by height with format.
QOpenGLFramebufferObject::~QOpenGLFramebufferObject()
Destroys the framebuffer object and its buffers.
Attachment QOpenGLFramebufferObject::attachment() const
Returns the attachment of the framebuffer object.
bool QOpenGLFramebufferObject::bind()
Makes the framebuffer object the target of drawing in the current context, and returns true if it is valid.
See also release().
bool QOpenGLFramebufferObject::bindDefault()
Binds the default framebuffer of the current context and returns true if there is a current context.
void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, QOpenGLFramebufferObject *source, const QRect &sourceRect, GLbitfield buffers = GL_COLOR_BUFFER_BIT, GLenum filter = GL_NEAREST)
Copies sourceRect of source to targetRect of target, with filter where the sizes differ. The buffers are a combination of GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT. A null framebuffer object means the default framebuffer of the current context. Rectangles are in OpenGL's coordinates, from the bottom left.
void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, QOpenGLFramebufferObject *source, GLbitfield buffers = GL_COLOR_BUFFER_BIT, GLenum filter = GL_NEAREST)
This is an overloaded function.
Copies all of source to all of target, with filter, for buffers.
void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, QOpenGLFramebufferObject *source, const QRect &sourceRect, GLbitfield buffers, GLenum filter, int readColorAttachmentIndex, int drawColorAttachmentIndex, FramebufferRestorePolicy restorePolicy = RestoreFrameBufferBinding)
This is an overloaded function.
Copies sourceRect of source to targetRect of target for buffers with filter, and binds the framebuffer that restorePolicy names afterwards. The color attachment indexes readColorAttachmentIndex and drawColorAttachmentIndex must be 0.
QOpenGLFramebufferObjectFormat QOpenGLFramebufferObject::format() const
Returns the format of the framebuffer object, with the number of samples and the internal format it got.
GLuint QOpenGLFramebufferObject::handle() const
Returns the OpenGL name of the framebuffer object.
bool QOpenGLFramebufferObject::hasOpenGLFramebufferBlit()
Returns true if the current context can copy between framebuffers with blitFramebuffer(), which needs desktop OpenGL 3.0 or OpenGL ES 3.0.
bool QOpenGLFramebufferObject::hasOpenGLFramebufferObjects()
Returns true: every context Nt creates has framebuffer objects.
int QOpenGLFramebufferObject::height() const
Returns the height of the framebuffer object in pixels.
bool QOpenGLFramebufferObject::isBound() const
Returns true if the framebuffer object is bound in the current context.
bool QOpenGLFramebufferObject::isValid() const
Returns true if the framebuffer object was created and is complete.
bool QOpenGLFramebufferObject::release()
Binds the default framebuffer of the current context again, and returns true if there is a current context.
See also bind() and QOpenGLContext::defaultFramebufferObject().
void QOpenGLFramebufferObject::setAttachment(Attachment attachment)
Replaces the depth and stencil buffers with attachment.
QSize QOpenGLFramebufferObject::size() const
Returns the size of the framebuffer object in pixels.
GLuint QOpenGLFramebufferObject::takeTexture()
Returns the texture of the colors and gives it to the caller, who must delete it. The framebuffer object is not valid afterwards.
GLuint QOpenGLFramebufferObject::texture() const
Returns the texture of the colors, or 0 if the object is multisampled.
See also takeTexture().
QImage QOpenGLFramebufferObject::toImage(bool flipped = true) const
Returns the contents of the framebuffer object as a premultiplied image. OpenGL keeps rows from the bottom up; if flipped is true, the default, the image has its first row at the top, as it is painted. A multisampled object is resolved first.
int QOpenGLFramebufferObject::width() const
Returns the width of the framebuffer object in pixels.