demosys.opengl package

Submodules

demosys.opengl.fbo module

class demosys.opengl.fbo.FBO

Bases: object

Frame buffer object

clear(red=0.0, green=0.0, blue=0.0, alpha=0.0, depth=1.0)

Clears the FBO using glClear.

static create(size, components=4, depth=False, dtype='f1', layers=1)

Create a single or multi layer FBO

Parameters:
  • size – (tuple) with and height
  • components – (tuple) number of components. 1, 2, 3, 4
  • depth – (bool) Create a depth attachment
  • dtype – (string) data type per r, g, b, a …
  • layers – (int) number of color attachments
Returns:

A new FBO

static create_from_textures(color_buffers: List[demosys.opengl.texture.Texture2D], depth_buffer: demosys.opengl.texture.DepthTexture = None)

Create FBO from existing textures

Parameters:
  • color_buffers – List of textures
  • depth_buffer – Depth texture
Returns:

FBO instance

draw_color_layer(layer=0, pos=(0.0, 0.0), scale=(1.0, 1.0))

Draw a color layer in the FBO. :param layer: Layer ID :param pos: (tuple) offset x, y :param scale: (tuple) scale x, y

draw_depth(near, far, pos=(0.0, 0.0), scale=(1.0, 1.0))

Draw a depth buffer in the FBO.

Parameters:
  • near – projection near.
  • far – projection far.
  • pos – (tuple) offset x, y
  • scale – (tuple) scale x, y
release(stack=True)

Bind FBO popping it from the stack

Parameters:stack – (bool) If the bind should be popped form the FBO stack.
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
stack = []
use(stack=True)

Bind FBO adding it to the stack.

Parameters:stack – (bool) If the bind should push the current FBO on the stack.
exception demosys.opengl.fbo.FBOError

Bases: Exception

Generic FBO Error

class demosys.opengl.fbo.WindowFBO(window)

Bases: object

Fake FBO representing default render target

clear()

Dummy clear method

release()

Dummy release method

use()

Sets the viewport back to the buffer size of the screen/window

demosys.opengl.shader module

exception demosys.opengl.shader.ShaderError

Bases: Exception

class demosys.opengl.shader.ShaderProgram(path=None, name=None)

Bases: object

Represents a shader program

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)

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, value=None)
class demosys.opengl.shader.ShaderSource(type, name, source)

Bases: object

Helper class to deal with shader source files. This represents a single shader (vert/frag/geo)

find_out_attribs()

Get all out attributes in the shader source. :return: List of attribute names

print()

Print the shader lines

demosys.opengl.texture module

class demosys.opengl.texture.BaseTexture

Bases: object

Wraps the basic functionality of the ModernGL methods

depth

Is this a depth texture?

dtype

The data type of the texture

height

Height of the texture

mgl_instance
read(level: int = 0, alignment: int = 1) → bytes

Read the content of the texture into a buffer.

Parameters:
  • level – The mipmap level.
  • alignment – The byte alignment of the pixels.
Returns:

bytes

read_into(buffer: bytearray, level: int = 0, alignment: int = 1, write_offset: int = 0)

Read the content of the texture into a buffer.

Parameters:
  • buffer – (bytearray) The buffer that will receive the pixels.
  • level – (int) The mipmap level.
  • alignment – (int) The byte alignment of the pixels.
  • write_offset – (int) The write offset.
size

The size of the texture

use(location=0)

Bind the texture.

Parameters:location – The texture location. (GL_TEXTURE0 + location)
width

Width of the texture

write(data: bytes, viewport=None, level: int = 0, alignment: int = 1)

Update the content of the texture.

Parameters:
  • data – (bytes) – The pixel data.
  • viewport – (tuple) – The viewport.
  • level – (int) – The mipmap level.
  • alignment – (int) – The byte alignment of the pixels.
class demosys.opengl.texture.DepthTexture(size, data=None, samples=0, alignment=4)

Bases: demosys.opengl.texture.BaseTexture

Depth Texture

draw(near, far, pos=(0.0, 0.0), scale=(1.0, 1.0))

Draw depth buffer linearized. :param near: Near plane in projection :param far: Far plane in projection :param pos: (tuple) offset x, y :param scale: (tuple) scale x, y

quad = None
shader = None
class demosys.opengl.texture.Texture2D(path: str = None, mipmap: bool = False)

Bases: demosys.opengl.texture.BaseTexture

2D Texture

build_mipmaps(base=0, max_level=1000)

Build mipmaps for this texture

Parameters:
  • base – Level to build from
  • max_level – Max levels
classmethod create(size, components, data=None, samples=0, alignment=1, dtype='f1', mipmap=False) → demosys.opengl.texture.Texture2D

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

Returns:Texture object
draw(pos=(0.0, 0.0), scale=(1.0, 1.0))

Draw texture :param shader: override shader :param pos: (tuple) offset x, y :param scale: (tuple) scale x, y

classmethod from_image(path, image=None, **kwargs)

Creates a texture 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

quad = None
set_image(image, flip=True)

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

Parameters:
  • image – The PIL/Pillow image object
  • flip – Flip the image top to bottom
shader = None

demosys.opengl.vao module

class demosys.opengl.vao.BufferInfo(buffer: moderngl.Buffer, buffer_format: str, attributes=None)

Bases: object

Container for a vbo with additional information

content(attributes: List[str])

Build content tuple for the buffer

has_attribute(name)
vertex_size
class demosys.opengl.vao.VAO(name, mode=4)

Bases: object

Vertex Array Object

buffer(buffer, buffer_format: str, attribute_names)

Register a buffer/vbo for the VAO. This can be called multiple times. adding multiple buffers (interleaved or not)

Parameters:
  • buffer – The buffer object. Can be ndarray or Buffer
  • buffer_format – The format of the buffer (‘f’, ‘u’, ‘i’)
draw(shader: demosys.opengl.shader.ShaderProgram, mode=None)

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

Parameters:
  • shader – The shader to draw with
  • mode – Override the draw mode (GL_TRIANGLES etc)
index_buffer(buffer, index_element_size=4)

Set the index buffer for this VAO

Parameters:
  • buffer – Buffer object or ndarray
  • index_element_size – Byte size of each element. 1, 2 or 4
transform(shader, buffer: moderngl.Buffer, mode=None, vertices=-1, first=0, instances=1)

Transform vertices. Stores the output in a single buffer.

Parameters:
  • buffer – The buffer to store the output
  • mode – Draw mode (for example POINTS
  • vertices – The number of vertices to transform
  • first – The index of the first vertex to start with
  • instances – The number of instances
Returns:

exception demosys.opengl.vao.VAOError

Bases: Exception

Module contents