Framebuffer
- class moderngl.Framebuffer
A
Framebuffer
is a collection of buffers that can be used as the destination for rendering.The buffers for Framebuffer objects reference images from either Textures or Renderbuffers. Create a
Framebuffer
usingContext.framebuffer()
.
Create
- Context.simple_framebuffer(size: Tuple[int, int], components: int = 4, *, samples: int = 0, dtype: str = 'f1') Framebuffer
Creates a
Framebuffer
with a single color attachment and depth buffer usingmoderngl.Renderbuffer
attachments.- Parameters:
size (tuple) – The width and height of the renderbuffer.
components (int) – The number of components 1, 2, 3 or 4.
- Keyword Arguments:
samples (int) – The number of samples. Value 0 means no multisample format.
dtype (str) – Data type.
- Returns:
Framebuffer
object
- Context.framebuffer(color_attachments: Any = (), depth_attachment: Optional[Union[Texture, Renderbuffer]] = None) Framebuffer
A
Framebuffer
is a collection of buffers that can be used as the destination for rendering. The buffers for Framebuffer objects reference images from either Textures or Renderbuffers.- Parameters:
color_attachments (list) – A list of
Texture
orRenderbuffer
objects.depth_attachment (Renderbuffer or Texture) – The depth attachment.
- Returns:
Framebuffer
object
Methods
- Framebuffer.clear(red: float = 0.0, green: float = 0.0, blue: float = 0.0, alpha: float = 0.0, depth: float = 1.0, *, viewport: Optional[Union[Tuple[int, int], Tuple[int, int, int, int]]] = None, color: Optional[Tuple[float, float, float, float]] = None) None
Clear the framebuffer.
If a viewport passed in, a scissor test will be used to clear the given viewport. This viewport take presence over the framebuffers
scissor
. Clearing can still be done with scissor if no viewport is passed in.This method also respects the
color_mask
anddepth_mask
. It can for example be used to only clear the depth or color buffer or specific components in the color buffer.If the viewport is a 2-tuple it will clear the
(0, 0, width, height)
where(width, height)
is the 2-tuple.If the viewport is a 4-tuple it will clear the given viewport.
- Parameters:
red (float) – color component.
green (float) – color component.
blue (float) – color component.
alpha (float) – alpha component.
depth (float) – depth value.
- Keyword Arguments:
viewport (tuple) – The viewport.
color (tuple) – Optional tuple replacing the red, green, blue and alpha arguments
- Framebuffer.read(viewport: Optional[Union[Tuple[int, int], Tuple[int, int, int, int]]] = None, components: int = 3, *, attachment: int = 0, alignment: int = 1, dtype: str = 'f1', clamp: bool = False) bytes
Read the content of the framebuffer.
# Read the first color attachment's RGBA data data = fbo.read(components=4) # Read the second color attachment's RGB data data = fbo.read(attachment=1) # Read the depth attachment data = fbo.read(attachment=-1) # Read the lower left 10 x 10 pixels from the first color attachment data = fbo.read(viewport=(0, 0, 10, 10))
- Parameters:
viewport (tuple) – The viewport.
components (int) – The number of components to read.
- Keyword Arguments:
attachment (int) – The color attachment number. -1 for the depth attachment
alignment (int) – The byte alignment of the pixels.
dtype (str) – Data type.
clamp (bool) – Clamps floating point values to
[0.0, 1.0]
- Returns:
bytes
- Framebuffer.read_into(buffer: Any, viewport: Optional[Union[Tuple[int, int], Tuple[int, int, int, int]]] = None, components: int = 3, *, attachment: int = 0, alignment: int = 1, dtype: str = 'f1', write_offset: int = 0) None
Read the content of the framebuffer into a buffer.
- Parameters:
buffer (bytearray) – The buffer that will receive the pixels.
viewport (tuple) – The viewport.
components (int) – The number of components to read.
- Keyword Arguments:
attachment (int) – The color attachment.
alignment (int) – The byte alignment of the pixels.
dtype (str) – Data type.
write_offset (int) – The write offset.
- Framebuffer.use() None
Bind the framebuffer. Sets the target for rendering commands.
- Framebuffer.release() None
Release the ModernGL object.
Attributes
- Framebuffer.viewport
Get or set the viewport of the framebuffer.
- Type:
tuple
- Framebuffer.scissor
Get or set the scissor box of the framebuffer.
When scissor testing is enabled fragments outside the defined scissor box will be discarded. This applies to rendered geometry or
Framebuffer.clear()
.Setting is value enables scissor testing in the framebuffer. Setting the scissor to
None
disables scissor testing and reverts the scissor box to match the framebuffer size.Example:
# Enable scissor testing >>> ctx.scissor = 100, 100, 200, 100 # Disable scissor testing >>> ctx.scissor = None
- Type:
tuple
- Framebuffer.color_mask
The color mask of the framebuffer.
Color masking controls what components in color attachments will be affected by fragment write operations. This includes rendering geometry and clearing the framebuffer.
Default value:
(True, True, True, True)
.Examples:
# Block writing to all color components (rgba) in color attachments fbo.color_mask = False, False, False, False # Re-enable writing to color attachments fbo.color_mask = True, True, True, True # Block fragment writes to alpha channel fbo.color_mask = True, True, True, False
- Type:
tuple
- Framebuffer.depth_mask
The depth mask of the framebuffer.
Depth mask enables or disables write operations to the depth buffer. This also applies when clearing the framebuffer. If depth testing is enabled fragments will still be culled, but the depth buffer will not be updated with new values. This is a very useful tool in many rendering techniques.
Default value:
True
- Type:
bool
- Framebuffer.width
The width of the framebuffer.
Framebuffers created by a window will only report its initial size. It’s better get size information from the window itself.
- Type:
int
- Framebuffer.height
The height of the framebuffer.
Framebuffers created by a window will only report its initial size. It’s better get size information from the window itself.
- Type:
int
- Framebuffer.size
The size of the framebuffer.
Framebuffers created by a window will only report its initial size. It’s better get size information from the window itself.
- Type:
tuple
- Framebuffer.samples
The samples of the framebuffer.
- Type:
int
- Framebuffer.bits
The bits of the framebuffer.
- Type:
dict
- Framebuffer.color_attachments
The color attachments of the framebuffer.
- Type:
tuple
- Framebuffer.depth_attachment
The depth attachment of the framebuffer.
- Type:
- Framebuffer.glo
The internal OpenGL object.
This values is provided for debug purposes only.
- Type:
int
- Framebuffer.mglo
Internal representation for debug purposes only.
- Framebuffer.extra: Any
Attribute for storing user defined objects