From 2a3337a912569c6d961d002178cdbac7912e1ecc Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 13 Dec 2020 16:10:40 +0300 Subject: [PATCH] Implement bump-mapping in pcg --- libs/pcg/include/psemek/pcg/bump.hpp | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 libs/pcg/include/psemek/pcg/bump.hpp diff --git a/libs/pcg/include/psemek/pcg/bump.hpp b/libs/pcg/include/psemek/pcg/bump.hpp new file mode 100644 index 00000000..0b931c03 --- /dev/null +++ b/libs/pcg/include/psemek/pcg/bump.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include + +#include + +namespace psemek::pcg +{ + + inline gfx::basic_pixmap> bump(gfx::pixmap_float const & height_map, seamless_tag) + { + gfx::basic_pixmap> result(height_map.dims()); + + float const dx = 1.f / result.width(); + float const dy = 1.f / result.height(); + + for (auto i : result.indices()) + { + auto ix0 = i; + ix0[0] = (ix0[0] + result.width() - 1) % result.width(); + + auto ix1 = i; + ix1[0] = (ix1[0] + 1) % result.width(); + + auto iy0 = i; + iy0[1] = (iy0[1] + result.height() - 1) % result.height(); + + auto iy1 = i; + iy1[1] = (iy1[1] + 1) % result.height(); + + float h = height_map(i); + float hx0 = height_map(ix0); + float hx1 = height_map(ix1); + float hy0 = height_map(iy0); + float hy1 = height_map(iy1); + + auto n = geom::vector::zero(); + + n += geom::normalized(geom::vector{hx0 - h, 0.f, dx}); + n += geom::normalized(geom::vector{h - hx1, 0.f, dx}); + n += geom::normalized(geom::vector{0.f, hy0 - h, dy}); + n += geom::normalized(geom::vector{0.f, h - hy1, dy}); + + result(i) = geom::normalized(n); + } + + return result; + } + +}