From e46e1c4033b9d96325de3295eb442a5b1fa19f19 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Apr 2022 23:31:47 +0800 Subject: Global diffuse lighting. (gamma not corrected) Some operations on pixel. Make ray3 support copy semantic. Fix vec3 operands does not filter out vec3-vec3 as parameters. random_uv_gen generating random unit vectors. --- bitmap.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'bitmap.h') diff --git a/bitmap.h b/bitmap.h index 1e35395..5b07914 100644 --- a/bitmap.h +++ b/bitmap.h @@ -44,8 +44,30 @@ struct pixel { static inline pixel from_normalized(const vec3d &v3d) { return from_normalized(v3d.x / 2.0 + 0.5, v3d.y / 2.0 + 0.5, v3d.z / 2.0 + 0.5); } + + static inline pixel black() { + return pixel{(T) 0, (T) 0, (T) 0}; + } }; +template< + typename T, + typename S, + typename = typename std::enable_if::value, S>::type +> +pixel operator*(const pixel &pixel, S scale) { + return ::pixel < T > {.r=(T) (pixel.r * scale), .g=(T) (pixel.g * scale), .b=(T) (pixel.b * scale)}; +} + +template< + typename T, + typename S, + typename = typename std::enable_if::value, S>::type +> +pixel operator*(S scale, const pixel &pixel) { + return ::pixel < T > {.r=(T) (pixel.r * scale), .g=(T) (pixel.g * scale), .b=(T) (pixel.b * scale)}; +} + // Mix two colors a and b. Returns a*u + b*v template inline pixel mix(const pixel &a, const pixel &b, double u, double v) { @@ -93,7 +115,7 @@ public: bitmap result{images[0].width, images[0].height}; const auto m = images.size(); const auto n = images[0].content.size(); - uintmax_t acc_r, acc_g, acc_b; + uint_fast32_t acc_r, acc_g, acc_b; for (size_t i = 0; i < n; ++i) { acc_r = 0; acc_g = 0; @@ -103,7 +125,7 @@ public: acc_g += images[j].content[i].g; acc_b += images[j].content[i].b; } - result.content[i] = pixel{(T) (acc_r / m), (T) (acc_g / m), (T) (acc_b / m)}; + result.content[i] = pixel{(T) (1.0 * acc_r / m), (T) (1.0 * acc_g / m), (T) (1.0 * acc_b / m)}; } return result; } -- cgit v1.2.3