toytracer/camera.h

50 lines
1.4 KiB
C
Raw Permalink Normal View History

2020-06-03 22:39:14 +00:00
#ifndef CAMERA_H
#define CAMERA_H
2020-06-06 22:17:33 +00:00
#include <cmath>
2020-06-03 22:39:14 +00:00
#include "vec3.h"
class Camera {
2020-06-06 22:32:31 +00:00
public:
Camera(Point3 lookfrom, Point3 lookat, Vec3 vup,
double vfov, // vertical field-of-view in degrees
2020-06-07 20:25:43 +00:00
double aspect_ratio, double aperture, double focus_dist, double t0 = 0, double t1 = 0) {
2020-06-06 22:32:31 +00:00
auto theta = degrees_to_radians(vfov);
auto h = std::tan(theta / 2);
auto viewport_height = 2.0 * h;
auto viewport_width = aspect_ratio * viewport_height;
w = unit_vector(lookfrom - lookat);
u = unit_vector(cross(vup, w));
v = cross(w, u);
origin = lookfrom;
horizontal = focus_dist * viewport_width * u;
vertical = focus_dist * viewport_height * v;
lower_left_corner = origin - horizontal / 2 - vertical / 2 - focus_dist * w;
lens_radius = aperture / 2;
2020-06-07 20:25:43 +00:00
time0 = t0;
time1 = t1;
2020-06-06 22:32:31 +00:00
}
Ray get_ray(double s, double t) const {
Vec3 rd = lens_radius * random_in_unit_disk();
Vec3 offset = u * rd.x() + v * rd.y();
2020-06-07 20:25:43 +00:00
return Ray(origin + offset, lower_left_corner + s * horizontal + t * vertical - origin - offset,
random_double(time0, time1));
2020-06-06 22:32:31 +00:00
}
private:
Point3 origin;
Point3 lower_left_corner;
Vec3 horizontal;
Vec3 vertical;
Vec3 u, v, w;
double lens_radius;
2020-06-07 20:25:43 +00:00
double time0, time1; // shutter open/close times
2020-06-03 22:39:14 +00:00
};
#endif // CAMERA_H