diff options
author | Keuin <[email protected]> | 2022-04-11 22:13:57 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-04-11 22:13:57 +0800 |
commit | 43714bd116945573e7e4c854445462afa7f9b1b4 (patch) | |
tree | 7570dd1ab5e0feb38e504b4a49f4e8d2d0774873 /main_simple_scanner.cpp | |
parent | f6661b0243cb359fb4929aee06d1247944f3d2dd (diff) |
Implement ray3, timer and a simple viewport scanner.
Fix bitmap wrong pixel sequence.
Remove default constructor of bitmap.
Add pixel mixture method.
Diffstat (limited to 'main_simple_scanner.cpp')
-rw-r--r-- | main_simple_scanner.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/main_simple_scanner.cpp b/main_simple_scanner.cpp new file mode 100644 index 0000000..a1d75f7 --- /dev/null +++ b/main_simple_scanner.cpp @@ -0,0 +1,60 @@ +// +// Created by Keuin on 2022/4/11. +// + +#include <cstdint> +#include <iostream> + +#include "vec.h" +#include "ray.h" +#include "bitmap.h" +#include "timer.h" + + +class viewport { + uint16_t width, height; + vec3d center; + bitmap8b bitmap_{width, height}; + + static pixel8b color(const ray3d &r) { + const auto u = (r.direction().y + 1.0) * 0.5; + return mix( + pixel8b::from_normalized(1.0, 1.0, 1.0), + pixel8b::from_normalized(0.5, 0.7, 1.0), + 1.0 - u, + u + ); + } + +public: + viewport() = delete; + + viewport(uint16_t width, uint16_t height, vec3d viewport_center) : + width(width), height(height), center(viewport_center) {} + + void render(vec3d viewpoint) { + const auto r = center - viewpoint; + const int half_width = width / 2, half_height = height / 2; + for (int j = -half_height; j < half_height; ++j) { + for (int i = -half_width; i < half_width; ++i) { + const auto dir = r + vec3d{static_cast<double>(i), static_cast<double>(j), 0}; + const ray3d ray{viewpoint, dir}; // from camera to pixel (on the viewport) + const auto pixel = color(ray); + bitmap_.set(i + half_width, j + half_height, pixel); + } + } + } + + const bitmap8b &bitmap() const { + return bitmap_; + } +}; + +int main() { + viewport vp{1920, 1080, vec3d{0, 0, -100}}; + timer tm; + tm.start_measure(); + vp.render(vec3d{0, 0, 0}); // camera position as the coordinate origin + tm.stop_measure(); + vp.bitmap().write_plain_ppm(std::cout); +}
\ No newline at end of file |