1
0
raytracing-in-a-weekend-oop/camera.h

55 lines
1.2 KiB
C
Raw Permalink Normal View History

2022-03-13 17:19:05 +00:00
#ifndef CAMERA_H
#define CAMERA_H
#include "rtweekend.h"
2022-03-13 18:45:12 +00:00
#include "vec3.h"
#include "ray.h"
2022-03-13 17:19:05 +00:00
class camera {
public:
2022-03-13 18:45:12 +00:00
camera(
point3 lookfrom,
point3 lookat,
vec3 vup,
double vfov, // vertical field-of-view in degrees,
2022-03-13 18:53:40 +00:00
double aspect_ratio,
double aperture,
double focus_dist
2022-03-13 18:45:12 +00:00
) {
auto theta = degrees_to_radians(vfov);
auto h = tan(theta/2);
auto viewport_height = 2.0 * h;
2022-03-13 17:19:05 +00:00
auto viewport_width = aspect_ratio * viewport_height;
2022-03-13 18:53:40 +00:00
w = unit_vector(lookfrom - lookat);
u = unit_vector(cross(vup, w));
v = cross(w, u);
2022-03-13 18:45:12 +00:00
origin = lookfrom;
2022-03-13 18:53:40 +00:00
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;
2022-03-13 17:19:05 +00:00
}
2022-03-13 18:45:12 +00:00
ray get_ray(double s, double t) const {
2022-03-13 18:53:40 +00:00
vec3 rd = lens_radius * random_in_unit_sphere();
vec3 offset = u* rd.x() + v * rd.y();
return ray(
origin + offset,
lower_left_corner + s*horizontal + t*vertical - origin - offset
);
2022-03-13 17:19:05 +00:00
}
private:
point3 origin;
point3 lower_left_corner;
vec3 horizontal;
vec3 vertical;
2022-03-13 18:53:40 +00:00
vec3 u, v, w;
double lens_radius;
2022-03-13 17:19:05 +00:00
};
#endif