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 (Version) – OpenGL version.
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.

It is reccommanded to use a separate framebuffer when rendering.

The size is not really important for the default framebuffer. It is only useful to get a viewport with a visible size. Size (1, 1) is great to save memory, however makes harder to detect when the viewport was forgotten to set.

Keyword Arguments:
 
  • size (tuple) – Initial framebuffer size.
  • require (Version) – OpenGL version.
Returns:

context

Return type:

Context

class Context

Create a Context using:

The create_context() must be used when rendering in a window. The create_standalone_context() must be used when rendering without a window.

Members:

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 not None 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:
  • dst (Buffer) – Destination buffer.
  • src (Buffer) – Source buffer.
  • size (int) – Size to copy.
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:
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:

Buffer

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:

Texture

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:

Texture3D

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:

Texture

vertex_array(program, content, index_buffer=None) → VertexArray

Create a VertexArray.

Parameters:
  • program (Program) – The program used by render() and transform().
  • content (list) – A list of (buffer, format, attributes).
  • index_buffer (Buffer) – An index buffer.
Returns:

vertex array

Return type:

VertexArray

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:
Returns:

vertex array

Return type:

VertexArray

program(shaders, varyings=()) → Program

Create a Program object.

Only linked programs will be returned.

For more information please see: Program and Shader

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:

Program

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'])
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:

Framebuffer

renderbuffer(size, components=4, samples=0, floats=False) → Renderbuffer

Renderbuffer objects are OpenGL objects that contain images. They are created and used specifically with Framebuffer 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:

Renderbuffer

depth_renderbuffer(size, samples=0) → Renderbuffer

Renderbuffer objects are OpenGL objects that contain images. They are created and used specifically with Framebuffer 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 – The default framebuffer.

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.