1. 程式人生 > >Ray tracing in a weekend (十)

Ray tracing in a weekend (十)

Positionable Camera

#ifndef CAMERAH
#define CAMERAH
#define M_PI 3.1415926

#include"ray.h"

//之前關於image plain的資訊和viewing ray的生成
//都直接寫在main裡,現在要將它們封裝進camera
//類裡,畢竟image plain本來就是camera的可視範圍
//而viewing ray也是由camera生成的
class camera
{
public:
	//vfov是角度/degree
	camera(vec3 lookfrom,vec3 lookat,vec3 vup,float vfov,float aspect)
	{
		vec3 u, v, w;//camera frame中各軸(方向向量)
		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*u - half_height*v - w;
		//確定視野
		horizontal = 2 * half_width*u;//fov/視野的寬
		vertical = 2*half_height*v;//fov/視野的高
	}

	ray get_ray(float u, float v)
	{
		//直接寫在main裡時並沒有最後減origin,那是因為origin為(0.0,0.0,0.0)
		//然而實際上是需要的
		return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
	}
	vec3 origin;
	vec3 lower_left_corner;
	vec3 horizontal;
	vec3 vertical;
};
#include "stdafx.h"
#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
	camera cam(vec3(-2,2,1),vec3(0,0,-1),vec3(0,1,0),90,float(nx)/float(ny));
	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;
}