Construct GLUniforms to access uniforms in a GLProgram.
We have a vertex shader with source such as this:
1 #version 130 2 3 uniform mat4 projection; 4 uniform mat4 modelView; 5 in vec3 position; 6 7 void main() 8 { 9 gl_Position = projection * modelView * vec4(position, 1.0); 10 }
We have the following uniforms specification struct:
1 struct Uniforms 2 { 3 import gl3n.linalg; 4 mat4 projection; 5 mat4 modelView; 6 }
NOTE: Array uniforms are not supported at the moment, but will be supported in future.
The vertex shader above is used by a GLProgram program.
The following code builds a GLUniforms struct:
1 try 2 { 3 auto uniforms = GLUniforms!Uniforms(program); 4 } 5 catch(OpenGLException e) 6 { 7 writeln("ERROR: uniforms in a program have unexpected types: ", e); 8 return; 9 }
The GLUniforms constructor enforces that types of uniforms in program match types in Uniforms - the uniforms specification struct. Note that if program is missing any uniform, there is no error, only a logged warning, and a dummy uniform is created. This is because some GPU drivers agressively optimize and remove uniforms, and we don't want to trigger an error just because a user is running our program on a GPU we didn't test.
Finally, the following code sets the uniforms through uniforms.
// mat4 projectionMatrix, modelViewMatrix uniforms.projection = projectionMatrix; uniforms.modelView = modelViewMatrix;
A type-safe API for manipulating GLSL uniform variables.
'Uniforms specification' struct Spec specifies types and names of uniforms in a program. GLUniforms!Spec has properties with names matching fields of Spec. Uniforms in a program can be set by setting these properties.