From 43714bd116945573e7e4c854445462afa7f9b1b4 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 11 Apr 2022 22:13:57 +0800 Subject: Implement ray3, timer and a simple viewport scanner. Fix bitmap wrong pixel sequence. Remove default constructor of bitmap. Add pixel mixture method. --- main_simple_scanner.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 main_simple_scanner.cpp (limited to 'main_simple_scanner.cpp') 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 +#include + +#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(i), static_cast(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 -- cgit v1.2.3