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

随机推荐
- Linux中级——“驱动” 控制硬件必须学会的底层知识
驱动认知 1. 什么是驱动 驱动就是对底层硬件设备的操作进行封装,并向上层提供函数接口. 设备分类: linux系统将设备分为3类:字符设备.块设备.网络设备. 字符设备:指只能一个字节一个字节读写的 ...
- Linux下错误解决方案
错误 "E: Unable to correct problems, you have held broken packages."这种问题包破坏问题,可能是由于镜像源与系统版本不 ...
- Maven / Gradle 依赖管理
添加外部依赖 向你的 Maven / Gradle 项目添加依赖的过程可分为如下几步: 搜索依赖 搜索你要安装的依赖,比如你需要 MySQL Connector/J,可以在谷歌搜索"MySQ ...
- Go实现常用的排序算法
一.插入排序 1.从第一个元素开始,该元素可以认为已经被排序 2.取出下一个元素,在已经排序的元素序列中从后向前扫描 3.如果该元素(已排序)大于新元素,将该元素移到下一位置 4.重复步骤3,直到找到 ...
- QT原理与源码分析之QT对象类型QObject源码中的间接的设计思想
这一篇文章介绍QT框架中QT对象类型QObject类型的源代码在设计上的一个比较优秀的设计思想. QObject类型定义 QObject 直接来看QObject的源代码.为了表达更简洁更直观,这里省略 ...
- 七、Scrapy框架-案例1
1. 豆瓣民谣Top排名爬取 1.1 构建scrapy项目 安装Scrapy库 pip install scrapy 创建Scrapy项目 通过cmd进入命令窗口,执行命令scrapy startpr ...
- 使用hexo进行github博客搭建
1.你必须建一个存储库,这个存储库要和你的github名称一致(不然就会404),如下图: 2.如果是window配置,需要安装git和node.js 3.下载hexo npm config set ...
- threejs渲染基础的3D场景
// 创建一个场景对象 let scene = new THREE.Scene(); // 创建一个相机对象 let camera = new THREE.PerspectiveCamera(75, ...
- C#的类和对象,继承
/// 类与对象 /// 类和对象是面向编程的两个核心概念 /// 类:类是对一群具有相同特征的或者行为事物的统称 类 是图纸 /// 对象是由类创造出来的一个具体存在 可以直接使用 对象是图纸造出来 ...
- ADO.NET 和 ORM的区别
ADO: 1 大量的Sql语句-业务不同,Sql语句不同 2 需要根据不同的场景编写不同Sql语句-灵活去编写Sql语句-提前优化Sql 语句-提供高性能的Sql语句 3 不适合快速开发 4 可编程性 ...