首页 > 其他分享 >GLES3.0中文API-glGetActiveUniform详解

GLES3.0中文API-glGetActiveUniform详解

时间:2022-12-12 21:32:31浏览次数:86  
标签:glGetActiveUniform GLES3.0 userData INT uniform instance API ARRAY GL


前言

         glGetActiveUniform()函数看了官方的解释,也看了别人的一些帖子,但是基本上都没有实际的代码,没有实操总觉得隔了一层,雾里看花理解不了。下面亲自写段代码验证下这个函数。

官方文档


Name

glGetActiveUniform — Returns information about an active uniform variable for the specified program object

C Specification

​void glGetActiveUniform(​

GLuint program,

GLuint index,

GLsizei bufSize,

GLsizei *length,

GLint *size,

GLenum *type,

GLchar *name​​)​​;

Parameters

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.

Description

​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

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

The ​type

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

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

Errors

​GL_INVALID_VALUE​​​ is generated if ​program

​GL_INVALID_OPERATION​​​ is generated if ​program

​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

Associated Gets

​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​

API Version Support

OpenGL ES API Version

Function Name

2.0

3.0

3.1

3.2

glGetActiveUniform





See Also

​glGetUniform​​​, ​​glGetUniformLocation​​​, ​​glLinkProgram​​​, ​​glUniform​​​, ​​glUseProgram​

Copyright

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. ​​http://opencontent.org/openpub/​​.

中文翻译

    这里感谢

​​flycatdeng​​

​​flycatdeng的翻译​​

名称

glGetActiveUniform - 返回有关活动统一变量的信息

C规范

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_FLOATGL_FLOAT_VEC2GL_FLOAT_VEC3GL_FLOAT_VEC4GL_INT_GLEC_VEC2GL_INT_VEC3GL_INT_VEC4GL_BOOLGL_BOOL_VEC2GL_BOOL_VEC3GL_BOOL_VEC4GL_FLOAT_MAT2GL_FLOAT_MAT3GL_FLOAT_MAT4GL_SAMPLER_2DGL_SAMPLER_CUBE

如果数组的一个或多个元素处于活动状态,则在name中返回数组的名称,类型以type返回,并且size参数返回使用的最高数组元素索引加上1,具体由编译器确定和/或链接器。对于统一阵列,仅报告一个活动的统一变量。

声明为结构或结构数组的统一变量不会由此函数直接返回。相反,这些统一变量中的每一个都将被简化为包含“.”和“[]”运算符的基本组成部分,使得每个名称作为​​glGetUniformLocation​​的参数有效。

统一变量的大小将以size返回。除数组之外的统一变量将具有1的维度大小。结构和结构数组将如前所述减少,使得返回的每个名称将是先前列表中的数据类型。

活动统一变量列表可以包括内置的统一变量(以前缀“gl_”开头)以及用户定义的统一变量名称。

此函数将返回尽可能多的有关指定的活动统一变量的信息。如果没有可用信息,则length为0,name为空字符串(如果在失败的链接操作后调用此函数,则可能发生这种情况)。如果发生错误,则返回值lengthsizetypename将不会被修改。

错误

GL_INVALID_VALUEprogram不是OpenGL生成的值。

GL_INVALID_OPERATIONprogram不是程序对象。

GL_INVALID_VALUEindex>=程序中活动统一变量的数量。

GL_INVALID_VALUEbufSize<0

相关Gets

​​glGet​​ 参数GL_MAX_VERTEX_UNIFORM_VECTORSGL_MAX_FRAGMENT_UNIFORM_VECTORS

​​glGetProgramiv​​ 参数GL_ACTIVE_UNIFORMSGL_ACTIVE_UNIFORM_MAX_LENGTH

​​glIsProgram​​

另见

​​glGetActiveAttrib​​,​​glGetUniform​​,​​glGetUniformLocation​​,​​glLinkProgram​​,​​glUniform​​,​​glUseProgram​​

版权

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

​​javascript:void(0)​​

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

代码

//
// Demonstrates drawing multiple objects in a single draw call with
// geometry instancing
//

#include "esUtil.h"
#include <stdlib.h>
#include <math.h>

//随机种子
#ifdef _WIN32
#define srandom srand
#define random rand
#endif

//函数功能:找到uniform统一变量的位置
//glGetUniformLocation()

//函数功能:加载uniform统一变量值
//glUniform1f()
//glUniformMatrix4fv()





#define NUM_INSTANCES 100
#define POSITION_LOC 0
#define COLOR_LOC 1
#define MVP_LOC 2

