openGL API glUniformMatrix4fv详解

openGL API glUniformMatrix4fv详解

官网

官网说明

Name
glUniform — Specify the value of a uniform variable for the current program object

C Specification
void glUniform1f( GLint location,
GLfloat v0);

void glUniform2f( GLint location,
GLfloat v0,
GLfloat v1);

void glUniform3f( GLint location,
GLfloat v0,
GLfloat v1,
GLfloat v2);

void glUniform4f( GLint location,
GLfloat v0,
GLfloat v1,
GLfloat v2,
GLfloat v3);

void glUniform1i( GLint location,
GLint v0);

void glUniform2i( GLint location,
GLint v0,
GLint v1);

void glUniform3i( GLint location,
GLint v0,
GLint v1,
GLint v2);

void glUniform4i( GLint location,
GLint v0,
GLint v1,
GLint v2,
GLint v3);

void glUniform1ui( GLint location,
GLuint v0);

void glUniform2ui( GLint location,
GLuint v0,
GLuint v1);

void glUniform3ui( GLint location,
GLuint v0,
GLuint v1,
GLuint v2);

void glUniform4ui( GLint location,
GLuint v0,
GLuint v1,
GLuint v2,
GLuint v3);

void glUniform1fv( GLint location,
GLsizei count,
const GLfloat *value);

void glUniform2fv( GLint location,
GLsizei count,
const GLfloat *value);

void glUniform3fv( GLint location,
GLsizei count,
const GLfloat *value);

void glUniform4fv( GLint location,
GLsizei count,
const GLfloat *value);

void glUniform1iv( GLint location,
GLsizei count,
const GLint *value);

void glUniform2iv( GLint location,
GLsizei count,
const GLint *value);

void glUniform3iv( GLint location,
GLsizei count,
const GLint *value);

void glUniform4iv( GLint location,
GLsizei count,
const GLint *value);

void glUniform1uiv( GLint location,
GLsizei count,
const GLuint *value);

void glUniform2uiv( GLint location,
GLsizei count,
const GLuint *value);

void glUniform3uiv( GLint location,
GLsizei count,
const GLuint *value);

void glUniform4uiv( GLint location,
GLsizei count,
const GLuint *value);

void glUniformMatrix2fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix3fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix4fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix2x3fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix3x2fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix2x4fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix4x2fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix3x4fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

void glUniformMatrix4x3fv( GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value);

Parameters
location
Specifies the location of the uniform variable to be modified.

count
For the vector (glUniform*v) commands, specifies the number of elements that are to be modified. This should be 1 if the targeted uniform variable is not an array, and 1 or more if it is an array.

For the matrix (glUniformMatrix*) commands, specifies the number of matrices that are to be modified. This should be 1 if the targeted uniform variable is not an array of matrices, and 1 or more if it is an array of matrices.

transpose
For the matrix commands, specifies whether to transpose the matrix as the values are loaded into the uniform variable.

v0, v1, v2, v3
For the scalar commands, specifies the new values to be used for the specified uniform variable.

value
For the vector and matrix commands, specifies a pointer to an array of count values that will be used to update the specified uniform variable.

Description
glUniform modifies the value of a uniform variable or a uniform variable array. The location of the uniform variable to be modified is specified by location, which should be a value returned by glGetUniformLocation. glUniform operates on the program object that was made part of current state by calling glUseProgram.

The commands glUniform{1|2|3|4}{f|i|ui} are used to change the value of the uniform variable specified by location using the values passed as arguments. The number specified in the command should match the number of components in the data type of the specified uniform variable (e.g., 1 for float, int, unsigned int, bool; 2 for vec2, ivec2, uvec2, bvec2, etc.). The suffix f indicates that floating-point values are being passed; the suffix i indicates that integer values are being passed; the suffix ui indicates that unsigned integer values are being passed, and this type should also match the data type of the specified uniform variable. The i variants of this function should be used to provide values for uniform variables defined as int, ivec2, ivec3, ivec4, or arrays of these. The ui variants of this function should be used to provide values for uniform variables defined as unsigned int, uvec2, uvec3, uvec4, or arrays of these. The f variants should be used to provide values for uniform variables of type float, vec2, vec3, vec4, or arrays of these. Either the i, ui or f variants may be used to provide values for uniform variables of type bool, bvec2, bvec3, bvec4, or arrays of these. The uniform variable will be set to false if the input value is 0 or 0.0f, and it will be set to true otherwise.

All active uniform variables defined in a program object are initialized to 0 when the program object is linked successfully. They retain the values assigned to them by a call to glUniform until the next successful link operation occurs on the program object, when they are once again initialized to 0.

