载入中...
搜索中...
未找到
Heightmap.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
6namespace eve::procgen {
7
9
11 width_ = width > 0 ? width : 0;
12 height_ = height > 0 ? height : 0;
13 data_.assign(size_t(width_) * size_t(height_), 0.f);
14}
15
16int Heightmap::getWidth() const { return width_; }
17int Heightmap::getHeight() const { return height_; }
18
19bool Heightmap::inBounds(int x, int y) const {
20 return x >= 0 && y >= 0 && x < width_ && y < height_;
21}
22
23void Heightmap::setHeight(int x, int y, float h) {
24 if (!inBounds(x, y)) return;
25 data_[size_t(y * width_ + x)] = h;
26}
27
28float Heightmap::height(int x, int y) const {
29 if (!inBounds(x, y)) return 0.f;
30 return data_[size_t(y * width_ + x)];
31}
32
33namespace {
34float lerpHeight(float a, float b, float t) { return a + (b - a) * t; }
35} // namespace
36
37float Heightmap::sampleBilinear(float x, float y) const {
38 if (width_ <= 0 || height_ <= 0) return 0.f;
39 const float fx = std::clamp(x, 0.f, float(width_ - 1));
40 const float fy = std::clamp(y, 0.f, float(height_ - 1));
41 const int x0 = int(std::floor(fx));
42 const int y0 = int(std::floor(fy));
43 const int x1 = std::min(x0 + 1, width_ - 1);
44 const int y1 = std::min(y0 + 1, height_ - 1);
45 const float tx = fx - float(x0);
46 const float ty = fy - float(y0);
47 const float top = lerpHeight(data_[size_t(y0 * width_ + x0)], data_[size_t(y0 * width_ + x1)], tx);
48 const float bot = lerpHeight(data_[size_t(y1 * width_ + x0)], data_[size_t(y1 * width_ + x1)], tx);
49 return lerpHeight(top, bot, ty);
50}
51
52float Heightmap::sampleBilinearSeamless(float x, float y) const {
53 if (width_ <= 0 || height_ <= 0) return 0.f;
54 float px = std::fmod(x, float(width_));
55 float py = std::fmod(y, float(height_));
56 if (px < 0.f) px += float(width_);
57 if (py < 0.f) py += float(height_);
58 const int x0 = int(std::floor(px));
59 const int y0 = int(std::floor(py));
60 const int x1 = (x0 + 1) % width_;
61 const int y1 = (y0 + 1) % height_;
62 const float tx = px - float(x0);
63 const float ty = py - float(y0);
64 const float top = lerpHeight(data_[size_t(y0 * width_ + x0)], data_[size_t(y0 * width_ + x1)], tx);
65 const float bot = lerpHeight(data_[size_t(y1 * width_ + x0)], data_[size_t(y1 * width_ + x1)], tx);
66 return lerpHeight(top, bot, ty);
67}
68
70 TerrainSampler local = sampler;
71 if (local.getWorldWidth() <= 0 || local.getWorldHeight() <= 0) {
73 }
75 const auto fn = local.asFunction();
76 for (int y = 0; y < height; ++y) {
77 for (int x = 0; x < width; ++x) {
78 hm.setHeight(x, y, fn(float(x) + 0.5f, float(y) + 0.5f));
79 }
80 }
81 return hm;
82}
83
84bool Heightmap::toGrid(Grid2D &out, const TerrainBands &bands) const {
85 if (width_ <= 0 || height_ <= 0) return false;
86 out.resize(width_, height_);
87 for (int y = 0; y < height_; ++y) {
88 for (int x = 0; x < width_; ++x) {
89 out.setCell(x, y, int(bands.semanticAt(data_[size_t(y * width_ + x)])));
90 }
91 }
92 return true;
93}
94
95} // namespace eve::procgen
int y
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
int h
std::vector< Colorf > px
uint32_t a
uint32_t b
int width
SettlementPipeline::Stage fn
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
Definition Grid2D.h:16
void resize(int width, int height)
Definition Grid2D.cpp:13
void setCell(int x, int y, int semantic)
Definition Grid2D.cpp:23
In-memory terrain heightmap: a dense float grid (row-major, index = y * width + x) materialized from ...
Definition Heightmap.h:19
float sampleBilinear(float x, float y) const
Bilinear height at continuous coordinate within [0, w) x [0, h).
Definition Heightmap.cpp:37
bool toGrid(Grid2D &out, const TerrainBands &bands) const
Classify heights into a semantic Grid2D (source for tilemap generation).
Definition Heightmap.cpp:84
void resize(int width, int height)
Definition Heightmap.cpp:10
float height(int x, int y) const
Definition Heightmap.cpp:28
float sampleBilinearSeamless(float x, float y) const
Bilinear height with wrap-around (seamless tiling).
Definition Heightmap.cpp:52
void setHeight(int x, int y, float h)
Definition Heightmap.cpp:23
static Heightmap generate(const TerrainSampler &sampler, int width, int height)
Materialize a Heightmap by sampling sampler at every pixel center.
Definition Heightmap.cpp:69
bool inBounds(int x, int y) const
Definition Heightmap.cpp:19
Deterministic terrain height sampling (Red Blob Games / Perlin fBm recipe).
void setWorldSize(int width, int height)
std::function< float(float, float)> asFunction() const
uint32_t semanticAt(float height) const