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 {
|
|
|
|
public:
|
2020-06-06 22:17:33 +00:00
|
|
|
Camera(Point3 lookfrom,
|
|
|
|
Point3 lookat,
|
|
|
|
Vec3 vup,
|
|
|
|
double vfov, // vertical field-of-view in degrees
|
|
|
|
double aspect_ratio) {
|
|
|
|
auto theta = degrees_to_radians(vfov);
|
|
|
|
auto h = std::tan(theta/2);
|
|
|
|
auto viewport_height = 2.0 * h;
|
2020-06-03 22:39:14 +00:00
|
|
|
auto viewport_width = aspect_ratio * viewport_height;
|
|
|
|
|
2020-06-06 22:17:33 +00:00
|
|
|
auto w = unit_vector(lookfrom - lookat);
|
|
|
|
auto u = unit_vector(cross(vup, w));
|
|
|
|
auto v = cross(w, u);
|
|
|
|
|
|
|
|
origin = lookfrom;
|
|
|
|
horizontal = viewport_width * u;
|
|
|
|
vertical = viewport_height * v;
|
|
|
|
lower_left_corner = origin - horizontal / 2 - vertical / 2 - w;
|
2020-06-03 22:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ray get_ray(double u, double v) const {
|
|
|
|
return Ray(origin, lower_left_corner + u * horizontal + v * vertical - origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Point3 origin;
|
|
|
|
Point3 lower_left_corner;
|
|
|
|
Vec3 horizontal;
|
|
|
|
Vec3 vertical;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CAMERA_H
|