games101_Homework5
使用光线追踪来渲染图像,实现两个部分:光线的生成和光线与三角的求交
你需要修改的函数是:
• 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;
}
效果图

随机推荐
- grpc坑之Could not find TLS ALPN provider; no working netty-tcnative
在使用grpc过程中偶尔会遇到下面报错: Initialization of bean failed; nestedexception is java.lang.IllegalStateExcepti ...
- 2.3 Exception model
2.3.1 异常状态 2.3.1 异常类型
- 线性dp:LeetCode674. 最长连续递增序列
LeetCode674. 最长连续递增序列 阅读本文之前,需要先了解"动态规划方法论",这在我的文章以前有讲过 链接:动态规划方法论 本文之前也讲过一篇文章:最长递增子序列,这道题 ...
- 零基础学习人工智能—Python—Pytorch学习(九)
前言 本文主要介绍卷积神经网络的使用的下半部分. 另外,上篇文章增加了一点代码注释,主要是解释(w-f+2p)/s+1这个公式的使用. 所以,要是这篇文章的代码看不太懂,可以翻一下上篇文章. 代码实现 ...
- Go plan9 汇编:手写汇编
原创文章,欢迎转载,转载请注明出处,谢谢. 0. 前言 在 Go plan9 汇编: 打通应用到底层的任督二脉 一文中介绍了从应用程序到汇编指令的转换.本文将结合汇编和 Go 程序实现手写基本的汇编指 ...
- Docker网络中篇-docker网络的四种类型
通过上一篇学习,我们对docker网络有了初步的了解.本篇,咱们就来实战docker网络. docker网络实战 实战docker网络,我们将从以下几个案例来讲解 1:birdge是什么? 2:hos ...
- docker高级篇第二章-分布式存储之实战案例:主从容错切换迁移案例
在上一篇,学会了3主3从的Redis集群搭建.那么接下来,我们就来学习Redis集群主从容错切换迁移案例 本次案例从模拟两个场景 1:数据读写存储 a:一个新key数据来了,是否会按照预设的进入槽中? ...
- 测距技术 超声波、毫米波、激光雷达LIDAR
超声波 技术成熟,成本之选 声波传输慢,高速时误差大 超声波雷达有40kHz.48kHz和58kHz三种,频率越高,灵敏度越高,探测角度越小.在工作状态,通过收发超声波,超声波雷达能以1-3cm精度测 ...
- C#自定义控件—旋转按钮
C#用户控件之旋转按钮 按钮功能:手自动旋转,标签文本显示.点击二次弹框确认(源码在最后边): [制作方法] 用方法找到控件的中心坐标,画背景外环.内圆:再绘制矩形开关,进行角度旋转即可获得: [关键 ...
- 一步一步将PlantUML类图导出为自定义格式的XMI文件
一步一步将PlantUML类图导出为自定义格式的XMI文件 说明: 首次发表日期:2024-09-08 PlantUML官网: https://plantuml.com/zh/ PlantUML命令行 ...