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

随机推荐
- Unity FpsSample Demo研究
1.前言 Unity FpsSample Demo大约是2018发布,用于官方演示MLAPI(NetCode前身)+DOTS的一个FPS多人对战Demo. Demo下载地址(需要安装Git LFS) ...
- 【2】Kaggle 医学影像数据读取
赛题名称:RSNA 2024 Lumbar Spine Degenerative Classification 中文:腰椎退行性病变分类 kaggle官网赛题链接:https://www.kaggle ...
- Android 国际化:新增越南语语系(Java)
前提: 1. 在res文件夹中,新增values-vi文件夹(越南语文件夹) 2. 在步骤1的文件夹中,新增strings.xml 背景: 1. targetSdkVersion 29 2. Jdk ...
- 😶🌫️ SpringBoot中MongoDB的骚操作用法
不知道大家在工作项目中有没有使用MongoDB,在哪些场景中使用.MongoDB作为NoSQL数据库,不像SQL数据库那样,可以使用Mybatis框架. 如果需要在SpringBoot中使用Mongo ...
- 如何在 Web 前端做 3D 音效处理
一.背景 在社交元宇宙.大逃杀等类型的游戏场景下,用户在通过简单语音交流外,结合场景也需要一些立体声效果来让用户感知游戏角色周围其他用户的存在及其对应的距离和方位,提高语音互动的趣味性. 为了满足 ...
- 浅谈 C# 中的顶级语句
前言 在C# 9版本中引入了一项新特性:顶级语句,这一特性允许在不显式定义 Main 方法的情况下直接编写代码. 传统的写法 namespace TestStatements{ internal ...
- Figma 学习笔记 – Component
参考 Guide to Components in Figma Figma Tutorial: Components - The Basics (Youtube) 定义与用途 Figma 的 Comp ...
- python 属性装饰器和对应的setter方法,属性的封装和安全性控制
当我们在类中定义属性时,通常希望能够对属性的读取和写入进行控制,以确保数据的完整性和安全性.属性装饰器和对应的setter方法提供了一种实现属性封装和安全性控制的方法. 属性装饰器是Python的一种 ...
- SXYZ-6.28训练赛
今天上午出中考成绩,所以下午打了一场训练赛,只有两个小时,没有昨天毒瘤,但也很毒瘤(还是模拟赛好) 关于中考可以看我的中考游记 (为了保护隐私,以后都把姓名涂掉) 为什么还是倒数啊~ T1 binar ...
- 【VMware VCF】使用 SoS 实用程序检查 VCF 环境的运行状态以及收集组件的日志信息。
VMware Cloud Foundation 解决方案中有一个叫 Supportability and Serviceability(SoS)可支持性和可维护性的实用程序,可能你在初始构建 VCF ...