_glutils

This package provides utility functions to handle OpenGL resources.

The gl module provides a wrapper to OpenGL based on PyOpenGL.

_glutils.gl

This module loads PyOpenGL and provides a namespace for OpenGL.

testGL()[source]

Test if required OpenGL version and extensions are available.

This MUST be run with an active OpenGL context.

enabled(capacity, enable=True)[source]

Context manager enabling an OpenGL capacity.

This is not checking the current state of the capacity.

Parameters:
  • capacity – The OpenGL capacity enum to enable/disable
  • enable (bool) – True (default) to enable during context, False to disable
disabled(capacity, disable=True)[source]

Context manager disabling an OpenGL capacity.

This is not checking the current state of the capacity.

Parameters:
  • capacity – The OpenGL capacity enum to disable/enable
  • disable (bool) – True (default) to disable during context, False to enable
glGetActiveAttrib(program, index)[source]

Wrap PyOpenGL glGetActiveAttrib

Utility functions

For OpenGL context management:

getCurrent()[source]

Returns platform dependent object of current OpenGL context.

This is useful to associate OpenGL resources with the context they are created in.

Returns:Platform specific OpenGL context
setCurrent(context=<class 'silx.gui._glutils.Context._DEFAULT_CONTEXT'>)[source]

Set a platform dependent OpenGL context

Parameters:context – Platform dependent GL context
current(context)[source]

Context manager setting the platform-dependent GL context

Parameters:context – Platform dependent GL context

For type checking and conversion:

sizeofGLType(type_)[source]

Returns the size in bytes of an element of type type_

isSupportedGLType(type_)[source]

Test if a numpy type or dtype can be converted to a GL type.

numpyToGLType(type_)[source]

Returns the GL type corresponding the provided numpy type or dtype.

Program

class Program(vertexShader, fragmentShader, attrib0='position')[source]

Wrap OpenGL shader program.

The program is compiled lazily (i.e., at first program use()). When the program is compiled, it stores attributes and uniforms locations. So, attributes and uniforms must be used after use().

This object supports multiple OpenGL contexts.

Parameters:
  • vertexShader (str) – The source of the vertex shader.
  • fragmentShader (str) – The source of the fragment shader.
  • attrib0 (str) – Attribute’s name to bind to position 0 (default: ‘position’). On certain platform, this attribute MUST be active and with an array attached to it in order for the rendering to occur….
attributes

Vertex attributes names and locations as a dict of {str: int}.

WARNING: Read-only usage. To use only with a valid OpenGL context and after use() has been called for this context.

uniforms

Program uniforms names and locations as a dict of {str: int}.

WARNING: Read-only usage. To use only with a valid OpenGL context and after use() has been called for this context.

program

OpenGL id of the program.

WARNING: To use only with a valid OpenGL context and after use() has been called for this context.

use()[source]

Make use of the program, compiling it if necessary

setUniformMatrix(name, value, transpose=True, safe=False)[source]

Wrap glUniformMatrix[2|3|4]fv

Parameters:
  • name (str) – The name of the uniform.
  • value (numpy.ndarray with 2 or 3 dimensions of float32) – The 2D matrix (or the array of matrices, 3D). Matrices are 2x2, 3x3 or 4x4.
  • transpose (bool) – Whether to transpose (True, default) or not.
  • safe (bool) – False: raise an error if no uniform with this name; True: silently ignores it.
Raises:

KeyError – if no uniform corresponds to name.

Texture

class Texture(internalFormat, data=None, format_=None, shape=None, texUnit=0, minFilter=None, magFilter=None, wrap=None)[source]

Base class to wrap OpenGL 2D and 3D texture

Parameters:
  • internalFormat – OpenGL texture internal format
  • data (numpy.ndarray or None) – The data to copy to the texture or None for an empty texture
  • format – Input data format if different from internalFormat
  • shape (List[int]) – If data is None, shape of the texture (height, width) or (depth, height, width)
  • texUnit (int) – The texture unit to use
  • minFilter – OpenGL texture minimization filter (default: GL_NEAREST)
  • magFilter – OpenGL texture magnification filter (default: GL_LINEAR)
  • wrap (OpenGL wrap mode or 2 or 3-tuple of wrap mode) – Texture wrap mode for dimensions: (t, s) or (r, t, s) If a single value is provided, it used for all dimensions.
target

OpenGL target type of this texture

ndim

The number of dimensions: 2 or 3

internalFormat

Texture internal format

shape

Shape of the texture: (height, width) or (depth, height, width)

name

OpenGL texture name.

It is None if not initialized or already discarded.

minFilter

Minifying function parameter (GL_TEXTURE_MIN_FILTER)

magFilter

Magnification function parameter (GL_TEXTURE_MAG_FILTER)

prepare()[source]

Synchronizes the OpenGL texture.

This method must be called with a current OpenGL context.

bind(texUnit=None)[source]

Bind the texture to a texture unit.

The OpenGL texture is updated if needed.

This method must be called with a current OpenGL context.

Parameters:texUnit (int) – The texture unit to use
discard()[source]

Delete associated OpenGL texture.

This method must be called with a current OpenGL context.

update(format_, data, offset=(0, 0, 0), copy=True)[source]

Update the content of the texture.

Texture is not resized, so data must fit into texture with the given offset.

This update is performed lazily during next call to prepare() or bind(). Data MUST not be changed until then.

