Framebuffer#

FrameBuffer#

class arcade.gl.Framebuffer(ctx: Context, *, color_attachments=None, depth_attachment=None)[source]#

Bases: object

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 (Context) – The context this framebuffer belongs to

  • color_attachments (List[arcade.gl.Texture]) – List of color attachments.

  • depth_attachment (arcade.gl.Texture) – A depth attachment (optional)

is_default = False#

Is this the default framebuffer? (window buffer)

property glo: ctypes.c_uint#

The OpenGL id/name of the framebuffer

Type

GLuint

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: Optional[Tuple[int, int, int, int]]#

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 to None

# 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 ctx: Context#

The context this object belongs to.

Type

arcade.gl.Context

property width: int#

The width of the framebuffer in pixels

Type

int

property height: int#

The height of the framebuffer in pixels

Type

int

property size: Tuple[int, int]#

Size as a (w, h) tuple

Type

tuple (int, int)

property samples: int#

Number of samples (MSAA)

Type

int

property color_attachments: List[arcade.gl.texture.Texture]#

A list of color attachments

Type

list of arcade.gl.Texture

property depth_attachment: arcade.gl.texture.Texture#

Depth attachment

Type

arcade.gl.Texture

property depth_mask: bool#

Get or set the depth mask (default: 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

bool

activate()[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 (bool) – Force the framebuffer binding even if the system already believes it’s already bound.

clear(color=(0.0, 0.0, 0.0, 0.0), *, depth: float = 1.0, normalized: bool = False, viewport: Optional[Tuple[int, int, int, int]] = None)[source]#

Clears the framebuffer:

# Clear framebuffer using the color red in normalized form
fbo.clear(color=(1.0, 0.0, 0.0, 1.0), normalized=True)
# Clear the framebuffer using arcade's colors (not normalized)
fb.clear(color=arcade.color.WHITE)

If the background color is an RGB value instead of RGBA` we assume alpha value 255.

Parameters
  • color (tuple) – A 3 or 4 component tuple containing the color

  • depth (float) – Value to clear the depth buffer (unused)

  • normalized (bool) – If the color values are normalized or not

  • viewport (Tuple[int, int, int, int]) – The viewport range to clear

read(*, viewport=None, components=3, attachment=0, dtype='f1') bytearray[source]#

Read framebuffer pixels

Parameters
  • viewport (Tuple[int,int,int,int]) – The x, y, with, height to read

  • components (int) –

  • attachment (int) – The attachment id to read from

  • dtype (str) – The data type to read

Returns

pixel data as a bytearray

resize()[source]#

Detects size changes in attachments. This will reset the viewport to 0, 0, width, height.

delete()[source]#

Destroy the underlying OpenGL resource. Don’t use this unless you know exactly what you are doing.

static delete_glo(ctx, framebuffer_id)[source]#

Destroys the framebuffer object

Parameters
  • ctx – OpenGL context

  • framebuffer_id – Framebuffer to destroy (glo)

DefaultFrameBuffer#

class arcade.gl.framebuffer.DefaultFrameBuffer(ctx: Context)[source]#

Bases: arcade.gl.framebuffer.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: Optional[Tuple[int, int, int, int]]#

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 to None

# 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)