The commands glUniform{1|2|3|4}{f|i|ui}v can be used to modify a single uniform variable or a uniform variable array. These commands pass a count and a pointer to the values to be loaded into a uniform variable or a uniform variable array. A count of 1 should be used if modifying the value of a single uniform variable, and a count of 1 or greater can be used to modify an entire array or part of an array. When loading n elements starting at an arbitrary position m in a uniform variable array, elements m + n - 1 in the array will be replaced with the new values. If m + n - 1 is larger than the size of the uniform variable array, values for all array elements beyond the end of the array will be ignored. The number specified in the name of the command indicates the number of components for each element in value, and it should match the number of components in the data type of the specified uniform variable (e.g., 1 for float, int, bool; 2 for vec2, ivec2, bvec2, etc.). The data type specified in the name of the command must match the data type for the specified uniform variable as described previously for glUniform{1|2|3|4}{f|i|ui}.

For uniform variable arrays, each element of the array is considered to be of the type indicated in the name of the command (e.g., glUniform3f or glUniform3fv can be used to load a uniform variable array of type vec3). The number of elements of the uniform variable array to be modified is specified by count

The commands glUniformMatrix{2|3|4|2x3|3x2|2x4|4x2|3x4|4x3}fv are used to modify a matrix or an array of matrices. The numbers in the command name are interpreted as the dimensionality of the matrix. The number 2 indicates a 2 × 2 matrix (i.e., 4 values), the number 3 indicates a 3 × 3 matrix (i.e., 9 values), and the number 4 indicates a 4 × 4 matrix (i.e., 16 values). Non-square matrix dimensionality is explicit, with the first number representing the number of columns and the second number representing the number of rows. For example, 2x4 indicates a 2 × 4 matrix with 2 columns and 4 rows (i.e., 8 values). If transpose is GL_FALSE, each matrix is assumed to be supplied in column major order. If transpose is GL_TRUE, each matrix is assumed to be supplied in row major order. The count argument indicates the number of matrices to be passed. A count of 1 should be used if modifying the value of a single matrix, and a count greater than 1 can be used to modify an array of matrices.

Notes
glUniform1i and glUniform1iv are the only two functions that may be used to load uniform variables defined as sampler types. Loading samplers with any other function will result in a GL_INVALID_OPERATION error.

If count is greater than 1 and the indicated uniform variable is not an array, a GL_INVALID_OPERATION error is generated and the specified uniform variable will remain unchanged.

Other than the preceding exceptions, if the type and size of the uniform variable as defined in the shader do not match the type and size specified in the name of the command used to load its value, a GL_INVALID_OPERATION error will be generated and the specified uniform variable will remain unchanged.

If location is a value other than -1 and it does not represent a valid uniform variable location in the current program object, an error will be generated, and no changes will be made to the uniform variable storage of the current program object. If location is equal to -1, the data passed in will be silently ignored and the specified uniform variable will not be changed.

Errors
GL_INVALID_OPERATION is generated if there is no current program object.

GL_INVALID_OPERATION is generated if the size of the uniform variable declared in the shader does not match the size indicated by the glUniform command.

GL_INVALID_OPERATION is generated if one of the signed or unsigned integer variants of this function is used to load a uniform variable of type float, vec2, vec3, vec4, or an array of these, or if one of the floating-point variants of this function is used to load a uniform variable of type int, ivec2, ivec3, ivec4, unsigned int, uvec2, uvec3, uvec4, or an array of these.

GL_INVALID_OPERATION is generated if one of the signed integer variants of this function is used to load a uniform variable of type unsigned int, uvec2, uvec3, uvec4, or an array of these.

GL_INVALID_OPERATION is generated if one of the unsigned integer variants of this function is used to load a uniform variable of type int, ivec2, ivec3, ivec4, or an array of these.

GL_INVALID_OPERATION is generated if location is an invalid uniform location for the current program object and location is not equal to -1.

GL_INVALID_VALUE is generated if count is less than 0.

GL_INVALID_OPERATION is generated if count is greater than 1 and the indicated uniform variable is not an array variable.

GL_INVALID_OPERATION is generated if a sampler is loaded using a command other than glUniform1i and glUniform1iv.

Associated Gets
glGet with the argument GL_CURRENT_PROGRAM

glGetActiveUniform with the handle of a program object and the index of an active uniform variable

glGetUniform with the handle of a program object and the location of a uniform variable

