代码资源下载

-------------------------------------------------------------------------------------------------------------------------------

总体结构框架:

本节采用的光线追踪算法是Whitted 发明的。 trace the reflected rays as they bounce between reflective objects in the scene. 具体分为下面5个步骤:

1.添加max_depth 标示最大碰撞次数;

2.ray tracer 记录光线碰撞次数

3. 实现一个新的追踪类whitted, 该类中 trace_ray 使用记录

4.使用 PerfectSpecular BRDF

5 implement 至少一个 reflective material constructs the reflected rays and ray traces them

vp.set_hres(600);
vp.set_vres(600);
vp.set_samples(num_samples);
vp.set_max_depth(2);    tracer_ptr = new Whitted(this);
background_color = black;MultiJittered* sampler_ptr = new MultiJittered(num_samples);

返回黑色当深度值大于 max_depth 并且把 它存储在 ShadeRec 对象中。 这个函数循环调用。

RGBColor
Whitted::trace_ray(const Ray ray, const int depth) const { if (depth > world_ptr->vp.max_depth) return(black); else { ShadeRec sr(world_ptr->hit_objects(ray));    if (sr.hit_an_object) { sr.depth = depth; sr.ray = ray;    return (sr.material_ptr->shade(sr));   } else return (world_ptr->background_color); }
}

定义一个A Reflective Material 材质,继承Phong 模型,这样可以使用 ambient,diffuse and specular direct illumination。

该函数中也是循环调用了 trace_ray , 因为Whitted::trace_ray中也是循环调用的

RGBColor
Reflective::shade(ShadeRec& sr) {    RGBColor L(Phong::shade(sr));  // direct illumination Vector3D wo = -sr.ray.d; Vector3D wi;    RGBColor fr = reflective_brdf->sample_f(sr, wo, wi); Ray reflected_ray(sr.hit_point, wi); reflected_ray.depth = sr.depth + 1; L += fr * sr.w.tracer_ptr->trace_ray(reflected_ray, sr.depth + 1) * (sr.normal * wi); return (L);
}

Ply Files:

PLY 文件是Greg Turk 在90年代中期首创用于存储多边形meshes 。这种文件格式可以是ASCII 或者 二进制,本例子中使用的PLY 文件用于存储三角形坐标(x,y,z)和有索引信息的三角形面片。如下,3 0 1 2 的3 表示有3 个顶点,0,1,2 表示顶点索引

Computing the Normals使用顶点周围的三角形,按相邻三角形信息可以优化算法for each vertex{normal = zero normalfor each triangle that shares the vertexadd the triangle normal to normalnormalize the normal}add the normal to the mesh

multi-Jittered Sampling

为什么需要使用 采样呢?

1. avoid aliasing when samping pixels

2. Cameras with a finite-area lens are used to show depth of field, the lens must be samples

3.to represent area lights and soft shadows more accurately, we must sample the light suiraces

4.Global illumination and glossy reflection requires sampling of BRDFs ans BTDFs

本质上说,该技术结合了 jittering 和 n-rooks 采样技术

16 * 16 的grid,分为 4 * 4的 grid,用随机方法初始化每个 4*4 的 grid jittered 分配,也是 n-rooks 分配方法

Pinhole

Camera Arrangements

每个相机都有自己的 look-at 点 , 所以视线方向是平行的(with toe-in, the left and right cameras have a common look-at point )。view planes 都是一致的

两个窗口也是一致的,这点要求摄像机使用非对称的 view frustums 视体锥

这样,左右摄像机的window需要做偏移,如下

pinhole_ptr->set_eye(-6, 5, 11);
pinhole_ptr->set_lookat(0 , 0, 0);
pinhole_ptr->set_view_distance(5200);
pinhole_ptr->compute_uvw();
set_camera(pinhole_ptr); 

The purpose of each shading function is to compute the exitant  radiance at the hit point in the w0 direction

we specify a matte materail with an ambient reflection coefficient ka, a diffuse reflection coefficient kd, and

a diffuse color cd.

The Matte:: shade function in listing computes the ambient illumination and then loops over the lights to compute

the direct diffuse illumination.   the ambient BRDF calls rho and the diffuse BRDF callls f.   先计算漫反射,然后计算

diffuse specular reflection.

RGBColor
Matte::shade(ShadeRec& sr) {Vector3D    wo          = -sr.ray.d;RGBColor   L           = ambient_brdf->rho(sr, wo) * sr.w.ambient_ptr->L(sr);int        num_lights  = sr.w.lights.size();for (int j = 0; j < num_lights; j++) {Vector3D wi = sr.w.lights[j]->get_direction(sr);float ndotwi = sr.normal * wi;if (ndotwi > 0.0) {bool in_shadow = false;if (sr.w.lights[j]->casts_shadows()) {Ray shadowRay(sr.hit_point, wi);in_shadow = sr.w.lights[j]->in_shadow(shadowRay, sr);}if (!in_shadow)L += diffuse_brdf->f(sr, wo, wi) * sr.w.lights[j]->L(sr) * ndotwi;}}return (L);
}

