基于 LearnOpenGL 教程的 OpenGL 学习代码集合,使用 CMake 构建系统。
LearnOpenGL_VSCode/
├── 00_configuration/ # OpenGL 环境配置
├── 01_texture/ # 纹理基础
├── 02_transform/ # 变换矩阵
├── 03_camera/ # 相机系统
├── 04_lightcast/ # 光照投射
├── 05_basicLighting/ # 基础光照
├── 06_lightingMap/ # 光照贴图
└── .vscode/ # VS Code 配置
每个子项目都包含:
CMakeLists.txt- CMake 配置文件include/- 头文件(GLAD, GLFW, GLM, shader_s.h, camera.h 等)lib/- 静态库文件(libglfw3.a, libglad.a)src/- 源代码(main.cpp)shaders/- GLSL 着色器文件res/- 纹理等资源文件(部分项目)
-
CMake (3.15+)
cmake --version -
MinGW GCC (8.1.0+)
gcc --version- 确保 MinGW 的
bin目录已添加到系统 PATH
- 确保 MinGW 的
- GLFW 3 - 窗口管理
- GLAD - OpenGL 函数加载器
- GLM - 数学库
- stb_image - 图像加载
# 进入任意子项目目录
cd 06_lightingMap
# 创建并进入构建目录
mkdir build
cd build
# 配置 CMake(使用 MinGW)
cmake -G "MinGW Makefiles" ..
# 编译
cmake --build .
# 运行程序
cd ..\output
.\06_lightingMap.exe# 替换 <项目名> 为实际项目名(如 06_lightingMap)
cd <项目名>
if (Test-Path build) { Remove-Item -Recurse -Force build }
mkdir build; cd build
cmake -G "MinGW Makefiles" ..
cmake --build .
cd ..\output
.\<项目名>.exe# 进入任意子项目目录
cd 06_lightingMap
# 创建并进入构建目录
mkdir build && cd build
# 配置项目
cmake ..
# 编译
make
# 运行程序
cd ../output
./<项目名># 删除构建目录重新开始
Remove-Item -Recurse -Force build项目已配置 .vscode/c_cpp_properties.json:
- 编译器:
E:/mingw/bin/gcc.exe - C++ 标准:C++17
- 包含路径:
${workspaceFolder}/**/include
如果 IntelliSense 报错但编译成功,尝试:
- Ctrl+Shift+P →
C/C++: Restart IntelliSense for All Files - 重启 VS Code
本地头文件使用引号,系统头文件使用尖括号:
// 系统库 - 尖括号
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
// 本地头文件 - 引号
#include "shader_s.h"
#include "camera.h"
#include "stb_image.h"原因:CMake 默认使用 Visual Studio 编译器,但库是用 MinGW 编译的。
解决:配置时必须指定 MinGW:
cmake -G "MinGW Makefiles" ..检查:
- 确保
include/目录包含所有头文件 - 检查
.vscode/c_cpp_properties.json的includePath - 重启 IntelliSense
CMake 会自动复制必要的 DLL(如 glfw3.dll)到 output/ 目录。
CMake 会自动复制 shaders/ 和 res/ 到 output/ 目录。运行时确保在 output/ 目录下执行程序。
建议按以下顺序学习:
- 00_configuration - OpenGL 环境配置
- 01_texture - 纹理映射
- 02_transform - 变换矩阵
- 03_camera - 相机系统
- 04_lightcast - 光照投射
- 05_basicLighting - Phong 光照模型
- 06_lightingMap - 漫反射贴图与镜面光贴图
- LearnOpenGL CN - 中文教程
- LearnOpenGL - 英文原版
- OpenGL 官方文档
- GLFW 文档
- GLM 文档
本项目仅用于学习目的。相关库的许可协议请参考各自的官方文档。
Happy Coding! 🎨