OpenGL Objects

We proved some simple and powerful wrappers over OpenGL features in the demosys.opengl package.

  • Texture: Textures from images or manually constructed/generated
  • Shader: Shader programs currently supporting vertex/fragment/geometry shaders
  • Frame Buffer Object: Offscreen rendering targets represented as textures
  • Vertex Array Object: Represents the geometry we are drawing using a shader

Texture

Textures are normally loaded by requesting the resource by path/name in the initializer of an effect using the self.get_texture method inherited from the Effect base class. We use the PIL/Pillow library to image data from file.

Textures can of course also be crated manually if needed.

class demosys.opengl.texture.Texture(name=None, path=None, width=0, height=0, depth=0, lod=0, target=GL_TEXTURE_2D, internal_format=GL_RGBA8, format=GL_RGBA, type=GL_UNSIGNED_BYTE, mipmap=False, anisotropy=0, min_filter=GL_LINEAR, mag_filter=GL_LINEAR, wrap_s=GL_CLAMP_TO_EDGE, wrap_t=GL_CLAMP_TO_EDGE, wrap_r=GL_CLAMP_TO_EDGE)

Bases: object

Represents a texture

bind()

Binds the texture to the currently active texture unit

classmethod create_2d(**kwargs)

Creates a 2d texture. All parameters are passed on the texture initializer.

Returns:Texture object
classmethod from_image(path, image=None, **kwargs)

Creates and image from a image file using Pillow/PIL. Additional parameters is passed to the texture initializer.

Parameters:
  • path – The path to the file
  • image – The PIL/Pillow image object (Can be None)
Returns:

Texture object

set_image(image)

Set pixel data using a image file with PIL/Pillow.

Parameters:image – The PIL/Pillow image object
set_interpolation(min_filter, mag_filter)

Sets the texture interpolation mode

Parameters:
  • min_filter – Min filter mode (glenum)
  • mag_filter – Max filter mode (glenum)
set_texture_repeat(wrap_s, wrap_t, wrap_r)

Sets the texture repeat mode

Parameters:
  • wrap_s – Repeat mode S (glenum)
  • wrap_t – Repeat mode T (glenum)
  • wrap_r – Repeat mode R (glenum)
size

Get the dimensions of the texture

Returns:(w, h) tuple

Shader

In oder to draw something to the screen, we need a shader. There is no other way.

Shader should ideally always be loaded from .glsl files located in a shaders directory in your effect or project global resource directory. Shaders have to be written in a single file were the different shader types are separated using preprocessors.

Note

We wish to support loading shaders in other common formats such as separate files for each shader type. Feel free to make a pull request or create an issue on github.

Like textures they are loaded in the effect using the get_shader method in the initializer.

Once we have the reference to the shader object we will need a VAO object in order to bind it. We could of course just call bind(), but the VAOs will do this for you. More details in the VAO section below.

#version 410

#if defined VERTEX_SHADER
// Vertex shader here
#elif defined FRAGMENT_SHADER
// Fragment shader here
#elif defined GEOMETRY_SHADER
// Geometry shader here
#endif

Once the shader is bound we can set uniforms through the various uniform_ methods.

Assuming we have a reference to a shader in s:

# Set the uniform (float) with name 'value' to 1.0
s.uniform_1f("value", 1.0)
# Set the uniform (mat4) with name `m_view' to a 4x4 matrix
s.uniform_mat4("m_view", view_matrix)
# Set the sampler2d uniform to use a Texture object we have loaded
s.sampler_2d(0, "texture0", texture)

The Shader class contains an internal cache of all the uniform variables the shader has, so it will generally do very efficient type checks at run time and give useful error feedback if something is wrong.

Other than setting uniforms and using the right file format for shaders, there are not much more to them.

Note

We are planning to support passing in preprocessors to shader. Please make an issue or a pull request on github.

class demosys.opengl.shader.Shader(path)

Bases: object

Represents a shader program

bind()

Bind the shader

build_attribute_map()

Builds an internal attribute map by querying the program. This way we don’t have to query OpenGL (can cause slowdowns) This information is also used when the shader and VAO negotiates the buffer binding.

build_uniform_map()

Builds an internal uniform map by querying the program. This way we don’t have to query OpenGL (can cause slowdowns)

delete()

Frees the memory and invalidates the name associated with the program

Links the program. Raises ShaderError if the linker failed.

prepare()

