diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2020-12-20 22:42:37 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2020-12-20 22:42:37 +0100 |
| commit | 643708a9922cabe9b37329ee41a8668087dc92d7 (patch) | |
| tree | e4052ae5b08569d97af39afc8456852d836d6c9f /src | |
| parent | 44c1eec63fc51d1a4eb888cde5e8f4b874876bab (diff) | |
Add a camera class
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/camera.cpp | 22 | ||||
| -rw-r--r-- | src/color.cpp | 12 | ||||
| -rw-r--r-- | src/main.cpp | 28 |
4 files changed, 38 insertions, 26 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 268dd02..996db85 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ include_directories(${CMAKE_SOURCE_DIR}/include) -add_executable(path-tracer main.cpp vec3.cpp color.cpp ray.cpp sphere.cpp hittable_list.cpp) +add_executable(path-tracer main.cpp vec3.cpp color.cpp ray.cpp sphere.cpp hittable_list.cpp camera.cpp) diff --git a/src/camera.cpp b/src/camera.cpp new file mode 100644 index 0000000..cd43359 --- /dev/null +++ b/src/camera.cpp @@ -0,0 +1,22 @@ +#include "camera.hpp" + +camera::camera(point3 position, double aspect_ratio, double focal_length) + : position(position), focal_length(focal_length) { + auto viewport_height = 2.0; + auto viewport_width = viewport_height * aspect_ratio; + + horizontal = vec3 { viewport_width, 0.0, 0.0 }; + vertical = vec3 { 0.0, viewport_height, 0.0 }; + + bottom_left_pixel = position - horizontal / 2.0 - vertical / 2.0 - vec3 { 0.0, 0.0, focal_length }; + + std::cerr << bottom_left_pixel << std::endl; +} + +ray camera::get_ray(double u, double v) const { + point3 viewport_pixel = bottom_left_pixel + u * horizontal + v * vertical; + return ray { + position, + viewport_pixel - position + }; +} diff --git a/src/color.cpp b/src/color.cpp index 53a79f9..b0e60e1 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -1,7 +1,11 @@ #include "color.hpp" -void write_color(std::ostream& out, const color& pixel_color) { - out << static_cast<uint32_t>(255.0 * pixel_color.x) << ' ' - << static_cast<uint32_t>(255.0 * pixel_color.y) << ' ' - << static_cast<uint32_t>(255.0 * pixel_color.z) << std::endl; +void write_color(std::ostream& out, const color& pixel_color, uint32_t samples_per_pixel) { + + auto scale = 1.0 / samples_per_pixel; + auto scaled_color = pixel_color * scale; + + out << static_cast<uint32_t>(255.0 * scaled_color.x) << ' ' + << static_cast<uint32_t>(255.0 * scaled_color.y) << ' ' + << static_cast<uint32_t>(255.0 * scaled_color.z) << std::endl; } diff --git a/src/main.cpp b/src/main.cpp index f6b3012..15fd165 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,17 +42,7 @@ int main() { const vec3 vertical { 0.0, viewport_height / height, 0.0 }; // We use a right handed coord system - const point3 eye_pos {}; - const double focal_length = 1.0; - - const point3 viewport_first_pixel { - -viewport_width / 2.0, - -viewport_height / 2.0, - -focal_length - }; - - const point3 first_pixel = eye_pos + viewport_first_pixel; - std::cerr << viewport_first_pixel << std::endl; + camera camera { {}, aspect_ratio, 1.0 }; // PPM format header std::cout << "P3\n" << width << '\n' << height << "\n 255" << std::endl; @@ -60,18 +50,14 @@ int main() { std::cerr << "Generating image" << std::endl; for (ssize_t row = height - 1; row >= 0; row--) { - std::cerr << row << " lines remaining" << std::endl; + // std::cerr << row << " lines remaining" << std::endl; for (size_t column = 0; column < width; column++) { - vec3 x = horizontal * column; - vec3 y = vertical * row; - - vec3 pixel_pos = first_pixel + x + y; - vec3 direction = pixel_pos - eye_pos; - - ray ray { eye_pos, direction }; + auto u = double(column) / (width - 1.0); + auto v = double(row) / (height - 1.0); + auto ray = camera.get_ray(u, v); - write_color(std::cout, ray_color(ray, world)); - // write_color(std::cout, color(direction.unit().y, direction.unit().y, direction.unit().y)); + write_color(std::cout, ray_color(ray, world), 1.0); + // write_color(std::cout, color(ray.direction.unit().y, ray.direction.unit().y, ray.direction.unit().y)); // write_color(std::cout, color(y.y, y.y, y.y)); } } |
