summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-04-13 13:34:50 +0800
committerKeuin <[email protected]>2022-04-13 13:34:50 +0800
commitd883ff94342f418b87ac2333b3fdf779bd2dfa0f (patch)
tree5ccd2cac2ed714d59eb27d90c7d78e39741aa966
parentd1a748cea4c1c33d6979c32181ff4a17da9ecd71 (diff)
Generalize hitlist and basic_viewport. (HDR in the future)
-rw-r--r--hitlist.h13
-rw-r--r--main_simple_scanner.cpp7
-rw-r--r--viewport.h21
3 files changed, 25 insertions, 16 deletions
diff --git a/hitlist.h b/hitlist.h
index 18a8226..0c65e1a 100644
--- a/hitlist.h
+++ b/hitlist.h
@@ -19,7 +19,8 @@
#include <cstdint>
-// A world
+// A world, T is color depth
+template<typename T>
class hitlist {
std::vector<std::shared_ptr<object>> objects;
@@ -34,7 +35,7 @@ public:
}
// Given a ray, compute the color.
- pixel8b color(const ray3d &r) const {
+ pixel<T> color(const ray3d &r) const {
// Detect hits
bool hit = false;
double hit_t = std::numeric_limits<double>::infinity();
@@ -53,15 +54,15 @@ public:
const auto nv = hit_obj->normal_vector(r.at(hit_t));
// return obj->color();
// visualize normal vector at hit point
- return pixel8b::from_normalized(nv);
+ return pixel<T>::from_normalized(nv);
}
// Does not hit anything. Get background color (infinity)
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),
+ pixel<T>::from_normalized(1.0, 1.0, 1.0),
+ pixel<T>::from_normalized(0.5, 0.7, 1.0),
1.0 - u,
u
);
@@ -70,4 +71,6 @@ public:
};
+using hitlist8b = hitlist<uint8_t>;
+
#endif //RT_HITLIST_H
diff --git a/main_simple_scanner.cpp b/main_simple_scanner.cpp
index 06476d8..5f59558 100644
--- a/main_simple_scanner.cpp
+++ b/main_simple_scanner.cpp
@@ -19,8 +19,8 @@
void generate_image(uint16_t image_width, uint16_t image_height, double viewport_width, double focal_length,
double sphere_z, double sphere_r, const std::string &caption = "", unsigned caption_scale = 1) {
double r = 1.0 * image_width / image_height;
- basic_viewport vp{viewport_width, viewport_width / r, vec3d{0, 0, -focal_length}};
- hitlist world;
+ basic_viewport8b vp{viewport_width, viewport_width / r, vec3d{0, 0, -focal_length}};
+ hitlist8b world;
bias_ctx bias{false, 0};
world.add_object(std::make_shared<sphere>(
vec3d{0, -100.5, -1},
@@ -49,6 +49,9 @@ int main(int argc, char **argv) {
argv[0]);
return 0;
}
+#ifndef NDEBUG
+ std::cerr << "Notice: assertion is enabled." << std::endl;
+#endif
std::string iw{argv[1]}, ih{argv[2]}, vw{argv[3]}, fl{argv[4]}, sz{argv[5]}, sr{argv[6]}, cap{};
if (argc == 8) {
// with caption
diff --git a/viewport.h b/viewport.h
index 2610ff5..72e938e 100644
--- a/viewport.h
+++ b/viewport.h
@@ -43,13 +43,14 @@ public:
template<typename T>
class viewport {
public:
- virtual bitmap<T> render(const hitlist &world, vec3d viewpoint, uint16_t image_width, uint16_t image_height) = 0;
+ virtual bitmap<T> render(const hitlist<T> &world, vec3d viewpoint, uint16_t image_width, uint16_t image_height) = 0;
};
using viewport8b = viewport<uint8_t>;
// Single sampled viewport which supports bias sampling
-class basic_viewport : public viewport8b {
+template<typename T>
+class basic_viewport : public viewport<T> {
const double half_width, half_height; // viewport size
const vec3d center; // coordinate of the viewport center point
@@ -59,22 +60,22 @@ public:
basic_viewport(double width, double height, vec3d viewport_center) :
half_width(width / 2.0), half_height(height / 2.0), center(viewport_center) {}
- virtual bitmap8b
- render(const hitlist &world, vec3d viewpoint, uint16_t image_width, uint16_t image_height) override {
+ virtual bitmap<T>
+ render(const hitlist<T> &world, vec3d viewpoint, uint16_t image_width, uint16_t image_height) override {
bias_ctx bc{};
return render(world, viewpoint, image_width, image_height, bc);
}
- virtual /**
+ /**
* Generate the image seen on given viewpoint.
* @param bx bias on x axis (0.0 <= bx < 1.0)
* @param by bias on y axis (0.0 <= by < 1.0)
* @return
*/
- bitmap8b render(const hitlist &world, vec3d viewpoint,
- uint16_t image_width, uint16_t image_height,
- bias_ctx &bias) const {
- bitmap8b image{image_width, image_height};
+ virtual bitmap<T> render(const hitlist<T> &world, vec3d viewpoint,
+ uint16_t image_width, uint16_t image_height,
+ bias_ctx &bias) const {
+ bitmap<T> image{image_width, image_height};
double bx, by;
const auto r = center - viewpoint;
const int img_hw = image_width / 2, img_hh = image_height / 2;
@@ -101,4 +102,6 @@ public:
}
};
+using basic_viewport8b = basic_viewport<uint8_t>;
+
#endif //RT_VIEWPORT_H