glGetUniformLocation with the handle of a program object and the name of a uniform variable

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
glUniform1f ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform1fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform1i ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform1iv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform1ui - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform1uiv - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform2f ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform2fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform2i ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform2iv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform2ui - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform2uiv - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform3f ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform3fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform3i ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform3iv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform3ui - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform3uiv - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform4f ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform4fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform4i ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform4iv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform4ui - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniform4uiv - - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix2fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix2x3fv - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix2x4fv - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix3fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix3x2fv - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix3x4fv - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix4fv ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix4x2fv - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glUniformMatrix4x3fv - ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
See Also
glLinkProgram, glUseProgram

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/.

翻译

为当前程序对象指定Uniform变量的值。(译者注:注意,由于OpenGL/OpenGL ES由C语言编写,但是C语言不支持函数的重载,所以会有很多名字相同后缀不同的函数版本存在。其中函数名中包含数字(1、2、3、4)表示接受这个数字个用于更改uniform变量的值,i表示32位整形,f表示32位浮点型,ub表示8位无符号byte,ui表示32位无符号整形,v表示接受相应的指针类型。 )

1.函数原型

函数原型:
void glUniform1f(GLint location, GLfloat v0);

void glUniform2f(GLint location, GLfloat v0, GLfloat v1);

void glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2);

void glUniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);

void glUniform1i(GLint location, GLint v0);

void glUniform2i(GLint location, GLint v0, GLint v1);

void glUniform3i(GLint location, GLint v0, GLint v1, GLint v2);

void glUniform4i(GLint location, GLint v0, GLint v1, GLint v2, GLint v3);

2.参数列表:

location

指明要更改的uniform变量的位置

count

指明要更改的元素个数。如果目标uniform变量不是一个数组,那么这个值应该设为1;如果是数组,则应该设置为>=1。
如果是matrix矩阵:指明要更改的矩阵个数

transpose

指明是否要转置矩阵,并将它作为uniform变量的值。必须为GL_FALSE。

value

指明一个指向count个元素的指针,用来更新指定的uniform变量。

3.描述:

glUniform更改一个uniform变量或数组的值。要更改的uniform变量的位置由location指定,location的值应该由glGetUniformLocation函数返回。通过调用glUseProgram,glUniform操作的程序对象将成为当前状态的一部分。

glUniform{1|2|3|4}{f|i}使用传进来的实参,修改通过location指定的uniform变量。

所有在程序对象中定义的活动uniform变量,在程序对象链接成功后都会被初始化为0.直到下一次程序对象链接成功再一次被初始化为0前,它们将保留通过调用glUniform赋给它们的值。

glUniform{1|2|3|4}{f|i}v可以用来更改单个uniform变量的值,或者一个uniform变量数组。

glUniformMatrix{2|3|4}fv用来更改一个矩阵或一个矩阵数组。

4.描述:

GL_INVALID_OPERATION 没有当前程序对象;着色器中的uniform变量的尺寸和glUniform中指明的尺寸不一致;glUniform的整形变体加载float类型的uniform变量,或者float类型的变体加载整形的uniform变量;location不是当前程序对象的有效uniform位置,并且location的值不等于-1;count的值大于1,但是指明的uniform变量不是一个数组;如果采样器不是调用glUniform1i 和glUniform1iv。

GL_INVALID_VALUE count小于0;transpose不是GL_FALSE。

代码

#include "glew/glew.h"
#include "glfw/glfw3.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"   // glm::translate, glm::rotate, glm::scale, glm::perspective
#include "glm/gtc/type_ptr.hpp"           // glm::value_ptr
#include "Utils.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

static const int Screen_Width = 800;
static const int Screen_Height = 600;
static const int NumberVAOs = 1;
static const int NumberVBOs = 2;
int width = 0;
int height = 0;


Utils util = Utils();
float cameraX = 0.f, cameraY = 0.f, cameraZ = 0.f;
float cubeLocX = 0.f, cubeLocY = 0.f, cubeLocZ = 0.f;
GLuint renderingProgram = 0;
GLuint vao[NumberVAOs] = { 0 };
GLuint vbo[NumberVBOs] = { 0 };

// variable allocation for display
GLuint mvLoc = 0, projLoc = 0;
float aspect = 0.f;
glm::mat4 pMat(1), vMat(1), mMat(1), mvMat(1);

