Context#
- class moderngl.Context#
Create#
ModernGL Objects#
- Context.program(*, vertex_shader: str, fragment_shader: Optional[str] = None, geometry_shader: Optional[str] = None, tess_control_shader: Optional[str] = None, tess_evaluation_shader: Optional[str] = None, varyings: Tuple[str, ...] = (), fragment_outputs: Optional[Dict[str, int]] = None, varyings_capture_mode: str = 'interleaved') Program #
- Context.simple_vertex_array(program: Program, buffer: Buffer, *attributes: Union[List[str], Tuple[str, ...]], index_buffer: Optional[Buffer] = None, index_element_size: int = 4, mode: Optional[int] = None) VertexArray #
- Context.vertex_array(*args, **kwargs) VertexArray #
- Context.texture(size: Tuple[int, int], components: int, data: Optional[Any] = None, *, samples: int = 0, alignment: int = 1, dtype: str = 'f1', internal_format: Optional[int] = None) Texture #
- Context.depth_texture(size: Tuple[int, int], data: Optional[Any] = None, *, samples: int = 0, alignment: int = 4) Texture #
- Context.texture3d(size: Tuple[int, int, int], components: int, data: Optional[Any] = None, *, alignment: int = 1, dtype: str = 'f1') Texture3D #
- Context.texture_array(size: Tuple[int, int, int], components: int, data: Optional[Any] = None, *, alignment: int = 1, dtype: str = 'f1') TextureArray #
- Context.texture_cube(size: Tuple[int, int], components: int, data: Optional[Any] = None, *, alignment: int = 1, dtype: str = 'f1', internal_format: Optional[int] = None) TextureCube #
- Context.external_texture(glo: int, size: Tuple[int, int], components: int, samples: int, dtype: str) Texture #
- Context.simple_framebuffer(size: Tuple[int, int], components: int = 4, *, samples: int = 0, dtype: str = 'f1') Framebuffer #
- Context.framebuffer(color_attachments: Any = (), depth_attachment: Optional[Union[Texture, Renderbuffer]] = None) Framebuffer #
- Context.renderbuffer(size: Tuple[int, int], components: int = 4, *, samples: int = 0, dtype: str = 'f1') Renderbuffer #
- Context.depth_renderbuffer(size: Tuple[int, int], *, samples: int = 0) Renderbuffer #
- Context.scope(framebuffer: Optional[Framebuffer] = None, enable_only: Optional[int] = None, *, textures: Tuple[Tuple[Texture, int], ...] = (), uniform_buffers: Tuple[Tuple[Buffer, int], ...] = (), storage_buffers: Tuple[Tuple[Buffer, int], ...] = (), samplers: Tuple[Tuple[Sampler, int], ...] = (), enable: Optional[int] = None) Scope #
- Context.query(*, samples: bool = False, any_samples: bool = False, time: bool = False, primitives: bool = False) Query #
- Context.compute_shader(source: str) ComputeShader #
- Context.sampler(repeat_x: bool = True, repeat_y: bool = True, repeat_z: bool = True, filter: Optional[Tuple[int, int]] = None, anisotropy: float = 1.0, compare_func: str = '?', border_color: Optional[Tuple[float, float, float, float]] = None, min_lod: float = -1000.0, max_lod: float = 1000.0, texture: Optional[Texture] = None) Sampler #
- Context.clear_samplers(start: int = 0, end: int = -1) None #
- Context.release() None #
Methods#
- Context.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 #
- Context.enable_only(flags: int) None #
- Context.enable(flags: int) None #
- Context.disable(flags: int) None #
- Context.enable_direct(enum: int) None #
- Context.disable_direct(enum: int) None #
- Context.finish() None #
- Context.copy_buffer(dst: Buffer, src: Buffer, size: int = -1, *, read_offset: int = 0, write_offset: int = 0) None #
- Context.copy_framebuffer(dst: Union[Framebuffer, Texture], src: Framebuffer) None #
- Context.detect_framebuffer(glo: Optional[int] = None) Framebuffer #
- Context.gc() int #
- Context.__enter__()#
- Context.__exit__(exc_type, exc_val, exc_tb)#
Attributes#
- Context.gc_mode#
- Context.objects#
- Context.line_width#
- Context.point_size#
- Context.depth_func#
- Context.blend_func#
- Context.blend_equation#
- Context.viewport#
- Context.scissor#
- Context.version_code#
- Context.screen#
- Context.fbo#
- Context.front_face#
- Context.cull_face#
- Context.wireframe#
- Context.max_samples#
- Context.max_integer_samples#
- Context.max_texture_units#
- Context.default_texture_unit#
- Context.max_anisotropy#
- Context.multisample#
- Context.patch_vertices#
- Context.provoking_vertex#
- Context.polygon_offset#
- Context.error#
- Context.extensions#
- Context.info#
- Context.mglo#
- Context.extra#
Context Flags#
Context flags are used to enable or disable states in the context.
These are not the same enum values as in opengl, but are rather
bit flags so we can or
them together setting multiple states
in a simple way.
These values are available in the Context
object and in the
moderngl
module when you don’t have access to the context.
import moderngl
# From moderngl
ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
# From context
ctx.enable_only(ctx.DEPTH_TEST | ctx.CULL_FACE)
- Context.NOTHING = 0#
- Context.BLEND = 1#
- Context.DEPTH_TEST = 2#
- Context.CULL_FACE = 4#
- Context.RASTERIZER_DISCARD = 8#
- Context.PROGRAM_POINT_SIZE = 16#
Primitive Modes#
- Context.POINTS = 0#
- Context.LINES = 1#
- Context.LINE_LOOP = 2#
- Context.LINE_STRIP = 3#
- Context.TRIANGLES = 4#
- Context.TRIANGLE_STRIP = 5#
- Context.TRIANGLE_FAN = 6#
- Context.LINES_ADJACENCY = 10#
- Context.LINE_STRIP_ADJACENCY = 11#
- Context.TRIANGLES_ADJACENCY = 12#
- Context.TRIANGLE_STRIP_ADJACENCY = 13#
- Context.PATCHES = 14#
Texture Filters#
Also available in the Context
instance
including mode details.
- Context.NEAREST = 9728#
- Context.LINEAR = 9729#
- Context.NEAREST_MIPMAP_NEAREST = 9984#
- Context.LINEAR_MIPMAP_NEAREST = 9985#
- Context.NEAREST_MIPMAP_LINEAR = 9986#
- Context.LINEAR_MIPMAP_LINEAR = 9987#
Blend Functions#
Blend functions are used with Context.blend_func
to control blending operations.
# Default value
ctx.blend_func = ctx.SRC_ALPHA, ctx.ONE_MINUS_SRC_ALPHA
- Context.ZERO = 0#
- Context.ONE = 1#
- Context.SRC_COLOR = 768#
- Context.ONE_MINUS_SRC_COLOR = 769#
- Context.SRC_ALPHA = 770#
- Context.ONE_MINUS_SRC_ALPHA = 771#
- Context.DST_ALPHA = 772#
- Context.ONE_MINUS_DST_ALPHA = 773#
- Context.DST_COLOR = 774#
- Context.ONE_MINUS_DST_COLOR = 775#
Blend Function Shortcuts#
- Context.DEFAULT_BLENDING = (770, 771)#
- Context.ADDITIVE_BLENDING = (1, 1)#
- Context.PREMULTIPLIED_ALPHA = (770, 1)#
Blend Equations#
Used with Context.blend_equation
.
- Context.FUNC_ADD = 32774#
- Context.FUNC_SUBTRACT = 32778#
- Context.FUNC_REVERSE_SUBTRACT = 32779#
- Context.MIN = 32775#
- Context.MAX = 32776#
Other Enums#
- Context.FIRST_VERTEX_CONVENTION = 36429#
- Context.LAST_VERTEX_CONVENTION = 36430#
Examples#
ModernGL Context#
import moderngl
# create a window
ctx = moderngl.create_context()
print(ctx.version_code)
Standalone ModernGL Context#
import moderngl
ctx = moderngl.create_standalone_context()
print(ctx.version_code)
ContextManager#
context_manager.py
1import moderngl
2
3
4class ContextManager:
5 ctx = None
6
7 @staticmethod
8 def get_default_context(allow_fallback_standalone_context=True) -> moderngl.Context:
9 '''
10 Default context
11 '''
12
13 if ContextManager.ctx is None:
14 try:
15 ContextManager.ctx = moderngl.create_context()
16 except:
17 if allow_fallback_standalone_context:
18 ContextManager.ctx = moderngl.create_standalone_context()
19 else:
20 raise
21
22 return ContextManager.ctx
example.py
1from context_manager import ContextManager
2
3ctx = ContextManager.get_default_context()
4print(ctx.version_code)