IGLShader
Module: graphics
A base class for managing OpenGL shader programs with caching for uniforms
to optimize performance in graphics rendering, particularly for FEA visualization.
This class handles compilation, linking, and uniform setting for vertex,
fragment, and optional geometry shaders. It caches uniform locations and
values to avoid redundant OpenGL calls.
__init__
method
__init__(self, vs_src, fs_src, gs_src)
Initializes the shader program by compiling and linking the provided
shader sources.
shader sources.
Args
| Name | Type | Description |
|---|---|---|
| vs_src | The source code for the vertex shader as a string. | |
| fs_src | The source code for the fragment shader as a string. | |
| gs_src | Optional source code for the geometry shader as a string. | If None, no geometry shader is used. |
Raises
RuntimeError: If shader compilation or program linking fails.
_compile
method
_compile(self, src, shader_type)
use
method
use(self)
Activates this shader program for rendering.
This method binds the shader program to the current OpenGL context,
making it active for subsequent draw calls.
reset_cache
method
reset_cache(self)
Clears the uniform state cache.
Call this method if the shader program is reloaded or the OpenGL
context changes to ensure uniform values are re-set.
_get_location
method
_get_location(self, name)
_is_same
method
_is_same(self, name, value)
_update_cache
method
_update_cache(self, name, value)
set_mat4
method
set_mat4(self, name, mat)
Sets a 4x4 matrix uniform in the shader, with caching to avoid
redundant updates.
redundant updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| mat | The matrix object, assumed to have a data() method returning | a contiguous float array. |
set_vec3
method
set_vec3(self, name, v)
Sets a 3-component vector uniform in the shader, with caching to avoid
redundant updates.
redundant updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| v | A tuple of three floats representing the vector components. |
set_vec4
method
set_vec4(self, name, v)
Sets a 4-component vector uniform in the shader, with caching to avoid
redundant updates.
redundant updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| v | A tuple of four floats representing the vector components. |
set_float
method
set_float(self, name, v)
Sets a float uniform in the shader, with caching to avoid redundant
updates.
updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| v | The float value to set. |
set_int
method
set_int(self, name, v)
Sets an integer uniform in the shader, with caching to avoid redundant
updates.
updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| v | The integer value to set. |
set_uint
method
set_uint(self, name, v)
Sets an unsigned integer uniform in the shader, with caching to avoid
redundant updates.
redundant updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| v | The unsigned integer value to set. |
set_int_array
method
set_int_array(self, name, arr)
Sets an array of integers as a uniform in the shader, with caching to
avoid redundant updates.
avoid redundant updates.
Args
| Name | Type | Description |
|---|---|---|
| name | The name of the uniform variable in the shader. | |
| arr | A list of integers to set as the array. |