openGL API-glGetUniformLocation

时间:2022-12-12 16:57:21


Name

glGetUniformLocation — Returns the location of a uniform variable

C Specification

​GLint glGetUniformLocation(​

GLuint program,

const GLchar *name​​)​​;

Parameters

program

Specifies the program object to be queried.

name

Points to a null terminated string containing the name of the uniform variable whose location is to be queried.

Description

​glGetUniformLocation ​​​returns an integer that represents the location of a specific uniform variable within a program object. ​name​​ must be a null terminated string that contains no white space. ​name​​ must be an active uniform variable name in ​program​​ that is not a structure, an array of structures, or a subcomponent of a vector or a matrix. This function returns -1 if ​name​​ does not correspond to an active uniform variable in ​program​​, if ​name​​ starts with the reserved prefix "gl_", or if ​name

Uniform variables that are structures or arrays of structures may be queried by calling ​​glGetUniformLocation​​​ for each field within the structure. The array element operator "[]" and the structure field operator "." may be used in ​name​​ in order to select elements within an array or fields within a structure. The result of using these operators is not allowed to be another structure, an array of structures, or a subcomponent of a vector or a matrix. Except if the last part of ​name

The actual locations assigned to uniform variables are not known until the program object is linked successfully. 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 ​​glUniform​​​ to set the value of the uniform variable or to ​​glGetUniform​​ in order to query the current value of the uniform variable. After a program object has been linked successfully, the index values for uniform variables remain fixed until the next link command occurs. Uniform variable locations and values can only be queried after a link if the link was successful.

Errors

​GL_INVALID_VALUE​​​ is generated if ​program

​GL_INVALID_OPERATION​​​ is generated if ​program

​GL_INVALID_OPERATION​​​ is generated if ​program

Associated Gets

​glGetActiveUniform​​​ with arguments ​program

​glGetProgram​​​ with arguments ​program​​ and ​​GL_ACTIVE_UNIFORMS​​​ or ​​GL_ACTIVE_UNIFORM_MAX_LENGTH​

​glGetUniform​​​ with arguments ​program

​glIsProgram​

Version Support

OpenGL Version

Function / Feature Name

2.0

2.1

3.0

3.1

3.2

3.3

4.0

4.1

4.2

4.3

4.4

4.5

​glGetUniformLocation​













See Also

​glLinkProgram​​​, ​​glUniform​

Copyright

Copyright © 2003-2005 3Dlabs Inc. Ltd. Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. ​​http://opencontent.org/openpub/​​.

# 翻译


名称

glGetUniformLocation- 返回统一变量的位置(获取一致变量在着色器程序中的位置序号,通过该序号可以设置一致变量的值,如果没有该变量则返回-1)

C规范

GLint glGetUniformLocation(GLuint program,const GLchar *name);

参数

program

指定要查询的程序对象。

name

要查询其位置的统一变量的名称。

描述

glGetUniformLocation返回一个整数,表示程序对象中特定统一变量的位置。 name必须是不包含空格的空终止字符串。 name必须是程序中的活动统一变量名,它不能是结构,也不能是结构数组或向量或矩阵的子组件。 如果name与程序中的活动统一变量不对应,或者name以保留前缀“gl_”开头,则此函数返回-1。

可以通过为结构中的每个字段调用glGetUniformLocation来查询作为结构或结构数组的统一变量。 数组元素运算符“[]”和结构字段运算符“.” 可以在name中使用,以便选择数组中的元素或结构中的字段。 使用这些运算符的结果不允许是另一个结构,结构数组或向量或矩阵的子组件。 除非name的最后一部分表示统一变量数组,否则可以使用数组的名称或使用“[0]”附加的名称来检索数组的第一个元素的位置。

在程序对象成功链接之前,分配给统一变量的实际位置是不知道的。发生链接后,命令glGetUniformLocation可用于获取统一变量的位置。 然后可以将此位置值传递给​​glUniform​​以设置统一变量的值或​​glGetUniform​​以查询统一变量的当前值。成功链接程序对象后,统一变量的索引值保持不变,直到发生下一个链接命令。 如果链接成功,则只能在链接后查询统一变量位置和值。

错误

GL_INVALID_VALUE program不是OpenGL生成的值。

GL_INVALID_OPERATION program不是程序对象。

GL_INVALID_OPERATION program没有成功链接。

举个栗子:

在openGL中,我们创建100个平移向量,它包含着10×10格子所有位置。除了生成 translations 数组
外,我们还需要把这些位移数据发送到顶点着色器的uniform数组:

shader.Use();
for(GLuint i = 0; i < 100; i++)
{
stringstream ss;
string index;
ss << i;
index = ss.str();
GLint location = glGetUniformLocation(shader.Program, ("offsets[" + index + "]").c
_str())
glUniform2f(location, translations[i].x, translations[i].y);
}

这段代码的意思是:有100个正方体,他们有各自的顶点位置保存在数组 offset[100],向shader中传递offset[100]这个数组,顶点着色器shader中的代码可能是:

#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec3 color;
out vec3 fColor;
uniform vec2 offsets[100];
void main()
{
vec2 offset = offsets[gl_InstanceID];
gl_Position = vec4(position + offset, 0.0f, 1.0f);
fColor = color;
}

相关Gets

​​glGetActiveUniform​​ 参数program和活动的统一变量索引。

​​glGetProgramiv​​ 参数program和​GL_ACTIVE_UNIFORMS 或 ​GL_ACTIVE_UNIFORM_MAX_LENGTH

​​glGetUniform​​ 参数program和统一变量的名称

​​glIsProgram​​

另见

​​glLinkProgram​​,​​glUniform​​

版权

https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glGetUniformLocation.xml

Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见http://oss.sgi.com/projects/FreeB/.

参考

​​flycatdeng​​