天天看點

openGL API glUniformMatrix4fv詳解

openGL API glUniformMatrix4fv詳解

文章目錄

  • ​​openGL API glUniformMatrix4fv詳解​​
  • ​​官網​​
  • ​​翻譯​​
  • ​​1.函數原型​​
  • ​​2.參數清單:​​
  • ​​3.描述:​​
  • ​​4.描述:​​
  • ​​代碼​​
  • ​​運作效果​​
  • ​​源碼下載下傳​​

官網

​​官網說明​​

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;
}      

運作效果

源碼下載下傳