12

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.

genpfault
  • 47,669
  • 9
  • 68
  • 119
Ted pottel
  • 6,503
  • 16
  • 67
  • 124

3 Answers3

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.

kvark
  • 4,916
  • 2
  • 22
  • 32
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;  
Edvin
  • 1,307
  • 2
  • 12
  • 21
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.

vmpstr
  • 4,841
  • 2
  • 23
  • 24
  • Thank you,I did a sercah and found uniforms [UNIFORM_TRANSLATE] = glGetUniformLocation (programId, "position"); – Ted pottel Jan 26 '11 at 23:10