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;
}
效果图

随机推荐
- java汉字占用字节
若使用utf-8编码,中文占3个字节,英文的话只占一个字节 System.out.println("人".getBytes().length); 输出3 若使用unicode编码, ...
- TCP/IP协议竟然有这么多漏洞?
据2020年上半年中国互联网网络安全监测数据分析报告显示,恶意程序控制服务器.拒绝服务攻击(DDoS)等网络攻击行为有增无减.时至今日,网络攻击已经成为影响网络信息安全.业务信息安全的主要因素之一. ...
- MySQL 亿级数据平滑迁移实战
作者:来自 vivo 互联网服务器团队- Li Gang 本文介绍了一次 MySQL 数据迁移的流程,通过方案选型.业务改造.双写迁移最终实现了亿级数据的迁移. 一.背景 预约业务是 vivo 游戏中 ...
- java_父类子类
private 只有自身能访问自身 自身 同包子 不同包子类 同包类 其他类 可以访问 不能继承 不能继承 不能访问 不能访问 package/friendly/default == 不写 自身 同包 ...
- LaTeX 插入伪代码
使用 algorithm 包和 algpseudocode 包 algorithm 包 用途: 提供一个浮动体环境来包含算法(类似于 figure 和 table 环境),使得算法可以自动编号并出现在 ...
- 使用go+gin编写日志中间,实现自动化收集http访问信息,错误信息等,自动化生成日志文件
1.首先在logger包下 点击查看代码 package logger import ( "fmt" "io" "net/http" &qu ...
- 编译器实现之旅——第十三章 if语句和while语句的代码生成器分派函数的实现
在上一章的旅程中,我们已经实现了表达式类代码生成器分派函数,而在这一章的旅程中,我们将要实现if语句和while语句的代码生成器分派函数.if语句和while语句是两种典型的带有跳转指令的语句.观察C ...
- 计算机图形学(第四版) PDF 中文版
目录 图书介绍 下载地址 图书介绍 <计算机图形学(第四版)>是2014年电子工业出版社出版的图书,作者是Donald Hearn.M. Pauline Baker.Warren R. C ...
- C# WinForm避免程序重复启动,限制程序只能运行一个实例【转】
记录一下,原文:https://blog.csdn.net/xggbest/article/details/104231935 禁止多个进程运行,当重复运行时激活以前的进程 Program.cs: u ...
- 小tips:vue2中broadcast和dispatch的实现
/* * broadcast 事件广播 * @param {componentName} 组件名称 * @param {eventName} 事件名 * @param {params} 参数 * 遍历 ...