OpenGL是使用双缓冲的(front buffer, back buffer),front buffer用于展示,back buffer用于绘制,结束后将front buffer 与 back buffer的位置对调,此时的front buffer变成了back buffer,back buffer变成了front buffer。
glfwTerminate();函数结束window
glClearColor(R, G ,B, A);清屏后的颜色
glClear(GL_COLOR_BUFFER_BIT);清屏
glfwWindowShouldClose(window);函数可以检测window是否被关闭
glfwSwapBuffers(window);函数可以交换缓冲
GLFWwindow* window = glfwCreateWindow(窗口宽, 窗口长, 窗口名字, NULL, NULL);创建一个窗口
glfwInit();初始化glfw
glfwWindowInit(参数...);具体初始化什么样的glfw
glfwmakeContextCurrent(window);使用window这个window作为当前的窗口
glewExperimental = true;
if( glewInit() != GLEW_OK );若glewInit()初始化失败
glViewport(0, 0, 800, 600);设置可以绘制的窗口范围
glfwPollEvents(); glfwPollEvents函数检查是否触发了任何事件(如键盘输入或鼠标移动事件),更新窗口状态,并调用相应的函数(我们可以通过回调方法注册)。
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); 如果按了ESCAPE键的话,就关闭window
VAO:存放模型位置点数据,法线数据等信息
glGenVertexArray(1, &VAO); 获取所有模型所有VAO的信息并存在变量VAO中
glBindVertexArray(VAO); 绑定VAO
VBO:存放模型数据,并没有处理是位置点数据还是法线数据,只有没有分类的数据
#include
#include
void processInput(GLFWwindow* window) {
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//若需要在mac做操作才把这段代码启用
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if(glewInit() != GLEW_OK){
printf("Init GLEW failed.");
glfwTerminate();
return -1;
}
glViewport(0, 0, 800, 600);
unsigned int VAO;
glGenVertexArray(1, &VAO);
glBindVertexArray(VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffers(GL_ARRAY_BUFFER, &VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
processInput(window);
}
glfwTerminate();
return 0;
}