GLSL Uniform Structures

Do you know that you can use data structures as uniform variables in GLSL?

In the last version of the code sample pack, there is a demo (host_api/RubikCube/Cube_Rotation_Quaternion/demo_v3.xml) where the vertex shader has the following code:

#version 150
in vec4 gxl3d_Position;
uniform mat4 gxl3d_ViewProjectionMatrix;

struct Transform
{
  vec4 position;
  vec4 axis_angle;
};
uniform Transform T;


...
...

vec3 rotate_vertex_position(vec3 position, vec3 axis, float angle)
{
  ...
}

void main()
{
  vec3 P = rotate_vertex_position(gxl3d_Position.xyz, T.axis_angle.xyz, T.axis_angle.w);
  P += T.position.xyz;
  gl_Position = gxl3d_ViewProjectionMatrix * vec4(P, 1);
  ...
  ...
}

In this vertex shader, the variable T is a structure (struct Transform {...}) and is used as an uniform variable. Setting the values of the uniforms from Lua (or Python, or in C/C++) is quite simple:

gh_gpu_program.bind(gpu_prog)
gh_gpu_program.uniform4f(gpu_prog, "T.axis_angle", 1, 0, 0, angle)
gh_gpu_program.uniform4f(gpu_prog, "T.position", x, y, z, 1)




Leave a Comment

Your email address will not be published. Required fields are marked *