13

I'm trying to learn how to program vertex shaders. In Apple's sample project they have a line to set a

glUniform1f(uniforms[UNIFORM_TRANSLATE], (Glfloat)transY);

Then this value is used in

// value passt in f
// glUniform1f(uniforms[UNIFORM_TRANSLATE](Glfloat)transY);
uniform float translate;

void main()
{
    gl_Position.y+=sin( translate);
…

I was unable to find a list of all uniforms of all the uniforms.

Does any one know where I can find a list of all the uniforms and a good book or tutorial on learning how to program vertex shaders.

3 Answers 3

28

Uniform parameter is a data passed to GL shader, which doesn't change during the draw call.

You can query a linked GLSL program for a list of active uniforms with the following code:

int total = -1;
glGetProgramiv( program_id, GL_ACTIVE_UNIFORMS, &total ); 
for(int i=0; i<total; ++i)  {
    int name_len=-1, num=-1;
    GLenum type = GL_ZERO;
    char name[100];
    glGetActiveUniform( program_id, GLuint(i), sizeof(name)-1,
        &name_len, &num, &type, name );
    name[name_len] = 0;
    GLuint location = glGetUniformLocation( program_id, name );
}

This code retrieves a number of active uniforms and iterates though them, extracting name, type, number of values and uniform locations.

1
  • The size argument of glGetActiveUniform(), what it is? Here glGetActiveUniform it says Returns the size of the uniform variable.. Size in the sense of dimensionality of the variable, or size in bytes? Commented Sep 19, 2022 at 19:41
1

In addition to kvark's answer. You can add these lines of code to get a nice and beautiful readable format of the most common uniforms:

std::cout << "Uniform Info Name: " << name << " Location: " << location << " Type: ";
        if (type == GL_FLOAT_MAT4)
            std::cout << "mat4";
        else if (type == GL_FLOAT_VEC3)
            std::cout << "vec3";
        else if (type == GL_FLOAT_VEC4)
            std::cout << "vec4";
        else if (type == GL_FLOAT)
            std::cout << "float";
        else if (type == GL_INT)
            std::cout << "int";
        else if (type == GL_BOOL)
            std::cout << "bool";
        else if (type == GL_SAMPLER_2D)
            std::cout << "sampler2d";
        else
            std::cout << type;

        std::cout << std::endl;  
0

I think in that example code, UNIFORM_TRANSLATE is defined to 0, and then there's code like this:

uniforms [UNIFORM_TRANSLATE] = glGetUniformLocation (programId, "position");

so all the uniforms are retrieved by their names -- "position" in this case.

1
  • Thank you,I did a sercah and found uniforms [UNIFORM_TRANSLATE] = glGetUniformLocation (programId, "position");
    – Ted pottel
    Commented Jan 26, 2011 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.