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 – The flag to enable.
disable(flag)

Disable flags.

Valid flags are:

Parameters:flag – 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. Use this method to copy framebuffer content into a texture. Use this method to downsample framebuffers, it will allow to read the framebuffer’s content. Use this method to 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) – Width, height.
  • components (int) – The number of components 1, 2, 3 or 4.
  • alignment (int) – The byte alignment 1, 2, 4 or 8.
  • data (bytes) – Content of the image.
Keyword Arguments:
 

floats (bool) – Use floating point precision.

Returns:

texture

Return type:

Texture

depth_texture(size, data=None, samples=0) → Texture

Create a Texture.

Parameters:
  • size (tuple) – Width, height.
  • alignment (int) – The byte alignment 1, 2, 4 or 8.
  • data (bytes) – Content of the image.
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:
  • program (Program) – The program used by render and transform.
  • buffer (Buffer) – The buffer.
  • attributes (list) – A list of attribute names.
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 the 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

Create a Shader.

Parameters:source (str) – The source code in GLSL.
Returns:geometry shader
Return type:Shader
tess_evaluation_shader(source) → Shader

Create a Shader.

Parameters:source (str) – The source code in GLSL.
Returns:tesselation evaluation shader
Return type:Shader
tess_control_shader(source) → Shader

Create a Shader.

Parameters:source (str) – The source code in GLSL.
Returns:tesselation control shader
Return type:Shader
framebuffer(color_attachments, depth_attachment=None) → Framebuffer

Create a Framebuffer.

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

Create a Renderbuffer.

Parameters:
  • size (tuple) – The width and height.
  • components (int) – The number of components 1, 2, 3 or 4.
Keyword Arguments:
 
  • samples – The number of samples. Value 0 means no multisample format.
  • floats – Use floating point precision.
Returns:

renderbuffer

Return type:

Renderbuffer

depth_renderbuffer(size, samples=0) → Renderbuffer

Create a Renderbuffer.

Parameters:size (tuple) – The width and height.
Keyword Arguments:
 samples – The number of samples. Value 0 means no multisample format.
Returns:depth renderbuffer
Return type:Renderbuffer
compute_shader(source) → ComputeShader

Create a ComputeShader.

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.

default_texture_unit

int – The default texture unit.

max_texture_units

int – The max texture units.

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.