Ray tracing in a weekend (七)

Diffuse materials(漫反射材质)

实现原理(还没有引入light)

首先要将object的shape和material分开来理解。一个具有diffuse materials的object实际就是说与其surface相交的光线会以随机的方向反射(也可能被完全吸收,阴影的来源),从视线(viewing ray)的角度来说,也就是接触到其surface的viewing ray会以随机的方向反射。Diffuse objects that don’t emit light merely take on the color of their surroundings, but they modulate that with their own intrinsic color.

如上图,只要再写一个以origin(0,0,0)为球心,随机生成该单位球内一点的函数(random_in_unit_sphere),结合点P就可以构造出反射Ray。

在color函数中,不再像前面那样简单地以点P处的法向量通过映射得到color,而是分为以下两种情况:

1.ray a接触到object,求出反射ray b,ray a生成的color便是ray b生成color的50%,依次递推,直至最后一条反射ray不再与object相交(实际上真正在乎的是第一条和最后一条ray生成的color)。

2.ray没有接触到object,直接通过该ray的direction.y()求出一个RGB color。

gamma校正

最后呈现color之前要进行gamma校正。

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

using namespace std;

float drand48()//伪
{
	return rand() % 10000 / 10000.0;
}
vec3 random_in_unit_sphere()
{
	vec3 p;
	do
	{
		//得到x,y,z坐标都介于-1和1的点
		p = 2.0*vec3(drand48(), drand48(), drand48()) - vec3(1, 1, 1);
	} while (p.squared_length() >= 1.0);
		//随机得到的点如果不在以origin为球心的单位球里,便一直进行循环
	return p;
}
//此处的world就是把整个场景里的所有object视为一体(即hitable_list)
vec3 color(const ray& r,hitable *world)
{
	hit_record rec;
	//如果viewing ray(反向光线)与hitable object相交
	//tmin采用0.001是基于对showdow的考量,见fundamentals p86
	//然而当前还没有引入light,阴影部分是因为当光线到达此处时经历了太多次反射
	if (world->hit(r, 0.001, FLT_MAX, rec))
	{
		//计算出与点P相切的单位球内的随机一点(实际就是求出ray的反射方向)
		vec3 target = rec.p + rec.normal + random_in_unit_sphere();
		//视线追踪实际就是反向光线追踪,这里的0.5可以理解为
		//光线每接触到一次surface便损失50%的能量(被surface吸收了)
		//该算法用到了递归思想:函数自己调用自己
		return 0.5*color(ray(rec.p, target - rec.p), world);
	}
	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[2];//我们自己定义world是什么,此处定义为两个sphere
	list[0] = new sphere(vec3(0, 0, -1), 0.5);
	list[1] = new sphere(vec3(0, -100.5, -1), 100);
	hitable *world = new hitable_list(list, 2);//初始化world
	camera cam;
	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);
			}
			col /= float(ns);
			//进行gamma校正,一般来说取gamma=2,原理及公式见fundamentals p63
			col = vec3(sqrt(col.x()), sqrt(col.y()), sqrt(col.z()));
			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/82856373