Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find a list of all the uniforms in OpenGL es 2.0 vertex shader pro

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.

like image 362
Ted pottel Avatar asked Jan 24 '11 15:01

Ted pottel


People also ask

How do you use uniforms in OpenGL?

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 do you value your uniform on OpenGL?

After linking has occurred, the command glGetUniformLocation can be used to obtain the location of a uniform variable. This location value can then be passed to glGetUniform or glGetnUniform in order to query the current value of the uniform variable.

How many times is the vertex shader called?

Vertex shader executes 3 X 5 = 15 times. Geometry shader executes only 3 times. Fragment shader executes as many as the involved pixels count.

What is OpenGL vertex shader?

The Vertex Shader is the programmable Shader stage in the rendering pipeline that handles the processing of individual vertices. Vertex shaders are fed Vertex Attribute data, as specified from a vertex array object by a drawing command.


2 Answers

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.

like image 51
kvark Avatar answered Nov 11 '22 01:11

kvark


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;  
like image 1
Edvin Avatar answered Nov 11 '22 02:11

Edvin