在这部分的课程中,我们将专注于使用光线追踪来渲染图像。在光线追踪中最重要的操作之一就是找到光线与物体的交点。一旦找到光线与物体的交点,就可以执行着色并返回像素颜色。在这次作业中,我们需要实现两个部分:光线的生成和光线与三角的相交。本次代码框架的工作流程为:
main 函数开始。我们定义场景的参数,添加物体(球体或三角形)到场景中,并设置其材质,然后将光源添加到场景中。Render(scene) 函数。在遍历所有像素的循环里,生成对应的光线并将返回的颜色保存在帧缓冲区(framebuffer)中。在渲染过程结束后,帧缓冲区中的信息将被保存为图像。CastRay 函数,该函数调用 trace 来查询光线与场景中最近的对象的交点。你需要修改的函数是:
Renderer.cpp 中的 Render():这里你需要为每个像素生成一条对应的光线,然后调用函数 castRay() 来得到颜色,最后将颜色存储在帧缓冲区的相应像素中。Triangle.hpp 中的 rayTriangleIntersect(): v0, v1, v2 是三角形的三个顶点,orig 是光线的起点,dir 是光线单位化的方向向量。tnear, u, v 是你需要使用我们课上推导的 Moller-Trumbore 算法来更新的参数。
为了得到每个像素的光线,我们需要得到每个像素的世界坐标,如何从像素坐标 i,j 到世界坐标 x,y 就是我们需要完成的。
分析代码框架可以发现,我们已经将眼睛的位置防止在坐标原点
Vector3f eye_pos(0);
并且从下面代码也可以看出,屏幕与 x O y xOy xOy 平面的距离为 -1,即屏幕位于 z = − 1 z=-1 z=−1 的平面上
Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
对于每一宽 w i d t h width width 高 h e i g h t height height 的帧,我们需要将上面的像素坐标 ( i , j ) (i,j) (i,j) 映射到世界坐标 ( x , y ) (x,y) (x,y)

首先,先从像素坐标得到像素中心的坐标 ( i + 0.5 , j + 0.5 ) (i+0.5, j+0.5) (i+0.5,j+0.5),再将坐标进行归一化 ( i + 0.5 w i d t h , j + 0.5 h e i g h t ) (\frac{i+0.5}{width}, \frac{j+0.5}{height}) (widthi+0.5,heightj+0.5);因为 frame 的 y y y 坐标与世界坐标式相反的,所以将 y y y 坐标取反 ( i + 0.5 w i d t h , − j + 0.5 h e i g h t ) (\frac{i+0.5}{width}, -\frac{j+0.5}{height}) (widthi+0.5,−heightj+0.5);我们知道屏幕的中心是在坐标 ( 0 , 0 , − 1 ) (0, 0, -1) (0,0,−1) 上,所以需要将坐标平移使得其上下左右对称 ( i + 0.5 w i d t h − 0.5 , − j + 0.5 h e i g h t + 0.5 ) (\frac{i+0.5}{width}-0.5, -\frac{j+0.5}{height}+0.5) (widthi+0.5−0.5,−heightj+0.5+0.5);
因为我们知道
s
c
a
l
e
=
tan
(
f
o
v
/
2
)
=
h
e
i
g
h
t
/
2
,
i
m
a
g
e
A
s
p
e
c
t
R
a
t
i
o
=
w
i
d
t
h
/
h
e
i
g
h
t
scale = \tan(fov/2)=height/2,\ imageAspectRatio=width/height
scale=tan(fov/2)=height/2, imageAspectRatio=width/height
所以
h
e
i
g
h
t
=
2
s
c
a
l
e
,
w
i
d
t
h
=
2
s
c
a
l
e
⋅
i
m
a
g
e
A
s
p
e
c
t
R
a
t
i
o
height=2scale,\ width=2scale\cdot imageAspectRatio
height=2scale, width=2scale⋅imageAspectRatio

