Add several parameters to camera.

This commit is contained in:
Faerbit 2020-06-07 00:17:33 +02:00
parent 3683a309c5
commit a35cf30535
2 changed files with 20 additions and 9 deletions

View File

@ -1,20 +1,30 @@
#ifndef CAMERA_H #ifndef CAMERA_H
#define CAMERA_H #define CAMERA_H
#include <cmath>
#include "vec3.h" #include "vec3.h"
class Camera { class Camera {
public: public:
Camera() { Camera(Point3 lookfrom,
auto aspect_ratio = 16.0 / 9.0; Point3 lookat,
auto viewport_height = 2.0; 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;
auto viewport_width = aspect_ratio * viewport_height; auto viewport_width = aspect_ratio * viewport_height;
auto focal_length = 1.0;
origin = Point3(0, 0, 0); auto w = unit_vector(lookfrom - lookat);
horizontal = Vec3(viewport_width, 0.0, 0.0); auto u = unit_vector(cross(vup, w));
vertical = Vec3(0.0, viewport_height, 0.0); auto v = cross(w, u);
lower_left_corner = origin - horizontal / 2 - vertical / 2 - Vec3(0, 0, focal_length);
origin = lookfrom;
horizontal = viewport_width * u;
vertical = viewport_height * v;
lower_left_corner = origin - horizontal / 2 - vertical / 2 - w;
} }
Ray get_ray(double u, double v) const { Ray get_ray(double u, double v) const {

View File

@ -49,8 +49,9 @@ int main() {
world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), 0.5, std::make_shared<Dielectric>(1.45))); world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), 0.5, std::make_shared<Dielectric>(1.45)));
world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), -0.45, std::make_shared<Dielectric>(1.45)));
Camera cam; Camera cam(Point3(-2, 2, 1), Point3(0, 0, -1), Vec3(0, 1, 0), 75, aspect_ratio);
auto image = std::make_unique<Color[]>(image_height*image_width); auto image = std::make_unique<Color[]>(image_height*image_width);