OpenGL Context

class arcade.context.ArcadeContext(window: BaseWindow, gc_mode: str = 'context_gc', gl_api: str = 'gl')[source]

Bases: Context

An OpenGL context implementation for Arcade with added custom features. This context is normally accessed through arcade.Window.ctx.

Pyglet users can use the base Context class and extend that as they please.

This is part of the low level rendering API in arcade and is mainly for more advanced usage

Parameters:
  • window – The pyglet window

  • gc_mode – The garbage collection mode for opengl objects. auto is just what we would expect in python while context_gc (default) requires you to call Context.gc(). The latter can be useful when using multiple threads when it’s not clear what thread will gc the object.

bind_window_block() None[source]

Binds the projection and view uniform buffer object. This should always be bound to index 0 so all shaders have access to them.

get_framebuffer_image(fbo: Framebuffer, components: int = 4, flip: bool = True) Image[source]

Shortcut method for reading data from a framebuffer and converting it to a PIL image.

Parameters:
  • fbo – Framebuffer to get image from

  • components – Number of components to read

  • flip – Flip the image upside down

load_compute_shader(path: str | Path, common: Iterable[str | Path] = ()) ComputeShader[source]

Loads a compute shader from file. This methods supports resource handles.

Example:

ctx.load_compute_shader(":shader:compute/do_work.glsl")
Parameters:
  • path – Path to texture

  • common – Common source injected into compute shader

load_program(*, vertex_shader: str | Path, fragment_shader: str | Path | None = None, geometry_shader: str | Path | None = None, tess_control_shader: str | Path | None = None, tess_evaluation_shader: str | Path | None = None, common: Iterable[str | Path] = (), defines: Dict[str, Any] | None = None, varyings: Sequence[str] | None = None, varyings_capture_mode: str = 'interleaved') Program[source]

Create a new program given a file names that contain the vertex shader and fragment shader. Note that fragment and geometry shader are optional for when transform shaders are loaded.

This method also supports the resource handles.

Example:

# The most common use case if having a vertex and fragment shader
program = window.ctx.load_program(
    vertex_shader="vert.glsl",
    fragment_shader="frag.glsl",
)
Parameters:
  • vertex_shader – path to vertex shader

  • fragment_shader – path to fragment shader (optional)

  • geometry_shader – path to geometry shader (optional)

  • tess_control_shader – Tessellation Control Shader

  • tess_evaluation_shader – Tessellation Evaluation Shader

  • common – Common files to be included in all shaders

  • defines – Substitute #define values in the source

  • varyings – The name of the out attributes in a transform shader. This is normally not necessary since we auto detect them, but some more complex out structures we can’t detect.

  • varyings_capture_mode – The capture mode for transforms. "interleaved" means all out attribute will be written to a single buffer. "separate" means each out attribute will be written separate buffers. Based on these settings the transform() method will accept a single buffer or a list of buffer.

load_texture(path: str | Path, *, flip: bool = True, build_mipmaps: bool = False, internal_format: int | None = None, compressed: bool = False) Texture2D[source]

Loads and creates an OpenGL 2D texture. Currently, all textures are converted to RGBA for simplicity.

Example:

# Load a texture in current working directory
texture = window.ctx.load_texture("background.png")

# Load a texture using Arcade resource handle
texture = window.ctx.load_texture(":textures:background.png")

# Load and compress a texture
texture = window.ctx.load_texture(
    ":textures:background.png",
    internal_format=gl.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
    compressed=True,
)
Parameters:
  • path – Path to texture

  • flip – Flips the image upside down

  • build_mipmaps – Build mipmaps for the texture

  • internal_format – The internal format of the texture. This can be used to override the default internal format when using sRGBA or compressed textures.

  • compressed – If the internal format is a compressed format meaning your texture will be compressed by the GPU.

pyglet_rendering()[source]

Context manager for doing rendering with pyglet ensuring context states are reverted. This affects things like blending.

reset() None[source]

Reset context flags and other states. This is mostly used in unit testing.

shader_inc(source: str) str[source]

Parse a shader source looking for #include directives and replace them with the contents of the included file.

The #include directive must be on its own line and the file and the path should use a resource handle.

Example:

#include :my_shader:lib/common.glsl
Parameters:

source – Shader

atlas_size: Tuple[int, int] = (512, 512)
default_atlas

The default texture atlas. This is created when arcade is initialized. All sprite lists will use use this atlas unless a different atlas is passed in the arcade.SpriteList constructor.

Type:

TextureAtlas

projection_matrix

Get the current projection matrix. This 4x4 float32 matrix is calculated by cameras.

This property simply gets and sets pyglet’s projection matrix.

Type:

pyglet.math.Mat4

view_matrix

Get the current view matrix. This 4x4 float32 matrix is calculated when setting view_matrix_2d.

This property simply gets and sets pyglet’s view matrix.

Type:

pyglet.math.Mat4

viewport

Get or set the viewport for the currently active framebuffer. The viewport simply describes what pixels of the screen OpenGL should render to. Normally it would be the size of the window’s framebuffer:

# 4:3 screen
ctx.viewport = 0, 0, 800, 600
# 1080p
ctx.viewport = 0, 0, 1920, 1080
# Using the current framebuffer size
ctx.viewport = 0, 0, *ctx.screen.size
Type:

tuple (x, y, width, height)