Compiles all the shaders and links the program. If the linking is successful, it builds the uniform and attribute map.

set_fragment_source(source)

Set the fragment shader source

Parameters:source – (string) Fragment shader source
set_geometry_source(source)

Set the geometry shader source

Parameters:source – (string) Geometry shader source
set_source(source)

Set a single source file. This is used when you have all shaders in one file separated by preprocessors.

Parameters:source – (string) The shader source
set_vertex_source(source)

Set the vertex shader source

Parameters:source – (string) Vertex shader source
uniform(name)

Get the uniform location. Raises ShaderError if the uniform is not found.

Parameters:name – The name of the uniform
Returns:Uniform object
uniform_1b(name, value)

Sets an bool

Parameters:
  • name – Name of the uniform
  • value – Integer value
uniform_1d(name, value)

Set a double uniform

Parameters:
  • name – Name of the uniform
  • value – double value
uniform_1dv(name, value, count=1)

Set a double uniform

Parameters:
  • name – Name of the uniform
  • value – float array
  • count – Length of the uniform array (default 1)
uniform_1f(name, value)

Set a float uniform

Parameters:
  • name – Name of the uniform
  • value – float value
uniform_1fv(name, value, count=1)

Set a float uniform

Parameters:
  • name – Name of the uniform
  • count – Length of the uniform array (default 1)
  • value – float array
uniform_1i(name, value)

Sets an int

Parameters:
  • name – Name of the uniform
  • value – Integer value
uniform_1iv(name, value, count=1)

Sets an int

Parameters:
  • name – Name of the uniform
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_1ui(name, value)

Sets an uint

Parameters:
  • name – Name of the uniform
  • value – Integer value
uniform_1uiv(name, value, count=1)

Sets an uint

Parameters:
  • name – Name of the uniform
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_2b(name, x, y)

Sets an bvec2

Parameters:
  • name – Uniform name
  • x – bool
  • y – bool
uniform_2d(name, x, y)

Set a dvec2 uniform

Parameters:
  • name – name of the uniform
  • x – double value
  • y – double value
uniform_2dv(name, value, count=1)

Set a dvec2 uniform

Parameters:
  • name – name of the uniform
  • value – float array
  • count – Length of the uniform array (default 1)
uniform_2f(name, x, y)

Set a vec2 uniform

Parameters:
  • name – name of the uniform
  • x – float value
  • y – float value
uniform_2fv(name, value, count=1)

Set a vec2 uniform

Parameters:
  • name – name of the uniform
  • count – Length of the uniform array (default 1)
  • value – float array
uniform_2i(name, x, y)

Sets an ivec2

Parameters:
  • name – Uniform name
  • x – Integer
  • y – Integer
uniform_2iv(name, value, count=1)

Sets an ivec2

Parameters:
  • name – Uniform name
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_2ui(name, x, y)

Sets an uvec2

Parameters:
  • name – Uniform name
  • x – Integer
  • y – Integer
uniform_2uiv(name, value, count=1)

Sets an uvec2

Parameters:
  • name – Uniform name
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_3b(name, x, y, z)

Sets an bvec3

Parameters:
  • name – Uniform name
  • x – bool
  • y – bool
  • z – bool
uniform_3d(name, x, y, z)

Set a dvec3 uniform

Parameters:
  • name – Name of the uniform
  • x – double value
  • y – double value
  • z – double value
uniform_3dv(name, value, count=1)

Set a dvec3 uniform

Parameters:
  • name – Name of the uniform
  • value – float array
  • count – Length of the uniform array (default 1)
uniform_3f(name, x, y, z)

Set a vec3 uniform

Parameters:
  • name – Name of the uniform
  • x – float value
  • y – float value
  • z – float value
uniform_3fv(name, value, count=1)

Set a vec3 uniform

Parameters:
  • name – Name of the uniform
  • count – Length of the uniform array (default 1)
  • value – float array
uniform_3i(name, x, y, z)

Sets an ivec3

Parameters:
  • name – Uniform name
  • x – Integer
  • y – Integer
  • z – Integer
uniform_3iv(name, value, count=1)

Sets an ivec3

Parameters:
  • name – Uniform name
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_3ui(name, x, y, z)

Sets an uvec3

Parameters:
  • name – Uniform name
  • x – Integer
  • y – Integer
  • z – Integer
uniform_3uiv(name, value, count=1)

Sets an uvec3

