在我们的相机已经变得非常智能之后,是时候应该暂时休息一下了,我们用一个通过本渲染器能得到的最漂亮最梦幻的场景来为我们的初级阶段内容画上一个完美的句号。
在这之前还有一件事情要做,我们的相机太过完美,以至于现实世界中的相机有的缺点他都没有。为了我们能得到更真实的场景,我们甚至不得不想方设法模仿现实相机的某些特点。

传统相机的局限

现实中的相机可无法把它获取的所有光线都汇聚到一点,通过我们的逆光路模型来解释,就是它无法精准的控制发射光线的起点都重合在一起。
image.png
如上图所示,inside表示相机内部,outside是相机的外部。
相机通过镜头采集光线,镜头采集到的光线会被汇总,在镜头后的胶片上成像。在镜头之外,根据镜头凹透镜的焦距,在某个距离处采集到的光线都来自于一点,这个平面上的所有物体都处于完美对焦状态,而其他距离下的物体,都会因为光线无法精准落于一点而出现模糊。离完美对焦距离越远,就越模糊。

模拟相机散焦模糊效果

我们不需要模拟相机的内部,在《抗锯齿》那一章我们编写的多采样求平均的算法就可以完美模拟从镜头采集到成像的过程,现在,我们只需要改变我们光线的发射位置,假装我们的光线是从镜头上任意一点发射的即可。
image.png
具体要怎么做呢?
1.我们要如何模拟镜头呢?
我们可以以原来的固定光线发射点为圆心,找一个圆片,这个圆片和虚拟视口所在平面平行,和相机正对方向垂直。然后我们在这个圆片上随机找一个点,以这个点为起点发射光线(这个圆片就是虚拟相机的镜头)。这个圆片的大小对应着真实相机中的镜头光圈大小,圆片越大,散焦模糊效果就越大。
2.我们要如何模拟光线聚焦效果呢?
我们的光线是射向虚拟视口上的虚拟像素内的。在我们原来的设计里,虚拟视口和相机位置的距离始终为1,这意味着,我们的光线都会在镜头前方距离为1的平面上聚焦,如果我们需要让他能在任意位置聚焦,我们只需要改变虚拟视口距离相机的距离即可。
还记得我们是如何在代码中规定虚拟视口的位置的吗?
lower_left_corner = origin - horizontal/2 - vertical/2 - w;
如果我想把它推到和相机位置距离为2的平面上,我仅仅是修改w的系数就可以了吗?比如:
~~ lower_left_corner = origin - horizontal/2 - vertical/2 - 2*w; ~~
这样就足够了吗?不行,看下图:
image.png
图上描绘的是相机空间下VoW面上的截面,假设中间的绿色竖线就是原先的虚拟视口,它离相机的距离为1,现在我们要把它移动到右边的蓝色平面上,根据相似三角形,如果它离相机的距离改成2,那他的高度会同步扩大为2,且因为像素数量没变,每个虚拟像素的大小也会增加为2。这意味着我们的horizontal和vertical也得同步进行放大。
图中射向虚拟视口的光线假设命中了第x行,如果像素的大小扩大两倍,这根光线的延长线也会命中蓝色视口的第x行。如果像素没有放大,它将命中第2x行,这就不对了。
明白了以上两点,我们就可以开始写代码了。
更改相机代码,现在我们可以传入焦距和光圈大小:

  1. class camera {
  2. public:
  3. camera(
  4. point3 lookfrom,
  5. point3 lookat,
  6. vec3 vup,
  7. double vfov,
  8. double aspect_ratio,
  9. //光圈直径
  10. double aperture,
  11. //焦距
  12. double focus_dist
  13. ) {
  14. auto theta = degrees_to_radians(vfov);
  15. auto h = tan(theta/2);
  16. auto viewport_height = 2.0 * h;
  17. auto viewport_width = aspect_ratio * viewport_height;
  18. w = unit_vector(lookfrom - lookat);
  19. u = unit_vector(cross(vup, w));
  20. v = cross(w, u);
  21. origin = lookfrom;
  22. //我们要把虚拟视口推到离相机位置有focus_dist的距离的平面上。
  23. //原本的距离是1,距离变成原来的focus_dist倍。
  24. //所以,horizontal和vertical得同步乘以focus_dist倍。
  25. horizontal = focus_dist * viewport_width * u;
  26. vertical = focus_dist * viewport_height * v;
  27. //距离不再是w,而是focus_dist*w。
  28. lower_left_corner = origin - horizontal/2 - vertical/2 - focus_dist*w;
  29. //处理一下,拿到半径。
  30. lens_radius = aperture / 2;
  31. }
  32. ray get_ray(double s, double t) const {
  33. // 生成一个偏移值。
  34. // 这个random_in_unit_disk()函数会产生一个XoY平面上的以原点为圆心的单位圆片内随机一点。
  35. // 该函数我们之后给出。
  36. vec3 rd = lens_radius * random_in_unit_disk();
  37. // 把圆片从XoY平面,调整到uv平面上。这段结合后面给出的random_in_unit_disk函数一起看效果最佳。
  38. vec3 offset = u * rd.x() + v * rd.y();
  39. return ray(
  40. // 顶点加上偏移。
  41. origin + offset,
  42. // -offset使得光线还是朝虚拟视口上的当前像素上发射的。
  43. lower_left_corner + s*horizontal + t*vertical - origin - offset
  44. );
  45. }
  46. private:
  47. point3 origin;
  48. point3 lower_left_corner;
  49. vec3 horizontal;
  50. vec3 vertical;
  51. vec3 u, v, w;
  52. double lens_radius;
  53. };

