【Ray Tracing in One Weekend】(ch11&12)景深效果与封面图的渲染

Chapter 11&12: Defocus Blur & Final Scene

在 Camera.h 中修改如下:

#pragma once
#define _USE_MATH_DEFINES
#include "Ray.h"
#include <math.h>

//在z=0平面上产生一个“起点在原点,长度小于1,方向随机”的向量。为什么是z=0平面,这个和相机的倾斜方向有关。(相机的倾斜方向为view up (简称vup,一般设置为(0,1,0)))
Vec3 RandomInUnitDisk() {
    Vec3 p;
    do {
        p = 2.0*Vec3((rand() % (100) / (float)(100)), (rand() % (100) / (float)(100)), 0) - Vec3(1, 1, 0);
    } while (dot(p, p) >= 1.0);
    return p;
}

class Camera 
{
public:
    //vfov: top to bottom in degrees
    Camera(Vec3 lookfrom, Vec3 lookat, Vec3 vup, float vfov, float aspect, float aperture, float focus_dist)
    {
        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 = focus_dist * 2 * half_width*u;
        vertical = focus_dist * 2 * half_height*v;
    }

    Ray getRay(float s, float t) 
    {
        Vec3 rd = lens_radius * RandomInUnitDisk();
        Vec3 offset = u * rd.x() + v * rd.y();
        return Ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset);
    }

    Vec3 lower_left_corner;
    Vec3 origin;
    Vec3 horizontal;
    Vec3 vertical;
    Vec3 u, v, w;
    float lens_radius;
};

Main.cpp 中:

Hitable *RandomScene() {
    int n = 500;
    Hitable **list = new Hitable *[n + 1];
    /*定义一个包含n+1个元素的数组,数组的每个元素是指向hitable对象的指针。然后将数组的指针赋值给list。所以,list是指针的指针。*/
    list[0] = new Sphere(Vec3(0, -1000, 0), 1000, new Lambertian(Vec3(0.5, 0.5, 0.5)));
    /*先创建一个中心在(0,-1000,0)半径为1000的超大漫射球,将其指针保存在list的第一个元素中。*/
    int i = 1;
    for (int a = -11; a < 11; a++) {
        for (int b = -11; b < 11; b++) {
            /*两个for循环中会产生(11+11)*(11+11)=484个随机小球*/
            float choose_mat = (rand() % (100) / (float)(100));
            /*产生一个(0,1)的随机数,作为设置小球材料的阀值*/
            Vec3 center(a + 0.9*(rand() % (100) / (float)(100)), 0.2,b + 0.9*(rand() % (100) / (float)(100)));
            /*” a+0.9*(rand()%(100)/(float)(100))”配合[-11,11]产生(-11,11)之间的随机数,而不是[-11,11)之间的22个整数。使得球心的x,z坐标是(-11,11)之间的随机数*/
            if ((center - Vec3(4, 0.2, 0)).length() > 0.9) {
                /*避免小球的位置和最前面的大球的位置太靠近*/
                if (choose_mat < 0.8) {     //diffuse  
                                            /*材料阀值小于0.8,则设置为漫反射球,漫反射球的衰减系数x,y,z都是(0,1)之间的随机数的平方*/
                    list[i++] = new Sphere(center, 0.2,
                        new Lambertian(Vec3(
                        (rand() % (100) / (float)(100))*(rand() % (100) / (float)(100)),
                            (rand() % (100) / (float)(100))*(rand() % (100) / (float)(100)),
                            (rand() % (100) / (float)(100))*(rand() % (100) / (float)(100)))));
                }
                else if (choose_mat < 0.95) {
                    /*材料阀值大于等于0.8小于0.95,则设置为镜面反射球,镜面反射球的衰减系数x,y,z及模糊系数都是(0,1)之间的随机数加一再除以2*/
                    list[i++] = new Sphere(center, 0.2,
                        new Metal(Vec3(0.5*(1 + (rand() % (100) / (float)(100))),
                            0.5*(1 + (rand() % (100) / (float)(100))),
                            0.5*(1 + (rand() % (100) / (float)(100)))),
                            0.5*(1 + (rand() % (100) / (float)(100)))));
                }
                else {
                    /*材料阀值大于等于0.95,则设置为介质球*/
                    list[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 HitableList(list, i);
}

int main()
{
    ofstream outfile;
    outfile.open("ch12_HighImage.ppm");

    int nx = 2000;
    int ny = 1000;
    //采样次数
    int ns = 100;
    outfile << "P3\n" << nx << " " << ny << "\n255\n";


    Hitable *world = RandomScene();

    Vec3 lookfrom(13.0f, 2.0f, 3.0f);
    Vec3 lookat(0.0f, 0.0f, 0.0f);
    float dist_to_focus = 10.0f;
    float aperture = 0.1f;

    Camera cam(lookfrom, lookat , Vec3(0.0f, 1.0f, 0.0f), 20, float(nx) / float(ny), aperture, dist_to_focus);

    //随机数引擎
    default_random_engine reng;
    uniform_real_distribution<float> uni_dist(0.0f, 1.0f);

    for (int j = ny - 1; j >= 0; j--)
    {
        for (int i = 0; i < nx; i++)
        {
            Vec3 col(0.0f, 0.0f, 0.0f);
            //每个区域采样ns次
            for (int s = 0; s < ns; s++)
            {
                float u = float(i + uni_dist(reng)) / float(nx);
                float v = float(j + uni_dist(reng)) / float(ny);
                Ray r = cam.getRay(u,v);
                //Vec3 p = r.point_at_parameter(2.0);
                //将本区域((u,v)到(u+1,v+1))的颜色值累加
                col += Color(r, world, 0);
            }
            //获得区域的颜色均值
            col /= float(ns);
            //gamma矫正
            col = Vec3(sqrt(col[0]), sqrt(col[1]), sqrt(col[2]));
            int ir = int(255.99*col[0]);
            int ig = int(255.99*col[1]);
            int ib = int(255.99*col[2]);
            outfile << ir << " " << ig << " " << ib << "\n";
        }
    }
    outfile.close();
    return 0;
}

最终效果图:(2000px*2000px高清版,渲染了近30个小时)

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值