Object oriented interface to OpenGL.
This module implements classes for the things that are “objects” in OpenGL, such as textures, FBO’s, VBO’s and shaders. Further, some convenience classes are implemented (like the collection class).
This set of classes provides a friendly (Pythonic) interface to OpenGL, and is designed to provide OpenGL’s full functionality.
All classes inherit from GLObject, which provide a basic interface, enabling, activating and deleting the object. Central to each visualization is the Program. Other objects, such as Texture2D and VertexBuffer should be set as uniforms and attributes of the Program object.
Example:
# Init
program = gloo.Program(vertex_source, fragment_source)
program['a_position'] = gloo.VertexBuffer(my_positions_array)
program['s_texture'] = gloo.Texture2D(my_image)
...
# Draw event handler
program['u_color'] = 0.0, 1.0, 0.0
program.draw(gl.GL_TRIANGLES)
Note
With vispy.gloo we strive to offer a Python interface that provides the full functionality of OpenGL. However, this layer is a work in progress and there are still a few known limitations. Most notably:
vispy.gloo.
GLObject
[source]¶Generic GL object that represents an object on the GPU.
When a GLObject is instantiated, it is associated with the currently active Canvas, or with the next Canvas to be created if there is no current Canvas
delete
()[source]¶Delete the object from GPU memory.
Note that the GPU object will also be deleted when this gloo object is about to be deleted. However, sometimes you want to explicitly delete the GPU object explicitly.
glir
¶The glir queue for this object.
id
¶The id of this GL object used to reference the GL object in GLIR. id’s are unique within a process.
vispy.gloo.
Program
(vert=None, frag=None, count=0)[source]¶Shader program object
A Program is an object to which shaders can be attached and linked to create the final program.
Uniforms and attributes can be set using indexing: e.g.
program['a_pos'] = pos_data
and program['u_color'] = (1, 0, 0)
.
Parameters: | vert : str
frag : str
count : int (optional)
|
---|
Notes
If several shaders are specified, only one can contain the main function. OpenGL ES 2.0 does not support a list of shaders.
bind
(data)[source]¶Bind a VertexBuffer that has structured data
Parameters: | data : VertexBuffer
|
---|
draw
(mode='triangles', indices=None, check_error=True)[source]¶Draw the attribute arrays in the specified mode.
Parameters: | mode : str | GL_ENUM
indices : array
check_error:
|
---|
set_shaders
(vert, frag, geom=None, update_variables=True)[source]¶Set the vertex and fragment shaders.
Parameters: | vert : str
frag : str
geom : str (optional)
update_variables : bool
|
---|
shaders
¶All currently attached shaders
variables
¶A list of the variables in use by the current program
The list is obtained by parsing the GLSL source code.
Returns: | variables : list
|
---|
vispy.gloo.buffer.
Buffer
(data=None, nbytes=None)[source]¶Generic GPU buffer.
A generic buffer is an interface used to upload data to a GPU array buffer (ARRAY_BUFFER or ELEMENT_ARRAY_BUFFER). It keeps track of buffer size but does not have any CPU storage. You can consider it as write-only.
The set_data is a deferred operation: you can call it even if an OpenGL context is not available. The update function is responsible to upload pending data to GPU memory and requires an active GL context.
The Buffer class only deals with data in terms of bytes; it is not aware of data type or element size.
Parameters: | data : ndarray | None
nbytes : int | None
|
---|
nbytes
¶Buffer size in bytes
resize_bytes
(size)[source]¶Resize this buffer (deferred operation).
Parameters: | size : int
|
---|
set_data
(data, copy=False)[source]¶Set data in the buffer (deferred operation).
This completely resets the size and contents of the buffer.
Parameters: | data : ndarray
copy: bool
|
---|
set_subdata
(data, offset=0, copy=False)[source]¶Set a sub-region of the buffer (deferred operation).
Parameters: | data : ndarray
offset: int
copy: bool
|
---|
vispy.gloo.buffer.
DataBuffer
(data=None)[source]¶GPU data buffer that is aware of data type and elements size
Parameters: | data : ndarray | None
|
---|
dtype
¶Buffer dtype
glsl_type
¶GLSL declaration strings required for a variable to hold this data.
itemsize
¶The total number of bytes required to store the array data
offset
¶Buffer offset (in bytes) relative to base
resize_bytes
(size)[source]¶Resize the buffer (in-place, deferred operation)
Parameters: | size : integer
|
---|
Notes
This clears any pending operations.
set_data
(data, copy=False, **kwargs)[source]¶Set data (deferred operation)
Parameters: | data : ndarray
copy: bool
**kwargs : dict
|
---|
set_subdata
(data, offset=0, copy=False, **kwargs)[source]¶Set a sub-region of the buffer (deferred operation).
Parameters: | data : ndarray
offset: int
copy: bool
**kwargs : dict
|
---|
size
¶Number of elements in the buffer
stride
¶Stride of data in memory
vispy.gloo.texture.
BaseTexture
(data=None, format=None, resizable=True, interpolation=None, wrapping=None, shape=None, internalformat=None, resizeable=None)[source]¶A Texture is used to represent a topological set of scalar values.
Parameters: | data : ndarray | tuple | None
format : str | enum | None
resizable : bool
interpolation : str | None
wrapping : str | None
shape : tuple | None
internalformat : str | None
resizeable : None
|
---|
format
¶The texture format (color channels).
interpolation
¶Texture interpolation for minification and magnification.
resize
(shape, format=None, internalformat=None)[source]¶Set the texture size and format
Parameters: | shape : tuple of integers
format : str | enum | None
internalformat : str | enum | None
|
---|
set_data
(data, offset=None, copy=False)[source]¶Set texture data
Parameters: | data : ndarray
offset: int | tuple of ints
copy: bool
|
---|
Notes
This operation implicitely resizes the texture to the shape of the data if given offset is None.
shape
¶Data shape (last dimension indicates number of color channels)
wrapping
¶Texture wrapping mode
vispy.gloo.
Texture2D
(data=None, format=None, resizable=True, interpolation=None, wrapping=None, shape=None, internalformat=None, resizeable=None)[source]¶Two dimensional texture
Parameters: | data : ndarray
format : str | enum | None
resizable : bool
interpolation : str
wrapping : str
shape : tuple
internalformat : str | None
resizeable : None
|
---|
glsl_sample
¶GLSL function that samples the texture.
glsl_sampler_type
¶GLSL type of the sampler.
glsl_type
¶GLSL declaration strings required for a variable to hold this data.
height
¶Texture height
width
¶Texture width
vispy.gloo.
Texture3D
(data=None, format=None, resizable=True, interpolation=None, wrapping=None, shape=None, internalformat=None, resizeable=None)[source]¶Three dimensional texture
Parameters: | data : ndarray | tuple | None
format : str | enum | None
resizable : bool
interpolation : str | None
wrapping : str | None
shape : tuple | None
internalformat : str | None
resizeable : None
|
---|
depth
¶Texture depth
glsl_sample
¶GLSL function that samples the texture.
glsl_sampler_type
¶GLSL type of the sampler.
glsl_type
¶GLSL declaration strings required for a variable to hold this data.
height
¶Texture height
width
¶Texture width
vispy.gloo.
TextureAtlas
(shape=(1024, 1024), dtype=<class 'numpy.float32'>)[source]¶Group multiple small data regions into a larger texture.
The algorithm is based on the article by Jukka Jylänki : “A Thousand Ways to Pack the Bin - A Practical Approach to Two-Dimensional Rectangle Bin Packing”, February 27, 2010. More precisely, this is an implementation of the Skyline Bottom-Left algorithm based on C++ sources provided by Jukka Jylänki at: http://clb.demon.fi/files/RectangleBinPack/.
Parameters: | shape : tuple of int
dtype : numpy.dtype object
|
---|
Notes
This creates a 2D texture that holds 1D float32 data. An example of simple access:
>>> atlas = TextureAtlas()
>>> bounds = atlas.get_free_region(20, 30)
>>> atlas.set_region(bounds, np.random.rand(20, 30).T)
vispy.gloo.wrappers.
read_pixels
(viewport=None, alpha=True, mode='color', out_type='unsigned_byte')[source]¶Read pixels from the currently selected buffer.
Under most circumstances, this function reads from the front buffer. Unlike all other functions in vispy.gloo, this function directly executes an OpenGL command.
Parameters: | viewport : array-like | None
alpha : bool
mode : str
out_type : str | dtype
|
---|---|
Returns: | pixels : array
|
Functionality to deal with GL Contexts in vispy. This module is defined in gloo, because gloo (and the layers that depend on it) need to be context aware. The vispy.app module “provides” a context, and therefore depends on this module. Although the GLContext class is aimed for use by vispy.app (for practical reasons), it should be possible to use GLContext without using vispy.app by overloading it in an appropriate manner.
An GLContext object acts as a placeholder on which different parts of vispy (or other systems) can keep track of information related to an OpenGL context.
vispy.gloo.context.
FakeCanvas
[source]¶Fake canvas to allow using gloo without vispy.app
Instantiate this class to collect GLIR commands from gloo interactions. Call flush() in your draw event handler to execute the commands in the active contect.
vispy.gloo.context.
GLContext
(config=None, shared=None)[source]¶An object encapsulating data necessary for a OpenGL context
Parameters: | config : dict | None
shared : instance of GLContext | None
|
---|
capabilities
¶The OpenGL capabilities
config
¶A dictionary describing the configuration of this GL context.
For the app backends to create the GLShared object.
Parameters: | name : str
ref : object
|
---|
glir
¶The glir queue for the context. This queue is for objects that can be shared accross canvases (if they share a contex).
set_viewport
(*args)[source]¶Set the OpenGL viewport
This is a wrapper for gl.glViewport.
Parameters: | *args : tuple
|
---|
Get the object that represents the namespace that can potentially be shared between multiple contexts.
Representation of a “namespace” that can be shared between different contexts. App backends can associate themselves with this object via add_ref().
This object can be used to establish whether two contexts/canvases share objects, and can be used as a placeholder to store shared information, such as glyph atlasses.
Add a reference for the backend object that gives access to the low level context. Used in vispy.app.canvas.backends. The given name must match with that of previously added references.
The name of the canvas backend that this shared namespace is associated with. Can be None.
The GLIR parser (shared between contexts)
A reference (stored internally via a weakref) to an object that the backend system can use to obtain the low-level information of the “reference context”. In Vispy this will typically be the CanvasBackend object.
vispy.gloo.context.
forget_canvas
(canvas)[source]¶Forget about the given canvas. Used by the canvas when closed.
vispy.gloo.context.
get_current_canvas
()[source]¶Get the currently active canvas
Returns None if there is no canvas available. A canvas is made active on initialization and before the draw event is emitted.
When a gloo object is created, it is associated with the currently active Canvas, or with the next Canvas to be created if there is no current Canvas. Use Canvas.set_current() to manually activate a canvas.