Parameters:
  • format – The OpenGL format of the data
  • data – The data to use to update the texture
  • offset (List[int]) – Offset in the texture where to copy the data
  • copy (bool) – True (default) to copy data, False to use as is (do not modify)

FramebufferTexture

class FramebufferTexture(internalFormat, shape, stencilFormat=GL_DEPTH24_STENCIL8, depthFormat=GL_DEPTH24_STENCIL8, **kwargs)[source]

Framebuffer with a texture.

Aimed at off-screen rendering to texture.

Parameters:
  • internalFormat – OpenGL texture internal format
  • shape (2-tuple of int) – Shape (height, width) of the framebuffer and texture
  • stencilFormat – Stencil renderbuffer format
  • depthFormat – Depth renderbuffer format
  • kwargs – Extra arguments for Texture constructor
shape

Shape of the framebuffer (height, width)

texture

The texture this framebuffer is rendering to.

The life-cycle of the texture is managed by this object

name

OpenGL name of the framebuffer

bind()[source]

Bind this framebuffer for rendering

discard()[source]

Delete associated OpenGL resources including texture

Vertex Buffer

class VertexBuffer(data=None, size=None, usage=None, target=None)[source]

Object handling an OpenGL vertex buffer object

Parameters:
  • data (numpy.ndarray or None) – Data used to fill the vertex buffer
  • size (int) – Size in bytes of the buffer or None for data size
  • usage – OpenGL vertex buffer expected usage pattern: GL_STREAM_DRAW, GL_STATIC_DRAW (default) or GL_DYNAMIC_DRAW
  • target – Target buffer: GL_ARRAY_BUFFER (default) or GL_ELEMENT_ARRAY_BUFFER
target

The target buffer of the vertex buffer

usage

The expected usage of the vertex buffer

name

OpenGL Vertex Buffer object name (int)

size

Size in bytes of the Vertex Buffer Object (int)

bind()[source]

Bind the vertex buffer

update(data, offset=0, size=None)[source]

Update vertex buffer content.

Parameters:
  • data (numpy.ndarray) – The data to put in the vertex buffer
  • offset (int) – Offset in bytes in the buffer where to put the data
  • size (int) – If provided, size of data to copy
discard()[source]

Delete the vertex buffer

class VertexBufferAttrib(vbo, type_, size, dimension=1, offset=0, stride=0, normalization=False)[source]

Describes data stored in a vertex buffer

Convenient class to store info for glVertexAttribPointer calls

Parameters:
  • vbo (VertexBuffer) – The vertex buffer storing the data
  • type (int) – The OpenGL type of the data
  • size (int) – The number of data elements stored in the VBO
  • dimension (int) – The number of type_ element(s) in [1, 4]
  • offset (int) – Start offset of data in the vertex buffer
  • stride (int) – Data stride in the vertex buffer
itemsize

Size in bytes of a vertex buffer element (int)

itemSize

Size in bytes of a vertex buffer element (int)

setVertexAttrib(attribute)[source]

Call glVertexAttribPointer with objects information

vertexBuffer(arrays, prefix=None, suffix=None, usage=None)[source]

Create a single vertex buffer from multiple 1D or 2D numpy arrays.

It is possible to reserve memory before and after each array in the VBO

Parameters:
  • arrays (Iterable of numpy.ndarray) – Arrays of data to store
  • prefix (Iterable of int or None) – If given, number of elements to reserve before each array
  • suffix (Iterable of int or None) – If given, number of elements to reserve after each array
  • usage (int) – vertex buffer expected usage or None for default
Returns:

List of VertexBufferAttrib objects sharing the same vertex buffer

font

Text rasterisation feature leveraging Qt font and text layout support.

getDefaultFontFamily()[source]

Returns the default font family of the application

ULTRA_LIGHT = 0

Lightest characters: Minimum font weight

LIGHT = 25

Light characters

NORMAL = 50

Normal characters

SEMI_BOLD = 63

Between normal and bold characters

BOLD = 74

Thicker characters

BLACK = 87

Really thick characters

ULTRA_BLACK = 99

Thickest characters: Maximum font weight

rasterTextQt(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=1.0)[source]

Raster text using Qt.

It supports multiple lines.

Parameters:
  • text (str) – The text to raster
  • font (str or QFont) – Font name or QFont to use
  • size (int) – Font size in points Used only if font is given as name.
  • weight (int) – Font weight in [0, 99], see QFont.Weight. Used only if font is given as name.
  • italic (bool) – True for italic font (default: False). Used only if font is given as name.
  • devicePixelRatio (float) – The current ratio between device and device-independent pixel (default: 1.0)
Returns:

Corresponding image in gray scale and baseline offset from top

Return type:

(HxW numpy.ndarray of uint8, int)

rasterText(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=1.0)[source]

Raster text using Qt or matplotlib if there may be math syntax.

It supports multiple lines.

Parameters:
  • text (str) – The text to raster
  • font (str or QFont) – Font name or QFont to use
  • size (int) – Font size in points Used only if font is given as name.
  • weight (int) – Font weight in [0, 99], see QFont.Weight. Used only if font is given as name.
  • italic (bool) – True for italic font (default: False). Used only if font is given as name.
  • devicePixelRatio (float) – The current ratio between device and device-independent pixel (default: 1.0)
Returns:

Corresponding image in gray scale and baseline offset from top

Return type:

(HxW numpy.ndarray of uint8, int)