typedef struct
{
// Handle to a program object
GLuint programObject;

// VBOs
GLuint positionVBO;
GLuint colorVBO;
GLuint mvpVBO;
GLuint indicesIBO;

// Number of indices
int numIndices;

// Rotation angle
GLfloat angle[NUM_INSTANCES];
}UserData;


void test(ESContext* esContext)
{
UserData* userData = esContext->userData;
GLint maxUniformLen;
GLint numUniforms;
char* uniformName;
GLint index;
glGetProgramiv(userData->programObject, GL_ACTIVE_UNIFORMS, &numUniforms);
glGetProgramiv(userData->programObject, GL_ACTIVE_UNIFORM_MAX_LENGTH,
&maxUniformLen);
uniformName = malloc(sizeof(char) * maxUniformLen);
for (index = 0; index < numUniforms; index++)
{
GLint size;
GLenum type;
GLint location;
// Get the uniform info
glGetActiveUniform(userData->programObject, index, maxUniformLen, NULL,
&size, &type, uniformName);
// Get the uniform location
location = glGetUniformLocation(userData->programObject, uniformName);
switch (type)
{
case GL_FLOAT:
//
break;
case GL_FLOAT_VEC2:
//
break;
case GL_FLOAT_VEC3:
//
break;
case GL_FLOAT_VEC4:
//
break;
case GL_INT:
//
break;
// ... Check for all the types ...
default:
// Unknown type

break;
}
}
}


// 打印日志信息
void PrintLogMessage(ESContext* esContext, const char* msg)
{
UserData* userData = esContext->userData;
//GLint infoLen = GL_INFO_LOG_LENGTH;
GLint infoLen = 512;
char* infoLog = malloc(sizeof(char) * 512);
glGetProgramInfoLog(userData->programObject, infoLen, &infoLen, infoLog);
GLint headLen = strlen("Invoke ");
strcpy_s(infoLog, headLen + 3, "Invoke "); //这里长度必须+1,要覆盖'\0',否则越界,vs2019边界检查更严格
/********* (1)src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
/*********(2)strcat返回值有什么作用? 链式传递:strcat(a, strcat(b, c));
/********* 最重要的是,strcat函数不检查这些。*********/
//strcat(infoLog, "Invoke ");
//strcat_s(infoLog, msg, strlen(msg)+1);
int strMsgSize = strlen(msg);
int strInfoSize = strlen(infoLog);
//注意!!!strcat_s()第二个参数的大小是 src+des+1总和大小,+1是'\0'的大小
int retStatue = strcat_s(infoLog, strMsgSize + strInfoSize + 1, msg);
esLogMessage(" failed:Error message:%s\n", infoLog);

free(infoLog);
}

///
// Initialize the shader and program object
//
int Init(ESContext* esContext)
{
GLfloat* positions = NULL;
GLuint* indices = NULL;

UserData* userData = esContext->userData;
const char vShaderStr[] =
"#version 300 es \n"
"layout(location = 0) in vec4 a_position; \n"
"layout(location = 1) in vec4 a_color; \n"
"layout(location = 2) in mat4 a_mvpMatrix; \n"
"out vec4 v_color; \n"
"void main() \n"
"{ \n"
" v_color = a_color; \n"
" gl_Position = a_mvpMatrix * a_position; \n"
"} \n";

const char fShaderStr[] =
"#version 300 es \n"
"precision mediump float; \n"
"in vec4 v_color; \n"
"layout(location = 0) out vec4 outColor; \n"
"void main() \n"
"{ \n"
" outColor = v_color; \n"
"} \n";

// Load the shaders and get a linked program object
userData->programObject = esLoadProgram(vShaderStr, fShaderStr);

// Generate the vertex data
//这里暂时没有用到法线和纹理坐标这2个参数:所以第一个normal是NULL,第二个texCoords是NULL
userData->numIndices = esGenCube(0.1f, &positions, NULL, NULL, &indices);

// Index buffer object
glGenBuffers(1, &userData->indicesIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, userData->indicesIBO);

//glGetActiveUniform(userData->programObject, 0, NULL, NULL, )
/****** 功能:更新/修改uniform统一变量缓冲区中的统一变量数据
* **************/
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * userData->numIndices,
indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//这里为什么要释放局部指针变量,函数调用完成会释放2次?
free(indices);

// Position VBO for cube model
glGenBuffers(1, &userData->positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, userData->positionVBO);
//立方体是6个面,每个面4个顶点,所以是6*4=24,为什么还要*3 ????
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat) * 3, positions, GL_STATIC_DRAW);
//这里没有恢复到默认绑定状态
//glBindBuffer(GL_ARRAY_BUFFER, 0);
free(positions);

