17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 23:49:19 +02:00
Files
maszyna/rendering/terrainclipmap.cppm
maj00r b877bc0772 Cook the ground into a quadtree of mesh tiles, and keep the mesh the scenery drew
The ground in a scenery is already an irregular mesh, so the cook keeps it: it cuts the
triangles along the tile grid, welds what coincides, and builds each coarser level by
collapsing edges. Nothing is resampled, nothing is interpolated, and a hole the scenery left
stays a hole. What was here before threw the triangles away, kept their vertices and built a
fresh Delaunay triangulation over them, which filled every hole, merged the two heights of
every retaining wall into one, and - because it never erased the super-triangle - emitted
indices three too large together with triangles reaching out to the artificial corners.

Tiles form a quadtree, as Cesium Quantized Mesh intends. A distant tile is a large one, so the
horizon costs tens of tiles rather than the thousands a fixed grid needed, and the level to
draw follows from the tile's own geometric error against what the screen can resolve - which is
what the format carries that field for. Every vertex on a tile's border survives into all of
its coarser levels, and so does every edge between two of them, so two neighbours drawn at
different levels share their border exactly: there is nothing to stitch and nothing to morph.

The file layout is the format's, with two deliberate departures, both written down in
quantizedmeshformat.h: the geodetic header fields carry scenery metres, there being no
geographic reference to convert from, and the material a vertex belongs to rides in an
extension of ours. Texture coordinates are not stored at all - ground tiles in world space, so
the coordinate follows from the position and from how many metres one repeat of the material
covers, measured off the scenery once and kept in the terrain's table.

Tiles live in one archive beside the scenery, an entry each, compressed on its own. Baltyk
cooks to 546 kB against 2.2 MB of heightfields, quark to 361 kB against 9.6 MB, tomaszewo to
375 kB against 9.9 MB.

Drawing is one call a frame: every resident tile lives in the same vertex and index buffer,
with its vertices measured from the middle of the terrain so that the whole map fits in floats,
and a frame is one glMultiDrawElementsBaseVertex over the tiles the walk chose and the frustum
kept. On baltyk that settles at 291 tiles resident, 16 MB of mesh, 85 tiles and 6k triangles
drawn.

What counts as ground is what the scenery says, not what its triangles look like. A node under
origin, rotate or scale is an object standing on the ground; a material with an alpha channel is
a tree, a bush, a fence or a catenary mast. Everything else is laid whichever way it faces, so
the side of an embankment or a cutting goes in with the rest instead of leaving a hole along
every bank. Whatever is left out is recorded per node, and the scenery goes on drawing it.

A cook is a cache: it is used again only when the archive, the note of which nodes went into it
and the manifest of what the scenery was made from all agree. A manifest listing a file the
scenery names but does not have used to fail that test for ever, since an absent file never
matches - quark has such an include, and nothing beside it could ever be reused.

tools/terraincook cooks the same way outside the engine, and meshtest beside it checks what the
cook promises: a tile reads back as written, neighbouring tiles agree on their border to the
millimetre, a hole is not filled in, a wall is laid exactly once, and a material's tiling comes
out of the source. seamcheck.py goes: it tested the morphing constants of a heightfield nothing
reads any more.
2026-09-20 08:04:46 +02:00