Perfect Diffuse DRBF: where incident radiance is scattered equally in all directions.The function f returns the BRDF itself, unless it contains a delta function.

The function sample_f is use to compute the direction of reflected rays for simulating reflective materials and diffuse-diffuse light transport.

The function rho returns the bihemispherical reflectance phh.  in plain English, it means that the incoming radiance is the same from all directions and doesn’t vary with position.

in Lambertian, phh = kd * cd.

【Ray Trace from Groud Up】光线追踪代码实现解析相关推荐

  1. 带你从零开始徒手撸光线追踪代码(1)—— Ray Tracing in One Weekend

    前言   开坑一个新系列,由笔者带你不用api,只用最基本的C++语法写光线追踪,本系列可以看做是对<Ray Tracing in One Weekend>的翻译,当然原文虽然是英文,但有 ...

  2. 真分数c语言,C语言列出真分数序列代码及解析

    原标题:C语言列出真分数序列代码及解析 按递增顺序依次列出所有分母为60,分子小于60的最简分数. 分子.分母只有公因数1的分数叫做最简分数或者说分子和分母是互质数的分数,叫做最简分数,又称既约分数, ...

  3. 基于分类任务的信号(EEG)处理--代码分步解析

    本文由网友Jon_Snow_Stark授权分享 本篇文章是对作者对另一篇文章<基于分类任务的信号(EEG)处理>的扩展,之前文章是从宏观方面介绍利用EEG信号进行分类任务,本文利用详细的代 ...

  4. 【深度学习】RetinaNet 代码完全解析

    前言 本文就是大名鼎鼎的focalloss中提出的网络,其基本结构backbone+fpn+head也是目前目标检测算法的标准结构.RetinaNet凭借结构精简,清晰明了.可扩展性强.效果优秀,成为 ...

  5. Java打印整数的二进制表示(代码与解析)

    Java打印整数的二进制表示(代码与解析) int a=-99; for(int i=0;i<32;i++){int t=(a & 0x80000000>>>i)> ...

  6. Qt Creator使用Clang代码模型解析C ++文件

    Qt Creator使用Clang代码模型解析C ++文件 使用Clang代码模型解析C ++文件 关于Clang代码模型 配置C语代码模型 lang检查 在项目级别指定Clang代码模型设置 使用编 ...

  7. 如何在golang代码里面解析容器镜像

    简介:容器镜像在我们日常的开发工作中占据着极其重要的位置.通常情况下我们是将应用程序打包到容器镜像并上传到镜像仓库中,在生产环境将其拉取下来.然后用 docker/containerd 等容器运行时将 ...

  8. Vision Transformer(ViT)PyTorch代码全解析(附图解)

    Vision Transformer(ViT)PyTorch代码全解析 最近CV领域的Vision Transformer将在NLP领域的Transormer结果借鉴过来,屠杀了各大CV榜单.本文将根 ...

  9. python代码大全中文注释_零基础小白必看篇:Python代码注释规范代码实例解析操作(收藏)...

    本文内容主要介绍了Python代码注释规范代码实例解析,通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下!!! 一.代码注释介绍 注释就是对代码的解释和说明 ...

最新文章

  1. PHP复制和移动目录
  2. webpack 单独打包指定JS文件
  3. springboot 初始化线程池_springboot项目中线程池的使用
  4. android 反射调用方法可不可以重载,使用Java进行反射投射和重载方法调度
  5. HH SaaS电商系统的入库功能模块设计
  6. 代码扫描工具测试覆盖率工具
  7. 大数——大数阶乘(hdu1042)
  8. leetcode 354. 俄罗斯套娃信封问题(二维排序有关)
  9. Spring Boot Controller层单元测试
  10. TeamViewer最新版本TV 13介绍
  11. 计算机屏幕上的显示记录,什么样的桌面日历便笺既可以显示日期又可以提醒我所记录的时间表...
  12. 计算机毕业设计ssm龙腾集团员工信息管理系统39r5l系统+程序+源码+lw+远程部署
  13. PPT导出图片质量太差?简单操作直接导出印刷质地图片
  14. 联想笔记本G50-70无线网卡问题
  15. vivo计算机的隐藏功能教程,Vivo手机的5个隐藏功能,真的很实用,一定要打开
  16. 什么是 WPS(Wi-Fi Protected Setup)
  17. 全球及中国远程浏览器隔离解决方案行业投资分析与前景战略建议报告2022版
  18. XHR-XMLHttpRequest
  19. java生成随机邮箱_Java随机生成姓名、邮箱、手机号码
  20. 互联网医院网络安全等保建设方案

热门文章

  1. python学习之路:期权定价与python实现
  2. vue中使用海康实时监控详细代码
  3. 状态空间表示法----野人与修道士
  4. oracle 12c 安装scott,Oracle 12c中添加scott用户的方法
  5. 计算机专业买什么笔记本牌子,计算机专业买什么笔记本
  6. 为增强供应链安全,美国将设立“国家供应链完整性之月”
  7. 网络 CIDR 子网掩码 可用主机数计算
  8. Bias/variance tradeoff
  9. Image Processing Unit(IPU)简介
  10. 刻录ubuntu优盘启动遇到的问题及解决方法