Framebuffer
- class arcade.gl.Framebuffer(ctx: Context, *, color_attachments: Texture2D | list[Texture2D], depth_attachment: Texture2D | None = None)[source]
Bases:
An offscreen render target also called a Framebuffer Object in OpenGL. This implementation is using texture attachments. When creating a Framebuffer we supply it with textures we want our scene rendered into. The advantage of using texture attachments is the ability we get to keep working on the contents of the framebuffer.
The best way to create framebuffer is through
arcade.gl.Context.framebuffer()
:# Create a 100 x 100 framebuffer with one attachment ctx.framebuffer(color_attachments=[ctx.texture((100, 100), components=4)]) # Create a 100 x 100 framebuffer with two attachments # Shaders can be configured writing to the different layers ctx.framebuffer( color_attachments=[ ctx.texture((100, 100), components=4), ctx.texture((100, 100), components=4), ] )
- Parameters:
ctx – The context this framebuffer belongs to
color_attachments (optional) – A color attachment or a list of color attachments
depth_attachment (optional) – A depth attachment
- is_default = False
Is this the default framebuffer? (window buffer)
- property viewport: tuple[int, int, int, int]
Get or set the framebuffer’s viewport.
The viewport parameter are
(x, y, width, height)
. It determines what part of the framebuffer should be rendered to. By default the viewport is(0, 0, width, height)
.The viewport value is persistent all will automatically be applies every time the framebuffer is bound.
Example:
# 100, x 100 lower left with size 200 x 200px fb.viewport = 100, 100, 200, 200
- property scissor: tuple[int, int, int, int] | None
Get or set the scissor box for this framebuffer.
By default the scissor box is disabled and has no effect and will have an initial value of
None
. The scissor box is enabled when setting a value and disabled when set toNone
# Set and enable scissor box only drawing # in a 100 x 100 pixel lower left area ctx.scissor = 0, 0, 100, 100 # Disable scissoring ctx.scissor = None
- Type:
tuple (x, y, width, height)
- property depth_mask: bool
True
).It determines if depth values should be written to the depth texture when depth testing is enabled.
The depth mask value is persistent all will automatically be applies every time the framebuffer is bound.
- Type:
Get or set the depth mask (default
- activate() Generator[Framebuffer, None, None] [source]
Context manager for binding the framebuffer.
Unlike the default context manager in this class this support nested framebuffer binding.
- use(*, force: bool = False)[source]
Bind the framebuffer making it the target of all rendering commands
- Parameters:
force – Force the framebuffer binding even if the system already believes it’s already bound.
- clear(*, color: tuple[int, int, int] | tuple[int, int, int, int] | None = None, color_normalized: tuple[float, float, float] | tuple[float, float, float, float] | None = None, depth: float = 1.0, viewport: tuple[int, int, int, int] | None = None)[source]
Clears the framebuffer:
# Clear the framebuffer using Arcade's colors (not normalized) fb.clear(color=arcade.color.WHITE) # Clear framebuffer using the color red in normalized form fbo.clear(color_normalized=(1.0, 0.0, 0.0, 1.0))
If the background color is an
RGB
value instead ofRGBA`
we assume alpha value 255.- Parameters:
color – A 3 or 4 component tuple containing the color (prioritized over color_normalized)
color_normalized – A 3 or 4 component tuple containing the color in normalized form
depth – Value to clear the depth buffer (unused)
viewport – The viewport range to clear
- read(*, viewport=None, components=3, attachment=0, dtype='f1') bytes [source]
Read the raw framebuffer pixels.
Reading data from a framebuffer is much more powerful than reading date from textures. We can specify more or less what format we want the data. It’s not uncommon to throw textures into framebuffers just to get access to this read api.
- Parameters:
viewport – The x, y, with, height area to read.
components – The number of components to read. 1, 2, 3 or 4. This will determine the format to read.
attachment – The attachment id to read from
dtype – The data type to read. Pixel data will be converted to this format.
- resize()[source]
Detects size changes in attachments.
This will reset the viewport to
0, 0, width, height
.
- class arcade.gl.framebuffer.DefaultFrameBuffer(ctx: Context)[source]
Bases:
Framebuffer
Represents the default framebuffer.
This is the framebuffer of the window itself and need some special handling.
We are not allowed to destroy this framebuffer since it’s owned by pyglet. This framebuffer can also change size and pixel ratio at any point.
We’re doing some initial introspection to guess somewhat sane initial values. Since this is a dynamic framebuffer we cannot trust the internal values. We can only trust what the pyglet window itself reports related to window size and framebuffer size. This should be updated in the
on_resize
callback.- is_default = True
Is this the default framebuffer? (window buffer)
- property viewport: tuple[int, int, int, int]
Get or set the framebuffer’s viewport. The viewport parameter are
(x, y, width, height)
. It determines what part of the framebuffer should be rendered to. By default the viewport is(0, 0, width, height)
.The viewport value is persistent all will automatically be applies every time the framebuffer is bound.
Example:
# 100, x 100 lower left with size 200 x 200px fb.viewport = 100, 100, 200, 200
- property scissor: tuple[int, int, int, int] | None
Get or set the scissor box for this framebuffer.
By default the scissor box is disabled and has no effect and will have an initial value of
None
. The scissor box is enabled when setting a value and disabled when set toNone
:# Set and enable scissor box only drawing # in a 100 x 100 pixel lower left area ctx.scissor = 0, 0, 100, 100 # Disable scissoring ctx.scissor = None