本文主要参照 Ray Tracing: The Next Week,其中只是主要精炼光追相关理论,具体实现可参照原文。

一、动态模糊

如果要实动态模糊的效果,可以将原来的球体,改成球心在某条线段上的球体,例如代码中将sphere修改为moving_sphere,并将原来的center修改成center0, center1。每条射线都会有一个随机的系数t(范围0到1),使用t插值center0, center1,得到这条线段上的某个点作为moving_sphere的球心。

不同的射线会有不同的t,不同的t插值出来的球心位置就会不同。对某个像素随机采样ns次之后,这个像素就会产生ns条t值随机的射线,这ns条射线对应位置不同的球,从而渲染出一个模糊的像素。渲染这条线段附近区域的包含该球体的所有像素,就能看到球在这条线段上运动的模糊效果,从而模拟相机快门的长曝光时间。

ray增加参数:

class ray
{public:vec3 A; //起点vec3 B; //方向float _time;ray() {}ray(const vec3 &a, const vec3 &b, float ti=0.0f){A = a;B = b;_time = ti;}float time() const { return _time; }vec3 origin() const { return A; }vec3 direction() const { return B; }vec3 point_at_parameter(float t) const { return A + t * B; } //终点的坐标
};

camera增加随机产生time赋值给ray:

class camera
{public:vec3 origin;vec3 lower_left_corner;vec3 horizontal;vec3 vertical;vec3 u, v, w;float lens_radius;float time0, time1;//lookfrom为相机位置,lookat为观察位置,vup传(0,1,0),vfov为视野角度,aspect为屏幕宽高比//aperture为光圈大小,focus_dist为相机到观察点的距离camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect, float aperture, float focus_dist, float t0, float t1){time0 = t0;time1 = t1;lens_radius = aperture / 2;float theta = vfov * M_PI / 180;float half_height = tan(theta / 2);float half_width = aspect * half_height;origin = lookfrom;w = unit_vector(lookfrom - lookat);u = unit_vector(cross(vup, w));v = cross(w, u);lower_left_corner = origin - half_width * focus_dist * u - half_height * focus_dist * v - focus_dist * w;horizontal = 2 * half_width * focus_dist * u;vertical = 2 * half_height * focus_dist * v;}ray get_ray(float s, float t){vec3 rd = lens_radius * random_in_unit_disk();vec3 offset = u * rd.x() + v * rd.y();float time = time0 + random_double() * (time1 - time0);return ray(origin + offset, lower_left_corner + s * horizontal + t * vertical - origin - offset, time);}};

moving_sphere类:

class moving_sphere : public hittable
{public:vec3 center0, center1;float time0, time1;float radius;material *mat_ptr; /* NEW */vec3 center(float time) const{return center0 + ((time - time0) / (time1 - time0)) * (center1 - center0);}moving_sphere() {}moving_sphere(vec3 cen0, vec3 cen1, double t0, double t1, double r, material *m): center0(cen0), center1(cen1), time0(t0), time1(t1), radius(r), mat_ptr(m){};//如果命中了,命中记录保存到recvirtual bool hit(const ray &r, float t_min, float t_max, hit_record &rec) const{vec3 oc = r.origin() - center(r.time());float a = dot(r.direction(), r.direction());float b = dot(oc, r.direction());float c = dot(oc, oc) - radius * radius;float discriminant = b * b - a * c;if (discriminant > 0){float temp = (-b - sqrt(discriminant)) / a; //小实数根if (temp < t_max && temp > t_min){rec.t = temp;rec.p = r.point_at_parameter(rec.t);rec.normal = (rec.p - center(r.time())) / radius;rec.mat_ptr = mat_ptr; /* NEW */return true;}temp = (-b + sqrt(discriminant)) / a; //大实数根if (temp < t_max && temp > t_min){rec.t = temp;rec.p = r.point_at_parameter(rec.t);rec.normal = (rec.p - center(r.time())) / radius;rec.mat_ptr = mat_ptr; /* NEW */return true;}}return false;}
};

放置球体:

camera cam(lookfrom, lookat, vec3(0, 1, 0), 20, float(nx) / float(ny), aperture, dist_to_focus,0,0.5);hittable *random_scene() {int n = 500;hittable **list = new hittable*[n + 1];list[0] = new sphere(vec3(0, -1000, 0), 1000, new lambertian(vec3(0.5, 0.5, 0.5)));int i = 1;for (int a = -11; a < 11; a++) {for (int b = -11; b < 11; b++) {float choose_mat = random_double();vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());if ((center - vec3(4, 0.2, 0)).length() > 0.9) {if (choose_mat < 0.8) {  // diffuseif (b % 2 == 0) //动态模糊的球体{auto center2 = center + vec3(0, random_double(), 0);list[i++] = new moving_sphere(center, center2, 0.0, 1.0, 0.2,new lambertian(vec3(random_double()*random_double(),random_double()*random_double(),random_double()*random_double())));}else{list[i++] = new sphere(center, 0.2,new lambertian(vec3(random_double()*random_double(),random_double()*random_double(),random_double()*random_double())));}}else if (choose_mat < 0.95) { // metallist[i++] = new sphere(center, 0.2,new metal(vec3(0.5*(1 + random_double()),0.5*(1 + random_double()),0.5*(1 + random_double())),0.5*random_double()));}else {  // glasslist[i++] = new sphere(center, 0.2, new dielectric(1.5));}}}}list[i++] = new sphere(vec3(0, 1, 0), 1.0, new dielectric(1.5));list[i++] = new sphere(vec3(-4, 1, 0), 1.0, new lambertian(vec3(0.4, 0.2, 0.1)));list[i++] = new sphere(vec3(4, 1, 0), 1.0, new metal(vec3(0.7, 0.6, 0.5), 0.0));return new hittable_list(list, i);
}

【光线追踪系列九】物体动态模糊相关推荐

  1. 【光线追踪系列十一】纹理贴图

    本文主要参照 Ray Tracing: The Next Week,其中只是主要精炼光追相关理论,具体实现可参照原文. 一.纹理实现 实现之前,你应该已经充分理解了[光线追踪系列六]反射与金属类特性. ...

  2. 【光线追踪系列十】光追加速结构(BVH树)

    本文主要参照 Ray Tracing: The Next Week,其中只是主要精炼光追相关理论,具体实现可参照原文. 经过之前几部分的光追实现,我们大致可以实现了光追的效果,但其中有一个致命的bug ...

  3. 像素级动态模糊(Pixel Motion Blur)

    像素级动态模糊(Pixel Motion Blur) 动态模糊近几年广泛应用于游戏制作的一种特效,可以使得游戏所呈现出的运动画面更接近于真实相机所拍摄出的效果.      在真实世界中,运动模糊是指在 ...

  4. Python数据分析学习系列 九 绘图和可视化

    Python数据分析学习系列 九 绘图和可视化 资料转自(GitHub地址):https://github.com/wesm/pydata-book 有需要的朋友可以自行去github下载 信息可视化 ...

  5. Unity游戏画面参数解析与应用:垂直同步、动态模糊、抗锯齿

    前言 最近会在B站刷到一些关于 30帧暴涨90帧! 高 中 低端显卡运行3A大作优化指南[干货向] 游戏画质设置教程 等等这样关于画面与性能调整的的视频,看完之后受益良多,UP主们经过实际测试获取到宝 ...

  6. Unity Shader学习:动态模糊(shutter angle方式)

    Unity Shader学习:动态模糊 动态模糊一般有帧混合和motion vector两种,这里主要介绍motion vector的方法. Keijiro源码:https://github.com/ ...

  7. Ae动态模糊插件ReelSmart Motion Blur

    REVisionFX ReelSmart Motion Blur for Mac是一款运行在After Effects上的运动模糊插件,这款RSMB动态模糊插件支持自动跟踪动画运动的像素,然后添加自然 ...

  8. 机器学习、数据科学与金融行业 系列九:巴塞尔协议解读(1)介绍

    机器学习.数据科学与金融行业 系列九:巴塞尔协议解读(1)介绍 本篇不同于本系列文章中的其他文章,本文只是笔者研读巴塞尔标准后所整理的其主要内容,侧重于计算RWA方面.现分享出来,请读者指正.     ...

  9. Android音视频学习系列(九) — Android端实现rtmp推流

    系列文章 Android音视频学习系列(一) - JNI从入门到精通 Android音视频学习系列(二) - 交叉编译动态库.静态库的入门 Android音视频学习系列(三) - Shell脚本入门 ...

最新文章

  1. 2021-07-01带Left Join的SQL语句的执行顺序
  2. Kendo UI 简单使用
  3. 悬浮框_纯HTML实现某宝优惠券、商品列表和活动悬浮等布局(文末有源码)
  4. 马云缺席的一个半小时,李彦宏和马化腾都聊了什么
  5. ImageLoader displayers 之CircleBitmapDisplayer
  6. 【终极方法】Syntax error on tokens, delete these tokens
  7. Regarding empty field check in business document save
  8. 四川省中职计算机考试题,四川省计算机等级考试模拟试题(一级)
  9. 香甜的黄油(信息学奥赛一本通-T1345)
  10. 科学计算器 c语言源代码,科学计算器C语言代码
  11. 陕西国防学院计算机系网络教研室,陕西国防工业职业技术学院:全卫强副院长赴各院部调研教师发展工作...
  12. SVG中的text文字高度ascentbaselinedescent(资料及测试)
  13. 电脑开机后没反应,如何解决?
  14. 国家电网一二次融合配电终端(FTU)发展新方向馈线自动化:具备集中型馈线自动化/就地型馈线自动化包括电压时间型、电压电流型、自适应综合型)及零序电流,零序电压或外施信号法的单相接地故障选线功能
  15. 远程调用中间件(RPC)
  16. 第八篇order订单专题(5)限价止损单、跟踪止损单、跟踪限价止损单讲解
  17. android通知栏自定义软件,免root状态栏美化神器
  18. 快消品企业营销费用管理的困惑
  19. CSS库 Tailwind
  20. 磊科路由虚拟服务器设置,磊科(Netcore)NW717端口映射怎么设置教程

热门文章

  1. 不能打开数据库 ''。应用程序可能无法识别该数据库,或文件可能损坏。
  2. 2022金三银四前端面试题预告
  3. 你还不知道 BTree,B-Tree,B+Tree 的区别吗?
  4. 电力系统频率 matlab,低频减载的MATLAB仿真模型
  5. Android studio 安装配置SDK
  6. hao123.com上的邮箱登录
  7. 第三代电力电子半导体:SiC MOSFET学习笔记(四)SiC MOSFET传统驱动电路保护
  8. 百度地图、高德地图和腾讯地图定位不准确的解决方案
  9. Python——保存图片到本地
  10. (转载)JAVA小知识