glGetActiveUniform — Returns information about an active uniform variable for the specified program object
void glGetActiveUniform( | GLuint program, |
GLuint index, | |
GLsizei bufSize, | |
GLsizei *length, | |
GLint *size, | |
GLenum *type, | |
GLchar *name) ; |
program
Specifies the program object to be queried.
index
Specifies the index of the uniform variable to be queried.
bufSize
Specifies the maximum number of characters OpenGL is allowed to write in the character buffer indicated by name
.
length
Returns the number of characters actually written by OpenGL in the string indicated by name
(excluding the null terminator) if a value other than NULL
is passed.
size
Returns the size of the uniform variable.
type
Returns the data type of the uniform variable.
name
Returns a null terminated string containing the name of the uniform variable.
glGetActiveUniform
returns information about an active uniform variable in the program object specified by program
. The number of active uniform variables can be obtained by calling glGetProgramiv with the value GL_ACTIVE_UNIFORMS
. A value of zero for index
selects the first active uniform variable. Permissible values for index
range from zero to the number of active uniform variables minus one.
Shaders may use either built-in uniform variables, user-defined uniform variables, or both. Built-in uniform variables have a prefix of "gl_" and reference existing OpenGL state or values derived from such state (e.g., gl_DepthRange
, see the OpenGL Shading Language specification for a complete list.) User-defined uniform variables have arbitrary names and obtain their values from the application through calls to glUniform. A uniform variable (either built-in or user-defined) is considered active if it is determined during the link operation that it may be accessed during program execution. Therefore, program
should have previously been the target of a call to glLinkProgram, but it is not necessary for it to have been linked successfully.
The size of the character buffer required to store the longest uniform variable name in program
can be obtained by calling glGetProgramiv with the value GL_ACTIVE_UNIFORM_MAX_LENGTH
. This value should be used to allocate a buffer of sufficient size to store the returned uniform variable name. The size of this character buffer is passed in bufSize
, and a pointer to this character buffer is passed in name.
glGetActiveUniform
returns the name of the uniform variable indicated by index
, storing it in the character buffer specified by name
. The string returned will be null terminated. The actual number of characters written into this buffer is returned in length
, and this count does not include the null termination character. If the length of the returned string is not required, a value of NULL
can be passed in the length
argument.
The type
argument will return a pointer to the uniform variable's data type. The symbolic constants returned for uniform types are shown in the table below.
Returned Symbolic Contant | Shader Uniform Type |
---|---|
GL_FLOAT | float |
GL_FLOAT_VEC2 | vec2 |
GL_FLOAT_VEC3 | vec3 |
GL_FLOAT_VEC4 | vec4 |
GL_INT | int |
GL_INT_VEC2 | ivec2 |
GL_INT_VEC3 | ivec3 |
GL_INT_VEC4 | ivec4 |
GL_UNSIGNED_INT | unsigned int |
GL_UNSIGNED_INT_VEC2 | uvec2 |
GL_UNSIGNED_INT_VEC3 | uvec3 |
GL_UNSIGNED_INT_VEC4 | uvec4 |
GL_BOOL | bool |
GL_BOOL_VEC2 | bvec2 |
GL_BOOL_VEC3 | bvec3 |
GL_BOOL_VEC4 | bvec4 |
GL_FLOAT_MAT2 | mat2 |
GL_FLOAT_MAT3 | mat3 |
GL_FLOAT_MAT4 | mat4 |
GL_FLOAT_MAT2x3 | mat2x3 |
GL_FLOAT_MAT2x4 | mat2x4 |
GL_FLOAT_MAT3x2 | mat3x2 |
GL_FLOAT_MAT3x4 | mat3x4 |
GL_FLOAT_MAT4x2 | mat4x2 |
GL_FLOAT_MAT4x3 | mat4x3 |
GL_SAMPLER_2D | sampler2D |
GL_SAMPLER_3D | sampler3D |
GL_SAMPLER_CUBE | samplerCube |
GL_SAMPLER_2D_SHADOW | sampler2DShadow |
GL_SAMPLER_2D_ARRAY | sampler2DArray |
GL_SAMPLER_2D_ARRAY_SHADOW | sampler2DArrayShadow |
GL_SAMPLER_2D_MULTISAMPLE | sampler2DMS |
GL_SAMPLER_2D_MULTISAMPLE_ARRAY | sampler2DMSArray |
GL_SAMPLER_CUBE_SHADOW | samplerCubeShadow |
GL_SAMPLER_CUBE_MAP_ARRAY | samplerCubeArray |
GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW | samplerCubeArrayShadow |
GL_SAMPLER_BUFFER | samplerBuffer |
GL_INT_SAMPLER_2D | isampler2D |
GL_INT_SAMPLER_3D | isampler3D |
GL_INT_SAMPLER_CUBE | isamplerCube |
GL_INT_SAMPLER_2D_ARRAY | isampler2DArray |
GL_INT_SAMPLER_2D_MULTISAMPLE | isampler2DMS |
GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY | isampler2DMSArray |
GL_INT_SAMPLER_CUBE_MAP_ARRAY | isamplerCubeArray |
GL_INT_SAMPLER_BUFFER | isamplerBuffer |
GL_UNSIGNED_INT_SAMPLER_2D | usampler2D |
GL_UNSIGNED_INT_SAMPLER_3D | usampler3D |
GL_UNSIGNED_INT_SAMPLER_CUBE | usamplerCube |
GL_UNSIGNED_INT_SAMPLER_2D_ARRAY | usampler2DArray |
GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE | usampler2DMS |
GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY | usampler2DMSArray |
GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY | usamplerCubeArray |
GL_UNSIGNED_INT_SAMPLER_BUFFER | usamplerBuffer |
GL_IMAGE_2D | image2D |
GL_IMAGE_3D | image3D |
GL_IMAGE_CUBE | imageCube |
GL_IMAGE_2D_ARRAY | image2DArray |
GL_IMAGE_CUBE_MAP_ARRAY | imageCubeArray |
GL_IMAGE_BUFFER | imageBuffer |
GL_INT_IMAGE_2D | iimage2D |
GL_INT_IMAGE_3D | iimage3D |
GL_INT_IMAGE_CUBE | iimageCube |
GL_INT_IMAGE_2D_ARRAY | iimage2DArray |
GL_INT_IMAGE_CUBE_MAP_ARRAY | iimageCubeArray |
GL_INT_IMAGE_BUFFER | iimageBuffer |
GL_UNSIGNED_INT_IMAGE_2D | uimage2D |
GL_UNSIGNED_INT_IMAGE_3D | uimage3D |
GL_UNSIGNED_INT_IMAGE_CUBE | uimageCube |
GL_UNSIGNED_INT_IMAGE_2D_ARRAY | uimage2DArray |
GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY | uimageCubeArray |
GL_UNSIGNED_INT_IMAGE_BUFFER | uimageBuffer |
GL_UNSIGNED_INT_ATOMIC_COUNTER | atomic_uint |
If one or more elements of an array are active, the name of the array is returned in name
, the type is returned in type
, and the size
parameter returns the highest array element index used, plus one, as determined by the compiler and/or linker. Only one active uniform variable will be reported for a uniform array. If the active uniform is an array, the uniform name returned in name
will always be the name of the uniform array appended with "[0]".
Uniform variables that are declared as structures or arrays of structures will not be returned directly by this function. Instead, each of these uniform variables will be reduced to its fundamental components containing the "." and "[]" operators such that each of the names is valid as an argument to glGetUniformLocation. Each of these reduced uniform variables is counted as one active uniform variable and is assigned an index. A valid name cannot be a structure, an array of structures, or a subcomponent of a vector or matrix.
The size of the uniform variable will be returned in size
. Uniform variables other than arrays will have a size of 1. Structures and arrays of structures will be reduced as described earlier, such that each of the names returned will be a data type in the earlier list. If this reduction results in an array, the size returned will be as described for uniform arrays; otherwise, the size returned will be 1.
The list of active uniform variables may include both built-in uniform variables (which begin with the prefix "gl_") as well as user-defined uniform variable names.
This function will return as much information as it can about the specified active uniform variable. If no information is available, length
will be 0, and name
will be an empty string. This situation could occur if this function is called after a link operation that failed. If an error occurs, the return values length
, size
, type
, and name
will be unmodified.
GL_INVALID_VALUE
is generated if program
is not a value generated by OpenGL.
GL_INVALID_OPERATION
is generated if program
is not a program object.
GL_INVALID_VALUE
is generated if index
is greater than or equal to the number of active uniform variables in program
.
GL_INVALID_VALUE
is generated if bufSize
is less than 0.
glGet with argument GL_MAX_VERTEX_UNIFORM_COMPONENTS
, GL_MAX_FRAGMENT_UNIFORM_COMPONENTS
, GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS
, or GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS
.
glGetProgramiv with argument GL_ACTIVE_UNIFORMS
or GL_ACTIVE_UNIFORM_MAX_LENGTH
.
glIsProgram
OpenGL ES API Version | ||||
---|---|---|---|---|
Function Name | 2.0 | 3.0 | 3.1 | 3.2 |
glGetActiveUniform | ✔ | ✔ | ✔ | ✔ |
glGetUniform, glGetUniformLocation, glLinkProgram, glUniform, glUseProgram
Copyright © 2003-2005 3Dlabs Inc. Ltd. Copyright © 2010-2015 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. /.
glGetActiveUniform - 返回有关活动统一变量的信息
void glGetActiveUniform(GLuint program,
GLuint index,
GLsizei bufSize,
GLsizei *length,
GLint *size,
GLenum *type,
GLchar *name);
program
指定要查询的程序对象。
index
指定要查询的统一变量的索引。
bufSize
指定允许OpenGL在由name指示的字符缓冲区中写入的最大字符数。
length
如果传递NULL以外的值,则返回由name指示的字符串中的OpenGL实际写入的字符数(不包括空终止符)。
size
返回统一变量的大小。
type
返回统一变量的数据类型。
name
返回包含统一变量名称的以null结尾的字符串。
glGetActiveUniform返回有关程序指定的程序对象中的活动统一变量的信息。可以通过使用值GL_ACTIVE_UNIFORMS调用glGetProgramiv来获得活动的统一变量的数量。索引的值为0的是选择第一个活动的统一变量。索引的允许值范围从0到活动统一变量的数量减1。
着色器可以使用内置的统一变量,用户定义的统一变量或两者。内置的统一变量具有前缀“gl_”并且引用现有的OpenGL状态或从这种状态导出的值(例如,gl_DepthRange)。用户定义的统一变量具有任意名称,并通过调用glUniform从应用程序获取它们的值。如果在链接操作期间确定可以在程序执行期间访问它,则统一变量(内置或用户定义)被认为是活动的。因此,程序之前应该是调用glLinkProgram的目标,但它没有必要成功链接。
在程序中存储最长的统一变量名所需的字符缓冲区的大小可以通过调用值为GL_ACTIVE_UNIFORM_MAX_LENGTH的glGetProgramiv来获得。此值应用于分配足够大小的缓冲区来存储返回的统一变量名称。该字符缓冲区的大小在bufSize中传递,并且在该名称中传递指向该字符缓冲区的指针。
glGetActiveUniform返回由index指示的统一变量的名称,将其存储在name指定的字符缓冲区中。返回的字符串将以null结尾。写入此缓冲区的实际字符数以长度形式返回,并且此计数不包括空终止字符。如果不需要返回字符串的长度,则可以在length参数中传递NULL值。
type参数将返回指向统一变量数据类型的指针。可以返回符号常数GL_FLOAT,GL_FLOAT_VEC2,GL_FLOAT_VEC3,GL_FLOAT_VEC4,GL_INT_GLEC_VEC2,GL_INT_VEC3,GL_INT_VEC4,GL_BOOL,GL_BOOL_VEC2,GL_BOOL_VEC3,GL_BOOL_VEC4,GL_FLOAT_MAT2,GL_FLOAT_MAT3,GL_FLOAT_MAT4,GL_SAMPLER_2D或GL_SAMPLER_CUBE。
如果数组的一个或多个元素处于活动状态,则在name中返回数组的名称,类型以type返回,并且size参数返回使用的最高数组元素索引加上1,具体由编译器确定和/或链接器。对于统一阵列,仅报告一个活动的统一变量。
声明为结构或结构数组的统一变量不会由此函数直接返回。相反,这些统一变量中的每一个都将被简化为包含“.”和“[]”运算符的基本组成部分,使得每个名称作为glGetUniformLocation的参数有效。
统一变量的大小将以size返回。除数组之外的统一变量将具有1的维度大小。结构和结构数组将如前所述减少,使得返回的每个名称将是先前列表中的数据类型。
活动统一变量列表可以包括内置的统一变量(以前缀“gl_”开头)以及用户定义的统一变量名称。
此函数将返回尽可能多的有关指定的活动统一变量的信息。如果没有可用信息,则length为0,name为空字符串(如果在失败的链接操作后调用此函数,则可能发生这种情况)。如果发生错误,则返回值length,size,type和name将不会被修改。
GL_INVALID_VALUE:program不是OpenGL生成的值。
GL_INVALID_OPERATION:program不是程序对象。
GL_INVALID_VALUE:index>=程序中活动统一变量的数量。
GL_INVALID_VALUE:bufSize<0
glGet 参数GL_MAX_VERTEX_UNIFORM_VECTORS或GL_MAX_FRAGMENT_UNIFORM_VECTORS
glGetProgramiv 参数GL_ACTIVE_UNIFORMS或GL_ACTIVE_UNIFORM_MAX_LENGTH
glIsProgram
glGetActiveAttrib,glGetUniform,glGetUniformLocation,glLinkProgram,glUniform,glUseProgram
.0/l
Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见/.
代码
运行效果
// The MIT License (MIT)
//
// Copyright (c) 2013 Dan Ginsburg, Budirijanto Purnomo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.//
// Book: OpenGL(R) ES 3.0 Programming Guide, 2nd Edition
// Authors: Dan Ginsburg, Budirijanto Purnomo, Dave Shreiner, Aaftab Munshi
// ISBN-10: 0-321-93388-5
// ISBN-13: 978-0-321-93388-1
// Publisher: Addison-Wesley Professional
// URLs:
//
//
// Hello_Triangle.c
//
// This is a simple example that draws a single triangle with
// a minimal vertex/fragment shader. The purpose of this
// example is to demonstrate the basic concepts of
// OpenGL ES 3.0 rendering.
//#include "esUtil.h"
//#include "Common/Include/esUtil.h"
#include "esUtil.h"typedef struct
{// Handle to a program objectGLuint programObject;} UserData;///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader ( GLenum type, const char *shaderSrc )
{GLuint shader;GLint compiled;// Create the shader objectshader = glCreateShader ( type );if ( shader == 0 ){return 0;}// Load the shader sourceglShaderSource ( shader, 1, &shaderSrc, NULL );// Compile the shaderglCompileShader ( shader );// Check the compile statusglGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );if ( !compiled ){GLint infoLen = 0;glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );if ( infoLen > 1 ){char *infoLog = malloc ( sizeof ( char ) * infoLen );glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );esLogMessage ( "Error compiling shader:n%sn", infoLog );free ( infoLog );}glDeleteShader ( shader );return 0;}return shader;}///
// Initialize the shader and program object
//"precision mediump float;表示着色器中浮点变量的默认精度
//"out vec4 fragColor; 表示这个变量的值将被输出到颜色缓冲区
int Init ( ESContext *esContext )
{UserData *userData = esContext->userData;char vShaderStr[] ="#version 300 es n""layout(location = 0) in vec4 vPosition; n""uniform mat4 projection; n""uniform mat4 view; n""uniform mat4 model; n""void main() n""{ n"" gl_Position = projection * view * model * vPosition; n"" gl_Position = vPosition; n""} n";char fShaderStr[] ="#version 300 es n""precision mediump float; n""out vec4 fragColor; n""void main() n""{ n"" fragColor = vec4 ( 0.0, 1.0, 0.0, 1.0 ); n""} n";GLuint vertexShader;GLuint fragmentShader;GLuint programObject;GLint linked;GLint uniformActiveNum;GLint uniformMaxNum;GLsizei bufSize = 0;GLsizei uniformLeng;// Load the vertex/fragment shadersvertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );// Create the program objectprogramObject = glCreateProgram ( );if ( programObject == 0 ){return 0;}glAttachShader ( programObject, vertexShader );glAttachShader ( programObject, fragmentShader );// Link the programglLinkProgram ( programObject );// Check the link statusglGetProgramiv ( programObject, GL_LINK_STATUS, &linked );//获取uniform的数量//uniformNum:为什么不是3个uniform,注意光声明uniform变量,不使用这个uniform不做算,只有使用了才算数//注意:是要激活的,没激活不算数glGetProgramiv(programObject, GL_ACTIVE_UNIFORMS, &uniformActiveNum);glGetProgramiv(programObject, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNum);//0xb只有11个?为什么?GLint errorLogLen = 0;GLchar errorLog[512];glGetActiveUniform(programObject, 1, 512, NULL, 1, GL_FLOAT_MAT4, "projection");glGetProgramInfoLog(programObject, errorLogLen, NULL, errorLog);esLogMessage("glGetActiveUniform:%sn", errorLog);if ( !linked ){GLint infoLen = 0;glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );if ( infoLen > 1 ){char *infoLog = malloc ( sizeof ( char ) * infoLen );glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );esLogMessage ( "Error linking program:n%sn", infoLog );free ( infoLog );}glDeleteProgram ( programObject );return FALSE;}// Store the program objectuserData->programObject = programObject;glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f );return TRUE;
}///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{UserData *userData = esContext->userData;GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f};// Set the viewportglViewport ( 0, 0, esContext->width, esContext->height );// Clear the color bufferglClear ( GL_COLOR_BUFFER_BIT );// Use the program objectglUseProgram ( userData->programObject );// Load the vertex dataglVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices ); //vVertices 对应顶点着色器位置0 layout(location = 0)glEnableVertexAttribArray ( 0 );glDrawArrays ( GL_TRIANGLES, 0, 3 );
}void Shutdown ( ESContext *esContext )
{UserData *userData = esContext->userData;glDeleteProgram ( userData->programObject );
}int esMain ( ESContext *esContext )
{esContext->userData = malloc ( sizeof ( UserData ) );esCreateWindow ( esContext, "Hello Triangle", 320, 240, ES_WINDOW_RGB );if ( !Init ( esContext ) ){return GL_FALSE;}esRegisterShutdownFunc ( esContext, Shutdown );esRegisterDrawFunc ( esContext, Draw );return GL_TRUE;
}
源码下载地址
本文发布于:2024-02-04 05:11:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170699823852357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |