picogl.backend.state

Render-state descriptors and command helpers for PicoGL backends.

The classes in this module are intentionally backend-neutral: they describe desired OpenGL state and delegate the actual gl calls to a backend object.

Classes

RasterState

Raster State

GLStateManager

Tracks capability state without querying OpenGL.

BlendState

Blend State

DepthState

Represents depth-related state settings used for rendering.

RenderState

Represents the rendering state configuration for a graphics pipeline.

RenderStateApplier

Manages the application of render states to a backend.

GLVertexBuffer

GLAttributeArray

gl Attribute Array

GLViewport

Represents a viewport in OpenGL with specified position and dimensions.

TestGLMesh

Representation of a GL mesh with vertices, optional indices, and attributes.

DrawCommand

Represents a command for draw a mesh with optional rendering parameters.

GLClipPlaneState

gl Clipping Plane State

Functions

gl_value(value) → Any

Return a raw OpenGL value for PicoGL enums or pass raw values through.

Module Contents

picogl.backend.state.gl_value(value: Any) → Any[source]

Return a raw OpenGL value for PicoGL enums or pass raw values through.

class picogl.backend.state.RasterState[source]

Raster State

polygon_mode: Any[source]
line_width: float = 1.0[source]
polygon_offset: tuple[float, float] = (0.0, 0.0)[source]
point_size: float | None = None[source]
apply(backend: Any) → None[source]

Applies rendering properties to the given backend.

This method configures the rendering settings of the provided backend based on its attributes. It will utilize the appropriate backend’s methods to apply the rendering properties.

Parameters: backend (Any): The rendering backend to apply the settings to. The backend should

have either a ‘raster’ attribute with an ‘apply()’ method or methods for setting polygon mode and line width.

Returns: None

class picogl.backend.state.GLStateManager(backend: CapabilityDriver)[source]

Tracks capability state without querying OpenGL.

backend[source]
_caps: dict[int, bool][source]
set_enabled(cap: int, enabled: bool) → None[source]

Sets the state of a specific capability.

This method enables or disables a given capability based on the specified flag. It ensures that the backend is updated only if the capability’s state changes.

Parameters: cap: int

The capability to be enabled or disabled.

enabled: bool

A boolean value indicating whether to enable (True) or disable (False) the capability.

Returns: None

is_enabled(cap: int) → bool[source]

Determines if a specific capability is enabled.

This method checks whether a given capability, identified by its integer representation, is currently enabled. The capability’s value is processed using the gl_value function, and the result is determined by querying the internal data structure.

Parameters: cap: int

The integer representation of the capability to check.

Returns: bool

True if the specified capability is enabled, False otherwise.

class picogl.backend.state.BlendState[source]

Blend State

enabled: bool = False[source]
src: Any[source]
dst: Any[source]
apply(state: GLStateManager)[source]

Applies blending state to the given GLStateManager.

This method adjusts the blending settings in the given OpenGL state manager based on the current configuration. If the backend of the GLStateManager supports blending and has a custom implementation, that custom behavior is invoked; otherwise, standard OpenGL blending state is configured.

Parameters: state (GLStateManager): The state manager to which the blending configuration

should be applied.

class picogl.backend.state.DepthState(test: bool = True, write: bool = True, enabled: bool | None = None)[source]

Represents depth-related state settings used for rendering.

This class encapsulates settings for depth test and depth write functionalities, commonly used in rendering pipelines. It ensures immutability, allowing safe usage in contexts where concurrent access or state integrity is required. The class provides mechanisms to apply depth states to an OpenGL rendering backend.

test[source]

Indicates whether depth testing is enabled (True) or disabled (False).

Type:

bool

write[source]

Indicates whether depth writing is enabled (True) or disabled (False).

Type:

bool

test: bool = True[source]
write: bool = True[source]
property enabled: bool[source]

Checks if the feature is currently enabled.

This property determines whether the feature or functionality controlled by the associated test attribute is active or not.

Returns:

True if the feature is enabled, False otherwise.

Return type:

bool

apply(state: GLStateManager)[source]

Applies depth-related settings to the given GLStateManager.

The method interacts with the provided GLStateManager to configure depth test and depth write capabilities. If the backend associated with the GLStateManager supports depth operations directly, it delegates the application of these settings to the backend; otherwise, it sets the appropriate GL pipeline capabilities and depth write settings.

