一个从零实现、面向教学的 C++17 CPU 基于物理的 路径追踪渲染器,同时也是 Bilibili 公开课 的配套代码仓库。
项目从最基础的Hello World开始,逐步实现 线程池、光线求交、BVH、路径追踪、重要性采样、 微表面模型、光源采样、MIS、环境光照以及光谱路径追踪。
实现上尽量贴近算法本身,少用复杂框架和语言技巧,方便对照原理阅读代码。
- Binned SAH BVH
- Anisotropic GGX Microfacet
- Multiple Importance Sampling
- Environment Lighting
- Spectral Path Tracing
Anisotropic GGX · Smith Masking-Shadowing · Conductor · Dielectric
Light Sampling · BSDF Sampling · MIS
Environment Map · Importance Sampling · HDR
Spectral Rendering · Wavelength Sampling · Spectral MIS
| 讲次 | 内容 | 视频 | 课件 | 代码 |
|---|---|---|---|---|
| 基础框架 | ||||
| 01 | 课程介绍 | 视频 | PPT | Lecture01 |
| 02 | 线程池与胶片 | 视频 | PPT | Lecture02 |
| 03 | 自旋锁与并行 for 循环 | 视频 | — | Lecture03 |
| 04 | 球体与相交测试 | 视频 | PPT | Lecture04 |
| 05 | 模型渲染 | 视频 | PPT | Lecture05 |
| 06 | 平面与场景 | 视频 | PPT | Lecture06 |
| 07 | 材质与极简光追 | 视频 | PPT | Lecture07 |
| 08 | 一些代码重构 | 视频 | — | Lecture08 |
| 性能优化与路径追踪 | ||||
| 09 | 性能优化(上):并行优化 | 视频 | PPT | Lecture09 |
| 10 | 性能优化(中):高性能 BVH 加速结构 | 视频 | PPT | Lecture10 |
| 11 | 性能优化(下):场景管理 | 视频 | — | Lecture11 |
| 12 | 路径追踪与重要性采样 | 视频 | PPT | Lecture12 |
| 13 | 代码勘误与新材质类 | 视频 | — | Lecture13 |
| 14 | 电介质与导体 | 视频 | PPT | Lecture14 |
| 微表面、采样与光照 | ||||
| 15 | 往期勘误与代码重构 | 视频 | — | Lecture15 |
| 16 | 微表面理论 | 视频 | PPT | Lecture16 |
| 17 | 实时预览 | 视频 | — | Lecture17 |
| 18 | 向光源采样 | 视频 | PPT | Lecture18 |
| 19 | 多重重要性采样 | 视频 | PPT | Lecture19 |
| 20 | BVH 构建优化 | 视频 | — | Lecture20 |
| 21 | 环境光照 | 视频 | PPT | Lecture21 |
| 光谱渲染 | ||||
| 22 | 代码勘误和一些改进 | 视频 | — | Lecture22 |
| 23 | 光谱渲染(上):色彩科学 | 视频 | PPT | Lecture23 |
| 24-1 | 光谱渲染(中):基础框架 | 视频 | PPT | Lecture24 |
| 24-2 | 光谱渲染(中)Spectral MIS | 视频 | PPT | Lecture24 |
| 25 | 光谱渲染(下) | 制作中 | — | — |
git clone --recursive https://github.com/HeaoYe/CPUPathTracing.git- 新建models、hdris、spectrums文件夹
- 下载资源文件
- 将.obj模型文件放入models文件夹
- 将.exr贴图文件放入hdris文件夹
- 将.csv光谱文件放入spectrums文件夹
- 文件夹结构预览
CPUPathTracing
├── hdris
│ ├── HdrOutdoorSnowMountainsEveningClear001_HDR_4K.exr
│ ├── kloppenheim_07_puresky_4k.exr
│ └── qwantani_night_puresky_4k.exr
├── models
│ ├── buddha.obj
│ ├── dragon_871k.obj
│ ├── dragon_87k.obj
│ └── simple_dragon.obj
├── spectrums
│ ├── Johnson-copper.csv
│ ├── Metameric_A_reflectance.csv
│ ├── Metameric_B_reflectance.csv
│ └── Zelmon-glass.csv
├── resource/
├── source/
├── thirdparty/
├── .vscode/
├── CMakeLists.txt
├── .gitignore
├── .gitmodules
├── README.md
└── LICENSEcmake -B build
cmake --build build -j 8./build/source/CPUPathTracing程序启动后会进入实时预览界面,可调整相机位置、视角与预览模式。
| 操作 | 功能 |
|---|---|
| Enter | 开始渲染 |
| Esc | 退出预览,不进行渲染 |
| Caps Lock | 捕获 / 释放鼠标 |
| W A S D | 移动相机 |
| Space / Shift | 相机上升 / 下降 |
| 鼠标移动 | 调整相机视角 |
| 鼠标滚轮 | 调整相机 FOV |
| Tab | 切换预览模式 |
| + / - | 调整预览目标 FPS,并自动改变预览分辨率 |
Tab 可在以下预览模式之间切换:
- 正常渲染
- 法线可视化
- BVH 包围盒相交测试热力图
- 三角形相交测试热力图
-
Physically Based Rendering: From Theory to Implementation, 4th Edition Matt Pharr, Wenzel Jakob, Greg Humphreys, 2023.
-
Robust Monte Carlo Methods for Light Transport Simulation Eric Veach, Ph.D. dissertation, Stanford University, 1997.
-
On fast Construction of SAH-based Bounding Volume Hierarchies Ingo Wald, IEEE/Eurographics Symposium on Interactive Ray Tracing, 33–40, 2007.
-
Microfacet Models for Refraction through Rough Surfaces Bruce Walter, Stephen R. Marschner, Hongsong Li, Kenneth E. Torrance, Eurographics Symposium on Rendering (EGSR), 195–206, 2007.
-
Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs Eric Heitz, Journal of Computer Graphics Techniques (JCGT), 3(2), 48–107, 2014.
-
Sampling the GGX Distribution of Visible Normals Eric Heitz, Journal of Computer Graphics Techniques (JCGT), 7(4), 1–13, 2018.
-
A re-determination of the trichromatic coefficients of the spectral colours W. D. Wright, Transactions of the Optical Society, 30(4), 141–164, 1929.
-
A re-determination of the mixture curves of the spectrum W. D. Wright, Transactions of the Optical Society, 31(4), 201–218, 1930.
-
The Colorimetric Properties of the Spectrum John Guild, Philosophical Transactions of the Royal Society A, 230, 149–187, 1932.
-
How the CIE 1931 color-matching functions were derived from Wright-Guild data Hugh S. Fairman, Michael H. Brill, Henry Hemmendinger, Color Research & Application, 22(1), 11–23, 1997.
-
A critical review of the development of the CIE1931 RGB color-matching functions Arthur D. Broadbent, Color Research & Application, 29(4), 267–272, 2004.
-
How the CIE 1931 RGB Color Matching Functions Were Developed from the Initial Color Matching Experiments Yuhao Zhu, blog article, 2020.
-
Hero Wavelength Spectral Sampling Alexander Wilkie, Sehera Nawaz, Marc Droske, Andrea Weidlich, Johannes Hanika, Computer Graphics Forum, 33(4), 123–131, 2014.
-
A Low-Dimensional Function Space for Efficient Spectral Upsampling Wenzel Jakob, Johannes Hanika, Computer Graphics Forum (Proceedings of Eurographics), 38(2), 147–155, 2019.
本项目原创源代码基于 MIT License 开源。
仓库中包含的第三方源代码遵循其各自的原始许可证, 相关版权与许可证声明保留在对应文件中。
resource/ 目录中的内容不包含在 MIT License 的授权范围内。
PPT 中引用的第三方图片及其他材料,其版权归各自权利人所有。
仓库 Release 中提供的模型、HDRI、光谱数据及其他外部资源 不包含在 MIT License 的授权范围内。