Parameters:
  • name – Uniform name
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_4b(name, x, y, z, w)

Sets an bvec4

Parameters:
  • name – Uniform name
  • x – bool
  • y – bool
  • z – bool
  • w – bool
uniform_4d(name, x, y, z, w)

Set a dvec4 uniform

Parameters:
  • name – Name of the uniform
  • x – double value
  • y – double value
  • z – double value
  • w – double value
uniform_4dv(name, value, count=1)

Set a dvec4 uniform

Parameters:
  • name – Name of the uniform
  • value – float array
  • count – Length of the uniform array (default 1)
uniform_4f(name, x, y, z, w)

Set a vec4 uniform

Parameters:
  • name – Name of the uniform
  • x – float value
  • y – float value
  • z – float value
  • w – float value
uniform_4fv(name, value, count=1)

Set a vec4 uniform

Parameters:
  • name – Name of the uniform
  • count – Length of the uniform array (default 1)
  • value – float array
uniform_4i(name, x, y, z, w)

Sets an ivec4

Parameters:
  • name – Uniform name
  • x – Integer
  • y – Integer
  • z – Integer
  • w – Integer
uniform_4iv(name, value, count=1)

Sets an ivec4

Parameters:
  • name – Uniform name
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_4ui(name, x, y, z, w)

Sets an uvec4

Parameters:
  • name – Uniform name
  • x – Integer
  • y – Integer
  • z – Integer
  • w – Integer
uniform_4uiv(name, value, count=1)

Sets an uvec4

Parameters:
  • name – Uniform name
  • value – integer array
  • count – Length of the uniform array (default 1)
uniform_check(name, expected_type)

Get a uniform and verify the expected type. This is used by the uniform_* methods for validating the actual type in the shader and the uniform we are trying to set. Raises ShaderError if the uniform is not found.

Parameters:
  • name – The name of the uniform
  • expected_type – The expected type of the uniform.
Returns:

The Uniform object

uniform_mat2(name, mat, transpose=GL_FALSE)

Sets a mat3 uniform

Parameters:
  • name – Name of the uniform
  • mat – matrix
  • transpose – Traspose the matrix
uniform_mat3(name, mat, transpose=GL_FALSE)

Sets a mat3 uniform

Parameters:
  • name – Name of the uniform
  • mat – matrix
  • transpose – Traspose the matrix
uniform_mat4(name, mat, transpose=GL_FALSE)

Set a mat4 uniform

Parameters:
  • name – Name of the uniform
  • mat – matrix
uniform_sampler_1d(unit, name, texture)

Sets a sampler1d

Parameters:
  • unit – The texture unit to use (0 - N)
  • name – Name of the uniform
  • texture – The Texture object
uniform_sampler_2d(unit, name, texture)

Sets a sampler2d

Parameters:
  • unit – The texture unit to use (0 - N)
  • name – Name of the uniform
  • texture – The Texture object
uniform_sampler_3d(unit, name, texture)

Sets a sampler3d

Parameters:
  • unit – The texture unit to use (0 - N)
  • name – Name of the uniform
  • texture – The Texture object

Vertex Array Object

Vertex Array Objects represents the geometry we are drawing with shaders. They keep track of the buffer binding states of one or multiple Vertex Buffer Objects.

VAOs and shaders interact in a very important way. The first time the VAO and shader interacts, they will figure out if they are compatible when it comes to the attributes in the shader and the buffers in the VAO.

When we create VAOs we tell explicitly what attribute name each buffer belongs to.

Example: I have three buffers representing positions, normals and uvs.

  • Map positions to in_position attribute with 3 components
  • Map normals to in_normal attribute with 3 components
  • Map uvs to the in_uv attribute with 2 components

The vertex shader will have to define the exact same attribute names:

in vec3 in_position;
in vec3 in_normal;
in vec2 in_uv

This is not entirely true. The shader will at least have to define the in_position. The other two attributes are optional. This is were the VAO and the Shader negotiates the attribute binding. The VAO object will on-the-fly generate a version of itself that supports the shaders attributes.

The VAO/Shader binding can also be used as a context manager as seen below, but this is optional. The context manager will return the reference to the shader so you can use a shorter name.

# Without context manager
vao.bind(shader)
shader.unform_1f("value", 1.0)
vao.draw()

# Bind the shader and negotiate attribute binding
with vao.bind(shader) as s:
    s.unform_1f("value", 1.0)
    # ...