// Random color for each instance
{
GLubyte colors[NUM_INSTANCES][4];
int instance;

srandom(0);

for (instance = 0; instance < NUM_INSTANCES; instance++)
{
colors[instance][0] = random() % 255;
//colors[NUM_INSTANCES][0] = random() % 255;

colors[instance][1] = random() % 255;
//colors[NUM_INSTANCES][1] = random() % 255;

colors[instance][2] = random() % 255;
//colors[NUM_INSTANCES][2] = random() % 255;

//最后一个alpha值
colors[instance][3] = 0;
//colors[instance][3] = 0;
//colors[NUM_INSTANCES][3] = random() % 255;
}

glGenBuffers(1, &userData->colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, userData->colorVBO);
//glBufferData(GL_ARRAY_BUFFER, sizeof(GLubyte) * NUM_INSTANCES * 4, colors, GL_STATIC_DRAW);
GLint gluByteSize = sizeof(GLubyte);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLubyte) * NUM_INSTANCES * 4, colors, GL_STATIC_DRAW);
}

// Allocate storage to store MVP per instance
{
int instance;

// Random angle for each instance, compute the MVP later
for (instance = 0; instance < NUM_INSTANCES; instance++)
{
userData->angle[instance] = (float)(random() % 32768) / 32767.0f * 360;
}

glGenBuffers(1, &userData->mvpVBO);
glBindBuffer(GL_ARRAY_BUFFER, userData->mvpVBO);
//这里参数为什么是NULL:为实例化存储数据到存储区的指针,如果为NULL,说明不需要拷贝数据
glBufferData(GL_ARRAY_BUFFER, sizeof(ESMatrix) * NUM_INSTANCES, NULL, GL_DYNAMIC_DRAW);
}

//恢复到默认绑定状态
glBindBuffer(GL_ARRAY_BUFFER, 0);

glClearColor(1.0f, 0.0f, 1.0f, 1.0f);

return GL_TRUE;
}

///
// Update MVP matrix based on time
//
void Updata(ESContext* esContext, float deltaTime)
{
UserData* userData = esContext->userData;
ESMatrix* matrixBuf;
ESMatrix perspective;
float aspect;
int instance = 0;
int numRows = 0;
int numColumns = 0;

// Compute the window aspect ratio
aspect = (GLfloat)esContext->width / (float)esContext->height;

// Generate a perspective matrix with a 60 degree FOV
//esMatrixLoadIdentity(matrixBuf);
esMatrixLoadIdentity(&perspective);
esPerspective(&perspective, 45.0f, aspect, 1.0f, 20.0f);

glBindBuffer(GL_ARRAY_BUFFER, userData->mvpVBO);
/****** 功能:将缓冲区对象数据存储映射到应用程序的地址空间,这个指针供应用程序使用,
/****** 以读取和更新缓冲区对象的内容。他可以代替**************/
/****** glBufferData或者glBufferSubData函数,以减少程序内存占用 ********/
/****** 返回值:返回请求的缓冲区数据存储范围的指针 ********/
/****** GL_MAP_READ_BIT:表示返回的指针可用于读取缓冲区对象数据。
* 如果指针用于查询排除此标志的映射,则不会产生GL错误,
* 但结果是未定义的,并且可能发生系统错误(可能包括程序终止)。
/****** GL_MAP_WRITE_BIT:表示返回的指针可用于修改缓冲区对象数据。
/* 如果指针用于修改一个排除此标志的映射,则不会产生GL错误,
/* 但结果是未定义的,并且可能发生系统错误(可能包括程序终止)。
* ********/
matrixBuf = glMapBufferRange(GL_ARRAY_BUFFER, 0,
sizeof(ESMatrix) * NUM_INSTANCES,
GL_MAP_WRITE_BIT);

// Compute a per-instance MVP that translates and rotates each instance differently
numRows = (int)sqrtf(NUM_INSTANCES);
numColumns = numRows;

for (instance = 0; instance < NUM_INSTANCES; instance++)
{
ESMatrix modelView;
float translateX = ((float)(instance % numRows) / (float)(numRows)) * 2.0f - 1.0f;
float translateY = ((float)(instance / numColumns) / (float)(numColumns)) * 2.0f - 1.0f;

// Generate a model view matrix to rotate/translate the cube
esMatrixLoadIdentity(&modelView);

// Per-instance translation
esTranslate(&modelView, translateX, translateY, -2.0f);

// Compute a rotation angle based on time to rotate the cube
userData->angle[instance] += (deltaTime * 40.0f);

if (userData->angle[instance] >= 360.0f)
{
userData->angle[instance] -= 360.0f;
}

// Rotate the cube
esRotate(&modelView, userData->angle[instance], 1.0f, 0.0f, 1.0f);

// Compute the final MVP by multiplying the
// modeler's and perspective matrices together
//matrixBuf[instance]:乘积,modelView:被乘数,perspective:乘数
esMatrixMultiply(&matrixBuf[instance], &modelView, &perspective);
}

// 指示更新已经完成和释放映射的指针
glUnmapBuffer(GL_ARRAY_BUFFER);
}

