mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 22:39:19 +02:00
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.
132 lines
3.4 KiB
C++
132 lines
3.4 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 <algorithm>
|
|
#include <cstddef>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include "scene/quantizedmeshreader.h"
|
|
|
|
module eu07.rendering.terraintileloader;
|
|
|
|
terrain_tile_loader::~terrain_tile_loader() {
|
|
|
|
close();
|
|
}
|
|
|
|
bool
|
|
terrain_tile_loader::open(
|
|
std::string const &Path, double const Originx, double const Originy, double const Originz ) {
|
|
|
|
close();
|
|
if( false == m_reader.open( Path ) ) { return false; }
|
|
m_originx = Originx;
|
|
m_originy = Originy;
|
|
m_originz = Originz;
|
|
m_stop = false;
|
|
m_thread = std::thread( &terrain_tile_loader::work, this );
|
|
return true;
|
|
}
|
|
|
|
void
|
|
terrain_tile_loader::close() {
|
|
|
|
if( true == m_thread.joinable() ) {
|
|
{
|
|
std::lock_guard<std::mutex> lock( m_mutex );
|
|
m_stop = true;
|
|
}
|
|
m_wake.notify_all();
|
|
m_thread.join();
|
|
}
|
|
std::lock_guard<std::mutex> lock( m_mutex );
|
|
m_pending.clear();
|
|
m_done.clear();
|
|
m_running.reset();
|
|
m_reader.close();
|
|
}
|
|
|
|
bool
|
|
terrain_tile_loader::underway( quantizedmesh::tile_address const &Tile ) const {
|
|
|
|
if( ( m_running.has_value() ) && ( *m_running == Tile ) ) { return true; }
|
|
return std::any_of(
|
|
m_done.begin(), m_done.end(),
|
|
[ &Tile ]( payload const &Done ) { return Done.data.tile == Tile; } );
|
|
}
|
|
|
|
void
|
|
terrain_tile_loader::want( std::vector<quantizedmesh::tile_address> const &Requests ) {
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock( m_mutex );
|
|
m_pending.clear();
|
|
for( auto const &tile : Requests ) {
|
|
if( false == underway( tile ) ) { m_pending.push_back( tile ); }
|
|
}
|
|
}
|
|
m_wake.notify_one();
|
|
}
|
|
|
|
std::size_t
|
|
terrain_tile_loader::collect( std::vector<payload> &Out, std::size_t const Count ) {
|
|
|
|
std::unique_lock<std::mutex> lock( m_mutex );
|
|
std::size_t moved { 0 };
|
|
while( ( moved < Count ) && ( false == m_done.empty() ) ) {
|
|
Out.emplace_back( std::move( m_done.front() ) );
|
|
m_done.pop_front();
|
|
++moved;
|
|
}
|
|
lock.unlock();
|
|
if( moved > 0 ) {
|
|
// room was made; the thread may be waiting for it
|
|
m_wake.notify_one();
|
|
}
|
|
return moved;
|
|
}
|
|
|
|
std::size_t
|
|
terrain_tile_loader::backlog() const {
|
|
|
|
std::lock_guard<std::mutex> lock( m_mutex );
|
|
return m_pending.size() + m_done.size() + ( m_running.has_value() ? 1 : 0 );
|
|
}
|
|
|
|
void
|
|
terrain_tile_loader::work() {
|
|
|
|
while( true ) {
|
|
quantizedmesh::tile_address tile;
|
|
{
|
|
std::unique_lock<std::mutex> lock( m_mutex );
|
|
m_wake.wait( lock, [ this ]() {
|
|
return m_stop || ( ( false == m_pending.empty() ) && ( m_done.size() < donelimit ) ); } );
|
|
if( true == m_stop ) { return; }
|
|
tile = m_pending.front();
|
|
m_pending.pop_front();
|
|
m_running = tile;
|
|
}
|
|
|
|
// the read itself happens outside the lock, so the render thread can replace the wish
|
|
// list while a tile is being decoded
|
|
payload result;
|
|
result.data.tile = tile;
|
|
result.valid = m_reader.read_tile( tile, result.data, m_originx, m_originy, m_originz );
|
|
|
|
std::lock_guard<std::mutex> lock( m_mutex );
|
|
m_running.reset();
|
|
m_done.emplace_back( std::move( result ) );
|
|
}
|
|
}
|