18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 06:14:58 +01:00

feat(editor): Fields in the Terrain work mode, the ground under them made wavy, natural slopes (#315)

* feat(editor): Fields in the Terrain work mode, the ground under them made wavy, natural slopes

A Fields tool beside Fill: LMB outlines a field, which gets the material chosen in the palette
and is kept in a list (terrain/<name>/patches.txt, or in the manifest while there are few),
where its material, repeat, angle, transition and grass are changed later.

- painted into the terrain (the paint of the chunks, its edge blended over a transition and
  shifted by a noise, the 3D grass under it left, taken away or of another kind), or
- a surface of its own: the ground cut away within the outline and the field drawn over the
  hole as a mesh following the heights of the terrain, made around the camera as the water
  is. It doesn't take one of the 8 materials of a chunk; its edge is sharp.

The ground under a field can be made wavy before: rolling hills, furrows, bumps, long waves,
a hollow or a mound, hummocks in a grid, strips of fields; with a profile (rounded, sharp
ridges, sharp valleys, terraces, flat tops), roughness, variation and a fade from the edge.
The chunks under it are made finer where the waves need it.

The formation modifiers get an optional natural shape (natural: wander, scale, gentler,
soften, roughness in the manifest): the formation and the edges of the track bed stay, the
slopes and their foot wander, get gentler and meet the ground softly. Without it the
modifiers shape the ground as before.

Formats: new optional entries patches, patches_file and natural of the manifest, and the
patches file; a terrain without them reads and saves as before, and the release doesn't read
them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* Fields: one noise for the edges of the fields and the natural slopes, one filling of the geometry of the water

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* Fields: the vertices handed to the geometry of the water by value

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* fix(editor): whole-number loop counters instead of float ones (Sonar cpp:S2193)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* fix(editor): Fields: the edits of a field stick, the natural slopes stay when the formations are laid again, no holes far off or by a missing chunk

The copy of the field the widgets edit is kept while one of them is held, so a drag ends with what it was dragged to. Laying
the formations again keeps the natural shape of the slopes of each. The surfaces of the fields are made as far as the chunks
are drawn, so their holes never show without them; a field over ground with no chunk is made of what has ground, and waits
only for chunks which are in the files but not loaded yet. Outline it again puts the points on the ground and the cut changes
that field. The holes of the fields are marked (254) and giving one back closes only its own, the holes cut by hand stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* fix(editor): Fields: a separate field is built after loading with the camera still; a cleared outline or another field chosen forgets the one to outline again; giving back a field keeps the holes of the others over it

- waits_for_ground counts a chunk as still coming while its pack isn't read
  yet (right after opening the scenery none is), and as not coming only when
  the pack is missing or read without that chunk. Before, the first build
  after opening left a saved separate field a hole until the camera moved.
- Clear the outline (Fields and the area fill) and choosing another field in
  the list forget the field put back by Outline it again, so the next cut is
  a new field and not the old one moved.
- All holes of separate fields share one mark, so closing one closed it under
  an overlapping field too; the separate fields left over the closed outline
  are cut again after it (when a field is given back or outlined again).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* chore(editor): Fields: one identifier a statement, contains() for the sets (Sonar cpp:S1659, cpp:S6171)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* ci(windows): build lua51.dll for the unit tests

(cherry picked from commit b3e6e02779)

* fix(terrain): the fields made again only when one comes or goes; patches_file a plain name; the hand holes leave the fields'

After the review of jerrrrycho at edb5160a:
- update() no longer rebuilds all the water and the separate fields every 200 m the camera moves. Past
  patch_move it compares the separate (not painted) fields within the reach with the ones made, and rebuilds
  only when one came within the reach or left it; otherwise only the centre moves. A terrain with only
  painted fields never rebuilds for a move, also in driving mode. The fields waiting for their ground are
  made again as before.
- patches_file of a manifest is taken only as a plain file name in the folder of the terrain. A path with
  folders, "..", or a root is logged ("Bad terrain: ...") and patches.txt is used instead, so a downloaded
  manifest can't have the patches read from, or saved over, a file elsewhere.
- The holes cut or closed by hand (the Holes tool, "Close the hole" of the Fill section) leave the hole
  samples of the fields (kFieldHole) as they are, so a field's mesh no longer stands over closed ground and
  z-fights with it, and "Give back" still finds the field's hole. The unmarked fallback of cut_field (the
  fields cut before the mark) thereby closes only unmarked holes.
