Passing a variable to an OpenGL GLSL shader

One option is to pass information via uniform variables.

After

glUseProgram(myShaderProgram);

you can use

GLint myUniformLocation = glGetUniformLocation(myShaderProgram, "myUniform");

and for example

glUniform1f(myUniformLocation, /* some floating point value here */);

In your vertex or fragment shader, you need to add the following declaration:

uniform float myUniform;

That’s it, in your shader you can now access (read-only) the value you passed earlier on via glUniform1f.

Of course uniform variables can be any valid GLSL type including complex types such
as arrays, structures or matrices. OpenGL provides a glUniform function with the usual suffixes,
appropriate for each type. For example, to assign to a variable of type vec3, one would
use glUniform3f or glUniform3fv.

Note: the value can’t be modified during execution of the shader, i.e. in a glBegin/glEnd block. It is read-only and the same for every fragment/vertex processed.

There are also several tutorials using uniforms, you can find them by googling “glsl uniform variable”.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)