From be4e32755ac2960a0c8b428e864e54da5aac85f4 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Thu, 4 Jun 2020 00:39:14 +0200 Subject: [PATCH] Add camera class and sampling. --- Makefile | 2 +- camera.h | 31 +++++++++++++++++++++++++++++++ color.h | 18 ++++++++++++++---- util.h | 17 +++++++++++++++++ wtracer.cpp | 27 +++++++++++++-------------- 5 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 camera.h diff --git a/Makefile b/Makefile index ce6458e..3a50d22 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CXX = g++ CXXFLAGS = -Wall -Wextra -O2 -std=c++14 -DEPS = util.h vec3.h color.h ray.h hittable.h hittable_list.h sphere.h +DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h OBJ = wtracer.o TARGET = wtracer diff --git a/camera.h b/camera.h new file mode 100644 index 0000000..f87f2af --- /dev/null +++ b/camera.h @@ -0,0 +1,31 @@ +#ifndef CAMERA_H +#define CAMERA_H + +#include "vec3.h" + +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; + + 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; +}; + +#endif // CAMERA_H diff --git a/color.h b/color.h index 582b0c1..4e0b86d 100644 --- a/color.h +++ b/color.h @@ -4,11 +4,21 @@ #include #include "vec3.h" +#include "util.h" -void write_color(std::ostream &out, Color pixel_color) { - out << static_cast(255.999 * pixel_color.x()) << " " - << static_cast(255.999 * pixel_color.y()) << " " - << static_cast(255.999 * pixel_color.z()) << "\n"; +void write_color(std::ostream &out, Color pixel_color, int sample_per_pixel) { + auto r = pixel_color.x(); + auto g = pixel_color.y(); + auto b = pixel_color.z(); + + auto scale = 1.0 / sample_per_pixel; + r *= scale; + g *= scale; + b *= scale; + + out << static_cast(256 * clamp(r, 0.0, 0.999)) << " " + << static_cast(256 * clamp(g, 0.0, 0.999)) << " " + << static_cast(256 * clamp(b, 0.0, 0.999)) << "\n"; } #endif // COLOR_H diff --git a/util.h b/util.h index 4750d34..df8fb88 100644 --- a/util.h +++ b/util.h @@ -2,6 +2,7 @@ #define UTIL_H #include +#include const double infinity = std::numeric_limits::infinity(); const double pi = 3.1415926535897932385; @@ -10,4 +11,20 @@ inline double degrees_to_radians(double degrees) { return degrees * pi / 180; } +inline double random_double() { + static std::uniform_real_distribution distribution(0.0, 1.0); + static std::mt19937 generator; + return distribution(generator); +} + +inline double random_double(double min, double max) { + return min + (max-min) * random_double(); +} + +inline double clamp(double x, double min, double max) { + if (x < min) return min; + if (x > max) return max; + return x; +} + #endif // UTIL_H diff --git a/wtracer.cpp b/wtracer.cpp index 6941344..96b18d5 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -7,6 +7,7 @@ #include "hittable_list.h" #include "sphere.h" +#include "camera.h" Color ray_color(const Ray& r, const Hittable& world) { hit_record rec; @@ -21,31 +22,29 @@ Color ray_color(const Ray& r, const Hittable& world) { int main() { const auto aspect_ratio = 16.0 / 9.0; const int image_width = 1280; + //const int image_width = 384; const int image_height = static_cast(image_width / aspect_ratio); + const int samples_per_pixel = 100; std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; - auto viewport_height = 2.0; - auto viewport_width = aspect_ratio * viewport_height; - auto focal_length = 1.0; - - auto origin = Point3(0, 0, 0); - auto horizontal = Vec3(viewport_width, 0, 0); - auto vertical = Vec3(0, viewport_height, 0); - auto lower_left_corner = origin - horizontal/2 - vertical/2 - Vec3(0, 0, focal_length); - Hittable_list world; world.add(std::make_shared(Point3(0, 0, -1), 0.5)); world.add(std::make_shared(Point3(0, -100.5, -1), 100)); + Camera cam; + for (int j = image_height - 1; j >= 0; --j) { std::cerr << "\rScanlines remaining: " << j << " " << std::flush; for (int i = 0; i < image_width; ++i) { - auto u = double(i) / (image_width - 1); - auto v = double(j) / (image_height - 1); - Ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin); - Color pixel_color = ray_color(r, world); - write_color(std::cout, pixel_color); + Color pixel_color(0, 0, 0); + for (int s = 0; s