54

我想获取着色器程序对象使用的所有制服和属性的列表。 glGetAttribLocation()&glGetUniformLocation()可用于将字符串映射到某个位置,但我真正想要的是字符串列表,而无需解析 glsl 代码。

注意:在 OpenGL 2.0glGetObjectParameteriv()中被替换为glGetProgramiv(). 枚举是GL_ACTIVE_UNIFORMS& GL_ACTIVE_ATTRIBUTES

4

4 回答 4

86

两个示例之间共享的变量:

GLint i;
GLint count;

GLint size; // size of the variable
GLenum type; // type of the variable (float, vec3 or mat4, etc)

const GLsizei bufSize = 16; // maximum name length
GLchar name[bufSize]; // variable name in GLSL
GLsizei length; // name length

属性

glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count);
printf("Active Attributes: %d\n", count);

for (i = 0; i < count; i++)
{
    glGetActiveAttrib(program, (GLuint)i, bufSize, &length, &size, &type, name);

    printf("Attribute #%d Type: %u Name: %s\n", i, type, name);
}

制服

glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count);
printf("Active Uniforms: %d\n", count);

for (i = 0; i < count; i++)
{
    glGetActiveUniform(program, (GLuint)i, bufSize, &length, &size, &type, name);

    printf("Uniform #%d Type: %u Name: %s\n", i, type, name);
}

OpenGL 文档/变量类型

可以在文档中找到表示变量类型的各种宏。如GL_FLOAT, GL_FLOAT_VEC3,GL_FLOAT_MAT4等。

于 2009-01-14T12:44:55.487 回答
56
于 2012-09-26T22:49:38.947 回答
20

对于那些发现这个问题并希望在 WebGL 中执行此操作的任何人,这里是 WebGL 等价物:

var program = gl.createProgram();
// ...attach shaders, link...

var na = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
console.log(na, 'attributes');
for (var i = 0; i < na; ++i) {
  var a = gl.getActiveAttrib(program, i);
  console.log(i, a.size, a.type, a.name);
}
var nu = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
console.log(nu, 'uniforms');
for (var i = 0; i < nu; ++i) {
  var u = gl.getActiveUniform(program, i);
  console.log(i, u.size, u.type, u.name);
}
于 2012-09-26T21:46:52.763 回答
0

Here is the corresponding code in python for getting the uniforms:

from OpenGL import GL
...
num_active_uniforms = GL.glGetProgramiv(program, GL.GL_ACTIVE_UNIFORMS)
for u in range(num_active_uniforms):
    name, size, type_ = GL.glGetActiveUniform(program, u)
    location = GL.glGetUniformLocation(program, name)

Apparently the 'new way' mentioned by Nicol Bolas does not work in python.

于 2018-05-17T02:49:39.273 回答