目录
1. 作业描述
在这部分的课程中,我们将专注于使用光线追踪来渲染图像。在光线追踪中最重要的操作之一就是找到光线与物体的交点。一旦找到光线与物体的交点,就可以执行着色并返回像素颜色。在这次作业中,我们需要实现两个部分:光线的生成和光线与三角的相交。本次代码框架的工作流程为:
- 从 main 函数开始。我们定义场景的参数,添加物体(球体或三角形)到场景中,并设置其材质,然后将光源添加到场景中。
- 调用 Render(scene) 函数。在遍历所有像素的循环里,生成对应的光线并将返回的颜色保存在帧缓冲区(framebuffer)中。在渲染过程结束后,帧缓冲区中的信息将被保存为图像。
- 在生成像素对应的光线后,我们调用 CastRay 函数,该函数调用 trace 来查询光线与场景中最近的对象的交点。
- 然后,我们在此交点执行着色。我们设置了三种不同的着色情况,并且已经为你提供了代码。
• global.hpp:包含了整个框架中会使用的基本函数和变量。
• Vector.hpp: 由于我们不再使用 Eigen 库,因此我们在此处提供了常见的向量操作,例如:dotProduct,crossProduct,normalize。
• Object.hpp: 渲染物体的父类。Triangle 和 Sphere 类都是从该类继承的。
• Scene.hpp: 定义要渲染的场景。包括设置参数,物体以及灯光。
• Renderer.hpp: 渲染器类,它实现了所有光线追踪的操作。
你需要修改的函数是:
• Renderer.cpp 中的 Render():这里你需要为每个像素生成一条对应的光线,然后调用函数 castRay()来得到颜色,最后将颜色存储在帧缓冲区的相应像素中。
• Triangle.hpp 中的 rayTriangleIntersect():v0, v1, v2 是三角形的三个顶点,orig 是光线的起点,dir 是光线单位化的方向向量。tnear, u, v是你需要使用我们课上推导的 Moller-Trumbore 算法来更新的参数
2. 解
本次作业要修改的部分很简单,但是还是有一些点需要注意
2.1 Render
根据题目要求,这部分我们需要为每个像素生成一条对应的光线,根据已知条件,我们已经有了相机的fov,场景的像素和宽高比,现在要求出相机到像素每个点的光线,但是根据这些,我们发现还差一个条件,那就是成像平面的深度未知,这样就无法通过fov求出场景在世界坐标系下的高,自然也无法根据宽高比求出它的宽,这条光线就不可求了,但是我们仔细看作业给的源代码中有这么一条语句:Vector3f dir = Vector3f(x, y, -1);// Don’t forget to normalize this direction! 它说的是不要忘记对求出来的光线方向归一化,也就是说这条向量就是相机到像素的原始向量,那么就隐含了一个成像平面的深度为-1的条件(相机向-z方向看),就此,我们就可以根据现有条件算出具体的光线了(默认遍历顺序是左上到右下,像素坐标(640,480)对应世界坐标系(0,0),在坐标转换时别忘了是转换的像素中心的坐标,对i和j要加0.5):
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;
float y;
// 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*
x = 2 * scale * imageAspectRatio / scene.width * (i + 0.5) - scale * imageAspectRatio;
y = - 2 * scale / scene.height * (j + 0.5) + scale;
Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
dir = normalize(dir);
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);
}
2.2 rayTriangleIntersect
这部分的代码就是对Lecture 13上提到的Möller Trumbore算法的具体实现:

算法具体的推导过程见:Möller Trumbore
需要注意的是计算结束以后别忘了对算得的tnear、u、v进行有效性计算,若范围正确则返回true
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, re;
E1 = v1 - v0;
E2 = v2 - v0;
S = orig - v0;
S1 = crossProduct(dir, E2);
S2 = crossProduct(S, E1);
re

本篇博客深入探讨光线追踪技术,重点在于实现光线与三角形的相交算法——Möller-Trumbore算法。文章详细解析了渲染流程,包括场景设置、光线生成、物体求交及着色处理。同时,介绍了如何在C++环境中完成这些操作,涉及到向量运算、光线追踪框架的理解和优化。最终,通过实际效果展示了算法的正确性。
最低0.47元/天 解锁文章
2761

被折叠的 条评论
为什么被折叠?



