写在前面
几个月前,因为我不会 C++,用自己熟悉的 C# 在材质那里出现好几次莫名其妙的 BUG,反复重写依旧复现 BUG,我的内心是极度崩溃的。最后几经波折选择直接拷贝 C++ 代码后自己再手动翻译成 C#,才终于渲染出了正确的结果,但心神俱疲,也只完成了本教程的 80% – 90% 的内容。现在 C++ 已经学了一段时间,为了能对 C++ 的工程能力的提升,再加上找的一个大佬写的 PBRT 总结中开头就提到要学 PBRT 建议必须先把光追系列的三本书学了。因此被劝退的我决定,重新用 C++ 学一遍这个光追圣经系列。
为了能彻底吃透这个系列,我重新思考了一种写笔记的方式,并用在这个系列。我准备每当书中的进度进行到一定程度、效果实现到一个阶段时,就要对以往修改的全部代码进行一次完整的、全局的分析,将每个函数的作用、每行代码的书写目的都记录总结下来。希望在这个过程中能不断地复习到那些基础知识,能对光追的工程实现能力有更上一层楼地提升。
向量类型的实现
构造一个基本的数学向量类型,其包含了一个 double
类型的数组。在类型中实现一些操作符的重载,诸如向量的取反,向量通过索引获取各维度数值等。对于赋值操作符需要在类型中重写,诸如加法、数乘。还有向量的长度,是由向量自身点乘自身再开方得到的。注意未开方的数值后面也有用,因此也封装为一个方法。
//vec3.h
class vec3 {
public:
double e[3];
public:
vec3():e{0,0,0}{}
vec3(double e0,double e1,double e2):e{e0,e1,e2}{}
double x() const { return e[0]; }
double y() const { return e[1]; }
double z() const { return e[2]; }
vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
double operator[](int i) const { return e[i]; }
double& operator[](int i) { return e[i]; }
vec3& operator+=(const vec3& v) {
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
vec3& operator*=(const double t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
return (*this);
}
vec3& operator/=(const double t) {
return (*this) *= 1 / t;
}
double length()const {
return sqrt(length_squared());
}
double length_squared()const {
return e[0] * e[0] + e[1] * e[1] + e[2] * e[2];
}
}
复制代码
向量类的别名
虽然点坐标、向量和颜色都是三维的表示,都可以使用 vec3
类型,但总会有些误解在里面,为了更好的区分它们,我们可以为 vec3
类型再起两个类型别名,诸如 color
和 point3
。
//vec3.h
using point3 = vec3;
using color = vec3;
复制代码
color.h
为 vec3
的别名 color
新建一个头文件,声明并并一个专属的函数以写入颜色值。
//color.h
#pragma once
#include "vec3.h"
#include <iostream>
void write_color(std::ostream& out, color pixel_color) {
out << static_cast<int>(255.999 * pixel_color.x()) << ' '
<< static_cast<int>(255.999 * pixel_color.y()) << ' '
<< static_cast<int>(255.999 * pixel_color.z()) << '\n';
}
复制代码
内联函数
将常用的向量数学运算封装为内联函数,诸如输出流左移操作符重载、加减乘除重载、向量的点积运算、叉积运算和单位化。
//vec3.h
inline std::ostream& operator<<(std::ostream& out, const vec3& v) {
return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
}
inline vec3 operator+(const vec3& u, const vec3& v) {
return vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
}
inline vec3 operator-(const vec3& u, const vec3& v) {
return vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2]);
}
inline vec3 operator*(const vec3& u, const vec3& v) {
return vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
}
inline vec3 operator*(double t, const vec3& v) {
return vec3(t * v.e[0], t * v.e[1], t * v.e[2]);
}
inline vec3 operator*(const vec3& v, double t) {
return t * v;
}
inline vec3 operator/(vec3 v, double t) {
return (1 / t) * v;
}
inline double dot(const vec3& u, const vec3& v) {
return u.e[0] * v.e[0]
+ u.e[1] * v.e[1]
+ u.e[2] * v.e[2];
}
inline vec3 cross(const vec3& u, const vec3& v) {
return vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1],
u.e[2] * v.e[0] - u.e[0] * v.e[2],
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
}
inline vec3 unit_vector(vec3 v) {
return v / v.length();
}
复制代码
光线类型的实现
光线是随着时间而增长的,起点为 方向为 的向量。因此在已知光线的起点和方向后,给出时间 即可求出光线在 时刻应到达的位置 点,即 。
//ray.h
#pragma once
#include "vec3.h"
class ray {
private:
point3 m_origin;
vec3 m_direction;
public:
ray(){}
ray(const point3& origin, const vec3& direction)
:m_origin(origin), m_direction(direction) {}
point3 origin()const { return m_origin; }
vec3 direction()const { return m_direction; }
point3 at(double t)const {
return m_origin + t * m_direction;
}
};
复制代码
光线与球体的碰撞检测
已知球体的半径和球心坐标,我们就可以得出一个三维球体的计算公式为 ,而球体表面上点的公式实际上又可以表达为所有到球心距离为球半径的点,即 ,再将 带入上式后可得出一个 为未知数的二元一次方程式: