载入中...
搜索中...
未找到
HexCoordinates.cpp
浏览该文件的文档.
2#include <cmath>
3#include <cstdint>
4#include "hexmap/HexMap.h"
5#include "hexmap/HexMapMesh.h"
7#include "hexmap/HexMetrics.h"
8#include "hexmap/HexNoise.h"
10
11// HexCoordinates.cpp
12namespace eve::hexmap {
13
15 const float diameter = HexMetrics::innerDiameter();
16 if (diameter <= 0.f) return HexCoordinates{};
17 const float fx = position.x / diameter;
18 const float fy = -fx;
19 const float offset = position.z / (HexMetrics::outerRadius() * 3.f);
20
21 const float ax = fx - offset;
22 const float ay = fy - offset;
23
24 std::int32_t ix = static_cast<std::int32_t>(std::lround(ax));
25 std::int32_t iy = static_cast<std::int32_t>(std::lround(ay));
26 std::int32_t iz = static_cast<std::int32_t>(std::lround(-ax - ay));
27
28 if (ix + iy + iz != 0) {
29 const float dx = std::fabs(ax - static_cast<float>(ix));
30 const float dy = std::fabs(ay - static_cast<float>(iy));
31 const float dz = std::fabs(-ax - ay - static_cast<float>(iz));
32 if (dx > dy && dx > dz)
33 ix = -iy - iz;
34 else if (dz > dy)
35 iz = -ix - iy;
36 }
37 return HexCoordinates{ix, iz};
38}
39
40} // namespace eve::hexmap
41
42// HexFogMesh.cpp
49namespace eve::hexmap {
50namespace {
51
53constexpr float kFogShadeSeen = 0.f;
55constexpr float kFogShadeUnknown = 1.f;
57constexpr float kFogCapLift = 0.25f;
59constexpr float kFogSkirtDepth = 6.f;
60
67[[nodiscard]] float cellTopY(const HexMap& map, HexCoordinates coordinates) noexcept {
68 const float land = map.cellPosition(coordinates).y;
69 const float surface = HexMetrics::waterSurfaceY(map.waterLevel(coordinates));
70 return land > surface ? land : surface;
71}
72
79[[nodiscard]] float neighbourhoodTopY(const HexMap& map, HexCoordinates coordinates) noexcept {
80 float top = cellTopY(map, coordinates);
81 for (std::int32_t i = 0; i < kHexDirectionCount; ++i) {
82 HexCoordinates neighbour{};
83 if (!map.getNeighbor(coordinates, static_cast<HexDirection>(i), neighbour)) continue;
84 const float candidate = cellTopY(map, neighbour);
85 if (candidate > top) top = candidate;
86 }
87 return top;
88}
89
91void emitTriangle(HexMeshData& out, const HexVec3& p0, const HexVec3& p1, const HexVec3& p2, float shade) {
92 const auto i0 = static_cast<std::uint32_t>(out.vertexCount());
93 out.addVertex(p0, shade, 0.f);
94 out.addVertex(p1, shade, 0.f);
95 out.addVertex(p2, shade, 0.f);
96 out.addTriangle(i0, i0 + 1u, i0 + 2u);
97}
98
112void appendFogColumn(const HexMap& map, HexCoordinates coordinates, float shade, HexMeshData& out) {
113 const HexNoise& noise = map.noise();
114 const HexVec3 ground = map.cellGroundPosition(coordinates);
115 const float top = neighbourhoodTopY(map, coordinates) + kFogCapLift;
116 const float bottom = HexMetrics::elevationY(HexMetrics::kMinElevation) - kFogSkirtDepth;
117
118 HexVec3 ring[kHexDirectionCount];
119 for (std::int32_t i = 0; i < kHexDirectionCount; ++i) {
120 HexVec3 corner = noise.perturb(ground + HexMetrics::corner(static_cast<HexDirection>(i)));
121 corner.y = top;
122 ring[i] = corner;
123 }
124 HexVec3 center = noise.perturb(HexVec3{ground.x, 0.f, ground.z});
125 center.y = top;
126
127 for (std::int32_t i = 0; i < kHexDirectionCount; ++i) {
128 const std::int32_t next = (i + 1) % kHexDirectionCount;
129 emitTriangle(out, center, ring[i], ring[next], shade);
130
131 HexVec3 topNear = ring[i];
132 HexVec3 topFar = ring[next];
133 HexVec3 botFar = ring[next];
134 HexVec3 botNear = ring[i];
135 botNear.y = bottom;
136 botFar.y = bottom;
137 const auto i0 = static_cast<std::uint32_t>(out.vertexCount());
138 out.addVertex(topNear, shade, 0.f);
139 out.addVertex(topFar, shade, 0.f);
140 out.addVertex(botFar, shade, 0.f);
141 out.addVertex(botNear, shade, 0.f);
142 out.addQuad(i0, i0 + 1u, i0 + 2u, i0 + 3u);
143 }
144}
145
146} // namespace
147
148void buildFogMesh(const HexMap& map, const HexVisibility& visibility, std::int32_t chunkIndex, HexMeshData& out) {
149 out.clear();
150 if (map.empty() || visibility.cellCount() != map.cellCount() || chunkIndex < 0 || chunkIndex >= map.chunkCount()) {
151 out.finalize();
152 return;
153 }
154
155 for (std::int32_t row = 0; row < HexMetrics::kChunkSizeZ; ++row) {
156 for (std::int32_t column = 0; column < HexMetrics::kChunkSizeX; ++column) {
157 const HexCoordinates coordinates = map.chunkCell(chunkIndex, column, row);
158 if (!map.contains(coordinates)) continue;
159 const std::int32_t index = map.indexOf(coordinates);
160 if (index < 0) continue;
161
162 float shade = 0.f;
163 if (!map.isExplored(coordinates)) {
164 shade = kFogShadeUnknown;
165 } else if (!visibility.isVisible(index)) {
166 shade = kFogShadeSeen;
167 } else {
168 // Fully visible: no overlay at all.
169 continue;
170 }
171 appendFogColumn(map, coordinates, shade, out);
172 }
173 }
174 out.finalize();
175}
176
177} // namespace eve::hexmap
178
179// HexMapMesh.cpp
180namespace eve::hexmap {
181
182void buildChunkSurfaceMesh(const HexMap& map, std::int32_t chunkIndex, HexSurface surface, HexMeshData& out) {
183 switch (surface) {
184 case HexSurface::Terrain: buildTerrainMesh(map, chunkIndex, out); return;
185 case HexSurface::Water: buildWaterMesh(map, chunkIndex, out); return;
186 case HexSurface::River: buildRiverMesh(map, chunkIndex, out); return;
187 case HexSurface::Road: buildRoadMesh(map, chunkIndex, out); return;
188 case HexSurface::Fog:
189 // The fog overlay also depends on the visibility counters, which a grid-only
190 // helper cannot see; `HexMapModule::rebuildChunk` dispatches it to `buildFogMesh`.
191 break;
192 case HexSurface::Wall:
194 // Both are declared by `HexFeatures.h` and dispatched by
195 // `HexMapModule::rebuildChunk`; `HexMapMesh` stays free of that header.
196 break;
197 }
198 out.clear();
199 out.finalize();
200}
201
202} // namespace eve::hexmap
203
204// HexMeshData.cpp
205namespace eve::hexmap {
206
207void HexMeshData::clear() noexcept {
208 positions_.clear();
209 normals_.clear();
210 uvs_.clear();
211 indices_.clear();
212}
213
214std::uint32_t HexMeshData::addVertex(HexVec3 position, float u, float v) {
215 const auto index = static_cast<std::uint32_t>(positions_.size() / 3u);
216 positions_.push_back(position.x);
217 positions_.push_back(position.y);
218 positions_.push_back(position.z);
219 uvs_.push_back(u);
220 uvs_.push_back(v);
221 return index;
222}
223
224void HexMeshData::addTriangle(std::uint32_t a, std::uint32_t b, std::uint32_t c) {
225 indices_.push_back(a);
226 indices_.push_back(b);
227 indices_.push_back(c);
228}
229
230void HexMeshData::addQuad(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d) {
231 addTriangle(a, c, b);
232 addTriangle(b, c, d);
233}
234
235void HexMeshData::finalize() noexcept {
236 normals_.assign(positions_.size(), 0.f);
237 for (std::size_t t = 0; t + 2 < indices_.size(); t += 3) {
238 const std::uint32_t ia = indices_[t];
239 const std::uint32_t ib = indices_[t + 1];
240 const std::uint32_t ic = indices_[t + 2];
241 const auto at = [this](std::uint32_t i) {
242 const std::size_t base = static_cast<std::size_t>(i) * 3u;
243 return HexVec3{positions_[base], positions_[base + 1], positions_[base + 2]};
244 };
245 const HexVec3 a = at(ia);
246 const HexVec3 b = at(ib);
247 const HexVec3 c = at(ic);
248 const HexVec3 ab = b - a;
249 const HexVec3 ac = c - a;
250 HexVec3 n{ab.y * ac.z - ab.z * ac.y, ab.z * ac.x - ab.x * ac.z, ab.x * ac.y - ab.y * ac.x};
251 const float len = std::sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
252 if (len > 1e-8f) {
253 n.x /= len;
254 n.y /= len;
255 n.z /= len;
256 } else {
257 n = HexVec3{0.f, 1.f, 0.f};
258 }
259 for (std::uint32_t i : {ia, ib, ic}) {
260 const std::size_t base = static_cast<std::size_t>(i) * 3u;
261 normals_[base] = n.x;
262 normals_[base + 1] = n.y;
263 normals_[base + 2] = n.z;
264 }
265 }
266}
267
268} // namespace eve::hexmap
uint32_t i0
Definition Grass.cpp:61
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::uint32_t ab
std::uint32_t ac
Axial hex coordinates and world-space conversion.
Per-chunk mesh generation for every hex map surface.
Editable hex cell grid: topology, queries, picking and authoring.
CPU vertex/index container shared by the hex surface builders.
Pointy-top hex metrics, directions and vertex helpers.
Deterministic asset-free noise for hex perturbation and generation.
float v
std::int32_t c
Reference-counted cell visibility and explored state.
size_t offset
std::array< float, 3 > position
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
float d
float t
Heightmap ground
ScriptStructure out
unsigned column
float dy
Definition Shadow.cpp:151
std::size_t at
uint32_t index
float bottom
An editable, chunked, pointy-top hex map.
Definition HexMap.h:72
std::int32_t chunkCount() const noexcept
Total number of chunks.
Definition HexMap.h:102
bool empty() const noexcept
Whether the map holds any cell.
Definition HexMap.h:92
std::int32_t indexOf(HexCoordinates coordinates) const noexcept
Linear cell index of coordinates (offset order), or -1 when outside the grid.
Definition HexMap.cpp:64
bool contains(HexCoordinates coordinates) const noexcept
Whether coordinates address a cell inside the grid.
Definition HexMap.h:122
std::int32_t cellCount() const noexcept
Total number of cells.
Definition HexMap.h:104
bool isExplored(HexCoordinates c) const noexcept
Whether a cell has ever been seen by a viewer (the fog-of-war latch).
Definition HexMap.cpp:348
HexCoordinates chunkCell(std::int32_t chunkIndex, std::int32_t column, std::int32_t row) const noexcept
Coordinates of the cell at (column, row) inside a chunk.
Definition HexMap.cpp:115
CPU-side triangle soup for one hex chunk surface.
Definition HexMeshData.h:24
void finalize() noexcept
Computes flat per-face normals. Must be called before upload.
void addTriangle(std::uint32_t a, std::uint32_t b, std::uint32_t c)
Appends a triangle with counter-clockwise winding.
void addQuad(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d)
Appends a quad as two triangles (a,c,b and b,c,d).
void clear() noexcept
Removes every vertex and index, keeping the allocated capacity.
std::uint32_t addVertex(HexVec3 position, float u, float v)
Appends one vertex.
static constexpr float outerRadius() noexcept
Radius of a hex cell in world units.
Definition HexMetrics.h:174
static constexpr float innerDiameter() noexcept
Width of one hex column.
Definition HexMetrics.h:178
static float waterSurfaceY(int waterLevel) noexcept
Y coordinate of a water surface at waterLevel.
Definition HexMetrics.h:273
static float elevationY(int elevation) noexcept
Y coordinate of a cell's elevation surface.
Definition HexMetrics.h:265
static HexVec3 corner(HexDirection direction, float scale=1.f) noexcept
Outer corner direction of a unit hex, scaled by scale.
Definition HexMetrics.h:183
static constexpr int kChunkSizeZ
Chunk size in the Z dimension.
Definition HexMetrics.h:158
static constexpr int kMinElevation
Minimum editable elevation.
Definition HexMetrics.h:160
static constexpr int kChunkSizeX
Chunk size in the X dimension.
Definition HexMetrics.h:156
Fog-of-war bookkeeping: how many viewers currently see each cell.
std::int32_t cellCount() const noexcept
Number of tracked cells.
bool isVisible(std::int32_t cellIndex) const noexcept
Whether any viewer currently sees the cell.
SQInteger top
Definition Snapshot.cpp:115
void buildRoadMesh(const HexMap &map, std::int32_t chunkIndex, HexMeshData &out)
Builds the road surface of one chunk.
HexSurface
Surface streams a chunk mesh is generated for.
Definition HexMap.h:19
@ Feature
Urban, farm, plant and special decorations; built by buildFeatureMesh.
@ Wall
City/farm walls, wall towers and bridges; built by buildWallMesh.
@ Fog
Fog-of-war overlay of a chunk.
void buildChunkSurfaceMesh(const HexMap &map, std::int32_t chunkIndex, HexSurface surface, HexMeshData &out)
Builds one chunk surface by dispatching on surface.
constexpr std::int32_t kHexDirectionCount
Number of hex edges / facing directions.
Definition HexMetrics.h:62
EVENGINE_API_WORLD void buildTerrainMesh(const HexMap &map, std::int32_t chunkIndex, HexMeshData &out)
Builds the ground, terrace and cliff surface of one chunk.
void buildRiverMesh(const HexMap &map, std::int32_t chunkIndex, HexMeshData &out)
Builds the river channel surface of one chunk.
EVENGINE_API_WORLD void buildWaterMesh(const HexMap &map, std::int32_t chunkIndex, HexMeshData &out)
Builds the water surface and shore band of one chunk.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
void buildFogMesh(const HexMap &map, const HexVisibility &visibility, std::int32_t chunkIndex, HexMeshData &out)
Builds the fog-of-war overlay of one chunk.
HexDirection
Hex facing directions, counter-clockwise from north-east.
Definition HexMetrics.h:59
Axial coordinates of one hex cell.
static HexCoordinates fromWorldPosition(HexVec3 position) noexcept
Converts a world-space XZ position back to cell coordinates.
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18