Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
0e73962c
Commit
0e73962c
authored
9 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Some documentation for GLSL package modules.
parent
63cf0779
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
fsl/fsleyes/gl/glsl/parse.py
+13
-5
13 additions, 5 deletions
fsl/fsleyes/gl/glsl/parse.py
fsl/fsleyes/gl/glsl/program.py
+95
-4
95 additions, 4 deletions
fsl/fsleyes/gl/glsl/program.py
fsl/fsleyes/gl/shaders.py
+1
-1
1 addition, 1 deletion
fsl/fsleyes/gl/shaders.py
setup.py
+1
-0
1 addition, 0 deletions
setup.py
with
110 additions
and
10 deletions
fsl/fsleyes/gl/glsl/parse.py
+
13
−
5
View file @
0e73962c
#!/usr/bin/env python
#
# parse.py - Simple parser for extracting information about a GLSL program.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
# Based on work by Nicolas P. Rougier
...
...
@@ -51,11 +51,12 @@ about the program.
from
__future__
import
print_function
import
sys
import
logging
import
pyparsing
as
pp
log
=
logging
.
getLogger
(
__name__
)
...
...
@@ -167,11 +168,14 @@ def parseGLSL(source):
return
decs
if
__name__
==
'
__main__
'
:
import
sys
def
main
():
"""
If this module is executed as a script, this function is called.
It expects a path to a ``glsl`` file as a single parameter. This file
is parsed, and information about it printed to standard output.
"""
if
len
(
sys
.
argv
)
!=
2
:
print
(
'
Usage:
glslparse
.py file.glsl
'
)
print
(
'
Usage:
{}
.py file.glsl
'
.
format
(
__name__
)
)
sys
.
exit
(
0
)
infile
=
sys
.
argv
[
1
]
...
...
@@ -187,3 +191,7 @@ if __name__ == '__main__':
print
(
'
\n
--{}--
\n
'
.
format
(
d
.
upper
()))
for
t
,
n
in
v
:
print
(
'
{}: {}
'
.
format
(
t
,
n
))
if
__name__
==
'
__main__
'
:
main
()
This diff is collapsed.
Click to expand it.
fsl/fsleyes/gl/glsl/program.py
+
95
−
4
View file @
0e73962c
...
...
@@ -30,14 +30,85 @@ GLSL_ATTRIBUTE_TYPES = {
'
vec3
'
:
(
gl
.
GL_FLOAT
,
3
),
'
vec4
'
:
(
gl
.
GL_FLOAT
,
3
)
}
"""
This dictionary contains mappings between GLSL data types, and their
corresponding GL types and sizes.
"""
class
ShaderProgram
(
object
):
"""
The ``ShaderProgram`` class encapsulates information and logic about
a GLSL 1.20 shader program, comprising a vertex shader and a fragment
shader. It provides methods to set shader attribute and uniform values,
to configure attributes, and to load/unload the program. Furthermore,
the ``ShaderProgram`` makes sure that all uniform and attribut variables
are converted to the appropriate type. The following methods are available
on a ``ShaderProgram``:
.. autosummary::
:nosignatures:
load
unload
delete
loadAtts
unloadAtts
set
setAtt
setIndices
Typical usage of a ``ShaderProgram`` will look something like the
following::
vertSrc =
'
vertex shader source
'
fragSrc =
'
vertex shader source
'
program = ShaderProgram(vertSrc, fragSrc)
# Load the program
program.load()
# Set some uniform values
program.set(
'
lighting
'
, True)
program.set(
'
lightPos
'
, [0, 0, -1])
# Create and set vertex attributes
vertices, normals = createVertices()
program.setAtt(
'
vertex
'
, vertices)
program.setAtt(
'
normal
'
, normals)
# Load the attributes
program.loadAtts()
# Draw the scene
gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(vertices))
def
__init__
(
self
,
vertSrc
,
fragSrc
,
indexed
=
False
):
# Clear the GL state
program.unload()
program.unloadAtts()
# Delete the program when
# we no longer need it
program.delete()
"""
def
__init__
(
self
,
vertSrc
,
fragSrc
,
indexed
=
False
):
"""
Create a ``ShaderProgram``.
:arg vertSrc: String containing vertex shader source code.
:arg fragSrc: String containing fragment shader source code.
:arg indexed: If ``True``, it is assumed that the vertices processed
by this shader program will be drawn using an index
array. A vertex buffer object is created to store
vertex indices - this buffer is expected to be populated
via the :meth:`setIndices` method.
"""
self
.
program
=
self
.
__compile
(
vertSrc
,
fragSrc
)
...
...
@@ -95,10 +166,16 @@ class ShaderProgram(object):
def
load
(
self
):
"""
Loads this ``ShaderProgram`` into the GL state.
"""
gl
.
glUseProgram
(
self
.
program
)
def
loadAtts
(
self
):
"""
Binds all of the shader program ``attribute`` variables - you
must set the data for each attribute via :meth:`setAtt` before
calling this method.
"""
for
att
in
self
.
vertAttributes
:
aPos
=
self
.
positions
[
att
]
...
...
@@ -124,6 +201,8 @@ class ShaderProgram(object):
def
unloadAtts
(
self
):
"""
Disables all vertex attributes, and unbinds associated vertex buffers.
"""
for
att
in
self
.
vertAttributes
:
gl
.
glDisableVertexAttribArray
(
self
.
positions
[
att
])
...
...
@@ -140,10 +219,12 @@ class ShaderProgram(object):
def
unload
(
self
):
"""
Unloads the GL shader program.
"""
gl
.
glUseProgram
(
0
)
def
delete
(
self
):
"""
Deletes all GL resources managed by this ``ShaderProgram`.
"""
gl
.
glDeleteProgram
(
self
.
program
)
for
buf
in
self
.
buffers
.
values
():
...
...
@@ -152,6 +233,7 @@ class ShaderProgram(object):
def
set
(
self
,
name
,
value
):
"""
Sets the value for the specified GLSL ``uniform`` variable.
"""
vPos
=
self
.
positions
[
name
]
vType
=
self
.
types
[
name
]
...
...
@@ -169,6 +251,11 @@ class ShaderProgram(object):
def
setAtt
(
self
,
name
,
value
,
divisor
=
None
):
"""
Sets the value for the specified GLSL ``attribute`` variable.
:arg divisor: If specified, this value is used as a divisor for this
attribute via the ``glVetexAttribDivisor`` function.
"""
aType
=
self
.
types
[
name
]
aBuf
=
self
.
buffers
[
name
]
...
...
@@ -196,6 +283,10 @@ class ShaderProgram(object):
def
setIndices
(
self
,
indices
):
"""
If an index array is to be used by this ``ShaderProgram`` (see the
``indexed`` argument to :meth:`__init__`), the index array may be set
via this method.
"""
if
self
.
indexBuffer
is
None
:
raise
RuntimeError
(
'
Shader program was not
'
...
...
This diff is collapsed.
Click to expand it.
fsl/fsleyes/gl/shaders.py
+
1
−
1
View file @
0e73962c
...
...
@@ -22,7 +22,7 @@ following functions:
compileShaders
Some functions are also provided which make setting program variables a bit
Some functions are also provided which make setting
ARB
program variables a bit
less painful:
...
...
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
0
View file @
0e73962c
...
...
@@ -50,6 +50,7 @@ setup(
install_requires
=
[
'
pyopengl>=3.1.0
'
,
'
pyparsing>=2.0.3
'
,
'
numpy>=1.8.1
'
,
'
scipy>=0.14.0
'
,
'
matplotlib>=1.3.1
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment