我想获取着色器程序对象使用的所有制服和属性的列表。 glGetAttribLocation()
&glGetUniformLocation()
可用于将字符串映射到某个位置,但我真正想要的是字符串列表,而无需解析 glsl 代码。
注意:在 OpenGL 2.0glGetObjectParameteriv()
中被替换为glGetProgramiv()
. 枚举是GL_ACTIVE_UNIFORMS
& GL_ACTIVE_ATTRIBUTES
。
两个示例之间共享的变量:
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);
}
可以在文档中找到表示变量类型的各种宏。如GL_FLOAT
, GL_FLOAT_VEC3
,GL_FLOAT_MAT4
等。
对于那些发现这个问题并希望在 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);
}
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.