# Finally draw the geometry
vao.draw()

When creating a VBO we need to use the OpenGL.arrays.vbo.VBO instance in PyOpenGL. We pass a numpy array to the constructor. It’s important to use the correct dtype so it matches the type passed in add_array_buffer.

Each VBO is first added to the VAO using add_array_buffer. This is simply to register the buffer and tell the VAO what format it has.

The map_buffer part will define the actual attribute mapping. Without this the VAO is not complete.

Calling build() will finalize and sanity check the VAO.

The VAO initializer also takes an optional argument mode were you can specify what the default draw mode is. This can be overridden in draw(mode=...).

The VAO will always do very strict error checking and give useful feedback when something is wrong. VAOs must also be assigned a name so the framework can reference it in error messages.

def quad_2d(width, height, xpos, ypos):
    """
    Creates a 2D quad VAO using 2 triangles.

    :param width: Width of the quad
    :param height: Height of the quad
    :param xpos: Center position x
    :param ypos: Center position y
    """
    pos = VBO(numpy.array([
        xpos - width / 2.0, ypos + height / 2.0, 0.0,
        xpos - width / 2.0, ypos - height / 2.0, 0.0,
        xpos + width / 2.0, ypos - height / 2.0, 0.0,
        xpos - width / 2.0, ypos + height / 2.0, 0.0,
        xpos + width / 2.0, ypos - height / 2.0, 0.0,
        xpos + width / 2.0, ypos + height / 2.0, 0.0,
    ], dtype=numpy.float32))
    normals = VBO(numpy.array([
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
    ], dtype=numpy.float32))
    uvs = VBO(numpy.array([
        0.0, 1.0,
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 0.0,
        1.0, 1.0,
    ], dtype=numpy.float32))
    vao = VAO("geometry:quad", mode=GL.GL_TRIANGLES)
    vao.add_array_buffer(GL.GL_FLOAT, pos)
    vao.add_array_buffer(GL.GL_FLOAT, normals)
    vao.add_array_buffer(GL.GL_FLOAT, uvs)
    vao.map_buffer(pos, "in_position", 3)
    vao.map_buffer(normals, "in_normal", 3)
    vao.map_buffer(uvs, "in_uv", 2)
    vao.build()
    return vao

We can also pass index/element buffers to VAOs. We can also use interleaved VBOs by passing the same VBO to map_buffer multiple times.

More examples can be found in the Geometry module.

class demosys.opengl.vao.VAO(name, mode=GL_TRIANGLES)

Bases: object

Vertex Array Object

add_array_buffer(format, vbo)

Register a vbo in the VAO. This can be called multiple times. This can be one or multiple buffers (interleaved or not)

Parameters:
  • format – The format of the buffer
  • vbo – The vbo object
bind(shader)

Bind the VAO using a shader. This is the standard way of binding so the shader and VAO can negotiate the needed attributes. This will generate new VAOs in the background on the fly (caching them) if needed.

Parameters:shader – The shader
Returns:A VAOBindContext object (optional use)
build()

Finalize the VAO. This runs various sanity checks on the input data.

draw(mode=None)

Draw the VAO. Will use glDrawElements if an element buffer is present and glDrawArrays if no element array is present.

Parameters:mode – Override the draw mode (GL_TRIANGLES etc)
generate_vao_combo(shader)

Create a VAO based on the shader’s attribute specification. This is called by bind(shader) and should not be messed with unless you are absolutely sure about what you are doing.

Parameters:shader – The shader we are generating the combo for
Returns:A new VAOCombo object with the correct attribute binding
map_buffer(vbo, attrib_name, components)

Map parts of the vbos to an attribute name. This can be called multiple times to describe hos the buffers map to attribute names. If the same vbo is passed more than once it must be an interleaved buffer.

Parameters:
  • vbo – The vbo
  • attrib_name – Name of the attribute in the shader
  • components – Number of components (for example 3 for a x, y, x position)
set_element_buffer(format, vbo)

Set the index buffer for this VAO

Parameters:
  • format – The format of the element buffer
  • vbo – the vbo object

Frame Buffer Object

Frame Buffer Objects are offscreen render targets. Internally they are simply textures that can be used further in rendering. FBOs can even have multiple layers so a shader can write to multiple buffers at once. They can also have depth/stencil buffers. Currently we use use a depth 24 / stencil 8 buffer by default as the depth format.

