|  | Home · All Classes · Modules

# QGLFramebufferObject Class Reference
[QtOpenGL module]

The QGLFramebufferObject class encapsulates an OpenGL framebuffer object. More...

Inherits QPaintDevice.

### Types

- enum Attachment { NoAttachment, CombinedDepthStencil, Depth }

### Methods

- __init__ (self, QSize size, unsigned long target = GL_TEXTURE_2D)
- __init__ (self, int width, int height, unsigned long target = GL_TEXTURE_2D)
- __init__ (self, QSize size, Attachment attachment, unsigned long target = GL_TEXTURE_2D, unsigned long internal_format = GL_RGBA8)
- __init__ (self, int width, int height, Attachment attachment, unsigned long target = GL_TEXTURE_2D, unsigned long internal_format = GL_RGBA8)
- Attachment attachment (self)
- bool bind (self)
- drawTexture (self, QRectF target, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)
- drawTexture (self, QPointF point, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)
- unsigned long handle (self)
- bool isValid (self)
- int metric (self, QPaintDevice.PaintDeviceMetric metric)
- QPaintEngine paintEngine (self)
- bool release (self)
- QSize size (self)
- unsigned long texture (self)
- QImage toImage (self)

### Static Methods

- bool hasOpenGLFramebufferObjects ()

## Detailed Description

The QGLFramebufferObject class encapsulates an OpenGL framebuffer object.

The QGLFramebufferObject class encapsulates an OpenGL framebuffer object, defined by the `GL_EXT_framebuffer_object` extension. In addition it provides a rendering surface that can be painted on with a QPainter, rendered to using native GL calls, or both. This surface can be bound and used as a regular texture in your own GL drawing code. By default, the QGLFramebufferObject class generates a 2D GL texture (using the `GL_TEXTURE_2D` target), which is used as the internal rendering target.

It is important to have a current GL context when creating a QGLFramebufferObject, otherwise initialization will fail.

OpenGL framebuffer objects and pbuffers (see QGLPixelBuffer) can both be used to render to offscreen surfaces, but there are a number of advantages with using framebuffer objects instead of pbuffers:

1. A framebuffer object does not require a separate rendering context, so no context switching will occur when switching rendering targets. There is an overhead involved in switching targets, but in general it is cheaper than a context switch to a pbuffer.
2. Rendering to dynamic textures (i.e. render-to-texture functionality) works on all platforms. No need to do explicit copy calls from a render buffer into a texture, as was necessary on systems that did not support the `render_texture` extension.
3. It is possible to attach several rendering buffers (or texture objects) to the same framebuffer object, and render to all of them without doing a context switch.
4. The OpenGL framebuffer extension is a pure GL extension with no system dependant WGL, AGL or GLX parts. This makes using framebuffer objects more portable.

Note that primitives drawn to a QGLFramebufferObject with QPainter will only be antialiased if the QPainter.HighQualityAntialiasing render hint is set. This is because there is currently no support for the `GL_EXT_framebuffer_multisample` extension, which is required to do multisample based antialiasing. Also note that the QPainter.HighQualityAntialiasing render hint requires the `GL_ARB_fragment_program` extension to work in OpenGL.

See also Framebuffer Object Example.

## Type Documentation

### QGLFramebufferObject.Attachment

This enum type is used to configure the depth and stencil buffers attached to the framebuffer object when it is created.

Constant | Value | Description
--- | --- | ---
`QGLFramebufferObject.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.
`QGLFramebufferObject.CombinedDepthStencil` | `1` | 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.
`QGLFramebufferObject.Depth` | `2` | A depth buffer is attached to the framebuffer object.

This enum was introduced in Qt 4.3.

See also attachment().

## Method Documentation

### QGLFramebufferObject.__init__ (self, QSize size, unsigned long target = GL_TEXTURE_2D)

Constructs an OpenGL framebuffer object and binds a 2D GL texture to the buffer of the size size. The texture is bound to the `GL_COLOR_ATTACHMENT0` target in the framebuffer object.

The target parameter is used to specify the GL 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`.

It is important that you have a current GL context set when creating the QGLFramebufferObject, otherwise the initialization will fail.

See also size(), texture(), and attachment().

### QGLFramebufferObject.__init__ (self, int width, int height, unsigned long target = GL_TEXTURE_2D)

Constructs an OpenGL framebuffer object and binds a 2D GL texture to the buffer of the given width and height.

See also size() and texture().

### QGLFramebufferObject.__init__ (self, QSize size, Attachment attachment, unsigned long target = GL_TEXTURE_2D, unsigned long internal_format = GL_RGBA8)

Constructs an OpenGL framebuffer object and binds a texture to the buffer of the given width and height.

The attachment parameter describes the depth/stencil buffer configuration, target the texture target and internal_format the internal texture format. The default texture target is `GL_TEXTURE_2D`, while the default internal format is `GL_RGBA8`.

See also size(), texture(), and attachment().

### QGLFramebufferObject.__init__ (self, int width, int height, Attachment attachment, unsigned long target = GL_TEXTURE_2D, unsigned long internal_format = GL_RGBA8)

Constructs an OpenGL framebuffer object and binds a texture to the buffer of the given size.

The attachment parameter describes the depth/stencil buffer configuration, target the texture target and internal_format the internal texture format. The default texture target is `GL_TEXTURE_2D`, while the default internal format is `GL_RGBA8`.

See also size(), texture(), and attachment().

### Attachment QGLFramebufferObject.attachment (self)

Returns the status of the depth and stencil buffers attached to this framebuffer object.

### bool QGLFramebufferObject.bind (self)

Switches rendering from the default, windowing system provided framebuffer to this framebuffer object. Returns true upon success, false otherwise.

### QGLFramebufferObject.drawTexture (self, QRectF target, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)

Draws the given texture, textureId, to the given target rectangle, target, in OpenGL model space. The textureTarget should be a 2D texture target.

The framebuffer object should be bound when calling this function.

Equivalent to the corresponding QGLContext.drawTexture().

This function was introduced in Qt 4.4.

### QGLFramebufferObject.drawTexture (self, QPointF point, unsigned long textureId, unsigned long textureTarget = GL_TEXTURE_2D)

This is an overloaded member function, provided for convenience.

Draws the given texture, textureId, at the given point in OpenGL model space. The textureTarget should be a 2D texture target.

The framebuffer object should be bound when calling this function.

Equivalent to the corresponding QGLContext.drawTexture().

This function was introduced in Qt 4.4.

### unsigned long QGLFramebufferObject.handle (self)

Returns the GL 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.

### bool QGLFramebufferObject.hasOpenGLFramebufferObjects ()

Returns true if the OpenGL `GL_EXT_framebuffer_object` extension is present on this system; otherwise returns false.

### bool QGLFramebufferObject.isValid (self)

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 2 width/height is specified as the texture size if the texture target is `GL_TEXTURE_2D`.

### int QGLFramebufferObject.metric (self, QPaintDevice.PaintDeviceMetric metric)

### QPaintEngine QGLFramebufferObject.paintEngine (self)

### bool QGLFramebufferObject.release (self)

Switches rendering back to the default, windowing system provided framebuffer. Returns true upon success, false otherwise.

### QSize QGLFramebufferObject.size (self)

Returns the size of the texture attached to this framebuffer object.

### unsigned long QGLFramebufferObject.texture (self)

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 GL code.

### QImage QGLFramebufferObject.toImage (self)

Returns the contents of this framebuffer object as a QImage.