把random_in_unit_disk函数写在vec3.h类的类外,这个函数负责生成一个XoY面上以原点为圆心的圆片内的随机一点:

  1. //XoY面上以原点为圆心的圆片内的随机一点。
  2. vec3 random_in_unit_disk() {
  3. while (true) {
  4. auto p = vec3(random_double(-1,1), random_double(-1,1), 0);
  5. if (p.length_squared() >= 1) continue;
  6. return p;
  7. }
  8. }

我们还是使用上一章的场景,但我们要调整相机,给一个很大的光圈直径,并且把对焦平面(虚拟视口)放在画面正中心的小球上:

  1. point3 lookfrom(3,3,2);
  2. point3 lookat(0,0,-1);
  3. vec3 vup(0,1,0);
  4. auto dist_to_focus = (lookfrom-lookat).length();
  5. auto aperture = 2.0;
  6. camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
  1. 你会得到这张图,我们的光圈很大,只要物体离虚拟视口有一点距离,就看不清了。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/25639626/1643976783038-8526d1b9-95c6-4cef-b5fc-413bf73fd84b.png#clientId=u1ae44729-72d5-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=205&id=uee1d1a3b&margin=%5Bobject%20Object%5D&name=image.png&originHeight=461&originWidth=812&originalType=binary&ratio=1&rotation=0&showTitle=false&size=164434&status=done&style=none&taskId=u2f027dfe-03c5-401b-952a-8561819218d&title=&width=360.8888888888889)

《蓝天下的我们》

我们现在来汇总我们在这十五篇文章中学到的所有知识,绘制一张图片,这将会是集大成之作,看代码:

  1. // 这个函数帮助生成一个丰富多彩的场景!!!
  2. hittable_list random_scene() {
  3. hittable_list world;
  4. auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
  5. //先来一个“地板”,它比我们之前创建过最大的球还要大十倍!!!
  6. world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));
  7. //在这个循环里我们将生成数个小球!!!
  8. for (int a = -11; a < 11; a++) {
  9. for (int b = -11; b < 11; b++) {
  10. //先进行一次随机采样,这次采样用来决定本轮循环生成的小球的材质!!!
  11. auto choose_mat = random_double();
  12. //在一个y=0.2这个平面上的一块方形区域里随机找一个点,作为这颗小球的球心!!!
  13. point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
  14. //我们腾出一个位置,让生成的这些小球离(4, 0.2, 0)这个点远一点!!!
  15. //因为这里我们要放置一个非常明显的大球,如果出现球与球重合就会影响美观了!!!
  16. if ((center - point3(4, 0.2, 0)).length() > 0.9) {
  17. //定义一个智能指针,但先别急着决定指向什么类型的材质对象!!!
  18. shared_ptr<material> sphere_material;
  19. if (choose_mat < 0.8) {//我们的材质有百分之八十的概率会是磨砂材质。
  20. auto albedo = color::random() * color::random();
  21. sphere_material = make_shared<lambertian>(albedo);
  22. world.add(make_shared<sphere>(center, 0.2, sphere_material));
  23. }
  24. else if (choose_mat < 0.95) {//我们的材质有百分之十五的概率会是金属材质。
  25. auto albedo = color::random(0.5, 1);
  26. auto fuzz = random_double(0, 0.5);
  27. sphere_material = make_shared<metal>(albedo, fuzz);
  28. world.add(make_shared<sphere>(center, 0.2, sphere_material));
  29. } else {//我们的材质有百分之五的概率会是玻璃材质。
  30. sphere_material = make_shared<dielectric>(1.5);
  31. world.add(make_shared<sphere>(center, 0.2, sphere_material));
  32. }
  33. }
  34. }
  35. }
  36. //三颗站在C位的大球!!!
  37. auto material1 = make_shared<dielectric>(1.5);
  38. world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
  39. auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
  40. world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
  41. auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
  42. world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
  43. return world;
  44. }
  45. int main() {
  46. //调整图片参数
  47. const auto aspect_ratio = 3.0 / 2.0;
  48. //分辨率调高点,玩票大的!!!
  49. const int image_width = 1200;
  50. const int image_height = static_cast<int>(image_width / aspect_ratio);
  51. //拉高采样率会增加最终艺术品的成色!!!
  52. const int samples_per_pixel = 500;
  53. const int max_depth = 50;
  54. //调用函数生成一个有着许多随机小球的场景!!!
  55. auto world = random_scene();
  56. // 放置相机
  57. point3 lookfrom(13,2,3);
  58. point3 lookat(0,0,0);
  59. vec3 vup(0,1,0);
  60. auto dist_to_focus = 10.0;
  61. //这次是小光圈。
  62. auto aperture = 0.1;
  63. camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
  64. // renderloop
  65. std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
  66. for (int j = image_height-1; j >= 0; --j) {
  67. ...
  68. }
  1. 你会得到:<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/25639626/1644241403158-330630b4-149f-456e-9abd-a749828948f6.png#clientId=uec382a0f-f9ee-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=360&id=uda441112&margin=%5Bobject%20Object%5D&name=image.png&originHeight=811&originWidth=1216&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1043260&status=done&style=none&taskId=ucd8ae5c9-a83d-429b-bc68-343985f8026&title=&width=540.4444444444445)<br />你可以给你的随机数生成器其他的种子,来改变小球的随机位置,直到你得到满意的结果。

参考文献

https://raytracing.github.io/books/RayTracingInOneWeekend.html
参考自《RayTracingInOneWeekend》第12节。