moderngl#

Attributes#

Attributes available in the root moderngl module. Some may be listed in their original sub-module, but they are imported during initialization.

Context Flags#

Also available in the Context instance including mode details.

moderngl.NOTHING: moderngl.Constant#

Represents no states. Can be used with Context.enable_only() to disable all states.

moderngl.BLEND: moderngl.Constant#

Enable/disable blending

moderngl.DEPTH_TEST: moderngl.Constant#

Enable/disable depth testing

moderngl.CULL_FACE: moderngl.Constant#

Enable/disable face culling

moderngl.RASTERIZER_DISCARD: moderngl.Constant#

Enable/disable rasterization

moderngl.PROGRAM_POINT_SIZE: moderngl.Constant#

Enables gl_PointSize in vertex or geometry shaders.

When enabled we can write to gl_PointSize in the vertex shader to specify the point size for each individual point.

If this value is not set in the shader the behavior is undefined. This means the points may or may not appear depending if the drivers enforce some default value for gl_PointSize.

When disabled Context.point_size is used.

Type

Context flag

Primitive Modes#

Also available in the Context instance including mode details.

moderngl.POINTS: moderngl.Constant#

Each vertex represents a point

moderngl.LINES: moderngl.Constant#

Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on. If the user specifies a non-even number of vertices, then the extra vertex is ignored.

moderngl.LINE_LOOP: moderngl.Constant#

As line strips, except that the first and last vertices are also used as a line. Thus, you get n lines for n input vertices. If the user only specifies 1 vertex, the drawing command is ignored. The line between the first and last vertices happens after all of the previous lines in the sequence.

moderngl.LINE_STRIP: moderngl.Constant#

The adjacent vertices are considered lines. Thus, if you pass n vertices, you will get n-1 lines. If the user only specifies 1 vertex, the drawing command is ignored.

moderngl.TRIANGLES: moderngl.Constant#

Vertices 0, 1, and 2 form a triangle. Vertices 3, 4, and 5 form a triangle. And so on.

moderngl.TRIANGLE_STRIP: moderngl.Constant#

Every group of 3 adjacent vertices forms a triangle. The face direction of the strip is determined by the winding of the first triangle. Each successive triangle will have its effective face order reversed, so the system compensates for that by testing it in the opposite way. A vertex stream of n length will generate n-2 triangles.

moderngl.TRIANGLE_FAN: moderngl.Constant#

The first vertex is always held fixed. From there on, every group of 2 adjacent vertices form a triangle with the first. So with a vertex stream, you get a list of triangles like so: (0, 1, 2) (0, 2, 3), (0, 3, 4), etc. A vertex stream of n length will generate n-2 triangles.

moderngl.LINES_ADJACENCY: moderngl.Constant#

These are special primitives that are expected to be used specifically with geomtry shaders. These primitives give the geometry shader more vertices to work with for each input primitive. Data needs to be duplicated in buffers.

moderngl.LINE_STRIP_ADJACENCY: moderngl.Constant#

These are special primitives that are expected to be used specifically with geomtry shaders. These primitives give the geometry shader more vertices to work with for each input primitive. Data needs to be duplicated in buffers.

moderngl.TRIANGLES_ADJACENCY: moderngl.Constant#

These are special primitives that are expected to be used specifically with geomtry shaders. These primitives give the geometry shader more vertices to work with for each input primitive. Data needs to be duplicated in buffers.

moderngl.TRIANGLE_STRIP_ADJACENCY: moderngl.Constant#

These are special primitives that are expected to be used specifically with geomtry shaders. These primitives give the geometry shader more vertices to work with for each input primitive. Data needs to be duplicated in buffers.

moderngl.PATCHES: moderngl.Constant#

primitive type can only be used when Tessellation is active. It is a primitive with a user-defined number of vertices, which is then tessellated based on the control and evaluation shaders into regular points, lines, or triangles, depending on the TES’s settings.

Texture Filters#

Also available in the Context instance including mode details.

moderngl.NEAREST: moderngl.Constant#

Returns the value of the texture element that is nearest (in Manhattan distance) to the specified texture coordinates.

moderngl.LINEAR: moderngl.Constant#

Returns the weighted average of the four texture elements that are closest to the specified texture coordinates. These can include items wrapped or repeated from other parts of a texture, depending on the values of texture repeat mode, and on the exact mapping.

moderngl.NEAREST_MIPMAP_NEAREST: moderngl.Constant#

