TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ Sample()

uint16_t Heightmap::Sample ( float  x,
float  y,
bool  interpolated 
) const

Definition at line 55 of file Heightmap.cpp.

56{
57 auto lerp = [](float a, float b, double t) { return a + (b - a) * t; };
58 auto clamp = [](float x, float min, float max) {
59 return x > max ? max : (x < min) ? min : x;
60 };
61
62 clamp(x, 0, 1);
63 clamp(y, 0, 1);
64
65 if (!interpolated)
66 return Get(x * (m_Width - 1), y * (m_Height - 1));
67
68 float x1 = std::floor(x * (m_Width - 1));
69 float y1 = std::floor(y * (m_Height - 1));
70 float x2 = x1 + 1;
71 float y2 = y1 + 1;
72
73 float xp = x * m_Width - x1;
74 float yp = y * m_Height - y1;
75
76 float h11 = Get(x1, y1);
77 float h21 = Get(x2, y1);
78 float h12 = Get(x1, y2);
79 float h22 = Get(x2, y2);
80
81 auto v1 = lerp(h11, h21, xp);
82 auto v2 = lerp(h12, h22, xp);
83 auto h = lerp(v1, v2, yp);
84 if (h < 0)
85 h = 0;
86 return (uint16_t) h;
87
88}