fsleyes.gl.shaders.arbp.program
¶
This module provides the ARBPShader
class, which encapsulates
an OpenGL shader program written according to the ARB_vertex_program
and ARB_fragment_program
extensions.
-
class
fsleyes.gl.shaders.arbp.program.
ARBPShader
(vertSrc, fragSrc, includePath, textureMap=None, constants=None, clean=True)¶ Bases:
object
The
ARBPShader
class encapsulates an OpenGL shader program written according to theARB_vertex_program
andARB_fragment_program
extensions. It parses and compiles vertex and fragment program code, and provides methods to load/unload the program, and to set vertex/fragment program parameters and vertex attributes.The
ARBPShader
class assumes that vertex/fragment program source has been written to work with the functions defined in thearbp.parse
module, which allows programs to be written so that parameter, vertex attribute and texture locations do not have to be hard coded in the source. Texture locations may be specified in__init__()
, and parameter/vertex attribute locations are automatically assigned by theARBPShader
.The following methods are available on an
ARBPShader
instance:Loads the shader program.
Unloads the shader program.
Deletes all GL resources managed by this
ARBPShader
.(Re-)generates the vertex and fragment program source code, and recompiles the programs.
Sets the value of the specified vertex attribute.
Typcical usage of an
ARBPShader
will look something like the following:vertSrc = 'vertex shader source' fragSrc = 'vertex shader source' # You must specify the texture unit # assignments at creation time. textures = { 'colourMapTexture' : 0, 'dataTexture' : 1 } program = ARBPShader(vertSrc, fragSrc, textures) # Load the program, and # enable program attributes # (texture coordinates) program.load() progra.loadAtts() # Set some parameters program.setVertParam('transform', np.eye(4)) program.setFragParam('clipping', [0, 1, 0, 0]) # Create and set vertex attributes vertices, normals = createVertices() program.setAtt('normals', normals) # Draw the scene gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(vertices)) # Clear the GL state program.unloadAtts() program.unload() # Delete the program when # it is no longer needed program.destroy()
Warning
The
ARBPShader
uses texture coordinates to pass vertex attributes to the shader programs. Therefore, if you are using anARBPShader
you cannot directly use texture coordinates.See also the
GLSLShader
, which provides similar functionality for GLSL shader programs.-
__init__
(vertSrc, fragSrc, includePath, textureMap=None, constants=None, clean=True)¶ Create an
ARBPShader
.- Parameters
vertSrc – Vertex program source.
fragSrc – Fragment program source.
textureMap – A dictionary of
{name : int}
mappings, specifying the texture unit assignments.constants – A dictionary of
{name : values}
mappings, specifying any constant parameters required by the programs. It is assumed that constant parameters are shared by the vertex and fragment programs.includePath – Path to a directory which contains any additional files that may be included in the given source files.
clean – If
True
(the default), the vertex and fragment program source is ‘cleaned’ before compilation - all comments, empty lines, and unncessary spaces are removed before compilation.
-
__del__
()¶ Prints a log message.
-
destroy
()¶ Deletes all GL resources managed by this
ARBPShader
.
-
recompile
()¶ (Re-)generates the vertex and fragment program source code, and recompiles the programs.
-
load
()¶ Loads the shader program.
-
loadAtts
()¶ Enables texture coordinates for all shader program attributes.
-
unload
()¶ Unloads the shader program.
-
unloadAtts
()¶ Disables texture coordinates on all texture units.
-
setVertParam
= <MagicMock name='mock.utils.memoize.Instanceify()()' id='140736137784080'>¶
-
setFragParam
= <MagicMock name='mock.utils.memoize.Instanceify()()' id='140736137784080'>¶
-
setConstant
= <MagicMock name='mock.utils.memoize.Instanceify()()' id='140736137784080'>¶
-
setAtt
(name, value)¶ Sets the value of the specified vertex attribute. Each vertex attribute is mapped to a texture coordinate. It is assumed that the given value is a
numpy
array of shape(n, l)
, wheren
is the number of vertices being drawn, andl
is the number of components in each vertex attribute coordinate.
-
_ARBPShader__cleanSource
(src)¶ Strips out comments and blank lines from the given string, unless
clean is False
(as passed into__init__()
).
-
_ARBPShader__compile
(vertSrc, fragSrc)¶ Called by
__init__()
. Compiles the vertex and fragment programs and returns references to the compiled programs.
-
_ARBPShader__generatePositions
(textureMap=None)¶ Called by
__init__()
. Generates positions for vertex/fragment program parameters and vertex attributes.The lengths of each vertex/fragment parameter are known (see
arbp.parse
), so these parameters are set up to be sequentially stored in the program parameter memory.Vertex attributes are passed to the vertex program as texture coordinates.
If texture units were not specified in
__init__
, texture units are also automatically assigned to each texture used in the fragment program.
-
_ARBPShader__getAttrTexUnit
(attr)¶ Returns the texture unit identifier which corresponds to the named vertex attribute.
-
_ARBPShader__normaliseParam
(value)¶ Used by
setVertParam()
andsetFragParam()
. Ensures that all vertex/fragment program parameters are vectors of length 4, or matrices of size(n, 4)
.
-
__dict__
= mappingproxy({'__module__': 'fsleyes.gl.shaders.arbp.program', '__doc__': "The ``ARBPShader`` class encapsulates an OpenGL shader program\n written according to the ``ARB_vertex_program`` and\n ``ARB_fragment_program`` extensions. It parses and compiles vertex\n and fragment program code, and provides methods to load/unload\n the program, and to set vertex/fragment program parameters and vertex\n attributes.\n\n\n The ``ARBPShader`` class assumes that vertex/fragment program source\n has been written to work with the functions defined in the\n :mod:`.arbp.parse` module, which allows programs to be written so that\n parameter, vertex attribute and texture locations do not have to be hard\n coded in the source. Texture locations may be specified in\n :meth:`__init__`, and parameter/vertex attribute locations are\n automatically assigned by the ``ARBPShader``.\n\n\n The following methods are available on an ``ARBPShader`` instance:\n\n .. autosummary::\n :nosignatures:\n\n load\n unload\n destroy\n recompile\n setVertParam\n setFragParam\n setAtt\n setConstant\n\n Typcical usage of an ``ARBPShader`` will look something like the\n following::\n\n vertSrc = 'vertex shader source'\n fragSrc = 'vertex shader source'\n\n # You must specify the texture unit\n # assignments at creation time.\n textures = {\n 'colourMapTexture' : 0,\n 'dataTexture' : 1\n }\n\n program = ARBPShader(vertSrc, fragSrc, textures)\n\n # Load the program, and\n # enable program attributes\n # (texture coordinates)\n program.load()\n progra.loadAtts()\n\n # Set some parameters\n program.setVertParam('transform', np.eye(4))\n program.setFragParam('clipping', [0, 1, 0, 0])\n\n # Create and set vertex attributes\n vertices, normals = createVertices()\n\n program.setAtt('normals', normals)\n\n # Draw the scene\n gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(vertices))\n\n # Clear the GL state\n program.unloadAtts()\n program.unload()\n\n # Delete the program when\n # it is no longer needed\n program.destroy()\n\n\n .. warning:: The ``ARBPShader`` uses texture coordinates to pass vertex\n attributes to the shader programs. Therefore, if you are using\n an ``ARBPShader`` you cannot directly use texture coordinates.\n\n\n See also the :class:`.GLSLShader`, which provides similar functionality for\n GLSL shader programs.\n ", '__init__': <function ARBPShader.__init__>, '__del__': <function ARBPShader.__del__>, 'destroy': <function ARBPShader.destroy>, 'recompile': <function ARBPShader.recompile>, 'load': <function ARBPShader.load>, 'loadAtts': <function ARBPShader.loadAtts>, 'unload': <function ARBPShader.unload>, 'unloadAtts': <function ARBPShader.unloadAtts>, 'setVertParam': <MagicMock name='mock.utils.memoize.Instanceify()()' id='140736137784080'>, 'setFragParam': <MagicMock name='mock.utils.memoize.Instanceify()()' id='140736137784080'>, 'setConstant': <MagicMock name='mock.utils.memoize.Instanceify()()' id='140736137784080'>, 'setAtt': <function ARBPShader.setAtt>, '_ARBPShader__normaliseParam': <function ARBPShader.__normaliseParam>, '_ARBPShader__getAttrTexUnit': <function ARBPShader.__getAttrTexUnit>, '_ARBPShader__generatePositions': <function ARBPShader.__generatePositions>, '_ARBPShader__cleanSource': <function ARBPShader.__cleanSource>, '_ARBPShader__compile': <function ARBPShader.__compile>, '__dict__': <attribute '__dict__' of 'ARBPShader' objects>, '__weakref__': <attribute '__weakref__' of 'ARBPShader' objects>})¶
-
__module__
= 'fsleyes.gl.shaders.arbp.program'¶
-
__weakref__
¶ list of weak references to the object (if defined)
-