Ray tracing in a weekend (十一)

Defocus Blur(散焦模糊)

在之前渲染所得的图像中所有物体都是“清晰”的,现在要让图像更趋近于真实的camera相片,即“有实有虚”。

camera.h

#define M_PI 3.1415926

#include"ray.h"
#include"random.h"




//ray的出发点限制在一个圆里,而非集中于一点
//先求出在z=0平面,以(0,0,0)为圆心的单位圆内的任意一点
//以便在后面进行上述操作(类似之前求以(0,0,0)为球心的单位球内一点)
vec3 random_in_unit_disk()
{
	vec3 p;
	do
	{
		p = 2.0*vec3(drand48(), drand48(), 0) - vec3(1, 1, 0);
	} while (dot(p, p) >= 1.0);
	return p;
}

//之前关于image plain的信息和viewing ray的生成
//都直接写在main里,现在要将它们封装进camera
//类里,毕竟image plain本来就是camera的可视范围
//而viewing ray也是由camera生成的
class camera
{
public:
	//vfov是角度(degree);aperture是光圈也就是镜头半径,决定了ray的出发点局限在多大的一个圈内;focus_dist是成像平面的变化比例
	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);
		//利用camera frame求出现在的lower_left_corner
		lower_left_corner = origin - half_width*focus_dist*u - half_height*focus_dist*v - focus_dist*w;
		//确定视野
		horizontal = 2 * half_width*focus_dist*u;//fov/视野的宽
		vertical = 2*half_height*focus_dist*v;//fov/视野的高
	}

	ray get_ray(float s, float t)
	{
		//将单位圆中任一点换为半径为镜头半径的圆内任一点
		vec3 rd = lens_radius*random_in_unit_disk();
		//将圆心变为camera frame的原点
		vec3 offset = rd.x()*u + rd.y()*v;
		return ray(origin+offset, lower_left_corner + s*horizontal + t*vertical - origin-offset);
	}
	vec3 origin;
	vec3 lower_left_corner;
	vec3 horizontal;
	vec3 vertical;
	vec3 u, v, w;//camera frame各轴
	float lens_radius;
};

RayTracer.cpp

#include"vector.h"
#include"ray.h"
#include"sphere.h"
#include"hitable_list.h"
#include"camera.h"
#include"material.h"
#include"random.h"
#include<random>
#include<cfloat>
#include<math.h>
#include<iostream>
#include<fstream>

using namespace std;

//此处的world就是把整个场景里的所有object视为一体(即hitable_list)
//depth是ray的传播深度,scatter一次加一
vec3 color(const ray& r,hitable *world,int depth)
{
	hit_record rec;
	//如果viewing ray(反向光线)与hitable object相交
	//tmin采用0.001是基于对showdow的考量,见fundamentals p86
	//然而当前还没有引入light,阴影部分是因为当光线到达此处时经历了太多次反射
	if (world->hit(r, 0.001, FLT_MAX, rec))
	{
		//当前还不能确定是difusse还是reflection,取决于hitable的material
		ray scattered;
		vec3 attenuation;//削弱
		//如果scatter次数小于50
		//并且ray接触的hitable object的material能成功调用scattered函数
		if (depth < 50 && rec.mat_ptr->scatter(r, rec, attenuation, scattered))
		{
			return attenuation*color(scattered, world, depth + 1);
		}
		else//scatter超过50次,能量全被吸完了;scattered函数调用失败
		{
			return vec3(0, 0, 0);//黑
		}
	}
	else//注意当前还是没有引入light,依然是由最后一条ray的direction的y值决定color
		//实际上可以认为是background在发光
	{
		vec3 unit_direction = unit_vector(r.direction());//得到单位方向向量,将y限定在-1至1之间
		float t = 0.5*(unit_direction.y() + 1.0);//间接用t代表y,将其限制在0至1之间
		return (1.0 - t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
		//所谓插值法,不同的ray对应的t不同,这些t决定了其对应的color为(1.0,1.0,1.0)和(0.5,0.7,1.0)之间某一RGB颜色
		//RGB各分量实际就是一个介于0.0至1.0的小数
	}
}

int main()
{
	int nx = 200;//200列
	int ny = 100;//100行
	int ns = 100;
	ofstream out("d:\\theFirstPpm.txt");
	out << "P3\n" << nx << " " << ny << "\n255" << endl;
	hitable *list[5];//我们自己定义world是什么,此处定义为两个sphere
	list[0] = new sphere(vec3(0, 0, -1), 0.5,new lambertian(vec3(0.1,0.2,0.5)));
	list[1] = new sphere(vec3(0, -100.5, -1), 100, new lambertian(vec3(0.8, 0.8, 0.0)));
	list[2] = new sphere(vec3(1, 0, -1), 0.5, new metal(vec3(0.8, 0.6, 0.2),0.3));
	list[3] = new sphere(vec3(-1, 0, -1), 0.5, new dielectric(1.5));
	list[4] = new sphere(vec3(-1, 0, -1), -0.45, new dielectric(1.5));
	hitable *world = new hitable_list(list, 5);//初始化world
	vec3 lookfrom(3, 3, 2);
	vec3 lookat(0, 0, -1);
	float dist_to_focus = (lookfrom - lookat).length();//焦距
	float aperture = 2.0;
	camera cam(lookfrom,lookat,vec3(0,1,0),20,float(nx)/float(ny),aperture,dist_to_focus);
	for (int j = ny - 1;j >= 0;j--)//行从上到下
	{
		for (int i = 0;i < nx;i++)//列从左到右
		{
			vec3 col(0, 0, 0);
			for (int s = 0;s < ns;s++)
			{
				float u = float(i + drand48()) / float(nx);
				float v = float(j + drand48()) / float(ny);
				ray r = cam.get_ray(u, v);
				vec3 p = r.point_at_parameter(2.0);
				col += color(r,world,0);
			}
			col /= float(ns);
			//进行gamma校正,一般来说取gamma=2,原理及公式见fundamentals p63
			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]);
			out << ir << " " << ig << " " << ib << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/UIUCGOGOGO/article/details/82970875