void setupVertices(void)
{
	float vertexPositions[108] = {
		-1.0f,  1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
		1.0f, -1.0f, -1.0f, 1.0f,  1.0f, -1.0f, -1.0f,  1.0f, -1.0f,
		1.0f, -1.0f, -1.0f, 1.0f, -1.0f,  1.0f, 1.0f,  1.0f, -1.0f,
		1.0f, -1.0f,  1.0f, 1.0f,  1.0f,  1.0f, 1.0f,  1.0f, -1.0f,
		1.0f, -1.0f,  1.0f, -1.0f, -1.0f,  1.0f, 1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f, -1.0f,  1.0f,  1.0f, 1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f, -1.0f, -1.0f, -1.0f, -1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f, -1.0f, -1.0f,  1.0f, -1.0f, -1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f,  1.0f, -1.0f,  1.0f,  1.0f, -1.0f, -1.0f,
		1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,  1.0f,
		-1.0f,  1.0f, -1.0f, 1.0f,  1.0f, -1.0f, 1.0f,  1.0f,  1.0f,
		1.0f,  1.0f,  1.0f, -1.0f,  1.0f,  1.0f, -1.0f,  1.0f, -1.0f
	};

	//生成顶点数组对象
	glGenVertexArrays(NumberVAOs, vao);
	glBindVertexArray(vao[0]);

	//生成顶点缓存对象,并返回缓存对象的标识符id
	glGenBuffers(NumberVBOs, vbo);
	//绑定/激活缓存对象,target告诉VBO该缓存对象将保存顶点数组数据还是索引数组数据:GL_ARRAY_BUFFER或GL_ELEMENT_ARRAY
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
	//glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
	//将顶点数据拷贝到缓存对象中
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions),  vertexPositions, GL_STATIC_DRAW);    //有问题
	//glDrawArrays(GL_TRIANGLES, 0, 108);
}

void init(GLFWwindow* window)
{
	renderingProgram = Utils::createShaderProgram("vertShader.glsl", "fragShader.glsl");
	cameraX = 0.f;
	cameraY = 0.f;
	cameraZ = 8.f;

	cubeLocX = 0.f;
	cubeLocY = -2.f;
	cubeLocZ = 0.f;

	setupVertices();
}

void display(GLFWwindow* window, double currentTime)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.f, 1.f, 1.f, 1.f);

	glUseProgram(renderingProgram);
	/*Utils::printProgramLog(renderingProgram);*/   //printProgramLog()是私有函数
	//获取uniform变量在着色器程序中的位置序号,通过该序号可以设置一致变量的值,如果没有该变量则返回-1
	mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
	projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");

	//glfwGetFramebufferSize(window, const_cast<int*>(&Screen_Width), const_cast<int*>(&Screen_Height));
	width = 800;
	height = 600;
	glfwGetFramebufferSize(window, &width, &height);
	aspect = (float)(Screen_Width) / (float)(Screen_Height);
	pMat = glm::perspective(glm::radians(60.f), aspect, 0.1f, 1000.f);  //有问题
	//pMat = glm::perspective(1.0472f, aspect, 0.1f, 1000.0f);

	vMat = glm::translate(glm::mat4(1.f), glm::vec3(-cameraX, -cameraY, -cameraZ));
	mMat = glm::translate(glm::mat4(1.f), glm::vec3(cubeLocX, cubeLocY, cubeLocZ));

	mvMat = mMat * vMat;

	glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
	glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(pMat));

	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
	//glBufferData(GL_ARRAY_BUFFER, sizeof(vbo), vbo, GL_STATIC_DRAW);
	//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);   //有问题
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(0);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glDrawArrays(GL_TRIANGLES, 0, 36);

}