174 lines
7.5 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
module;
#include <array>
#include <cstdint>
#include <functional>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "scene/quantizedmeshreader.h"
export module eu07.rendering.terrainclipmap;
import eu07.gl.shader;
import eu07.glm;
import eu07.rendering.terraintileloader;
import eu07.rendering.terrainstatus;
import eu07.rendering.terrainmesharena;
import eu07.rendering.terrainground;
export {
// Draws a scenery's cooked terrain: a quadtree of mesh tiles, each a piece of the ground as the
// scenery drew it, thinned out the further from the camera it is drawn.
//
// Each frame a walk of the quadtree settles on the tiles to draw: a tile is split when the ground
// it stands for would be off by more pixels than the detail setting allows, and only when all four
// of its children are on the card - otherwise it is drawn itself and the children are asked for, so
// that streaming shows coarse ground rather than a hole. Because every vertex on a tile's border
// survives into all of its coarser levels, two neighbours drawn at different levels share their
// border exactly, and nothing has to be stitched or morphed.
//
// Every resident tile lives in the same pair of buffers, so the whole terrain is drawn in one call.
class terrain_clipmap {
public:
// whether a sphere, in world space, can be seen: centre and radius
using visibility = std::function<bool( glm::dvec3 const &, float )>;
terrain_clipmap() = default;
~terrain_clipmap();
terrain_clipmap( terrain_clipmap const & ) = delete;
terrain_clipmap &operator=( terrain_clipmap const & ) = delete;
// opens a cooked terrain - an archive or a directory of tiles - and builds the gl resources.
// returns false when it cannot be read, leaving the object unusable but safe to destroy
bool open( std::string const &Path );
bool ready() const { return m_ready; }
void close();
// walks the quadtree, asks for what is missing, puts in what has arrived and retires what has
// not been wanted for a while. safe to call every frame: the reading happens on the loader's
// thread
void update( glm::dvec3 const &Viewpoint );
// draws the tiles the walk settled on that the test says can be seen
void render( glm::dvec3 const &Viewpoint, visibility const &Visible );
// how far from the viewpoint terrain is drawn, in metres
void range( float const Range ) { m_range = Range; }
float range() const { return m_range; }
// what the screen can resolve: the viewport's pixels per unit of size over distance, and how
// many pixels a tile's error may cover before a finer level is used
void detail( double const Pixelsperunit, double const Pixels );
// side of a tile at the finest level, in metres. terrains are drawn finest first, so that the
// depth test keeps the better surface where two of them describe the same ground
float gridstep() const { return m_finesttile; }
// pushes this terrain back in depth by the given rank, so that geometry lying on it wins the
// depth test, and a coarser terrain loses to a finer one describing the same ground
void depthrank( unsigned const Rank ) { m_depthrank = Rank; }
terrain_statistics const &stats() const { return m_stats; }
std::string const &path() const { return m_path; }
float tilesize() const { return m_finesttile; }
std::uint32_t levels() const { return m_levels; }
std::size_t backlog() const { return m_loader.backlog(); }
float detailpixels() const { return static_cast<float>( m_detail ); }
// side of a tile at each level, and how far that level's ground is off, for the debug panel
float leveltile( std::uint32_t const Level ) const;
float levelerror( std::uint32_t const Level ) const;
private:
// one tile held in the buffers
struct resident_tile {
terrain_mesh_arena::piece piece;
// for culling, in world space
glm::dvec3 centre { 0.0 };
float radius { 0.f };
std::uint32_t level { 0 };
std::uint64_t lastused { 0 };
};
// the tiles the walk settled on, in the order they will be drawn
void walk( glm::dvec3 const &Viewpoint );
// asks the loader for every tile the wanted cut of the quadtree is missing, and says whether this
// tile and everything under it is already there
bool need( quantizedmesh::tile_address const &Tile, glm::dvec3 const &Viewpoint );
// whether the level this tile stands for is close enough to the ground below it
bool good_enough( quantizedmesh::tile_address const &Tile, double Distance ) const;
void choose( quantizedmesh::tile_address const &Tile, glm::dvec3 const &Viewpoint );
// where a tile's square is, and how far the viewpoint is from it
void square( quantizedmesh::tile_address const &Tile, double &West, double &North, double &Side ) const;
double distance_to( quantizedmesh::tile_address const &Tile, glm::dvec3 const &Viewpoint ) const;
bool present( quantizedmesh::tile_address const &Tile ) const;
void ask_for( quantizedmesh::tile_address const &Tile, double Distance );
void put_in( terrain_tile_loader::payload const &Tile );
void retire_unused();
void resolve_uniforms();
void report_residency();
quantizedmesh::reader m_reader; // the table and which tiles there are; reading is the loader's
terrain_tile_loader m_loader;
terrain_mesh_arena m_arena;
terrain_ground m_ground;
quantizedmesh::terrain_table m_table;
glm::dvec3 m_origin { 0.0 }; // what the vertices in the buffers are measured from
float m_finesttile { 256.f };
std::uint32_t m_levels { 1 };
std::unordered_map<std::int64_t, resident_tile> m_tiles;
std::vector<std::int64_t> m_chosen;
std::vector<std::pair<double, quantizedmesh::tile_address>> m_wishes;
std::vector<quantizedmesh::tile_address> m_wanted;
std::vector<terrain_tile_loader::payload> m_arrived;
std::unordered_set<std::int64_t> m_asked; // what the last walk asked the loader for
// whether a subtree could be drawn, worked out once per walk
std::unordered_map<std::int64_t, bool> m_readiness;
// what the draw hands to gl, kept between frames so that a frame allocates nothing
std::vector<std::int32_t> m_counts;
std::vector<void const *> m_offsets;
std::vector<std::int32_t> m_bases;
glm::dvec3 m_walkpoint { 0.0 };
bool m_walked { false };
double m_pixelsperunit { 0.0 };
double m_detail { 3.0 };
double m_walkdetail { 0.0 };
float m_range { 6000.f };
float m_walkrange { -1.f };
std::optional<gl::program> m_shader;
struct uniform_locations {
int origin { -1 }; // where the terrain's origin is, relative to the camera
int world { -1 }; // and where it is in the world
int ground { -1 };
int materials { -1 };
} m_uniforms;
terrain_statistics m_stats;
std::uint64_t m_frame { 0 };
std::size_t m_reportedresidency { 0 };
std::size_t m_reporteddrawn { 0 };
std::uint64_t m_reportedframe { 0 };
bool m_reported { false };
unsigned m_depthrank { 0 };
// what the buffers may hold before tiles nobody asked for lately are let go
std::size_t m_budget { 192u << 20 };
std::size_t m_putsperframe { 16 };
bool m_ready { false };
std::string m_path;
};
} // export