Ray tracing in a weekend (二)

 ray.h

#ifndef RAYH
#define RAYH
#include"vector.h"

class ray//只存储原点和方向信息,线上具体一点用函数来表示
{
public:
	ray() {}
	ray(const vec3 &a, const vec3 &b):A(a),B(b){}
	vec3 origin() const { return A; }
	vec3 direction() const { return B; }
	vec3 point_at_parameter(float t) const { return A + t*B; }

	vec3 A;
	vec3 B;
};
#endif // !RAYH

 vector.h

#include<math.h>
#include<iostream>

class vec3//可以用来表示由三维向量携带的信息,如点、方向、位移、RGB等
{
public:
	vec3() {};//默认构造函数
	vec3(float e0, float e1, float e2) { e[0] = e0;e[1] = e1;e[2] = e2; }//构造函数
	//内联可以节省函数调用的消耗										 //内联可以节省函数调用的消耗
	inline float x() const { return e[0]; }//定义在类内的成员函数是默认内联的,而定义在类外的则必须显示内联
	inline float y() const { return e[1]; }
	inline float z() const { return e[2]; }
	inline float r() const { return e[0]; }
	inline float g() const { return e[1]; }
	inline float b() const { return e[2]; }

	inline const vec3& operator+() const { return *this; }//这里是取正运算而不是加法运算
	inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }//取负运算
	inline float operator[](int i) const { return e[i]; }//重载下标运算符
	//此处是常量成员函数的版本,当常量成员函数调用时返回的是内部元素的一个拷贝,不会改变自身
	inline float& operator[](int i) { return e[i]; }//普通版本返回的是内部元素的一个引用,调用后可以改变自身

	inline vec3& operator+=(const vec3 &v2);
	inline vec3& operator-=(const vec3 &v2);
	inline vec3& operator*=(const vec3 &v2);
	inline vec3& operator/=(const vec3 &v2);
	inline vec3& operator*=(const float t);//数乘
	inline vec3& operator/=(const float t);//数除

	inline float length() const
	{
		return sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
	}
	inline float squared_length() const
	{
		return e[0] * e[0] + e[1] * e[1] + e[2] * e[2];
	}
	inline void make_unit_vector();
	float e[3];
};

inline std::istream& operator>>(std::istream &is, vec3 &t)//输入输出流对象不可拷贝
{
	is >> t.e[0] >> t.e[1] >> t.e[2];
	return is;
}
inline std::ostream& operator<<(std::ostream &os, const vec3 &t)
{
	os << t.e[0] << t.e[1] << t.e[2];
	return os;
}
inline void vec3::make_unit_vector()
{
	float k = 1.0 / sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
	e[0] *= k;
	e[1] *= k;
	e[2] *= k;
}
inline vec3 operator+(const vec3 &v1, const vec3 &v2)//因为没有写成vec3的成员函数,所以采取了接受两个vec3对象为参数,返回第三个vec3对象的方法
{
	return vec3(v1.e[0] + v2.e[0], v1.e[1] + v2.e[1], v1.e[2] + v2.e[2]);
}
inline vec3 operator-(const vec3 &v1, const vec3 &v2)
{
	return vec3(v1.e[0] - v2.e[0], v1.e[1] - v2.e[1], v1.e[2] - v2.e[2]);
}
inline vec3 operator*(const vec3 &v1, const vec3 &v2)
{
	return vec3(v1.e[0] * v2.e[0], v1.e[1] * v2.e[1], v1.e[2] * v2.e[2]);
}
inline vec3 operator/(const vec3 &v1, const vec3 &v2)
{
	return vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]);
}
inline vec3 operator*(float t, const vec3 &v)
{
	return vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
}
inline vec3 operator/(const vec3 &v,float t)
{
	return vec3(v.e[0]/t, v.e[1]/t, v.e[2]/t);
}
inline float dot(const vec3 &v1, const vec3 &v2)
{
	return v1.e[0] * v2.e[0] + v1.e[1] * v2.e[1] + v1.e[2] * v2.e[2];
}
inline vec3 cross(const vec3 &v1, const vec3 &v2)
{
	return vec3((v1.e[1] * v2.e[2] - v1.e[2] * v2.e[1]), (-(v1.e[0] * v2.e[2] - v1.e[2] * v2.e[0])), (v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]));
}
//以前做+重载时也常采用如下方式,现在看来是有点问题的
inline vec3& vec3::operator+=(const vec3 &v)
{
	e[0] += v.e[0];
	e[1] += v.e[1];
	e[2] += v.e[2];
	return *this;
}
inline vec3& vec3::operator*=(const vec3 &v)
{
	e[0] *= v.e[0];
	e[1] *= v.e[1];
	e[2] *= v.e[2];
	return *this;
}
inline vec3& vec3::operator/=(const vec3 &v)
{
	e[0] /= v.e[0];
	e[1] /= v.e[1];
	e[2] /= v.e[2];
	return *this;
}
inline vec3& vec3::operator-=(const vec3 &v)
{
	e[0] -= v.e[0];
	e[1] -= v.e[1];
	e[2] -= v.e[2];
	return *this;
}
inline vec3& vec3::operator*=(const float t)
{
	e[0] *= t;
	e[1] *= t;
	e[2] *= t;
	return *this;
}
inline vec3& vec3::operator/=(const float t)
{
	float k = 1.0 / t;
	e[0] *= k;
	e[1] *= k;
	e[2] *= k;
	return *this;
}
inline vec3  unit_vector(vec3 v)
{
	return v / v.length();
}

 RayTracer.cpp

#include "stdafx.h"
#include"vector.h"
#include"ray.h"
#include<iostream>
#include<fstream>

using namespace std;

//camera朝上为y,朝image plain为-z,向右为x
vec3 color(const ray& r)//设置背景色(由y值决定蓝白)
{
	vec3 unit_direction = unit_vector(r.direction());//得到单位方向向量,将y限定在-1至1之间
	float t = 0.5*(unit_direction.y() + 1);//间接用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行
	ofstream out("d:\\theFirstPpm.txt");
	out << "P3\n" << nx << " " << ny << "\n255" << endl;
	vec3 lower_left_corner(-2.0, -1.0, -1.0);//image plain在camera frame中左下角坐标
	vec3 horizontal(4.0, 0.0, 0.0);//image plain在camera frame中水平方向的量度
	vec3 vertical(0.0, 2.0, 0.0);//image plain在camera frame中竖直方向的量度
	vec3 origin(0.0, 0.0, 0.0);
	for (int j = ny - 1;j >= 0;j--)//行从上到下
	{
		for (int i = 0;i < nx;i++)//列从左到右
		{
			float u = float(i) / float(nx);//当前pixel在水平方向上的比例(相对位置)
			float v = float(j) / float(ny);
			//构造viewing ray,direction参数实际就是intersection在camera frame中的坐标
			ray r(origin, lower_left_corner + u*horizontal + v*vertical);//将左下角作为求坐标时的参考点
			vec3 col = color(r);
			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;
}

Camera Frame

生成背景图像

猜你喜欢

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