使用光线追踪来渲染图像,实现两个部分:光线的生成和光线与三角的求交

你需要修改的函数是:

• Renderer.cpp 中的 Render():这里你需要为每个像素生成一条对应的光 线,然后调用函数 castRay() 来得到颜色,最后将颜色存储在帧缓冲区的相 应像素中。

• Triangle.hpp 中的 rayTriangleIntersect(): v0, v1, v2 是三角形的三个 顶点,orig 是光线的起点,dir 是光线单位化的方向向量。tnear, u, v 是你需 要使用我们课上推导的 Moller-Trumbore 算法来更新的参数。

Render()

void Renderer::Render(const Scene& scene)
{
std::vector<Vector3f> framebuffer(scene.width * scene.height); //需要填充的frame_buffer float scale = std::tan(deg2rad(scene.fov * 0.5f)); // t / |n|
float imageAspectRatio = scene.width / (float)scene.height; // r / t // Use this variable as the eye position to start your rays.
Vector3f eye_pos(0); // 从(0, 0, 0)发出光线
int m = 0;
for (int j = 0; j < scene.height; ++j)
{
for (int i = 0; i < scene.width; ++i) // 遍历每一个像素,像素点在n面上
{
// generate primary ray direction
// 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对应于宽度,i = 0 对应 x = -r, i = width-1对应 x = r, 列方程解i和x的关系有:x = (2r / w-1) * i - r = [(2 / w-1) * i - 1] * r
// y对应于高度,j = 0 对应 y = t, j = height-1对应 y = -t,列方程解j和y的关系有:y = (-2t / h-1) * j + t = [1 - (2 / h-1) * j] * t
// n = -1则 |n| = 1, t = scale, r = t * asp = scale *imageAsp
float x = (2 / (float)(scene.width - 1) * i - 1) * scale * imageAspectRatio;
float y = (1 - 2 / (float)(scene.height - 1) * j) * scale; Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
framebuffer[m++] = castRay(eye_pos, normalize(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);
}

rayTriangleIntersect()

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 = v1 - v0;
Vector3f E2 = v2 - v0;
Vector3f S = orig - v0;
Vector3f S1 = crossProduct(dir, E2);
Vector3f S2 = crossProduct(S, E1);
float det = dotProduct(E1, S1);
if(det == 0 || det < 0) return false; float reS1E1 = 1. / det;
float t = dotProduct(S2, E2) * reS1E1;
float b1 = dotProduct(S1, S) * reS1E1;
float b2 = dotProduct(S2, dir) *reS1E1;
if(t >= 0 && b1 >= 0 && b2 >= 0 && 1 - b1 - b2 >= 0){
tnear = t;
u = b1;
v = b2;
return true;
}
return tnear > 0;
}

效果图

随机推荐

  1. Next.js 中为什么 App Router 可能是未来,但 Pages Router 仍然重要?

    Next.js 作为一个强大的 React 框架,为开发者提供了两种路由系统:App Router 和 Pages Router.这两种路由系统各有特色,适用于不同的场景.本文将深入探讨这两种路由系统 ...

  2. 20.从0学ARM-移植uboot支持exynos4412

    经过前面一章的学习,我们已经了解了什么是uboot,本章主要目的是如何编译和移植uboot,增加串口.网络.emmc等功能,让他支持exynos4412开发板. 一.移植步骤 1. Uboot配置 指 ...

  3. CF650D Zip-line

    CF650D Zip-line 大概题面: 给定一个长度为 \(n\) 的序列以及\(m\)个操作,每个操作形如" \(a_i,b_i\) ",表示将序列中第 \(a_i\) 个数 ...

  4. 【Jmeter】之进行单接口批量压力测试

    目录: 一.安装Jmeter 二.接口压力测试 p.p1 { margin: 0; font: 14px ".PingFang SC"; color: rgba(17, 31, 4 ...

  5. win11(含win10)自带的一键镜像备份与还原方法

    winxp和win7时代小伙伴们备份或恢复系统时大都采用类似一键ghost类的软件制作系统盘的映像,当遇到故障时再恢复,但win10和win11其实自带有这种映像制作和还原功能,我们就再也不需要额外安 ...

  6. Web刷题之polarctf靶场(1)

    PolarCTF 1.XFF 打开靶场发现需要ip为1.1.1.1的用户才行, 打开BurpSuite进行抓包并对数据包进行修改,根据题目XFF提示 flag{847ac5dd4057b1ece411 ...

  7. 远距离跨网络实现windows远程桌面连接

    1.保证已经打开被连接电脑---远程访问---权限. 我的电脑--右键--属性--远程设置 2.选择允许连接 (选择用户和高级没有特殊设置可以不动,被连接电脑当前登陆的账号就可以满足权限) 3.打开- ...

  8. git 相关操作

        git diff 已经缓存的文件和刚刚修改过的没有缓存的文件的对比 git diff --stage   git status 查看本地文件的修改,是否进入缓存 git add 把刚刚修改过的 ...

  9. sicp每日一题[2.3]

    Exercise 2.3 Implement a representation for rectangles in a plane. (Hint: You may want to make use o ...

  10. 击败全球上千参赛队伍,合合信息获ICDAR“文本篡改检测”赛道冠军

    AI技术的快速发展激发了人们对于美好未来的畅享,也带来了潜在的危机,数据泄露.电信诈骗等系列风险与隐患开始浮出水面.利用科技手段构建可信的技术发展环境,保护使用者的信息及财产安全,正在成为行业共识. ...