Parameters: state (GLStateManager): The state manager used to manage OpenGL states and

capabilities.

Raises: None

class picogl.backend.state.RenderState(*, raster: RasterState | None = None, depth: DepthState | None = None, blend: BlendState | bool | None = None, blend_src: Any = GLBlendFactor.SRC_ALPHA, blend_dst: Any = GLBlendFactor.ONE_MINUS_SRC_ALPHA, depth_test: bool | None = None, depth_write: bool | None = None, line_width: float | None = None, polygon_mode: Any | None = None, polygon_offset: tuple[float, float] | None = None, point_size: float | None = None, program_point_size: bool = False, cull_face: bool = False, lighting: bool = False)[source]

Represents the rendering state configuration for a graphics pipeline.

The RenderState class encapsulates various rendering states such as blending, depth testing, polygon rendering modes, and other state-related settings. This class is intended to provide an immutable configuration object for managing rendering behavior.

blend[source]

Whether blending is enabled.

Type:

bool

blend_src[source]

The source blend factor for blending operations.

Type:

Any

blend_dst[source]

The destination blend factor for blending operations.

Type:

Any

depth_test[source]

Indicates whether depth testing is enabled.

Type:

bool

depth_write[source]

Specifies if writing to the depth buffer is enabled.

Type:

bool

line_width[source]

The width of rendered lines.

Type:

float

polygon_mode[source]

The polygon rendering mode.

Type:

Any

polygon_offset[source]

The polygon offset as a (factor, units) tuple.

Type:

tuple[float, float]

point_size[source]

The size of rendered points. None indicates default size.

Type:

float | None

program_point_size[source]

Indicates whether point size is controlled by the program.

Type:

bool

cull_face[source]

Whether face culling is enabled.

Type:

bool

lighting[source]

Whether lighting calculations are enabled.

Type:

bool

raster()[source]

Retrieve the rasterization state represented by a RasterState object.

depth()[source]

Retrieve the depth state represented by a DepthState object.

blend_state()[source]

Retrieve the blend state represented by a BlendState object.

blend: bool = False[source]
blend_src: Any[source]
blend_dst: Any[source]
depth_test: bool = True[source]
depth_write: bool = True[source]
line_width: float = 1.0[source]
polygon_mode: Any[source]
polygon_offset: tuple[float, float] = (0.0, 0.0)[source]
point_size: float | None = None[source]
program_point_size: bool = False[source]
cull_face: bool = False[source]
lighting: bool = False[source]
property raster: RasterState[source]

Property to retrieve the rasterization state.

The raster property gathers various rendering state settings related to rasterization and combines them into a RasterState object.

Returns:

An object representing the rasterization settings, including polygon mode, line width, polygon offset, and point size.

Return type:

RasterState

property depth: DepthState[source]

Returns the DepthState object representing the current depth state.

The DepthState object encapsulates whether depth testing is enabled, and whether depth writing is allowed. This property provides a way to retrieve the current state of depth testing and writing configured in the system.

Returns:

An object combining depth test and depth write settings, where test reflects the current depth test state and write reflects the current depth write state.

Return type:

DepthState

property blend_state: BlendState[source]

Retrieve the blend state of the object.

Provides the configuration of the blending state based on the object’s current attributes.

Returns:

An object representing the blend state, including whether blending is enabled and the source and destination blend factors.

Return type:

BlendState

class picogl.backend.state.RenderStateApplier(backend: Any)[source]

Manages the application of render states to a backend.

This class ensures that a given render state is applied incrementally to a backend, avoiding redundant operations. The purpose is to optimize rendering performance by minimizing redundant state changes. The apply method orchestrates the comparison of the current state with a new state and applies only the necessary differences.

backend[source]

The rendering backend to which the render states are applied.

Type:

Any

current[source]

The current render state that has been applied. This is updated each time a new state is applied.

Type:

RenderState | None

apply(state

RenderState): Applies a given render state to the backend. Only applies differences compared to the currently applied state to optimize the rendering process.

backend[source]
current: RenderState | None = None[source]
apply(state: RenderState)[source]
class picogl.backend.state.GLVertexBuffer(data: numpy.ndarray)[source]
data[source]
bind_legacy()[source]
class picogl.backend.state.GLAttributeArray[source]

gl Attribute Array

size: int[source]
dtype: Any[source]
stride: int[source]
pointer: Any[source]
enable_legacy(kind: picogl.backend.gl.state.client.GLClientState)[source]

