glad、glfw配置,VS 2019
原文参考 >> https://learnopengl.com/Getting-started/Creating-a-window
中文版参考 >> https://learnopengl-cn.github.io/01%20Getting%20started/02%20Creating%20a%20window/
GLFW
下载glfw
下载地址
一般解压至项目文件夹
链接
解决方案资源管理器 右键 >> 属性 >> VC++目录
- 包含目录 添加 include 文件夹路径
- 库目录 添加 lib 文件夹路径
链接器
附加依赖项:手动输入glfw3.lib;opengl32.lib
这时就可以在cpp文件中加入#include <GLFW/glfw3.h>
GLAD
glad提供在线服务 https://glad.dav1d.de/
- Language:C/C++
- API gl 选择版本大于等于之前 glfw 版本的选项
- Profile 选择 Core
- 勾选 Generate a loader
生成,下载zip文件,获得两个包含头文件的文件夹和一个glad.c
文件
包含头文件的文件夹放入 glfw 的 include 下,glad.c 也放入项目中
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream> #include <glad/glad.h> #include <GLFW/glfw3.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow* window);
int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
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); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize 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);
glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void processInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); }
|
运行成功获得一个如下图窗口,可通过Esc关闭