I am trying to get the type of an uniform variable defined in a fragment shader:
uniform vec3 color;
uniform float zoom;
uniform int max;
void main() {
...
}
The glGetActiveUniformARB(program, index, maxLength, *length, *size, *type, *name) seems to be the right API function to use but I don't know how to determinate index from a variable name. glGetUniformLocationARB returns the location of an uniform variable, that seems not to be the same as the index.
Uniforms are intended to be set by the user from OpenGL, rather than within the shader. However, you can initialize them to a default value using standard GLSL initalizer syntax: uniform vec3 initialUniform = vec3(1.0, 0.0, 0.0); This will cause the uniform to have this vector as its value, until the user changes it.
So, the alignment of a mat4 is like that of a vec4, which is 16 bytes. And the size of a mat4 is 4*sizeof(vec4) = 4*16 bytes. mat3 is treated as three vec4's. So, alignment is 16 bytes and size is 3*16 bytes.
vec3 is a floating point vector with three components. It can be initialized by: Providing a scalar value for each component. Providing one scalar value. This value is used for all components.
Well, the API kind-of assumes that if you know the name of the uniform, you also know the type (those 2 things are written next to each other in the code) so it does not allow a simple access to the type by name.
That said, you can iterate over all the active uniforms with glGetActiveUniformARB to find the one that interests you. Note also that this will only return valid data if the uniform is actually active (i.e. the GLSL compiler thought it was useful for the final computations).
(Typically, the expected usage is to iterate over all the uniforms, extract name and type, and then get their location from name to know how to update them at run-time. Not the other way around).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With