Enables the legacy client state for a specific OpenGL client state.

This method enables the specified OpenGL client state by calling the appropriate gl_enable_legacy_client_state function and then uses a corresponding handler to configure the relevant pointer settings for the client state. If the given client state is unsupported by the handlers, a RuntimeError is raised.

Parameters: kind (GLClientState): The type of OpenGL client state to enable. This determines the specific handler to be invoked for configuring pointer settings.

Raises: RuntimeError: If the argument kind cannot be processed by any of the handlers provided in the method.

class picogl.backend.state.GLViewport[source]

Bases: picogl.core.protocol.applicable.Applicable

Represents a viewport in OpenGL with specified position and dimensions.

This class encapsulates the functionality of an OpenGL viewport, allowing the user to define the x and y position, as well as the width and height of the viewport. The apply method sets the viewport in the OpenGL context using the specified attributes.

x[source]

int The x-coordinate of the lower-left corner of the viewport.

y[source]

int The y-coordinate of the lower-left corner of the viewport.

width[source]

int The width of the viewport in pixels.

height[source]

int The height of the viewport in pixels.

x: int = 0[source]
y: int = 0[source]
width: int = 0[source]
height: int = 0[source]
apply()[source]

Adjusts the viewport to the specified dimensions and coordinates.

The apply method sets the area of the window where OpenGL draws content. It defines the x and y coordinates of the lower-left corner of the viewport, as well as the width and height of the viewable region.

Raises:
  • This function may raise an OpenGL context-specific error if incorrect –

  • parameters are used. –

property size: tuple[int, int][source]
class picogl.backend.state.TestGLMesh(vertices, indices=None)[source]

Representation of a GL mesh with vertices, optional indices, and attributes.

This class allows for the creation and management of a graphical mesh, including the specification of vertices, optional indices, and associated attributes for rendering purposes. It provides methods to add attributes to the mesh and handle its draw using OpenGL.

vertices[source]

The vertices of the mesh as provided by the user.

Type:

Any

indices[source]

Optional indices for indexed rendering, allowing for more efficient rendering of shared vertices.

Type:

Optional[Any]

attributes[source]

A list of attribute arrays associated with the mesh, representing the additional data (e.g., colors, texture coordinates) needed for rendering.

Type:

list[GLAttributeArray]

vertices[source]
indices = None[source]
attributes: list[GLAttributeArray] = [][source]
add_attribute(attr: GLAttributeArray)[source]

Adds a GLAttributeArray instance to the attributes list.

Attributes represent specific data or configuration required for the functional operation of the object. This method allows adding new attributes dynamically to the internal list.

Parameters:

attr (GLAttributeArray) – The attribute object to add to the list.

draw()[source]

Draw the object using the attributes and indices provided. This method prepares and manages OpenGL draw by enabling necessary vertex attributes and rendering either indexed or non-indexed primitives based on the presence of indices.

Raises:

RuntimeError – If an OpenGL error occurs during the draw process.

class picogl.backend.state.DrawCommand[source]

Represents a command for draw a mesh with optional rendering parameters.

This class serves as a utility to encapsulate mesh draw operations alongside related rendering state, texture, and mode settings. It integrates with a rendering backend to facilitate the execution of draw commands.

mesh[source]

The mesh object to be drawn. Must support a draw method or be used with a backend capable of draw it.

Type:

Any

mode[source]

Optional draw mode. Determines how the mesh should be rendered. Not all rendering backends require this attribute.

Type:

int | None

texture[source]

Optional texture or reference to a texture. If specified, the texture is bound before draw the mesh.

Type:

GLTextureDriver | int | None

state[source]

Optional rendering state to apply before executing the draw command.

Type:

RenderState | None

execute(backend

Any): Executes the draw command by applying the rendering state, binding the texture, and invoking the appropriate backend-specific draw operation.

mesh: Any[source]
mode: int | None = None[source]
texture: picogl.texture.gltexture_driver.GLTextureDriver | int | None = None[source]
state: RenderState | None = None[source]
execute(backend: Any)[source]
class picogl.backend.state.GLClipPlaneState[source]

gl Clipping Plane State

enabled0: bool = False[source]
enabled1: bool = False[source]
plane0: numpy.ndarray = False[source]
plane1: numpy.ndarray = False[source]
apply(state: GLStateManager)[source]