Shaders

A Shader is a user-defined program designed to run on some stage of a graphics processor. Its purpose is to execute one of the programmable stages of the rendering pipeline.

Read more on shaders on OpenGL Wiki

Example usage

vertex = '''
    attribute vec2 position;
    void main (void)
    {
        gl_Position = vec4(0.85*position, 0.0, 1.0);
    } '''
fragment = '''
    void main(void)
    {
        gl_FragColor = vec4(1.0,1.0,0.0,1.0);
    } '''

quad = gloo.Program(vertex, fragment, count=4)
quad['position'] = [(-1,-1), (-1,+1), (+1,-1), (+1,+1)]

Content

Program

class glumpy.gloo.Program(vertex=None, fragment=None, geometry=None, count=0)

Bases: glumpy.gloo.globject.GLObject

A Program is an object to which shaders can be attached and linked to create a shader program.

Parameters:
  • vertex (str|None) – Vertex shader object (as code or filename)
  • fragment (str|None) – Fragment shader object (as code or filename)
  • geometry (str|None) – Geometry shader object (as code or filename)
  • count (int) – Optional. Number of vertices this program will use. This can be specified to initialize a VertexBuffer during program initialization.

Warning

If a shader is given as a string and contains a {, glumpy considers the string to be actual code. Else, glumpy will try to locate the file in the library (glumpy/library).

The actual compilation of a program is a complex operation since include msut be resolved and hooks must be inserted at the proper place.

active_attributes

List of active attributes requested from GPU (read only).

Note

An inactive attribute is an attribute that has been declared but that is not actually used in the shader.

Example:

attribute vec3 normal;    # Inactive
attribute vec3 position;  # Active
void main() {
    gl_Position = vec4(position, 1.0);
}
active_uniforms

List of active uniform requested from GPU (read only).

Note

An inactive uniform is a uniform that has been declared but that is not actually used in the shader.

Example:

uniform vec3 color;     # Inactive
void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
all_attributes

List of all attributes parsed from shaders source (read only).

all_uniforms

List of all uniform parsed from shaders source (read only).

bind(data)

Bind a vertex buffer to the program, matching buffer record names with program attributes.

Several buffers can be bound but the size of the different buffers must match.

draw(mode=GL_TRIANGLES, indices=None)

Draw using the specified mode & indices.

Parameters:
  • mode (gl.GLEnum) –
    One of
    • GL_POINTS
    • GL_LINES
    • GL_LINE_STRIP
    • GL_LINE_LOOP,
    • GL_TRIANGLES
    • GL_TRIANGLE_STRIP
    • GL_TRIANGLE_FAN
  • indices (IndexBuffer|None) – Vertex indices to be drawn. If none given, everything is drawn.
fragment

Fragment shader object

geometry

Geometry shader object

hooks

Hook names collected from vertex, fragment and geometry shaders.

Hooks are placeholder in a shader source code where shader snippet can be inserted.

Example:

attribute vec3 position;
void main () {
    gl_Position = <transform>(position); # "transform" is a hook
}
inactive_attributes

List of inactive attributes requested from GPU (read only).

Note

An inactive attribute is an attribute that has been declared but that is not actually used in the shader.

Example:

attribute vec3 normal;    # Inactive
attribute vec3 position;  # Active
void main() {
    gl_Position = vec4(position, 1.0);
}
inactive_uniforms

List of inactive uniforms requested from GPU (read only).

Note

An inactive uniform is a uniform that has been declared but that is not actually used in the shader.

Example:

uniform vec3 color;     # Inactive
void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
vertex

Vertex shader object

Shader

class glumpy.gloo.Shader(target, code)

Bases: glumpy.gloo.globject.GLObject

Abstract shader class.

Parameters:
  • target (gl.GLEnum) –
    • gl.GL_VERTEX_SHADER
    • gl.GL_FRAGMENT_SHADER
    • gl.GL_GEOMETRY_SHADER
  • code (str) – Shader code or a filename containing shader code

Note

If the shader code is actually a filename, the filename must be prefixed with file:. Note that you can also get shader code from the library module.

__setitem__(name, snippet)

Set a snippet on the given hook in the source code.

attributes

Shader attributes obtained from source code

code

Shader source code (built from original and snippet codes)

hooks

Shader hooks (place where snippets can be inserted)

reset()

Reset shader snippets

uniforms

Shader uniforms obtained from source code

VertexShader

class glumpy.gloo.VertexShader(code=None)

Bases: glumpy.gloo.shader.Shader

Vertex shader class

FragmentShader

class glumpy.gloo.FragmentShader(code=None)

Bases: glumpy.gloo.shader.Shader

Fragment shader class

GeometryShader

class glumpy.gloo.GeometryShader(code=None, vertices_out=0, input_type=None, output_type=None)

Bases: glumpy.gloo.shader.Shader

Geometry shader class.

Parameters:
  • code (str) – Shader code or a filename containing shader code
  • vertices_out (int) – Number of output vertices
  • input_type (gl.GLEnum) –
    • GL_POINTS
    • GL_LINES​, GL_LINE_STRIP​, GL_LINE_LIST
    • GL_LINES_ADJACENCY​, GL_LINE_STRIP_ADJACENCY
    • GL_TRIANGLES​, GL_TRIANGLE_STRIP​, GL_TRIANGLE_FAN
    • GL_TRIANGLES_ADJACENCY​, GL_TRIANGLE_STRIP_ADJACENCY
  • output_type (gl.GLEnum) –
    • GL_POINTS, GL_LINES​, GL_LINE_STRIP
    • GL_TRIANGLES​, GL_TRIANGLE_STRIP​, GL_TRIANGLE_FAN
input_type