int main(int argc, char** argv)
{
	int glfwStatue = glfwInit();
	if (GLFW_FALSE == glfwStatue)
	{
		cout << "GLFW initialize failed, invoke glfwInit().....Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
		glfwTerminate();
		exit(EXIT_FAILURE);
	}
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
	glfwWindowHint(GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_PROFILE);

	GLFWwindow* window = glfwCreateWindow(Screen_Width, Screen_Height, "Draw color cube", nullptr, nullptr);
	if (!window)
	{
		cout << "GLFW create window failed, invoke glfwCreateWindow().......Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	glfwMakeContextCurrent(window);

	int glewSatue = glewInit();
	if (GLEW_OK != glewSatue)
	{
		cout << "GLEW initialize failed, invoke glewInit().....Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
		exit(EXIT_FAILURE);
	}
	//由于这些原因,应用程序通常希望将交换间隔设置为1。可以将其设置为更高的值,但通常不建议这样做,因为这样会导致输入延迟。
	glfwSwapInterval(1);

	init(window);

	while (!glfwWindowShouldClose(window))
	{
		display(window, glfwGetTime());
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);

	return 0;
}

运行效果

在这里插入图片描述

源码下载

工程下载

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: glUniformMatrix4fvOpenGL中的一个函数,用于设置uniform变量的值为一个4x4的矩阵。其中,fv表示传入的参数类型为float数组,4表示矩阵的列数为4。通常情况下,该函数用于将矩阵变换等操作的结果传入着色器程序中的uniform变量,以更新图形渲染的状态。 是的,您的理解是正确的。glUniformMatrix4fv函数是OpenGL中的一个函数,它用于将一个4x4的浮点数矩阵传递给着色器程序中的uniform变量。该函数的参数包括uniform变量的位置、矩阵的个数、是否需要转置矩阵以及矩阵数据的指针。 该函数通常用于将模型变换矩阵、视图变换矩阵和投影变换矩阵等矩阵传递给着色器程序中的uniform变量,以更新图形渲染的状态。通过更新这些矩阵,可以实现物体在场景中的移动、旋转和缩放等操作,以及相机的位置和方向的变换等操作。 需要注意的是,由于OpenGL中使用的是列主序矩阵,因此在使用glUniformMatrix4fv函数传递矩阵数据时,需要将矩阵数据进行转置。此外,由于矩阵是一种比较复杂的数据结构,因此在使用该函数时需要注意矩阵的正确性和精度问题。补充一点,glUniformMatrix4fv函数也可以用于传递其他类型的矩阵数据,比如3x3的矩阵或者2x2的矩阵,只需要将函数参数中的4改为相应的列数即可。另外,在使用glUniformMatrix4fv函数传递矩阵数据时,需要注意参数transpose的取值,如果transpose参数为GL_TRUE,则矩阵会在传递到着色器之前被转置,否则矩阵不会被转置。通常情况下,由于OpenGL使用列主序矩阵,我们需要将transpose参数设置为GL_TRUE,以确保矩阵的正确性。 ### 回答2: gIUniformMatrix4fvOpenGL中的一个函数,用于将一个4×4的矩阵数据传递给着色器程序中的uniform变量。uniform变量是在顶点着色器和片元着色器之间进行数据传递的一种方式。 gIUniformMatrix4fv函数的作用是向着色器程序中的uniform变量传递一个4×4的矩阵数据。这个函数接受四个参数,分别是uniform变量的位置、矩阵的个数、是否进行转置操作以及要传递的矩阵数据。 在使用gIUniformMatrix4fv函数传递矩阵数据时,需要先获取uniform变量的位置。然后再通过gIUniformMatrix4fv函数将矩阵数据传递给uniform变量。 传递矩阵数据时可以选择进行转置操作,将矩阵进行行优先或列优先的转置。传递矩阵数据的方式有多种,可以使用浮点数组、矩阵类等。 gIUniformMatrix4fv函数的使用通常需要在渲染循环中进行,在每次渲染时将最新的矩阵数据传递给uniform变量,以实现对图形的变换、投影等操作。 总结来说,gIUniformMatrix4fv函数是用于将4×4矩阵数据传递给着色器程序中的uniform变量的一个OpenGL函数。它在图形渲染中扮演着传递数据的重要角色,可以实现对图形的变换、投影等操作。 ### 回答3: glUniformMatrix4fvOpenGL中的一个函数,用于在着色器程序中设置一个4x4的浮点数矩阵的uniform变量的值。 uniform变量是一种在着色器程序中声明的全局变量,它的值对于每个顶点或片元来说是常量不变的。通过使用glUniformMatrix4fv,我们可以将一个4x4的浮点数矩阵的值传递给着色器程序中对应的uniform变量。 glUniformMatrix4fv函数有四个参数:location、count、transpose和value。 - location是一个指示uniform变量在着色器程序中位置的句柄,可以通过glGetUniformLocation函数获取。这个句柄表示着色器程序中的一个uniform变量。 - count表示要设置的矩阵数量,通常为1。 - transpose指示是否需要对矩阵进行转置。通常情况下,我们使用非转置的矩阵。 - value是一个指向要传递给uniform变量的4x4浮点数矩阵数据的指针。 通过调用glUniformMatrix4fv函数,我们可以将通过value参数传递的矩阵数据设置到指定的uniform变量中,使得着色器程序中对应的uniform变量的值与我们传递的矩阵数据一致。 总之,glUniformMatrix4fv函数是OpenGL中用于设置着色器程序中4x4浮点数矩阵uniform变量的值的函数。通过它,我们可以将一个矩阵的值传递给着色器程序中对应的uniform变量,以实现矩阵变换等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值