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. --- vec.h | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'vec.h') diff --git a/vec.h b/vec.h index bf40e7d..2c8c2f4 100644 --- a/vec.h +++ b/vec.h @@ -6,6 +6,7 @@ #define RT_VEC_H #include +#include static inline bool eq(int a, int b) { return a == b; @@ -88,19 +89,31 @@ inline std::ostream &operator<<(std::ostream &out, const vec3 &vec) { } // product vec3 by a scalar -template +template< + typename T, + typename S, + typename = typename std::enable_if::value, S>::type +> inline vec3 operator*(const vec3 &vec, const S &b) { return vec3{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)}; } // product vec3 by a scalar -template +template< + typename T, + typename S, + typename = typename std::enable_if::value, S>::type +> inline vec3 operator*(const S &b, const vec3 &vec) { return vec3{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)}; } // product vec3 by the inversion of a scalar (div by a scalar) -template +template< + typename T, + typename S, + typename = typename std::enable_if::value, S>::type +> inline vec3 operator/(const vec3 &vec, const S &b) { return vec3{.x=(T) (vec.x / b), .y=(T) (vec.y / b), .z=(T) (vec.z / b)}; } @@ -129,4 +142,24 @@ using vec3f = vec3; // 3-dim vector (double) using vec3d = vec3; +// random unit vector generator +template +class random_uv_gen { + std::mt19937_64 mt; + std::uniform_real_distribution uni{-1.0, 1.0}; + +public: + explicit random_uv_gen(uint64_t seed) : mt{seed} {} + + vec3 operator()() { + while (true) { + const auto x = uni(mt), y = uni(mt), z = uni(mt); + const auto vec = vec3{.x=x, .y=y, .z=z}; + if (vec.mod2() <= 1.0) return vec; + } + } +}; + +using random_uv_gen_3d = random_uv_gen; + #endif //RT_VEC_H \ No newline at end of file -- cgit v1.2.3