diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/camera.cpp | 2 | ||||
| -rw-r--r-- | src/color.cpp | 8 | ||||
| -rw-r--r-- | src/main.cpp | 32 | ||||
| -rw-r--r-- | src/vec3.cpp | 2 |
4 files changed, 19 insertions, 25 deletions
diff --git a/src/camera.cpp b/src/camera.cpp index cd43359..21cf506 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -9,8 +9,6 @@ camera::camera(point3 position, double aspect_ratio, double focal_length) 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 { diff --git a/src/color.cpp b/src/color.cpp index b0e60e1..3cd3e60 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -1,11 +1,11 @@ #include "color.hpp" +#include "rt.hpp" 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; + out << static_cast<uint32_t>(256.0 * clamp(scaled_color.x, 0.0, 0.999)) << ' ' + << static_cast<uint32_t>(256.0 * clamp(scaled_color.y, 0.0, 0.999)) << ' ' + << static_cast<uint32_t>(256.0 * clamp(scaled_color.z, 0.0, 0.999)) << std::endl; } diff --git a/src/main.cpp b/src/main.cpp index 15fd165..f7c4b0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,38 +27,34 @@ int main() { const double aspect_ratio = 16.0 / 9.0; const size_t width = 400; const size_t height = width / aspect_ratio; + const uint32_t samples_per_pixel = 100; + + camera camera { {}, aspect_ratio, 1.0 }; // World hittable objects hittable_list world {}; world.add(std::make_shared<sphere>(point3(0,0,-1), 0.5)); world.add(std::make_shared<sphere>(point3(0,-100.5,-1), 100)); - // Viewport dimensions - const double viewport_height = 2.0; - const double viewport_width = viewport_height * aspect_ratio; - - // Unit vectors for viewport pixels - const vec3 horizontal { viewport_width / width, 0.0, 0.0 }; - const vec3 vertical { 0.0, viewport_height / height, 0.0 }; - - // We use a right handed coord system - camera camera { {}, aspect_ratio, 1.0 }; - // PPM format header std::cout << "P3\n" << width << '\n' << height << "\n 255" << std::endl; 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++) { - auto u = double(column) / (width - 1.0); - auto v = double(row) / (height - 1.0); - auto ray = camera.get_ray(u, v); + color pixel_color; + + for (size_t sample_index = 0; sample_index < samples_per_pixel; sample_index++) { + auto u = double(column + randd()) / (width - 1.0); + auto v = double(row + randd()) / (height - 1.0); + auto ray = camera.get_ray(u, v); + + pixel_color += ray_color(ray, world); + } - 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)); + write_color(std::cout, pixel_color, samples_per_pixel); } } diff --git a/src/vec3.cpp b/src/vec3.cpp index b850bfc..f4470dc 100644 --- a/src/vec3.cpp +++ b/src/vec3.cpp @@ -12,7 +12,7 @@ vec3 vec3::operator-() const { }; } -vec3& vec3::operator+=(vec3& vec) { +vec3& vec3::operator+=(vec3 vec) { x += vec.x; y += vec.y; z += vec.z; |
