Context¶
-
create_context
(require=None) → Context¶ Create a ModernGL context by loading OpenGL functions from an existing OpenGL context. An OpenGL context must exists. If rendering is done without a window please use the
create_standalone_context()
instead.Keyword Arguments: require (int) – OpenGL version code. Returns: context Return type: Context
-
create_standalone_context
(size=(256, 256), require=None) → Context¶ Create a standalone ModernGL context. This method will create a hidden window with the initial size given in the parameters. This will set the initial viewport as well.
Keyword Arguments: - size (tuple) – Initial framebuffer size.
- require (int) – OpenGL version code.
Returns: context
Return type:
-
class
Context
¶ Create a
Context
using:The
create_context()
must be used when rendering in a window. Thecreate_standalone_context()
must be used when rendering without a window.Members:
Context.program()
Context.vertex_shader()
Context.fragment_shader()
Context.geometry_shader()
Context.tess_evaluation_shader()
Context.tess_control_shader()
Context.buffer()
Context.simple_vertex_array()
Context.vertex_array()
Context.texture()
Context.depth_texture()
Context.renderbuffer()
Context.depth_renderbuffer()
Context.framebuffer()
-
clear
(red=0.0, green=0.0, blue=0.0, alpha=0.0, viewport=None)¶ Clear the framebuffer.
Values must be in
(0, 255)
range. If the viewport is notNone
then scrissor test will be used to clear the given viewport.If the viewport is a 2-tuple it will clear the
(0, 0, width, height)
where(width, height)
is the 2-tuple.If the viewport is a 4-tuple it will clear the given viewport.
Parameters: - red (float) – color component.
- green (float) – color component.
- blue (float) – color component.
- alpha (float) – alpha component.
Keyword Arguments: viewport (tuple) – The viewport.
-
enable
(flag)¶ Enable flags.
Valid flags are:
Parameters: flag (EnableFlag) – The flag to enable.
-
disable
(flag)¶ Disable flags.
Valid flags are:
Parameters: flag (EnableFlag) – The flag to disable.
-
finish
()¶ Wait for all drawing commands to finish.
-
copy_buffer
(dst, src, size=-1, read_offset=0, write_offset=0)¶ Copy buffer content.
Parameters: Keyword Arguments: - read_offset (int) – Read offset.
- write_offset (int) – Write offset.
-
copy_framebuffer
(dst, src)¶ Copy framebuffer content.
Use this method to:
- blit framebuffers.
- copy framebuffer content into a texture.
- downsample framebuffers. (it will allow to read the framebuffer’s content)
- downsample a framebuffer directly to a texture.
Parameters: - dst (Framebuffer or Texture) – Destination framebuffer or texture.
- src (Framebuffer) – Source framebuffer.
-
buffer
(data=None, reserve=0, dynamic=False) → Buffer¶ Create a
Buffer
.Parameters: data (bytes) – Content of the new buffer.
Keyword Arguments: - reserve (int) – The number of bytes to reserve.
- dynamic (bool) – Treat buffer as dynamic.
Returns: buffer
Return type:
-
texture
(size, components, data=None, samples=0, floats=False) → Texture¶ Create a
Texture
.Parameters: - size (tuple) – The width and height of the texture.
- components (int) – The number of components 1, 2, 3 or 4.
- data (bytes) – Content of the texture.
Keyword Arguments: - samples (int) – The number of samples. Value 0 means no multisample format.
- alignment (int) – The byte alignment 1, 2, 4 or 8.
- floats (bool) – Use floating point precision.
Returns: texture
Return type:
-
texture3d
(size, components, data=None, floats=False) → Texture3D¶ Create a
Texture3D
.Parameters: - size (tuple) – The width, height and depth of the texture.
- components (int) – The number of components 1, 2, 3 or 4.
- data (bytes) – Content of the texture.
Keyword Arguments: - alignment (int) – The byte alignment 1, 2, 4 or 8.
- floats (bool) – Use floating point precision.
Returns: texture
Return type:
-
depth_texture
(size, data=None, samples=0) → Texture¶ Create a
Texture
.Parameters: - size (tuple) – The width and height of the texture.
- data (bytes) – Content of the texture.
Keyword Arguments: - samples (int) – The number of samples. Value 0 means no multisample format.
- alignment (int) – The byte alignment 1, 2, 4 or 8.
Returns: depth texture
Return type:
-
vertex_array
(program, content, index_buffer=None) → VertexArray¶ Create a
VertexArray
.Parameters: - program (Program) – The program used by
render()
andtransform()
. - content (list) – A list of (buffer, format, attributes).
- index_buffer (Buffer) – An index buffer.
Returns: vertex array
Return type: - program (Program) – The program used by
-
simple_vertex_array
(program, buffer, attributes) → VertexArray¶ Create a
VertexArray
.This is an alias for:
format = detect_format(program, attributes) vertex_array(program, [(buffer, format, attributes)])
Parameters: - program (Program) – The program used by
render()
andtransform()
. - buffer (Buffer) – The buffer.
- attributes (list) – A list of attribute names.
Returns: vertex array
Return type: - program (Program) – The program used by
-
program
(shaders, varyings=()) → Program¶ Create a
Program
object.Only linked programs will be returned.
For more information please see:
Program
andShader
A single shader in the shaders parameter is also accepted. The varyings are only used when a transform program is created.
Parameters: - shaders (list) – A list of
Shader
objects. - varyings (list) – A list of varying names.
Returns: program
Return type: Examples
A simple program designed for rendering:
>>> my_render_program = ctx.program([ ... ctx.vertex_shader(''' ... #version 330 ... ... in vec2 vert; ... ... void main() { ... gl_Position = vec4(vert, 0.0, 1.0); ... } ... '''), ... ctx.fragment_shader(''' ... #version 330 ... ... out vec4 color; ... ... void main() { ... color = vec4(0.3, 0.5, 1.0, 1.0); ... } ... '''), ... ])
A simple program designed for transforming:
>>> my_vertex_shader = ctx.vertex_shader(''' ... #version 330 ... ... in vec4 vert; ... out float vert_length; ... ... void main() { ... vert_length = length(vert); ... } ... ''') >>> my_transform_program = ctx.program(my_vertex_shader, ['vert_length'])
- shaders (list) – A list of
-
vertex_shader
(source) → Shader¶ The Vertex Shader is a programmable Shader stage in the rendering pipeline that handles the processing of individual vertices.
Vertex shaders are fed Vertex Attribute data, as specified from a vertex array object by a drawing command. A vertex shader receives a single vertex from the vertex stream and generates a single vertex to the output vertex stream.
Parameters: source (str) – The source code in GLSL. Returns: vertex shader Return type: Shader Examples
Create a simple vertex shader:
>>> my_vertex_shader = ctx.vertex_shader(''' ... #version 330 ... ... in vec2 vert; ... ... void main() { ... gl_Position = vec4(vert, 0.0, 1.0); ... } ... ''')
-
fragment_shader
(source) → Shader¶ A Fragment Shader is the Shader stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value.
Parameters: source (str) – The source code in GLSL. Returns: fragment shader Return type: Shader Examples
Create a simple fragment shader:
>>> my_fragment_shader = ctx.fragment_shader(''' ... #version 330 ... ... out vec4 color; ... ... void main() { ... color = vec4(0.3, 0.5, 1.0, 1.0); ... } ... ''')
-
geometry_shader
(source) → Shader¶ A Geometry Shader is a Shader program written in GLSL that governs the processing of Primitives. Geometry shaders reside between the Vertex Shaders (or the optional Tessellation stage) and the fixed-function Vertex Post-Processing stage.
A geometry shader is optional and does not have to be used.
Parameters: source (str) – The source code in GLSL. Returns: geometry shader Return type: Shader
-
tess_evaluation_shader
(source) → Shader¶ Tessellation is the Vertex Processing stage in the OpenGL rendering pipeline where patches of vertex data are subdivided into smaller Primitives.
The Tessellation Evaluation Shader takes the tessellated patch and computes the vertex values for each generated vertex.
Parameters: source (str) – The source code in GLSL. Returns: tesselation evaluation shader Return type: Shader
-
tess_control_shader
(source) → Shader¶ The Tessellation Control Shader (TCS) determines how much tessellation to do. It can also adjust the actual patch data, as well as feed additional patch data to later stages. The Tessellation Control Shader is optional.
Parameters: source (str) – The source code in GLSL. Returns: tesselation control shader Return type: Shader
-
framebuffer
(color_attachments, depth_attachment=None) → Framebuffer¶ A
Framebuffer
is a collection of buffers that can be used as the destination for rendering. The buffers for Framebuffer objects reference images from either Textures or Renderbuffers.Parameters: - color_attachments (list) – A list of Texture or Renderbuffer objects.
- depth_attachment (Renderbuffer or Texture) – A Texture or Renderbuffer object.
Returns: framebuffer
Return type:
-
renderbuffer
(size, components=4, samples=0, floats=False) → Renderbuffer¶ Renderbuffer
objects are OpenGL objects that contain images. They are created and used specifically withFramebuffer
objects.Parameters: - size (tuple) – The width and height of the renderbuffer.
- components (int) – The number of components 1, 2, 3 or 4.
Keyword Arguments: - samples (int) – The number of samples. Value 0 means no multisample format.
- floats (bool) – Use floating point precision.
Returns: renderbuffer
Return type:
-
depth_renderbuffer
(size, samples=0) → Renderbuffer¶ Renderbuffer
objects are OpenGL objects that contain images. They are created and used specifically withFramebuffer
objects.Parameters: size (tuple) – The width and height of the renderbuffer. Keyword Arguments: samples (int) – The number of samples. Value 0 means no multisample format. Returns: depth renderbuffer Return type: Renderbuffer
-
compute_shader
(source) → ComputeShader¶ A
ComputeShader
is a Shader Stage that is used entirely for computing arbitrary information. While it can do rendering, it is generally used for tasks not directly related to drawing.Parameters: source (str) – The source of the compute shader. Returns: compute shader program Return type: ComputeShader
-
line_width
¶ float – Set the default line width.
-
point_size
¶ float – Set the default point size.
-
viewport
¶ tuple – The viewport.
Reading this property may force the GPU to sync. Use this property to set the viewport only.
-
max_samples
¶ int – The max samples.
-
max_integer_samples
¶ int – The max integer samples.
-
max_texture_units
¶ int – The max texture units.
-
default_texture_unit
¶ int – The default texture unit.
-
default_framebuffer
¶ Framebuffer – The default framebuffer.
-
wireframe
¶ bool – Wireframe settings for debugging.
-
error
¶ str – The result of glGetError() but human readable. This values is provided for debug purposes only.
-
vendor
¶ str – The vendor.
-
renderer
¶ str – The renderer.
-
version
¶ str – The OpenGL version.
-
version_code
¶ int – The OpenGL version.
-
info
¶ dict – The result of multiple glGet.