学霸笔记

如果是使用 windows 跑代码的同学,需要把 CMakeLists.txt 下的两处 -fsanitize=undefined 关键字给去掉,才能正常编译。

本次作业非常简单,建议可以看看 RayTracingInOneWeekend 进一步学习。

光线生成

void Renderer::Render(const Scene& scene)
{std::vector<Vector3f> framebuffer(scene.width * scene.height);float scale = std::tan(deg2rad(scene.fov * 0.5f));float imageAspectRatio = scene.width / (float)scene.height;// Use this variable as the eye position to start your rays.Vector3f eye_pos(0);int m = 0;for (int j = 0; j < scene.height; ++j){for (int i = 0; i < scene.width; ++i){// generate primary ray directionfloat x;float y;// 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 = float(i / (scene.width - 1.0)) * 2 - 1;y = (1 - float(j / (scene.height - 1.0))) * 2 - 1;x *= scale * imageAspectRatio;y *= scale;Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!dir = normalize(dir);framebuffer[m++] = castRay(eye_pos, dir, scene, 0);}UpdateProgress(j / (float)scene.height);}// save framebuffer to fileFILE* 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);
}

获取像素坐标,然后生成光线。根据标准平面的坐标范围 [ − 1 , 1 ] [-1, 1] [−1,1],同时 Y Y Y 轴正向为上, X X X 轴正向为右,得到当前的 ( x , y ) (x, y) (x,y) 映射值,然后再乘上相应的系数即可。

求解光线与三角形相交

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.auto E1 = v1 - v0;auto E2 = v2 - v0;auto S0 = orig - v0;auto S1 = crossProduct(dir, E2);auto S2 = crossProduct(S0, E1);auto res = Vector3f(dotProduct(S2, E2), dotProduct(S1, S0), dotProduct(S2, dir)) / (dotProduct(S1, E1));tnear = res.x;u = res.y;v = res.z;if (tnear >= 0 && u >= 0 && v >= 0 && u + v <= 1)return true;return false;
}

套公式即可,返回值 t n e a r tnear tnear 对应 t t t, u u u 对应 b 1 b_1 b1​, v v v 对应 b 2 b_2 b2​,合法解要求 t n e a r > = 0 tnear>=0 tnear>=0,同时要求在三角形之内,即可以用质心坐标表示,参数满足在 [ 0 , 1 ] [0,1] [0,1] 范围。

结果如下

Games101 作业5 光线与三角形相交相关推荐

  1. GAMES101 作业3(附三角形重心坐标,Blinn-Phong光照模型及法线贴图推导)

    目录 写在前面 第一题 三角形重心坐标 第二题 Blinn-Phong光照模型 第三题 纹理贴图 第四题 凹凸贴图实现及法线贴图推导 第五题 位移贴图 写在前面 main 函数中  std::func ...

  2. Möller-Trumbore算法-射线三角形相交算法

    Möller-Trumbore算法 一.概述 二.准备知识 三.Möller-Trumbore 算法推导 推导过程 四.代码实现 一.概述 Möller-Trumbore 射线三角相交算法是一种快速计 ...

  3. GAMES101作业7-路径追踪实现过程代码框架超全解读

    目录 Path Tracing算法过程讨论 蒙特卡洛积分 直接光照 direct illumination 间接光照 indirect illumination ​编辑 合成全局光照 解决一些存在的问 ...

  4. 计算机图形学学习笔记——Whitted-Style Ray Tracing(GAMES101作业5讲解)

    计算机图形学学习笔记--Whitted-Style Ray Tracing GAMES101作业5讲解 遍历所有的像素生成光线 光线与平面求交 遍历所有的像素生成光线 关于作业五中如何遍历所有的像素, ...

  5. GAMES101作业5-从头到尾理解代码Whitted光线追踪

    目录 Whitted Ray-Tracing Whitted光线追踪 What Why How 1 发射主射线primary ray 实现步骤 (1)定义相机 (2)计算primary主射线的方向 R ...

  6. GAMES101作业6及课程总结(重点解决SAH扩展作业)

    这次作业相对于作业5会麻烦一点点,而且框架相较于作业五的也麻烦了一点,当然作业的难点其实主要还是在扩展作业SAH那块. 目录 课程总结与理解(光线追踪) 框架梳理 作业一:光线生成 作业二:光线-三角 ...

  7. GAMES101作业6-BVH完成全过程

    目录 作业要求 Render.cpp TODO:需要的补充内容 Triangle.hpp 框架 Ray.hpp -> struct Ray Intersection.hpp -> stru ...

  8. games101——作业6

    文章目录 总览 开始实现 编译运行 代码框架 代码框架解析 作业代码 Render Triangle::getIntersection IntersectP getIntersection 进阶代码 ...

  9. GAMES101作业7提高-实现微表面模型你需要了解的知识

    目录 微表面材质模型 微平面理论 Microfacet Theory BSDF(浅浅的提一下) 微表面BRDF的实现 Cook-Torrance BRDF 漫反射的BRDF 镜面反射的BRDF 1 法 ...

最新文章

  1. windows上的svn服务器迁移到linux
  2. 在微信小程序上,帮助中心界面实现类似手风琴案例
  3. Python基础教程:高阶函数和函数嵌套
  4. Mac下安装SecureCRT客户端并激活
  5. 一个工作13年的SAP开发人员的回忆:电子科技大学2000级新生入学指南
  6. 将图的广度优先遍历在邻接矩阵和邻接表存储结构上分别实现_图解:什么是“图”?
  7. pov-inc_yourself劳自己-懒惰的设计师的POV和一些Figma
  8. Docker之Dockerfile详解
  9. iOS中Safari浏览器select下拉列表文字太长被截断的处理方法
  10. 先学python还是ros_ROS入门学习
  11. 博客园里输入latex公式
  12. MapBar纯绿色桌面版:小M
  13. Windows 10怎样不利用第三方付费软件实现对C盘的深度清理
  14. 定时任务Cron常用表达式
  15. 《Python股票量化交易从入门到实践》学习记录
  16. 北京思想聚合科技有限公司
  17. Matlab表格和时间表中的分组计算
  18. python程序执行完后重头开始做烧饼_从“程序员转行卖烧饼”想到IT人创业
  19. 采用直线逼近方式的圆弧插补
  20. Unity 判断是否为预制体的根节点

热门文章

  1. Django REST framework 序列化与请求响应
  2. (二)无状态的web应用(单py的Django占位图片服务器)
  3. 销售如何用套路运作一个项目?项目成功经验分享
  4. 【安全】WEB配置安全
  5. 梅科尔工作室-位青-鸿蒙笔记4
  6. 如何将写好的网页放在服务器上,怎么把网页放在云服务器上
  7. html 文本框输入,HTML文本框INPUT无法输入的解决
  8. NETBIOS名 和 Host名的不同
  9. 护卫神主机大师或者是主机管理系统中创建网站时提示开设失败的解决办法
  10. excel中添加、复制、删除下拉列表