mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-18 04:29:18 +02:00
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.
This commit is contained in:
@@ -23,49 +23,18 @@ export module eu07.rendering.terraintileloader;
|
||||
|
||||
export {
|
||||
|
||||
// Reads and decodes TIN (Quantized Mesh) terrain tiles on a thread of its own.
|
||||
// Reads and decodes terrain mesh tiles on a thread of its own.
|
||||
//
|
||||
// Reads .qm tile files, dequantizes vertices, and hands back ready-to-upload vertex/index
|
||||
// data. Does the I/O and decompression off the render thread. The renderer uploads at its own
|
||||
// pace, and the loader prioritizes nearest tiles first.
|
||||
//
|
||||
// Replaces heightfield tile loading entirely - TIN is the only terrain format now.
|
||||
// Reading a tile means finding it in the archive, unpacking it and turning the quantized vertices
|
||||
// back into something a vertex buffer will take. None of that belongs on the render thread. The
|
||||
// renderer says which tiles it wants, nearest first, and collects whatever has arrived at its own
|
||||
// pace.
|
||||
class terrain_tile_loader {
|
||||
|
||||
public:
|
||||
struct request {
|
||||
std::int32_t x { 0 }, z { 0 };
|
||||
std::uint32_t level { 0 };
|
||||
|
||||
bool operator==( request const & ) const = default;
|
||||
};
|
||||
|
||||
struct payload {
|
||||
request tile;
|
||||
quantizedmesh::reader::tile_data data;
|
||||
bool valid { false };
|
||||
|
||||
// Vertex data (dequantized, ready for GPU)
|
||||
std::vector<double> positions_x;
|
||||
std::vector<double> positions_y;
|
||||
std::vector<double> positions_z;
|
||||
std::vector<float> uvs_u;
|
||||
std::vector<float> uvs_v;
|
||||
std::vector<std::uint8_t> materials;
|
||||
|
||||
// Index data
|
||||
std::vector<std::uint32_t> indices;
|
||||
|
||||
// Edge indices for seamless LOD stitching
|
||||
std::vector<std::uint16_t> north_edge;
|
||||
std::vector<std::uint16_t> south_edge;
|
||||
std::vector<std::uint16_t> west_edge;
|
||||
std::vector<std::uint16_t> east_edge;
|
||||
|
||||
// Tile bounds (for placement)
|
||||
double center_x, center_y, center_z;
|
||||
double min_x, min_y, min_z;
|
||||
double max_x, max_y, max_z;
|
||||
double geometric_error;
|
||||
};
|
||||
|
||||
terrain_tile_loader() = default;
|
||||
@@ -73,14 +42,15 @@ public:
|
||||
terrain_tile_loader( terrain_tile_loader const & ) = delete;
|
||||
terrain_tile_loader &operator=( terrain_tile_loader const & ) = delete;
|
||||
|
||||
// opens the file on the loader's own handle and starts the thread
|
||||
bool open( std::string const &Path );
|
||||
// opens the source on the loader's own handle and starts the thread. Origin is what the
|
||||
// vertices come back measured from
|
||||
bool open( std::string const &Path, double Originx, double Originy, double Originz );
|
||||
// stops the thread; anything not collected is dropped
|
||||
void close();
|
||||
|
||||
// replaces the list of wanted tiles, nearest first. tiles being read or already read and
|
||||
// not yet collected are left out of it, so asking again every frame costs nothing
|
||||
void want( std::vector<request> const &Requests );
|
||||
// replaces the list of wanted tiles, nearest first. tiles being read or already read and not
|
||||
// yet collected are left out of it, so asking again every frame costs nothing
|
||||
void want( std::vector<quantizedmesh::tile_address> const &Requests );
|
||||
// moves up to Count finished tiles into Out, returning how many
|
||||
std::size_t collect( std::vector<payload> &Out, std::size_t const Count );
|
||||
// tiles queued or finished and not yet collected
|
||||
@@ -88,20 +58,21 @@ public:
|
||||
|
||||
private:
|
||||
void work();
|
||||
// whether the tile is being read or waits to be collected. caller holds the lock
|
||||
bool underway( request const &Tile ) const;
|
||||
// whether the tile is being read or waits to be collected. the caller holds the lock
|
||||
bool underway( quantizedmesh::tile_address const &Tile ) const;
|
||||
|
||||
quantizedmesh::reader m_reader;
|
||||
double m_originx { 0.0 }, m_originy { 0.0 }, m_originz { 0.0 };
|
||||
std::thread m_thread;
|
||||
mutable std::mutex m_mutex;
|
||||
std::condition_variable m_wake;
|
||||
std::deque<request> m_pending;
|
||||
std::optional<request> m_running;
|
||||
std::deque<quantizedmesh::tile_address> m_pending;
|
||||
std::optional<quantizedmesh::tile_address> m_running;
|
||||
std::deque<payload> m_done;
|
||||
bool m_stop { false };
|
||||
// finished tiles allowed to wait for collection. past this the thread stops reading:
|
||||
// the renderer uploads at its own pace, and a backlog read for where the camera was a
|
||||
// second ago is work spent on tiles nobody wants any more
|
||||
// finished tiles allowed to wait for collection. past this the thread stops reading: the
|
||||
// renderer uploads at its own pace, and a backlog read for where the camera was a second ago
|
||||
// is work spent on tiles nobody wants any more
|
||||
static constexpr std::size_t donelimit { 32 };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user