diff --git a/camera.h b/camera.h index f87f2af..d85f555 100644 --- a/camera.h +++ b/camera.h @@ -1,20 +1,30 @@ #ifndef CAMERA_H #define CAMERA_H +#include + #include "vec3.h" class Camera { public: - Camera() { - auto aspect_ratio = 16.0 / 9.0; - auto viewport_height = 2.0; + 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; auto viewport_width = aspect_ratio * viewport_height; - auto focal_length = 1.0; - 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 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; } Ray get_ray(double u, double v) const { diff --git a/wtracer.cpp b/wtracer.cpp index 9752d4b..eabefbf 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -49,8 +49,9 @@ int main() { world.add(std::make_shared(Point3(-1, 0, -1), 0.5, std::make_shared(1.45))); + world.add(std::make_shared(Point3(-1, 0, -1), -0.45, std::make_shared(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(image_height*image_width);