///
// Draw a triangle using the shader pair created in Init()
//
void Draw(ESContext* esContext)
{
UserData* userData = esContext->userData;
GLfloat* indices;

// Set the viewport
glViewport(0, 0, esContext->width, esContext->height);

// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Use the program object
glUseProgram(userData->programObject);

// Load the vertex position
glBindBuffer(GL_ARRAY_BUFFER, userData->positionVBO);
glVertexAttribPointer(POSITION_LOC, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat),
(GLvoid*)NULL);
glEnableVertexAttribArray(POSITION_LOC);

// Load the instance color buffer
glBindBuffer(GL_ARRAY_BUFFER, userData->colorVBO);
//为什么不用glBufferData(),因为在Init()初始化时调用过了
//glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * NUM_INSTANCES, indices, GL_DYNAMIC_DRAW);
//把最后一个参数变成(GLvoid*)(3 * sizeof(GLint)颜色更好看
glVertexAttribPointer(COLOR_LOC, 4, GL_UNSIGNED_BYTE, GL_TRUE,
4 * sizeof(GLubyte),
(GLvoid*)(3 * sizeof(GLint)));
//最后一个参数为什么是NULL
/*glVertexAttribPointer(COLOR_LOC, 4, GL_UNSIGNED_BYTE, GL_FLOAT, GL_FALSE,
4 * sizeof(GLubyte), (GLvoid*)(NULL));*/
glEnableVertexAttribArray(COLOR_LOC);

/****** 功能:访问每个实例的颜色数据
/****** 参数:COLOR_LOC:指定顶点颜色属性索引
/****** 参数:1:指定COLOR_LOC索引位置的通用颜色属性更新之间传递的是实例数量
* 如果divisor=0:默认情况下,如果没有指定,或者顶点属性的divisor等于0,
* 对每个顶点将读取一次顶点属性;
* 如果divisor=1:则每个图元实例颜色属性读取一次顶点属性
******/
glVertexAttribDivisor(COLOR_LOC, 1);

// Load the instance MVP buffer
glBindBuffer(GL_ARRAY_BUFFER, userData->mvpVBO);

// Load each matrix row of the MVP. Each row gets an increasing attribute location.
glVertexAttribPointer(MVP_LOC + 0, 4, GL_FLOAT, GL_FALSE,
sizeof(ESMatrix), (GLvoid*)NULL);
glVertexAttribPointer(MVP_LOC + 1, 4, GL_FLOAT, GL_FALSE,
sizeof(ESMatrix), (GLvoid*)(4 * sizeof(GLfloat)));
glVertexAttribPointer(MVP_LOC + 2, 4, GL_FLOAT, GL_FALSE,
sizeof(ESMatrix), (GLvoid*)(8 * sizeof(float)));
glVertexAttribPointer(MVP_LOC + 3, 4, GL_FLOAT, GL_FALSE,
sizeof(ESMatrix), (GLvoid*)(12 * sizeof(GLfloat)));
glEnableVertexAttribArray(MVP_LOC + 0);
glEnableVertexAttribArray(MVP_LOC + 1);
glEnableVertexAttribArray(MVP_LOC + 2);
glEnableVertexAttribArray(MVP_LOC + 3);

/****** 功能:访问每个实例的颜色数据
/****** 参数:MVP_LOC:指定模型视图投影矩阵(modelViewProjectionMatrix)位置属性索引
/****** 参数:1:指定MVP_LOC索引位置的通用颜色属性更新之间传递的是实例数量
* 如果divisor=0:默认情况下,如果没有指定,或者顶点属性的divisor等于0,
* 对每个顶点将读取一次顶点属性;
* 如果divisor=1:则每个图元实例颜色属性读取一次顶点属性
* One MVP per instance
******/
glVertexAttribDivisor(MVP_LOC + 0, 1);
glVertexAttribDivisor(MVP_LOC + 1, 1);
glVertexAttribDivisor(MVP_LOC + 2, 1);
glVertexAttribDivisor(MVP_LOC + 3, 1);

