Camera view geometry

This commit is contained in:
Armin Friedl 2022-07-30 20:50:18 +02:00
parent 0a0080ce80
commit b8bdd91e40
2 changed files with 23 additions and 20 deletions

View file

@ -5,29 +5,31 @@
class camera {
public:
camera() {
auto aspect_ratio = 16.0 / 9.0;
auto viewport_height = 2.0;
auto viewport_width = aspect_ratio * viewport_height;
auto focal_length = 1.0;
camera(double vfov /*vertical field-of-view in degrees*/, double aspect_ratio) {
origin = point3(0, 0, 0);
horizontal = vec3(viewport_width, 0.0, 0.0);
vertical = vec3(0.0, viewport_height, 0.0);
lower_left_corner =
origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length);
}
auto theta = degrees_to_radians(vfov);
auto h = tan(theta / 2);
auto viewport_height = 2.0 * h;
auto viewport_width = aspect_ratio * viewport_height;
auto focal_length = 1.0;
ray get_ray(double u, double v) const {
return ray(origin,
lower_left_corner + u * horizontal + v * vertical - origin);
}
origin = point3(0, 0, 0);
horizontal = vec3(viewport_width, 0.0, 0.0);
vertical = vec3(0.0, viewport_height, 0.0);
lower_left_corner =
origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length);
}
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;
point3 origin;
point3 lower_left_corner;
vec3 horizontal;
vec3 vertical;
};
#endif // CAMERA_H_

View file

@ -54,11 +54,12 @@ int main() {
make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), -0.4, material_left));
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
// Camera
camera cam;
camera cam(90, aspect_ratio);
// Render