Chooses the mipmap that most closely matches the size of the pixel being textured and uses the NEAREST` criterion (the texture element closest to the specified texture coordinates) to produce a texture value.

moderngl.LINEAR_MIPMAP_NEAREST: moderngl.Constant#

Chooses the mipmap that most closely matches the size of the pixel being textured and uses the LINEAR criterion (a weighted average of the four texture elements that are closest to the specified texture coordinates) to produce a texture value.

moderngl.NEAREST_MIPMAP_LINEAR: moderngl.Constant#

Chooses the two mipmaps that most closely match the size of the pixel being textured and uses the NEAREST criterion (the texture element closest to the specified texture coordinates ) to produce a texture value from each mipmap. The final texture value is a weighted average of those two values.

moderngl.LINEAR_MIPMAP_LINEAR: moderngl.Constant#

Chooses the two mipmaps that most closely match the size of the pixel being textured and uses the LINEAR criterion (a weighted average of the texture elements that are closest to the specified texture coordinates) to produce a texture value from each mipmap. The final texture value is a weighted average of those two values.

Blend Functions#

Also available in the Context instance including mode details.

moderngl.ZERO: moderngl.Constant#

(0,0,0,0)

moderngl.ONE: moderngl.Constant#

(1,1,1,1)

moderngl.SRC_COLOR: moderngl.Constant#

(Rs0/kR,Gs0/kG,Bs0/kB,As0/kA)

moderngl.ONE_MINUS_SRC_COLOR: moderngl.Constant#

(1,1,1,1) - (Rs0/kR,Gs0/kG,Bs0/kB,As0/kA)

moderngl.SRC_ALPHA: moderngl.Constant#

(As0/kA,As0/kA,As0/kA,As0/kA)

moderngl.ONE_MINUS_SRC_ALPHA: moderngl.Constant#

(1,1,1,1) - (As0/kA,As0/kA,As0/kA,As0/kA)

moderngl.DST_ALPHA: moderngl.Constant#

(Ad/kA,Ad/kA,Ad/kA,Ad/kA)

moderngl.ONE_MINUS_DST_ALPHA: moderngl.Constant#

(1,1,1,1) - (Ad/kA,Ad/kA,Ad/kA,Ad/kA)

moderngl.DST_COLOR: moderngl.Constant#

(Rd/kR,Gd/kG,Bd/kB,Ad/kA)

moderngl.ONE_MINUS_DST_COLOR: moderngl.Constant#

(1,1,1,1) - (Rd/kR,Gd/kG,Bd/kB,Ad/kA)

Shortcuts#

moderngl.DEFAULT_BLENDING: moderngl.Constant#

Shotcut for the default blending SRC_ALPHA, ONE_MINUS_SRC_ALPHA

moderngl.ADDITIVE_BLENDING: moderngl.Constant#

Shotcut for additive blending ONE, ONE

moderngl.PREMULTIPLIED_ALPHA: moderngl.Constant#

Shotcut for blend mode when using premultiplied alpha SRC_ALPHA, ONE

Blend Equations#

Also available in the Context instance including mode details.

moderngl.FUNC_ADD: moderngl.Constant#

source + destination

moderngl.FUNC_SUBTRACT: moderngl.Constant#

source - destination

moderngl.FUNC_REVERSE_SUBTRACT: moderngl.Constant#

destination - source

moderngl.MIN: moderngl.Constant#

Minimum of source and destination

moderngl.MAX: moderngl.Constant#

Maximum of source and destination

Provoking Vertex#

Also available in the Context instance including mode details.

moderngl.FIRST_VERTEX_CONVENTION: moderngl.Constant#

Specifies the first vertex should be used as the source of data for flat shaded varyings. Used with Context.provoking_vertex.

moderngl.LAST_VERTEX_CONVENTION: moderngl.Constant#

Specifies the last vertex should be used as the source of data for flat shaded varyings. Used with Context.provoking_vertex.

Functions#

Also see Context.

moderngl.create_context(require: Optional[int] = None, standalone: bool = False, share: bool = False, **settings: Dict[str, Any]) moderngl.Context

Create a ModernGL context by loading OpenGL functions from an existing OpenGL context. An OpenGL context must exists.

Example:

# Accept the current context version
ctx = moderngl.create_context()

# Require at least OpenGL 4.3
ctx = moderngl.create_context(require=430)

# Create a headless context requiring OpenGL 4.3
ctx = moderngl.create_context(require=430, standalone=True)
Keyword Arguments
  • require (int) – OpenGL version code (default: 330)

  • standalone (bool) – Headless flag

  • share (bool) – Attempt to create a shared context

  • **settings – Other backend specific settings

Returns

Context object

moderngl.create_standalone_context(require: Optional[int] = None, share: bool = False, **settings: Dict[str, Any]) moderngl.Context

Create a standalone/headless ModernGL context.

The preferred way of making a context is through moderngl.create_context().

Example:

# Create a context with highest possible supported version
ctx = moderngl.create_context()

# Require at least OpenGL 4.3
ctx = moderngl.create_context(require=430)
Keyword Arguments
  • require (int) – OpenGL version code.

  • share (bool) – Attempt to create a shared context

  • settings – keyword config values for the context backend

Returns

Context object

moderngl.detect_format(program: moderngl.Program, attributes: Any, mode: str = 'mgl') str

Detect format for vertex attributes.

The format returned does not contain padding.

Parameters
  • program (Program) – The program.

  • attributes (list) – A list of attribute names.

Returns

str