最后将归一化后的坐标乘以画面的大小,最终得到世界坐标为
(
2
s
c
a
l
e
⋅
i
m
a
g
e
A
s
p
e
c
t
R
a
t
i
o
⋅
(
i
+
0.5
w
i
d
t
h
−
0.5
)
,
2
s
c
a
l
e
(
−
j
+
0.5
h
e
i
g
h
t
+
0.5
)
)
(2 scale\cdot imageAspectRatio\cdot (\frac{i+0.5}{width}-0.5), 2scale(-\frac{j+0.5}{height}+0.5))
(2scale⋅imageAspectRatio⋅(widthi+0.5−0.5),2scale(−heightj+0.5+0.5))
所以
float x = 2 * scale * imageAspectRatio * ((i + 0.5) / scene.width - 0.5);
float y = 2 * scale * (-(j + 0.5) / scene.height + 0.5);
再设置方向向量,并初始化
Vector3f dir = normalize(Vector3f(x, y, -1));
完整代码:
void Renderer::Render(const Scene& scene)
{
std::vector<Vector3f> framebuffer(scene.width * scene.height);
float scale = std::tan(deg2rad(scene.fov * 0.5f));
float imageAspectRatio = scene.width / (float)scene.height;
// Use this variable as the eye position to start your rays.
Vector3f eye_pos(0);
int m = 0;
for (int j = 0; j < scene.height; ++j)
{
for (int i = 0; i < scene.width; ++i)
{
// generate primary ray direction
float x = 2 * scale * imageAspectRatio * ((i + 0.5) / scene.width - 0.5);
float y = 2 * scale * (-(j + 0.5) / scene.height + 0.5);
// TODO: Find the x and y positions of the current pixel to get the direction
// vector that passes through it.
// Also, don't forget to multiply both of them with the variable *scale*, and
// x (horizontal) variable with the *imageAspectRatio*
Vector3f dir = normalize(Vector3f(x, y, -1)); // Don't forget to normalize this direction!
framebuffer[m++] = castRay(eye_pos, dir, scene, 0);
}
UpdateProgress(j / (float)scene.height);
}
// save framebuffer to file
FILE* fp = fopen("binary.ppm", "wb");
(void)fprintf(fp, "P6\n%d %d\n255\n", scene.width, scene.height);
for (auto i = 0; i < scene.height * scene.width; ++i) {
static unsigned char color[3];
color[0] = (char)(255 * clamp(0, 1, framebuffer[i].x));
color[1] = (char)(255 * clamp(0, 1, framebuffer[i].y));
color[2] = (char)(255 * clamp(0, 1, framebuffer[i].z));
fwrite(color, 1, 3, fp);
}
fclose(fp);
}
Möller Trumbore Algorithm 是一种更快更便捷更直观的求解交点方式
O
→
+
t
D
→
=
(
1
−
b
1
−
b
2
)
P
→
0
+
b
1
P
→
1
+
b
2
P
→
2
\overrightarrow{\mathbf{O}}+t \overrightarrow{\mathbf{D}}=\left(1-b_{1}-b_{2}\right) \overrightarrow{\mathbf{P}}_{0}+b_{1} \overrightarrow{\mathbf{P}}_{1}+b_{2} \overrightarrow{\mathbf{P}}_{2}
O+tD=(1−b1−b2)P0+b1P1+b2P2
[
t
b
1
b
2
]
=
1
S
→
1
∙
E
→
1
[
S
→
2
⋅
E
→
2
S
→
1
⋅
S
→
S
→
2
∙
D
→
]
\left[
其中
E
→
1
=
P
→
1
−
P
→
0
E
→
2
=
P
→
2
−
P
→
0
S
→
=
O
→
−
P
→
0
S
→
1
=
D
→
×
E
→
2
S
→
2
=
S
→
×
E
→
1
解出来后,需要进行验证:
t
>
0
,
b
1
⩾
0
,
b
2
⩾
0
,
b
3
⩾
0
t>0,b_1\geqslant 0,b_2\geqslant 0,b_3\geqslant 0
t>0,b1⩾0,b2⩾0,b3⩾0
按照上面式子实现即可
bool rayTriangleIntersect(const Vector3f& v0, const Vector3f& v1, const Vector3f& v2, const Vector3f& orig,
const Vector3f& dir, float& tnear, float& u, float& v)
{
// TODO: Implement this function that tests whether the triangle
// that's specified bt v0, v1 and v2 intersects with the ray (whose
// origin is *orig* and direction is *dir*)
// Also don't forget to update tnear, u and v.
Vector3f E1, E2, S, S1, S2, res;
E1 = v1 - v0;
E2 = v2 - v0;
S = orig - v0;
S1 = crossProduct(dir, E2);
S2 = crossProduct(S, E1);
res = 1.0f / dotProduct(S1, E1) * Vector3f(dotProduct(S2, E2), dotProduct(S1, S), dotProduct(S2, dir));
tnear = res.x;
u = res.y;
v = res.z;
if (tnear > 0 && u >= 0 && v >= 0 && 1 - u - v >= 0) return true;
return false;
}
