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

喜你入骨 提交于 2020-06-09 17:07:05

问题


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.


回答1:


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.




回答2:


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;  



回答3:


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.



来源:https://stackoverflow.com/questions/4783912/how-can-i-find-a-list-of-all-the-uniforms-in-opengl-es-2-0-vertex-shader-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!