Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the data type of an uniform variable

Tags:

opengl

glsl

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.

like image 576
Franck Freiburger Avatar asked Jan 18 '11 12:01

Franck Freiburger


People also ask

How does a uniform variable get its value?

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.

How many bytes is a vec4?

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.

What is vec3 in OpenGL?

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.


1 Answers

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).

like image 128
Bahbar Avatar answered Nov 15 '22 13:11

Bahbar