Creating an FBO:

# Shorcut for creating a single layer FBO with depth buffer
fbo = FBO.create(1024, 1024, depth=True)

# Multilayer FBO (We really need to make a shortcut for this!)
fbo = FBO()
fbo.add_color_attachment(texture1)
fbo.add_color_attachment(texture2)
fbo.add_color_attachment(texture3)
fbo.set_depth_attachment(depth_texture)

# Binding and releasing FBOs
fbo.bind()
fbo.release()

# Using a context manager
with fbo:
    # Draw stuff in the FBO

When binding the FBOs with multiple color attachments it will automatically call glDrawBuffers enabling multiple outputs in the fragment shader.

Shader example with multiple layers:

#version 410

layout(location = 0) out vec4 outColor0;
layout(location = 1) out vec4 outColor1;
layout(location = 2) out vec4 outColor2;

void main( void ) {
    outColor0 = vec4(1.0, 0.0, 0.0, 1.0)
    outColor1 = vec4(0.0, 1.0, 0.0, 1.0)
    outColor1 = vec4(0.0, 0.0, 1.0, 1.0)
}

Will draw red, green and blue in the separate layers in the FBO/textures.

Warning

It’s important to use explicit attribute locations as not all drivers will guarantee preservation of the order and things end up in the wrong buffers!

Another very important feature of the FBO implementation is the concept of FBO stacks.

  • The default render target is the window frame buffer.
  • When the stack is empty we are rendering to the screen.
  • When binding an FBO it will be pushed to the stack and the correct viewport for the FBO will be set
  • When releasing the FBO it will be popped from the stack and the viewport for the default render target will be applied
  • This also means we can build deeper stacks with the same behavior
  • The maximum stack depth is currently 8 and the framework will aggressively react when FBOs are popped and pushed in the wrong order

A more complex example:

# Push fbo1 to stack, bind and set viewport
fbo1.bind()
# Push fbo2 to stack, bind and set viewport
fbo2.bind()
# Push fbo3 to stack, bind and set viewport
fbo3.bind()
# Pop fbo3 from stack, bind fbo2 and set the viewport
fbo3.release()
# Pop fbo2 from stack, bind fbo1 and set the viewport
fbo2.release()
# Pop fbo1 from stack, unbind the fbo and set the screen viewport
fbo1.release()

Using context managers:

with fbo1:
    with fbo2:
        with fbo2:
            pass

This is especially useful in realation to the draw method in effects. The last parameter is the target FBO. The effect will never know if the FBO passed in is the fake window FBO or an actual FBO. It might also do offscreen rendering to its own fbos and things start get get really ugly.

The FBO stack makes this fairly painless.

By using the bind_target decorator on the draw method of your effect you will never need to think about this issue. Not having to worry about resporting the viewport size is also a huge burden off our shoulders.

@effect.bind_target
def draw(self, time, frametime, target):
    # ...

There are of course ways to bypass the stack, but should be done with extreme caution.

Note

We are also aiming to support layered rendering using the geometry shader. Please make an issue or pull request on github.

class demosys.opengl.fbo.FBO

Bases: object

Frame buffer object

add_color_attachment(texture)

Add a texture as a color attachment.

Parameters:texture – The Texture object
bind(stack=True)

Bind FBO adding it to the stack.

Parameters:stack – (bool) If the bind should push the current FBO on the stack.
check_status()

Checks the completeness of the FBO

clear()

Clears the FBO using glClear.

classmethod create(width, height, depth=False, internal_format=GL_RGBA8, format=GL_RGBA, type=GL_UNSIGNED_BYTE, layers=1)

Convenient shortcut for creating single color attachment FBOs

Parameters:
  • width – Color buffer width
  • height – Coller buffer height
  • depth – (bool) Create a depth attachment
  • internal_format – The internalformat of the color buffer
  • format – The format of the color buffer
  • type – The type of the color buffer
Returns:

A new FBO

release(stack=True)

Bind FBO popping it from the stack

Parameters:stack – (bool) If the bind should be popped form the FBO stack.
set_depth_attachment(texture)

Set a texture as depth attachment.

Parameters:texture – The Texture object
size

Attempts to determine the pixel size of the FBO. Currently returns the size of the first color attachment. If the FBO has no color attachments, the depth attachment will be used. Raises `FBOError if the size cannot be determined.

Returns:(w, h) tuple representing the size in pixels