光线追踪-运动模糊
第一步
光线的类新增成员变量_time来记录当前时间点
class ray
{
public:
ray() {
}
ray(const vec3& a, const vec3& b, float ti = 0.0) {
A = a; B = b; _time = ti;}
vec3 origin() const {
return A; }
vec3 direction() const {
return B; }
float time() const {
return _time; }//time的封装函数
vec3 point_at_parameter(float t) const {
return A + t*B; }
vec3 A;
vec3 B;
float _time;//这是新增的成员变量——时间
};
第二步
- 给原有的camera类中加入float型的成员变量time0和time1作为记录物体运动的时间区间的两个端点。
- 并在camera类的构造函数中加入t0和t1两个参数来初始化time0和time1
- 在生成光线的方法中,在time0和time1之间随机取一个时间作为当前生成光线的时间点
class camera {
public:
camera(vec3 lookfrom, vec3 lookat, vec3 vup,
float vfov /* vfov is top to bottom in degrees */,
float aspect, float aperture, float focus_dist,
float t0, float t1) {
time0 = t0;//2
time1 = t1;//2
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 = 2*half_width*focus_dist*u;
vertical = 2*half_height*focus_dist*v;
}
ray get_ray(float s, float t)
{
vec3 rd = lens_radius*random_in_unit_disk();
vec3 offset = u * rd.x() + v * rd.y()