使用GLFW库+VS2019+GLAD源码
1.下载GLFW地址 https://www.glfw.org/download.html2.
2. 下载cmake编译工具 https://cmake.org/download/
3.使用cmake工具,生成vs2019项目
Configure设置成功后,点击Configure,直到生成成功,然后点击Generate,很快就会完成。
4. 用VS2019打开GLFW项目,编译生成lib
5.新建一个文件夹openGL,把GLFW项目中的include目录复制到openGL目录下。
6.在openGL目录下,新建一个lib文件夹,把GLFW项目编译好的lib(debug,release两个文件夹)复制到lib文件夹下
7.下载glad,https://glad.dav1d.de/
8.把glad解压后的include复制到openGL的目录下,与GLFW的include合并
9.创建windows工程(窗口工程)
10.把glad.c复制到项目里,新建main.cpp入口,代码如下
#include "GLFW/glfw3.h"
#include <glad/glad.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
int main()
{
glfwInit();//初始化glfw窗口
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//指定OpenGl主版本号
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//指定OpenGl子版本号
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//使用OpenGL的核心模式(可编程渲染管道的模式)
GLFWwindow* window = glfwCreateWindow(1280, 720, "LeanrOpenGL", NULL, NULL);//创建窗口
if (window == NULL) {
std::cout << "创建窗口失败" << std::endl;
glfwTerminate();//如果出错,终止进程
return -1;
}
glfwMakeContextCurrent(window);//将窗口上下文环境设置为当前主线程的上下文环境
//初始化glad
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
std::cout << "初始化glad失败" << std::endl;
return -1;
}
//注册窗口回调函数
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//渲染循环
while (!glfwWindowShouldClose(window)) {//检测是否关闭
processInput(window);//处理输入事件
// 渲染
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);//设置清屏颜色
glClear(GL_COLOR_BUFFER_BIT);//清屏
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);//将显示缓存和高屏缓存交换(显示高屏缓存中的内容)
glfwPollEvents();//检查是否有事件触发(例如鼠标输入和键盘输入事件)
}
glfwTerminate();//终止进程
return 0;
}
// process all input : query GLFW whether relevant keys are pressed / released this frameand react accordingly
// 函数处理输入事件
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)//检查Esc键有没有按下
glfwSetWindowShouldClose(window, true);//设置窗口关闭
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// 用于当窗口大小改变时,改变OpenGL视口的区域大小
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);//调整OpenGL的视口区域
}
11.配置项目
如果在编译时,出现glfw3.lib无法打开,在这里配置lib路径
12。编译项目成功
参考链接: