#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void
display (GLFWwindow *window, double currTime)
{
glClearColor (0.0, 1.0, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
}
void
init (GLFWwindow *window)
{
}
int
main (int argc, char *argv[])
{
if (!glfwInit()) {
return -1;
}
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 6);
GLFWwindow *window = glfwCreateWindow (600, 600, "Chapter2 - program1", nullptr, nullptr);
glfwMakeContextCurrent (window);
int errorCode = glewInit();
if (errorCode != GLEW_OK) {
cerr << "Error: " << glewGetErrorString (errorCode) << endl;
return -1;
}
glfwSwapInterval (1);
init (window);
while (!glfwWindowShouldClose (window)) {
display (window, glfwGetTime());
glfwSwapBuffers (window);
glfwPollEvents();
}
glfwDestroyWindow (window);
glfwTerminate();
return 0;
}
代码解读:
- 初始化GLFW库;
- 实例化
GLFWwindow
; - 初始化GLEW库;
- 调用一次init()函数;
- 重复调用
display()
函数
其中glClearColor()
是用指定的颜色来清除背景色。