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.

silx.gui.plot3d.glutils.gl.testGL()[source]

Test if required OpenGL version and extensions are available.

This MUST be run with an active OpenGL context.

silx.gui.plot3d.glutils.gl.enabled(*args, **kwds)[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
silx.gui.plot3d.glutils.gl.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
silx.gui.plot3d.glutils.gl.glGetActiveAttrib(program, index)[source]

Wrap PyOpenGL glGetActiveAttrib

Utility functions

For OpenGL context management:

silx.gui.plot3d.glutils.getGLContext()

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
Return type:None by default or a platform dependent object
silx.gui.plot3d.glutils.setGLContextGetter(getter=<function _defaultGLContextGetter at 0x7f9b65dfe668>)

Set a platform dependent function to retrieve the current OpenGL context

Parameters:getter (Function with no args returning the current OpenGL context) – Platform dependent GL context getter

For type checking and conversion:

silx.gui.plot3d.glutils.sizeofGLType(type_)

Returns the size in bytes of an element of type type_

silx.gui.plot3d.glutils.isSupportedGLType(type_)

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

silx.gui.plot3d.glutils.numpyToGLType(type_)

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

Program

class silx.gui.plot3d.glutils.Program(vertexShader, fragmentShader, attrib0='position')

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()

Make use of the program, compiling it if necessary

setUniformMatrix(name, value, transpose=True, safe=False)

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 silx.gui.plot3d.glutils.Texture(internalFormat, data=None, format_=None, shape=None, texUnit=0, minFilter=None, magFilter=None, wrap=None)

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 (2 or 3-tuple of int (height, width) or (depth, height, width)) – If data is None, shape of the texture
  • texUnit (int) – The texture unit to use
  • minFilter – OpenGL texture minimization filter
  • magFilter – OpenGL texture magnification filter
  • 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

discard()

Delete associated OpenGL texture

bind(texUnit=None)

Bind the texture to a texture unit.

Parameters:texUnit (int) – The texture unit to use
update(format_, data, offset=(0, 0, 0), texUnit=None)

Update the content of the texture.

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

Parameters:
  • format – The OpenGL format of the data
  • data – The data to use to update the texture
  • offset (2 or 3-tuple of int) – The offset in the texture where to copy the data
  • texUnit (int) – The texture unit to use (default: the one provided at init)

FramebufferTexture

class silx.gui.plot3d.glutils.FramebufferTexture(internalFormat, shape, stencilFormat=GL_DEPTH24_STENCIL8, depthFormat=GL_DEPTH24_STENCIL8, **kwargs)

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()

Bind this framebuffer for rendering

discard()

Delete associated OpenGL resources including texture

Vertex Buffer

class silx.gui.plot3d.glutils.VertexBuffer(data=None, size=None, usage=None, target=None)

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()

Bind the vertex buffer

update(data, offset=0, size=None)

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()

Delete the vertex buffer

class silx.gui.plot3d.glutils.VertexBufferAttrib(vbo, type_, size, dimension=1, offset=0, stride=0)

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)

Call glVertexAttribPointer with objects information

silx.gui.plot3d.glutils.vertexBuffer(arrays, prefix=None, suffix=None, usage=None)

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

Table Of Contents

Previous topic

Package structure

Next topic

scene

This Page