一、核心基础库下载
1、GLFW库下载
2、GLAD库下载
下载链接如下:
https://glad.dav1d.de/Web generator for the Glad toolhttps://glad.dav1d.de/ 选择
二、基础库相关配置
1、GLFW库配置
原始资源文件下载完毕后,需要生成glfw3.lib库,我们采用的工具是CMakeTools来完成,源代码目录我们选择GLFW的源代码的根目录,然后我们新建一个build 文件夹作为输出目录,Configure选择对应Visual Studio版本,2019/2022均可。
这里作者用到的是visual studio 2022 ,把目录放到D盘,如D:/glfw-3.4/。生成完毕后,在build目录下找到生成好的GLFW头文件目录,如下图
将include文件夹整体拷贝到glfw-3.4目录下,
确认glfw3.lib正确生成并存在于Debug目录
2、glad库配置
将下载好的glad库目录,放到磁盘目录下。有两个子文件夹,include&src
三、VSCode与CMakeList配置
1、配置文件目录索引
在.vscode文件目录中打开c_cpp_properties.json,添加库文件路径,使其能被正确的#include<>引用。作者编译器用VS2022,如果是2019可自行修正C:\\Program Files\\Microsoft Visual Studio\\2022。
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/glfw-3.4/include",
"D:/glad/include/"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.34.31933\\bin\\Hostx64\\x64\\cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
2、配置CMakeList
首先要将GLAD库src文件夹中的glad.c拷贝至执行文件同级目录下
/**
* |--MainWindow
* |--glad.c
* |--mainWindows.cpp
* |--CMakeLists.txt
*
/
打开CMakeLists.txt,进行配置
cmake_minimum_required(VERSION 3.10)
project(MainWindowProject)
#设置目录名
set(target_name "MainWindow")
#设置GLFW路径
set(glfw3_DIR "D:/glfw-3.4/build/src/")
find_package(glfw3 REQUIRED CONFIG PATHS ${glfw3_DIR} NO_DEFAULT_PATH)
#设置执行路径
add_executable(MainWindow mainWindows.cpp glad.c)
#链接引用头文件
target_include_directories(MainWindow PRIVATE D:/glfw-3.4/include/ D:/glad/include/)
#链接glfw3.lib库目录
target_link_directories(MainWindow PRIVATE D:/glfw-3.4/build/src/Debug/)
#链接glfw
target_link_libraries(MainWindow glfw3)
3、配置执行文件
在.vscode中新建两个文件,launch.json与settings.json这两个文件主要应用于编译与运行的配置
3.1tasks.json配置
{
"version": "2.0.0",
"windows":
{
"options":
{
}
},
"tasks": [
{
"label": "CMake Configure",
"type": "shell",
"command":"cmake -S . -B build",
"group": "build"
},
{
"label": "CMake Build",
"type": "shell",
"command":"cmake --build build",
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn":["CMake Configure"]
}
]
}
3.2launch.json配置
{
"version": "2.0.0",
"configurations": [
{
"name":"Debug Chapter1",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/build/MainWindow/Debug/MainWindow",
"args": [],
"preLaunchTask": "CMake Build",
"environment": [],
"cwd": "${workspaceRoot}",
"console":"externalTerminal"
},
]
}
4、配置glfw3Config.cmake正确路径
在D:\glfw-3.4\build\src目录中找到,glfw3Config.cmake进行glfw3Targets.cmake路径修正,
如下所示
include(CMakeFindDependencyMacro)
find_dependency(Threads)
include("${CMAKE_CURRENT_LIST_DIR}/../CMakeFiles/Export/lib/cmake/glfw3/glfw3Targets.cmake")
四、渲染基础窗口代码运行验证
1、进入mainWindow.cpp,进行头文件的引用验证
2、这里引用一个比较经典的GLFW窗口代码作为验证
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
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(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
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 processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
3、运行顺序,点击,选择CMake Build
4、CMakeBuild完成后,在左侧工具栏找到执行程序键
5、成功运行会生成一个淡青色背景的窗口,OpenGL配置顺利搭建成功
标签:glad,CMake,VSCode,GLFW,build,glfw,include,cmake,轻量级 From: https://blog.csdn.net/NMixSpace/article/details/139435277