// Bind the index buffer 索引缓冲区对象
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, userData->indicesIBO);

// Draw the cubes
glDrawElementsInstanced(GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT,
NULL, NUM_INSTANCES);
}

///
// Cleanup
//
void Shutdown(ESContext* esContext)
{
UserData* userData = esContext->userData;

glDeleteBuffers(1, &userData->positionVBO);
glDeleteBuffers(1, &userData->colorVBO);
glDeleteBuffers(1, &userData->mvpVBO);
glDeleteBuffers(1, &userData->indicesIBO);

// Delete program object
glDeleteProgram(userData->programObject);
}

int esMain(ESContext* esContext)
{
esContext->userData = malloc(sizeof(UserData));

GLboolean createWindowStatue = esCreateWindow(esContext, "openGLESExample_7_Instancing", 800, 600, ES_WINDOW_RGB);
if (GL_FALSE == createWindowStatue)
{
PrintLogMessage(esContext, "esCreateWindow");
return GL_FALSE;
}

if (!Init(esContext))
{
PrintLogMessage(esContext, "Init");
return GL_FALSE;
}

esRegisterShutdownFunc(esContext, Shutdown);
esRegisterUpdateFunc(esContext, Updata);
esRegisterDrawFunc(esContext, Draw);

return GL_TRUE;
}

程序运行效果

GLES3.0中文API-glGetActiveUniform详解_sed

 稍微修改202行的代码,让透明度也随机

GLES3.0中文API-glGetActiveUniform详解_Data_02

 运行效果如下

GLES3.0中文API-glGetActiveUniform详解_sed_03

​​工程源码下载​​

标签:glGetActiveUniform,GLES3.0,userData,INT,uniform,instance,API,ARRAY,GL
From: https://blog.51cto.com/u_15707179/5931885

相关文章

  • 瞧瞧别人家的API接口,那叫一个优雅
    前言在实际工作中,我们需要经常跟第三方平台打交道,可能会对接第三方平台API接口,或者提供API接口给第三方平台调用。那么问题来了,如果设计一个优雅的API接口,能够满足:安全性......
  • API网关的五大主要功能
    ​微服务架构将原本一个庞大的业务系统被拆分成许多粒度很小的系统进行独立部署和维护。这必然会导致跨系统交互复杂度增加、不同服务之间依赖关系变得更加复杂,这给微服务的......
  • API网关的五大主要功能
     微服务架构将原本一个庞大的业务系统被拆分成许多粒度很小的系统进行独立部署和维护。这必然会导致跨系统交互复杂度增加、不同服务之间依赖关系变得更加复杂,这给微服务......
  • openGL API-glGetUniformLocation
    NameglGetUniformLocation—ReturnsthelocationofauniformvariableCSpecification​​GLint glGetUniformLocation(​​GLuint program,constGLchar*name​​)......
  • java中的时间api
    packageDataTimeTest;/*java.util.data|---java.sql.Data类1.两个构造器的使用>构造器一:Date():创建一个对应当前时间的Date对象>构造器二:创建指定毫......
  • 功能强大的国产API管理神器 Eolink,亲测好用
    前言大家好,我是海拥,今天给大家讲讲最近很火的Eolink,一款功能强大且非常实用的国产​​API管理工具​​。在我们日常的前端、后端开发测试过程中经常会用到API,特别是在大型......
  • Flink TableAPI&SQL(二)
    2.6表和流的转换一般用于测试时候的数据输出,针对的是流数据。由于Table没有提供print()方法,所有要将Table数据类型转换成DataStream数据类型或者DataSet。2.6.1将表(......
  • HarmonyOS实现登录页面(二)相关api的创建(IDEA)
    创建步骤如何在IDEA里面创建相关程序创建成功后,上来一顿删除文件直到如下图新建3个包commoncontrollermodel在cotroller下面建类ApiController在model......
  • k8s API
    Job-CronJobJob:临时离线任务CronJob:定时离线任务Yaml文件描述Job:apiVersion:batch/v1kind:Jobmetadata:name:...labels:k:vk:v......
  • HTML5 中fullscreen 中的几个API和fullscreen欺骗
    HTML5中的fullscreen,目前可以在除IE和opera外的浏览器中使用,有的时候用来做全屏API,游戏呀,等都很有用。先看常见的API1element.requestFullScree......