- The field picked with "Outline it again" is forgotten when the Fill tool uses the outline up, and when
  another terrain tool than the fields is chosen, so the next field drawn is a new one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QRgQ1K1Qu7TCsa74zsNURd

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Hirek193 <polomarket73@gmail.com>
This commit is contained in:
pol102
2026-11-20 03:56:43 +01:00
committed by GitHub
parent c9c390017a
commit 142a3bc287
11 changed files with 2238 additions and 31 deletions

View File

@@ -615,8 +615,15 @@ void terrain_streamer::update(glm::dvec3 const &Camera)
ahead *= look_ahead_limit / glm::length(ahead);
auto const target{Camera + ahead};
// the pieces cut out near the camera: made anew as it moves on, and for the ones waiting for their ground, now and then
if (false == m_waterchanged && patches_due(Camera, now))
m_waterchanged = true;
if (m_waterchanged)
{
m_patchtried = now;
m_patchloaded = false;
build_water();
}
// finished work, within a few milliseconds
while (steady_seconds() - now < upload_budget)
@@ -630,6 +637,7 @@ void terrain_streamer::update(glm::dvec3 const &Camera)
m_results.pop_front();
}
process(done);
m_patchloaded = true;
m_settled = false;
}
@@ -899,6 +907,29 @@ void terrain_streamer::collect(std::vector<editor_terrain *> &Out) const
Out.push_back(entry.second.get());
}
bool terrain_streamer::waits_for_ground(heightmap::surface_patch const &Patch) const
{
glm::dvec2 low, high;
Patch.bounds(low, high);
auto const first{heightmap::chunk_at(low.x, low.y)};
auto const last{heightmap::chunk_at(high.x, high.y)};
for (auto z{std::min(first.second, last.second)}; z <= std::max(first.second, last.second); ++z)
for (auto x{std::min(first.first, last.first)}; x <= std::max(first.first, last.first); ++x)
{
chunk_key const key{x, z};
if (m_chunks.contains(key) || m_removed.contains(key))
continue;
// still coming while its pack isn't read yet (just after opening the scenery none is); not coming only when
// the pack is missing, or read and without that chunk
auto const pack{m_packs.find(heightmap::pack_of(key))};
if (pack == m_packs.end() || pack->second.status == pack_state::state::unknown || pack->second.status == pack_state::state::loading)
return true;
if (pack->second.status == pack_state::state::ready && pack->second.file->has(key))
return true;
}
return false;
}
editor_terrain *terrain_streamer::terrain_at(double X, double Z) const
{
auto const lookup{m_chunks.find(heightmap::chunk_at(X, Z))};
@@ -1423,11 +1454,18 @@ bool terrain_streamer::paint(double X, double Z, double Radius, double Strength,
});
}
bool terrain_streamer::fill(std::vector<glm::dvec2> const &Outline, double Feather, std::uint16_t Layer, double Strength)
{
return fill(Outline, Feather, Layer, Strength, 0.0, 4.0);
}
bool terrain_streamer::fill(std::vector<glm::dvec2> const &Outline, double Feather, std::uint16_t Layer, double Strength, double Noise, double NoiseScale)
{
if (Outline.size() < 3 || Strength <= 0.0)
return false;
Feather = std::max(0.0, Feather);
Noise = std::max(0.0, Noise);
glm::dvec2 low{std::numeric_limits<double>::max()}, high{-std::numeric_limits<double>::max()};
for (auto const &point : Outline)
{
@@ -1436,7 +1474,8 @@ bool terrain_streamer::fill(std::vector<glm::dvec2> const &Outline, double Feath
}
// the blend runs across the outline, half of it inside
auto const margin{Feather * 0.5};
return paint_area(low - glm::dvec2{margin}, high + glm::dvec2{margin}, Layer, true, [&](double const Px, double const Pz) {
auto const reach{margin + Noise};
return paint_area(low - glm::dvec2{reach}, high + glm::dvec2{reach}, Layer, true, [&](double const Px, double const Pz) {
glm::dvec2 const point{Px, Pz};
bool inside{false};
auto nearest{std::numeric_limits<double>::max()};
@@ -1446,7 +1485,7 @@ bool terrain_streamer::fill(std::vector<glm::dvec2> const &Outline, double Feath
auto const &b{Outline[j]};
if ((a.y > Pz) != (b.y > Pz) && Px < (b.x - a.x) * (Pz - a.y) / (b.y - a.y) + a.x)
inside = !inside;
if (Feather > 0.0)
if (Feather > 0.0 || Noise > 0.0)
{
auto const run{b - a};
auto const length{glm::dot(run, run)};
@@ -1454,10 +1493,15 @@ bool terrain_streamer::fill(std::vector<glm::dvec2> const &Outline, double Feath
nearest = std::min(nearest, glm::length(point - (a + run * t)));
}
}
if (Feather <= 0.0)
if (Feather <= 0.0 && Noise <= 0.0)
return inside ? Strength : 0.0;
// signed distance from the outline, inside positive, led smoothly through the blend
auto const t{std::clamp(((inside ? nearest : -nearest) + margin) / Feather, 0.0, 1.0)};
// signed distance from the outline, inside positive, the edge shifted by the noise, led smoothly through the blend
auto distance{inside ? nearest : -nearest};
if (Noise > 0.0)
distance += Noise * heightmap::natural_noise(Px, Pz, NoiseScale, 0);
if (Feather <= 0.0)
return distance > 0.0 ? Strength : 0.0;
auto const t{std::clamp((distance + margin) / Feather, 0.0, 1.0)};
return Strength * t * t * (3.0 - 2.0 * t);
});
}
@@ -1579,7 +1623,19 @@ bool terrain_streamer::cut(std::vector<glm::dvec2> const &Outline, bool Cut)
return cut_area(low, high, Cut, true, [&](double const Px, double const Pz) { return inside_outline(Outline, Px, Pz); });
}
bool terrain_streamer::cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bool const Cut, bool const Load, std::function<bool(double X, double Z)> const &Inside)
bool terrain_streamer::cut_field(std::vector<glm::dvec2> const &Outline, bool const Cut)
{
if (Outline.size() < 3)
return false;
auto const [low, high]{outline_bounds(Outline)};
auto const inside = [&](double const Px, double const Pz) { return inside_outline(Outline, Px, Pz); };
if (cut_area(low, high, Cut, true, inside, kFieldHole) || Cut)
return true;
return cut_area(low, high, false, true, inside); // cut before the holes of the fields were marked: all of it
}
bool terrain_streamer::cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bool const Cut, bool const Load, std::function<bool(double X, double Z)> const &Inside,
std::uint8_t const Mark)
{
bool changed{false};
for (auto *chunk : chunks_in(Min, Max, Load))
@@ -1593,13 +1649,18 @@ bool terrain_streamer::cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bo
auto const low{data->corner()};
auto const xs{index_range(low.x, step, data->holes + 1, Min.x, Max.x)};
auto const zs{index_range(low.y, step, data->holes + 1, Min.y, Max.y)};
std::uint8_t const value{Cut ? std::uint8_t{255} : std::uint8_t{0}};
std::uint8_t const value{Cut ? Mark : std::uint8_t{0}};
auto const marked{Mark != 255};
bool touched{false};
for (int iz = zs.first; iz <= zs.second; ++iz)
for (int ix = xs.first; ix <= xs.second; ++ix)
{
auto &sample{data->hole_mask[static_cast<std::size_t>(iz) * (data->holes + 1) + ix]};
if (sample == value || false == Inside(low.x + ix * step, low.y + iz * step))
// a marked cut leaves the holes there are as they are, a marked close closes its own only; the holes cut by hand (the
// Holes tool, the Fill section) leave the holes of the fields as they are both ways: a field is closed only by giving
// it back, and its mesh never stands over closed ground
auto const kept{marked ? (Cut ? sample != 0 : sample != Mark) : sample == kFieldHole};
if (sample == value || kept || false == Inside(low.x + ix * step, low.y + iz * step))
continue;
sample = value;
touched = true;
@@ -2120,6 +2181,7 @@ void terrain_streamer::remember_manifest()
m_change.manifest = true;
m_change.layers = m_manifest.layers;
m_change.water = m_manifest.water;
m_change.patches = m_manifest.patches;
m_change.water_material = m_manifest.water_material;
m_change.modifiers = m_manifest.modifiers;
m_change.grass = m_manifest.grass;
@@ -2168,6 +2230,7 @@ terrain_streamer::change terrain_streamer::restore(change const &Change)
opposite.manifest = true;
opposite.layers = m_manifest.layers;
opposite.water = m_manifest.water;
opposite.patches = m_manifest.patches;
opposite.water_material = m_manifest.water_material;
opposite.modifiers = m_manifest.modifiers;
opposite.grass = m_manifest.grass;
@@ -2176,6 +2239,7 @@ terrain_streamer::change terrain_streamer::restore(change const &Change)
m_manifest.layers = Change.layers;
m_manifest.modifiers = Change.modifiers;
m_manifest.water = Change.water;
m_manifest.patches = Change.patches;
m_manifest.grass = Change.grass;
if (false == Change.water_material.empty())
m_manifest.water_material = Change.water_material;
@@ -2285,6 +2349,170 @@ bool terrain_streamer::save(std::string *Error)
return true;
}
// water and the pieces of the ground cut out
namespace
{
// the grid a piece cut out of the terrain is laid on: its own, or for a piece following the terrain, one of its spacing laid from 0
// (as the grids of the chunks are) over the box of the outline
struct patch_grid
{
glm::dvec2 origin{0.0};
double step{1.0};
int columns{0};
int rows{0};
};
patch_grid grid_of(heightmap::surface_patch const &Patch)
{
patch_grid grid;
if (false == Patch.follow)
{
grid.origin = Patch.origin;
grid.step = Patch.spacing;
grid.columns = Patch.columns;
grid.rows = Patch.rows;
return grid;
}
glm::dvec2 low;
glm::dvec2 high;
Patch.bounds(low, high);
grid.step = std::max(0.25, Patch.spacing);
grid.origin = glm::floor(low / grid.step) * grid.step;
grid.columns = static_cast<int>(std::ceil((high.x - grid.origin.x) / grid.step)) + 1;
grid.rows = static_cast<int>(std::ceil((high.y - grid.origin.y) / grid.step)) + 1;
return grid;
}
// the triangles of a piece cut out of the terrain, in world space, facing up: the cells of its grid wholly inside the outline in two
// triangles each, cut along the diagonal the chunks are (from x0, z1 to x1, z0), the ones the outline runs through clipped to it
// triangle by triangle, so a piece following the terrain lies in the planes of its triangles. Height gives the heights; false where
// one isn't known yet (a chunk not loaded), then the piece is left for later and Missing set
template <typename Height_>
std::vector<glm::dvec3> patch_triangles(heightmap::surface_patch const &Patch, Height_ const &Height, bool &Missing)
{
std::vector<glm::dvec3> result;
Missing = false;
auto const &outline{Patch.outline};
auto const grid{grid_of(Patch)};
if (outline.size() < 3 || grid.columns < 2 || grid.rows < 2)
return result;
auto const step{grid.step};
auto const cellsx{grid.columns - 1};
auto const cellsz{grid.rows - 1};
auto const inside = [&](double const X, double const Z) {
bool in{false};
for (std::size_t i = 0, j = outline.size() - 1; i < outline.size(); j = i++)
if ((outline[i].y > Z) != (outline[j].y > Z) && X < (outline[j].x - outline[i].x) * (Z - outline[i].y) / (outline[j].y - outline[i].y) + outline[i].x)
in = !in;
return in;
};
// the cells the outline runs through
std::vector<char> edge(static_cast<std::size_t>(cellsx) * cellsz, 0);
auto const mark = [&](glm::dvec2 const &Point) {
auto const cx{static_cast<int>(std::floor((Point.x - grid.origin.x) / step))};
auto const cz{static_cast<int>(std::floor((Point.y - grid.origin.y) / step))};
for (int dz = -1; dz <= 1; ++dz)
for (int dx = -1; dx <= 1; ++dx)
if (cx + dx >= 0 && cx + dx < cellsx && cz + dz >= 0 && cz + dz < cellsz)
edge[static_cast<std::size_t>(cz + dz) * cellsx + cx + dx] = 1;
};
for (std::size_t i = 0; i < outline.size(); ++i)
{
auto const &a{outline[i]};
auto const &b{outline[(i + 1) % outline.size()]};
auto const samples{std::max(1, static_cast<int>(std::ceil(glm::distance(a, b) / (step * 0.25))))};
for (int k = 0; k <= samples; ++k)
mark(glm::mix(a, b, static_cast<double>(k) / samples));
}
// a polygon in the plan, as triangles facing up; a triangle with a corner over no ground is left out, the rest is made
auto const emit = [&](std::vector<glm::dvec2> const &Polygon) {
if (Polygon.size() < 3)
return;
auto const indices{heightmap::triangulate(Polygon)};
// the polygon is turned counterclockwise in the plan, the triangles go the other way round to face up
for (std::size_t i = 0; i + 2 < indices.size(); i += 3)
{
glm::dvec3 corners[3];
bool grounded{true};
int k{0};
for (auto const index : {indices[i + 2], indices[i + 1], indices[i]})
{
double height{0.0};
auto const &point{Polygon[index]};
if (false == Height(point.x, point.y, height))
{
grounded = false;
Missing = true;
}
corners[k++] = glm::dvec3{point.x, height, point.y};
}
if (grounded)
result.insert(result.end(), std::begin(corners), std::end(corners));
}
};
// Sutherland-Hodgman: the outline clipped to a triangle of a cell (A, B, C counterclockwise)
auto const clip = [&](glm::dvec2 const &A, glm::dvec2 const &B, glm::dvec2 const &C) {
std::vector<glm::dvec2> polygon{outline};
glm::dvec2 const corners[]{A, B, C};
for (int side = 0; side < 3 && polygon.size() >= 3; ++side)
{
auto const &p{corners[side]};
auto const &q{corners[(side + 1) % 3]};
auto const keep = [&](glm::dvec2 const &P) { return (q.x - p.x) * (P.y - p.y) - (q.y - p.y) * (P.x - p.x) >= 0.0; };
std::vector<glm::dvec2> out;
for (std::size_t i = 0; i < polygon.size(); ++i)
{
auto const &a{polygon[i]};
auto const &b{polygon[(i + 1) % polygon.size()]};
auto const ina{keep(a)};
auto const inb{keep(b)};
if (ina)
out.push_back(a);
if (ina != inb)
{
auto const da{(q.x - p.x) * (a.y - p.y) - (q.y - p.y) * (a.x - p.x)};
auto const db{(q.x - p.x) * (b.y - p.y) - (q.y - p.y) * (b.x - p.x)};
out.push_back(a + (b - a) * (da / (da - db)));
}
}
polygon = std::move(out);
}
// points closer than a millimetre merged, the clipping leaves them along the sides of the triangle
std::vector<glm::dvec2> clean;
for (auto const &point : polygon)
if (clean.empty() || glm::distance(clean.back(), point) > 1e-3)
clean.push_back(point);
while (clean.size() > 1 && glm::distance(clean.front(), clean.back()) <= 1e-3)
clean.pop_back();
emit(clean);
};
for (int cz = 0; cz < cellsz; ++cz)
for (int cx = 0; cx < cellsx; ++cx)
{
auto const x0{grid.origin.x + cx * step};
auto const z0{grid.origin.y + cz * step};
auto const x1{x0 + step};
auto const z1{z0 + step};
glm::dvec2 const a{x0, z0}, b{x1, z0}, c{x1, z1}, d{x0, z1};
if (edge[static_cast<std::size_t>(cz) * cellsx + cx])
{
// the two triangles of the cell, counterclockwise, either side of the diagonal from (x0, z1) to (x1, z0)
clip(a, b, d);
clip(b, c, d);
continue;
}
if (false == inside(x0 + 0.5 * step, z0 + 0.5 * step))
continue;
emit({a, b, d});
emit({b, c, d});
}
return result;
}
} // namespace
// water
void terrain_streamer::clear_water(bool const Touch)
@@ -2303,6 +2531,65 @@ void terrain_streamer::clear_water(bool const Touch)
m_watershapes.clear();
}
gfx::geometry_handle terrain_streamer::water_geometry(gfx::vertex_array Vertices)
{
// the geometry of water made before is filled anew, so editing the water doesn't pile up geometry
gfx::userdata_array nouserdata;
gfx::geometry_handle geometry{0, 0};
if (false == m_waterfree.empty())
{
geometry = m_waterfree.back();
m_waterfree.pop_back();
GfxRenderer->Replace(Vertices, nouserdata, geometry, GL_TRIANGLES);
return geometry;
}
if (m_waterbank.bank == 0 && m_waterbank.chunk == 0)
m_waterbank = GfxRenderer->Create_Bank();
return GfxRenderer->Insert(Vertices, nouserdata, m_waterbank, GL_TRIANGLES);
}
double terrain_streamer::reach_of_patches()
{
// as far as the chunks are drawn, their holes with them: never a hole without the surface over it
return std::max(patch_reach, static_cast<double>(Global.BaseDrawRange) * std::max(1.0, static_cast<double>(Global.fDistanceFactor)) + heightmap::chunk_size);
}
std::vector<std::size_t> terrain_streamer::patches_in_reach(glm::dvec2 const &Centre) const
{
std::vector<std::size_t> within;
auto const reach{reach_of_patches()};
for (std::size_t index = 0; index < m_manifest.patches.size(); ++index)
{
auto const &patch{m_manifest.patches[index]};
if (patch.paint)
continue; // laid into the paint of the chunks, no mesh of its own
glm::dvec2 boxlow;
glm::dvec2 boxhigh;
patch.bounds(boxlow, boxhigh);
if (m_hascamera && glm::distance(glm::clamp(Centre, boxlow, boxhigh), Centre) > reach)
continue;
within.push_back(index);
}
return within;
}
bool terrain_streamer::patches_due(glm::dvec3 const &Camera, double const Now)
{
if (m_manifest.patches.empty())
return false;
if (m_patchpending && m_patchloaded && Now - m_patchtried > 1.0)
return true;
glm::dvec2 const centre{Camera.x, Camera.z};
if (glm::distance(centre, m_patchcentre) <= patch_move)
return false;
// the painted fields (tens of thousands of them cut along the tracks) have no mesh, and the water doesn't move: made again only
// when the separate pieces within the reach change
if (patches_in_reach(centre) != m_patchesbuilt)
return true;
m_patchcentre = centre;
return false;
}
void terrain_streamer::build_water()
{
m_waterchanged = false;
@@ -2349,21 +2636,8 @@ void terrain_streamer::build_water()
vertices.push_back(vertex);
reach = std::max(reach, glm::length(point - centroid));
}
// the geometry of water made before is filled anew, so editing the water doesn't pile up geometry
gfx::userdata_array nouserdata;
water_shape entry;
if (false == m_waterfree.empty())
{
entry.geometry = m_waterfree.back();
m_waterfree.pop_back();
GfxRenderer->Replace(vertices, nouserdata, entry.geometry, GL_TRIANGLES);
}
else
{
if (m_waterbank.bank == 0 && m_waterbank.chunk == 0)
m_waterbank = GfxRenderer->Create_Bank();
entry.geometry = GfxRenderer->Insert(vertices, nouserdata, m_waterbank, GL_TRIANGLES);
}
entry.geometry = water_geometry(std::move(vertices));
if (entry.geometry.chunk == 0)
continue;
// two bounding vertices give the shape its area, the triangles are in the geometry
@@ -2388,4 +2662,103 @@ void terrain_streamer::build_water()
entry.section = &section;
m_watershapes.push_back(entry);
}
// the pieces of the ground cut out, with the geometry of the water: made anew together, in the same bank. Only the ones near
// the camera (pieces cut along the tracks of a whole network go in tens of thousands), again as it moves on; a piece over
// ground not loaded yet waits for it
m_patchcentre = glm::dvec2{m_camera.x, m_camera.z};
m_patchpending = false;
m_patchesbuilt = patches_in_reach(m_patchcentre);
for (auto const index : m_patchesbuilt)
{
auto const &patch{m_manifest.patches[index]};
auto const surface = [&](double const X, double const Z, double &Height) {
if (false == patch.follow)
{
if (patch.height(X, Z, Height))
return true;
// past the grid: the nearest of its edge
return patch.height(std::clamp(X, patch.origin.x, patch.origin.x + (patch.columns - 1) * patch.spacing),
std::clamp(Z, patch.origin.y, patch.origin.y + (patch.rows - 1) * patch.spacing), Height);
}
if (false == height_at(X, Z, Height))
return false;
Height += patch.lift;
return true;
};
bool missing{false};
auto const triangles{patch_triangles(patch, surface, missing)};
// made of what has the ground loaded; waited for only where the ground is still to come, not where the terrain has none
if (missing && patch.follow && waits_for_ground(patch))
m_patchpending = true;
if (triangles.empty())
continue;
glm::dvec2 low{std::numeric_limits<double>::max()};
glm::dvec2 high{-std::numeric_limits<double>::max()};
double bottom{std::numeric_limits<double>::max()};
double top{-std::numeric_limits<double>::max()};
for (auto const &point : triangles)
{
low = glm::min(low, glm::dvec2{point.x, point.z});
high = glm::max(high, glm::dvec2{point.x, point.z});
bottom = std::min(bottom, point.y);
top = std::max(top, point.y);
}
auto const middle{(low + high) * 0.5};
if (false == in_region(middle))
continue;
auto const name{patch.material.empty() ? (m_manifest.layers.empty() ? std::string{"grass"} : m_manifest.layers.front().material) : patch.material};
auto const material{GfxRenderer->Fetch_Material(name)};
auto const *materialdata{GfxRenderer->Material(material)};
auto const repeat{patch.size > 0.f ? static_cast<double>(patch.size) : (materialdata != nullptr && materialdata->GetSize().x > 0.f) ? static_cast<double>(materialdata->GetSize().x) : 8.0};
glm::dvec3 const centre{middle.x, 0.5 * (bottom + top), middle.y};
auto &section{simulation::Region->section(centre)};
section.create_geometry();
auto const origin{section.m_area.center};
auto const angle{glm::radians(static_cast<double>(patch.rotation))};
glm::dvec2 const along{std::cos(angle), std::sin(angle)};
glm::dvec2 const across{-along.y, along.x};
// the slope of the heights at the point, for the normal
auto const normal_at = [&](double const X, double const Z) {
auto const d{std::max(0.25, patch.spacing * 0.5)};
double hx0{0.0}, hx1{0.0}, hz0{0.0}, hz1{0.0};
if (false == (surface(X - d, Z, hx0) && surface(X + d, Z, hx1) && surface(X, Z - d, hz0) && surface(X, Z + d, hz1)))
return glm::vec3{0.f, 1.f, 0.f};
return glm::vec3(glm::normalize(glm::dvec3{-(hx1 - hx0) / (2.0 * d), 1.0, -(hz1 - hz0) / (2.0 * d)}));
};
gfx::vertex_array vertices;
vertices.reserve(triangles.size());
for (auto const &point : triangles)
{
glm::dvec2 const local{point.x - patch.origin.x, point.z - patch.origin.y};
gfx::basic_vertex vertex;
vertex.position = glm::vec3(point - origin);
vertex.normal = normal_at(point.x, point.z);
vertex.texture = {static_cast<float>(glm::dot(local, along) / repeat), static_cast<float>(-glm::dot(local, across) / repeat)};
vertex.tangent = {static_cast<float>(along.x), 0.f, static_cast<float>(along.y), 1.f};
vertices.push_back(vertex);
}
water_shape entry;
entry.geometry = water_geometry(std::move(vertices));
if (entry.geometry.chunk == 0)
continue;
auto const reach{glm::length(high - low) * 0.5};
std::vector<world_vertex> bounds(2);
bounds[0].position = glm::dvec3{low.x, bottom, low.y};
bounds[1].position = glm::dvec3{high.x, top, high.y};
scene::shape_node shape;
shape.make_terrain(material, std::move(bounds), origin);
shape.geometry(entry.geometry);
section.m_shapes.emplace_back(std::move(shape));
section.m_area.radius = std::max(section.m_area.radius, static_cast<float>(glm::length(section.m_area.center - centre) + reach));
entry.section = &section;
m_watershapes.push_back(entry);
}
}
void terrain_streamer::patches(std::vector<heightmap::surface_patch> Patches)
{
remember_manifest();
m_manifest.patches = std::move(Patches);
m_manifestchanged = true;
m_waterchanged = true;
}