demosys.opengl package¶
Subpackages¶
Submodules¶
demosys.opengl.fbo module¶
-
class
demosys.opengl.fbo.
FBO
¶ Bases:
object
Frame buffer object
-
add_color_attachment
(texture)¶ Add a texture as a color attachment.
Parameters: texture – The Texture object
-
bind
(stack=True)¶ Bind FBO adding it to the stack.
Parameters: stack – (bool) If the bind should push the current FBO on the stack.
-
check_status
()¶ Checks the completeness of the FBO
-
clear
()¶ Clears the FBO using
glClear
.
-
classmethod
create
(width, height, depth=False, internal_format=GL_RGBA8, format=GL_RGBA, type=GL_UNSIGNED_BYTE, layers=1)¶ Convenient shortcut for creating single color attachment FBOs
Parameters: - width – Color buffer width
- height – Coller buffer height
- depth – (bool) Create a depth attachment
- internal_format – The internalformat of the color buffer
- format – The format of the color buffer
- type – The type of the color buffer
Returns: A new FBO
-
depth_shader
= None¶
-
draw_color_layer
(layer=0, pos=(0.0, 0.0), scale=(1.0, 1.0))¶ Draw a color layer in the FBO. :param layer: Layer ID :param pos: (tuple) offset x, y :param scale: (tuple) scale x, y
-
draw_depth
(near, far, pos=(0.0, 0.0), scale=(1.0, 1.0))¶ Draw depth buffer linearized. :param near: Near plane in projection :param far: Far plane in projection :param pos: (tuple) offset x, y :param scale: (tuple) scale x, y
-
quad
= None¶
-
release
(stack=True)¶ Bind FBO popping it from the stack
Parameters: stack – (bool) If the bind should be popped form the FBO stack.
-
set_depth_attachment
(texture)¶ Set a texture as depth attachment.
Parameters: texture – The Texture object
-
size
¶ Attempts to determine the pixel size of the FBO. Currently returns the size of the first color attachment. If the FBO has no color attachments, the depth attachment will be used. Raises
`FBOError
if the size cannot be determined.Returns: (w, h) tuple representing the size in pixels
-
stack
= []¶
-
-
exception
demosys.opengl.fbo.
FBOError
¶ Bases:
Exception
Generic FBO Error
demosys.opengl.shader module¶
-
class
demosys.opengl.shader.
Attribute
(name, type, location)¶ Bases:
object
Stores information about a shader attribute
-
class
demosys.opengl.shader.
Shader
(path=None, name=None)¶ Bases:
object
Represents a shader program
-
bind
()¶ Bind the shader
-
build_attribute_map
()¶ Builds an internal attribute map by querying the program. This way we don’t have to query OpenGL (can cause slowdowns) This information is also used when the shader and VAO negotiates the buffer binding.
-
build_uniform_map
()¶ Builds an internal uniform map by querying the program. This way we don’t have to query OpenGL (can cause slowdowns)
-
delete
()¶ Frees the memory and invalidates the name associated with the program
-
link
()¶ Links the program. Raises
ShaderError
if the linker failed.
-
prepare
()¶ Compiles all the shaders and links the program. If the linking is successful, it builds the uniform and attribute map.
-
set_fragment_source
(source)¶ Set the fragment shader source
Parameters: source – (string) Fragment shader source
-
set_geometry_source
(source)¶ Set the geometry shader source
Parameters: source – (string) Geometry shader source
-
set_source
(source)¶ Set a single source file. This is used when you have all shaders in one file separated by preprocessors.
Parameters: source – (string) The shader source
-
set_vertex_source
(source)¶ Set the vertex shader source
Parameters: source – (string) Vertex shader source
-
uniform
(name, raise_on_error=True)¶ Get the uniform location. Raises
ShaderError
if the uniform is not found.Parameters: - name – The name of the uniform
- raise_on_error – Raise ShaderError when uniform is not found
Returns: Uniform object
-
uniform_1b
(name, value, uniform=None)¶ Sets an bool
Parameters: - name – Name of the uniform
- value – Integer value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1d
(name, value, uniform=None)¶ Set a double uniform
Parameters: - name – Name of the uniform
- value – double value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1dv
(name, value, count=1, uniform=None)¶ Set a double uniform
Parameters: - name – Name of the uniform
- value – float array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1f
(name, value, uniform=None)¶ Set a float uniform
Parameters: - name – Name of the uniform
- value – float value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1fv
(name, value, count=1, uniform=None)¶ Set a float uniform
Parameters: - name – Name of the uniform
- count – Length of the uniform array (default 1)
- value – float array
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1i
(name, value, uniform=None)¶ Sets an int
Parameters: - name – Name of the uniform
- value – Integer value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1iv
(name, value, count=1, uniform=None)¶ Sets an int
Parameters: - name – Name of the uniform
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1ui
(name, value, uniform=None)¶ Sets an uint
Parameters: - name – Name of the uniform
- value – Integer value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_1uiv
(name, value, count=1, uniform=None)¶ Sets an uint
Parameters: - name – Name of the uniform
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2b
(name, x, y, uniform=None)¶ Sets an bvec2
Parameters: - name – Uniform name
- x – bool
- y – bool
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2d
(name, x, y, uniform=None)¶ Set a dvec2 uniform
Parameters: - name – name of the uniform
- x – double value
- y – double value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2dv
(name, value, count=1, uniform=None)¶ Set a dvec2 uniform
Parameters: - name – name of the uniform
- value – float array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2f
(name, x, y, uniform=None)¶ Set a vec2 uniform
Parameters: - name – name of the uniform
- x – float value
- y – float value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2fv
(name, value, count=1, uniform=None)¶ Set a vec2 uniform
Parameters: - name – name of the uniform
- count – Length of the uniform array (default 1)
- value – float array
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2i
(name, x, y, uniform=None)¶ Sets an ivec2
Parameters: - name – Uniform name
- x – Integer
- y – Integer
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2iv
(name, value, count=1, uniform=None)¶ Sets an ivec2
Parameters: - name – Uniform name
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2ui
(name, x, y, uniform=None)¶ Sets an uvec2
Parameters: - name – Uniform name
- x – Integer
- y – Integer
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_2uiv
(name, value, count=1, uniform=None)¶ Sets an uvec2
Parameters: - name – Uniform name
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3b
(name, x, y, z, uniform=None)¶ Sets an bvec3
Parameters: - name – Uniform name
- x – bool
- y – bool
- z – bool
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3d
(name, x, y, z, uniform=None)¶ Set a dvec3 uniform
Parameters: - name – Name of the uniform
- x – double value
- y – double value
- z – double value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3dv
(name, value, count=1, uniform=None)¶ Set a dvec3 uniform
Parameters: - name – Name of the uniform
- value – float array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3f
(name, x, y, z, uniform=None)¶ Set a vec3 uniform
Parameters: - name – Name of the uniform
- x – float value
- y – float value
- z – float value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3fv
(name, value, count=1, uniform=None)¶ Set a vec3 uniform
Parameters: - name – Name of the uniform
- count – Length of the uniform array (default 1)
- value – float array
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3i
(name, x, y, z, uniform=None)¶ Sets an ivec3
Parameters: - name – Uniform name
- x – Integer
- y – Integer
- z – Integer
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3iv
(name, value, count=1, uniform=None)¶ Sets an ivec3
Parameters: - name – Uniform name
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3ui
(name, x, y, z, uniform=None)¶ Sets an uvec3
Parameters: - name – Uniform name
- x – Integer
- y – Integer
- z – Integer
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_3uiv
(name, value, count=1, uniform=None)¶ Sets an uvec3
Parameters: - name – Uniform name
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4b
(name, x, y, z, w, uniform=None)¶ Sets an bvec4
Parameters: - name – Uniform name
- x – bool
- y – bool
- z – bool
- w – bool
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4d
(name, x, y, z, w, uniform=None)¶ Set a dvec4 uniform
Parameters: - name – Name of the uniform
- x – double value
- y – double value
- z – double value
- w – double value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4dv
(name, value, count=1, uniform=None)¶ Set a dvec4 uniform
Parameters: - name – Name of the uniform
- value – float array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4f
(name, x, y, z, w, uniform=None)¶ Set a vec4 uniform
Parameters: - name – Name of the uniform
- x – float value
- y – float value
- z – float value
- w – float value
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4fv
(name, value, count=1, uniform=None)¶ Set a vec4 uniform
Parameters: - name – Name of the uniform
- count – Length of the uniform array (default 1)
- value – float array
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4i
(name, x, y, z, w, uniform=None)¶ Sets an ivec4
Parameters: - name – Uniform name
- x – Integer
- y – Integer
- z – Integer
- w – Integer
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4iv
(name, value, count=1, uniform=None)¶ Sets an ivec4
Parameters: - name – Uniform name
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4ui
(name, x, y, z, w, uniform=None)¶ Sets an uvec4
Parameters: - name – Uniform name
- x – Integer
- y – Integer
- z – Integer
- w – Integer
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_4uiv
(name, value, count=1, uniform=None)¶ Sets an uvec4
Parameters: - name – Uniform name
- value – integer array
- count – Length of the uniform array (default 1)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_check
(name, expected_type, raise_on_error=True)¶ Get a uniform and verify the expected type. This is used by the
uniform_*
methods for validating the actual type in the shader and the uniform we are trying to set. RaisesShaderError
if the uniform is not found.Parameters: - name – The name of the uniform
- expected_type – The expected type of the uniform.
- raise_on_error – Raise ShaderError when uniform do not match type
Returns: The Uniform object
-
uniform_mat2
(name, mat, transpose=GL_FALSE, uniform=None)¶ Sets a mat3 uniform
Parameters: - name – Name of the uniform
- mat – matrix
- transpose – Traspose the matrix (true/false)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_mat3
(name, mat, transpose=GL_FALSE, uniform=None)¶ Sets a mat3 uniform
Parameters: - name – Name of the uniform
- mat – matrix
- transpose – Traspose the matrix (true/false)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_mat4
(name, mat, transpose=GL_FALSE, uniform=None)¶ Set a mat4 uniform
Parameters: - name – Name of the uniform
- mat – matrix
- transpose – Traspose the matrix (true/false)
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_sampler_1d
(unit, name, texture, uniform=None)¶ Sets a sampler1d
Parameters: - unit – The texture unit to use (0 - N)
- name – Name of the uniform
- texture – The Texture object
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_sampler_2d
(unit, name, texture, uniform=None)¶ Sets a sampler2d
Parameters: - unit – The texture unit to use (0 - N)
- name – Name of the uniform
- texture – The Texture object
- uniform – For auto-injection for uniform instance. Do not use.
-
uniform_sampler_3d
(unit, name, texture, uniform=None)¶ Sets a sampler3d
Parameters: - unit – The texture unit to use (0 - N)
- name – Name of the uniform
- texture – The Texture object
- uniform – For auto-injection for uniform instance. Do not use.
-
-
exception
demosys.opengl.shader.
ShaderError
¶ Bases:
Exception
-
class
demosys.opengl.shader.
ShaderSource
(type, name, source)¶ Bases:
object
Helper class to deal with shader source files. This represents a single shader (vert/frag/geo)
-
compile
()¶ Compile the shader
-
delete
(program=None)¶ Frees the memory and invalidates the name associated with the shader object
-
find_out_attribs
()¶ Get all out attributes in the shader source. :return: List of attribute names
-
print
()¶ Print the shader lines
-
type_name
()¶ Returns a string representation of the shader type
-
-
class
demosys.opengl.shader.
TypeInfo
(name, value, size)¶ Bases:
object
Information about a data type in a glsl shader.
Example: “GL_FLOAT_VEC3” is a GL.GL_FLOAT_VEC3 of byte size 12
-
class
demosys.opengl.shader.
Uniform
(name, size, type, location)¶ Bases:
object
Stores information about a uniform
-
demosys.opengl.shader.
uniform_type
(uniform_type, name_arg_index=1)¶ Checks uniform parameter types and injects uniform instances into uniform methods.
demosys.opengl.texture module¶
-
class
demosys.opengl.texture.
Texture
(name=None, path=None, width=0, height=0, depth=0, lod=0, target=GL_TEXTURE_2D, internal_format=GL_RGBA8, format=GL_RGBA, type=GL_UNSIGNED_BYTE, mipmap=False, anisotropy=0, min_filter=GL_LINEAR, mag_filter=GL_LINEAR, wrap_s=GL_CLAMP_TO_EDGE, wrap_t=GL_CLAMP_TO_EDGE, wrap_r=GL_CLAMP_TO_EDGE)¶ Bases:
object
Represents a texture
-
bind
()¶ Binds the texture to the currently active texture unit
-
classmethod
create_2d
(**kwargs)¶ Creates a 2d texture. All parameters are passed on the texture initializer.
Returns: Texture object
-
draw
(shader=None, pos=(0.0, 0.0), scale=(1.0, 1.0))¶ Draw texture :param shader: override shader :param pos: (tuple) offset x, y :param scale: (tuple) scale x, y
-
classmethod
from_image
(path, image=None, **kwargs)¶ Creates and image from a image file using Pillow/PIL. Additional parameters is passed to the texture initializer.
Parameters: - path – The path to the file
- image – The PIL/Pillow image object (Can be None)
Returns: Texture object
-
quad
= None¶
-
set_image
(image)¶ Set pixel data using a image file with PIL/Pillow.
Parameters: image – The PIL/Pillow image object
-
set_interpolation
(min_filter, mag_filter)¶ Sets the texture interpolation mode
Parameters: - min_filter – Min filter mode (glenum)
- mag_filter – Max filter mode (glenum)
-
set_texture_repeat
(wrap_s, wrap_t, wrap_r)¶ Sets the texture repeat mode
Parameters: - wrap_s – Repeat mode S (glenum)
- wrap_t – Repeat mode T (glenum)
- wrap_r – Repeat mode R (glenum)
-
shader_2d
= None¶
-
size
¶ Get the dimensions of the texture
Returns: (w, h) tuple
-
demosys.opengl.vao module¶
-
class
demosys.opengl.vao.
ArrayBuffer
(format, vbo)¶ Bases:
object
Container for a vbo with additional information
-
size
¶ Returns: Size of the vbo in bytes
-
target
¶ Returns: Returns the trarget of the vbo. GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER etc
-
usage
¶ Returns: The usage of the vbo. GL_DYNAMIC_DRAW / GL_STATIC_DRAW
-
vertices
¶ Returns: The number of vertices based on the current stride
-
-
class
demosys.opengl.vao.
ArrayMapping
(array_buffer, attrib_name, components, offset)¶ Bases:
object
Keeps track of vbo to attribute mapping
-
class
demosys.opengl.vao.
VAO
(name, mode=GL_TRIANGLES)¶ Bases:
object
Vertex Array Object
-
add_array_buffer
(format, vbo)¶ Register a vbo in the VAO. This can be called multiple times. This can be one or multiple buffers (interleaved or not)
Parameters: - format – The format of the buffer
- vbo – The vbo object
-
bind
(shader)¶ Bind the VAO using a shader. This is the standard way of binding so the shader and VAO can negotiate the needed attributes. This will generate new VAOs in the background on the fly (caching them) if needed.
Parameters: shader – The shader Returns: A VAOBindContext object (optional use)
-
build
()¶ Finalize the VAO. This runs various sanity checks on the input data.
-
draw
(mode=None)¶ Draw the VAO. Will use
glDrawElements
if an element buffer is present andglDrawArrays
if no element array is present.Parameters: mode – Override the draw mode (GL_TRIANGLES etc)
-
generate_vao_combo
(shader)¶ Create a VAO based on the shader’s attribute specification. This is called by
bind(shader)
and should not be messed with unless you are absolutely sure about what you are doing.Parameters: shader – The shader we are generating the combo for Returns: A new VAOCombo object with the correct attribute binding
-
map_buffer
(vbo, attrib_name, components)¶ Map parts of the vbos to an attribute name. This can be called multiple times to describe hos the buffers map to attribute names. If the same vbo is passed more than once it must be an interleaved buffer.
Parameters: - vbo – The vbo
- attrib_name – Name of the attribute in the shader
- components – Number of components (for example 3 for a x, y, x position)
-
set_element_buffer
(format, vbo)¶ Set the index buffer for this VAO
Parameters: - format – The format of the element buffer
- vbo – the vbo object
-
-
class
demosys.opengl.vao.
VAOBindContext
(vao)¶ Bases:
object
Context managers for bound VAOs
-
class
demosys.opengl.vao.
VAOCombo
(key)¶ Bases:
object
VAO of a specific attribute configuration. These are the actual VAOs used when drawing.
-
add_array_mapping
(mapping)¶ Add ArrayMappings relevant to this VAO version
Parameters: mapping – The ArrayMapping to add
-
bind
()¶ Bind the VAO
-
-
exception
demosys.opengl.vao.
VAOError
¶ Bases:
Exception
-
demosys.opengl.vao.
type_size
(format)¶ Determines the byte size of a format