现代OpenGL项目推荐使用GLFW + GLAD来配置使用OpenGL。配置环境:
- VS2017
- glfw-3.4
- glad-opengl4.6-core
GLFW配置
下载源码 -> CMake生成VS2017项目 -> 编译生成glfw3dll.lib
、glfw3.dll
将源码中的include/GLFW
目录添加进附加包含目录
将生成的glfw3dll.lib
添加进VS工程属性Linker(链接器)选项卡里的Input(输入)选项卡中
#include <GLFW/glfw3.h>
创建窗口
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpengl", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create CLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(mpWindow, framebuffer_size_callback);
glfwSetCursorPosCallback(mpWindow, mouse_callback);
glfwSetScrollCallback(mpWindow, scroll_callback);
//GLAD
/*if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Fail to initialize GLAD";
return;
}*/
//渲染循环
while (!glfwWindowShouldClose(window)) {
// per-frame time logic
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
processInput(mpWindow);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //状态设置
glClear(GL_COLOR_BUFFER_BIT); //状态使用
// glfw:交换缓冲区和轮询IO事件(按键按下或释放,鼠标移动等)
glfwSwapBuffers(window);
glfwPollEvents();
}
//回收前面分配的GLFW资源
glfwTerminate();
-
初始化GLFW库
int glfwInit(void);
This function initializes the GLFW library. Before most GLFW functions can be used, GLFW must be initialized, and before an application terminates GLFW should be terminated in order to free any resources allocated during or after initialization.
@reentrancy This function must not be called from a callback.
@thread_safety This function must only be called from the main thread. -
终止GLFW库
销毁窗口,回收已分配资源