From ed66f6748c8121aacf6c3cd48b9ac001c85c0978 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Apr 2022 19:49:14 +0800 Subject: Implement Lambertian Reflection and Hemispherical Reflection. --- vec.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'vec.h') diff --git a/vec.h b/vec.h index 9a29f8b..8efbc9a 100644 --- a/vec.h +++ b/vec.h @@ -144,23 +144,38 @@ using vec3d = vec3; // random unit vector generator template -class random_uv_gen { +class rand_vec_gen { std::mt19937_64 mt; std::uniform_real_distribution uni{-1.0, 1.0}; public: - random_uv_gen() = delete; - explicit random_uv_gen(uint64_t seed) : mt{seed} {} + rand_vec_gen() = delete; - vec3 operator()() { + explicit rand_vec_gen(uint64_t seed) : mt{seed} {} + + // Get a random vector whose length is in [0, 1] + inline vec3 range01() { 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; + if (vec.mod2() <= 1.0) return vec.unit_vec(); } } + + // Get a unit vector with random direction. + inline vec3 normalized() { + return range01().unit_vec(); + } + + // Get a random vector whose length is in [0, 1] and + // has a direction difference less than 90 degree with given vector. + inline vec3 hemisphere(vec3 &vec) { + const auto v = range01(); + if (dot(v, vec) > 0) return v; + return -v; + } }; -using random_uv_gen_3d = random_uv_gen; +using random_uv_gen_3d = rand_vec_gen; #endif //RT_VEC_H \ No newline at end of file -- cgit v1.2.3