17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-31 09:59:06 +01:00

Stream the scenery: a baked terrain heightfield, a logic container, and prefabs

Today's work on the binary scene format plan (binary-scene-format-plan.md), from the
first measurement to a terrain that bakes itself on first run and streams from a
thread. The step-by-step history is kept on backup/scene-streaming-format-presquash-0916.

Measuring first. -loadprofile times a scenery load by stage and by node kind and reports
node counts and resident memory. On l204 terrain is 95% of the bytes and 99.8% of the
nodes; on quarkmce2007 vehicles, not models, dominate what is left. The terrain turned
out to be a heightfield in all but name, a ribbon covering 12% of its bounding box.

The heightfield. scene/heightfieldformat.h is the .ehf layout: Morton-ordered tiles
of 128 samples with one shared edge sample, a point-sampled mip chain, zstd per tile,
a nodata sentinel, a material palette with a measured tiling per material, and a stamp
of the rules that decided what counted as terrain. scene/heightfieldreader.h reads it
for the tool, the renderer and the loader alike, and scene/terraincooker.h writes it,
shared by the terraincook tool and the engine so a first-run bake is the file the tool
would have made. The cooker rasterises every triangle where it is drawn (through
scene::place_in_world, the placement the region applies too), keeps an upper surface
laid within half a metre over another and the lower of two further apart, and then
asks each triangle whether the heightfield shows it: laid, reaching a sample, keeping
nearly all it claimed, and with the ground around it drawn at every level out to 2 km.

Baking on first run. simulation/terrainbake bakes <scenario>.ehf the first time a
scenery is opened, choosing the grid step from the first 200,000 vertices, and on later
loads leaves out the triangles its node table says the heightfield shows. Everything
else - trees, grass tufts, gantries, bridge decks - is drawn as written, so a bad bake
can at worst draw something twice, and the heightfield sits 2 cm under the triangles so
the original wins there. Nodes are keyed by file, line, include parameters and
placement; an edited file changes its keys and those nodes are drawn again. A bake under
older rules, or without its table, is thrown away and redone. A launcher copy named
$scenario shares the scenario's files. -nobaketerrain shows the triangles as written.
A scenery can still name hand-cooked fields, several at different steps.

Drawing and streaming. rendering/terrainclipmap draws resident tiles at the mip level
their distance earns, deriving the grid from gl_VertexID and texturing in world space
with the scenery's own materials. rendering/terraintileloader reads and decodes tiles on
a thread of its own; the renderer rescans what is in range only after moving a quarter
of a tile, asks for what is missing nearest first, uploads at most 32 tiles a frame,
drops arrivals the camera has moved past, and keeps drawing a tile at the level it has
until the right one arrives. The terrain has its own range, terrainrange in the ini and
a slider in the debug panel, 20 km by default and independent of basedrawrange; the far
plane of an ordinary view is pushed out to it. The debug panel shows the range, tiles in
view, on the gpu, wanted and queued, and how many sit at each level.

Events. The event queue moved out of the events into a binary heap with a deterministic
tie-break, and an event is handed its activator instead of storing it, so basic_event
is a definition and nothing else.

The logic container. scene/logicformat.h and scene/logiccontainer write a scenery's
events, their targets and sibling chains as flat, deterministic arrays with a schema
hash that refuses a mismatched build. simulation/logiccook cooks it from the loaded
state whenever it is missing, stale or unreadable, reads it straight back to check it,
and resolves each target to a position in the registry its event kind searches.
-cooklogic forces a cook, -verifylogic only checks. Nothing reads it at load yet.

Prefabs. scene/prefab describes a signal by the aspects it can show - lights, speeds,
animation channels, a sound - instead of pasting an .inc per variant, and expands an
instance to ordinary scenery text. Tried on a light signal and a shape signal.

Screenshots without a person present: -shot x,y,z[,yaw[,pitch]], -shotsettle, -shotstay.
This commit is contained in:
maj00r
2026-09-16 23:18:09 +02:00
parent 366645ef59
commit 2660d14565
47 changed files with 7117 additions and 240 deletions

View File

@@ -1627,7 +1627,15 @@ void opengl33_renderer::setup_pass(viewport_config &Viewport, renderpass_config
Config.draw_range = std::max( 2000.f, Config.draw_range );
// projection
auto const zfar = ( Zfar > 1.f ? Zfar : Config.draw_range * Zfar );
auto zfar = ( Zfar > 1.f ? Zfar : Config.draw_range * Zfar );
if ((false == Ignoredebug) && (Zfar == 1.f))
{
// the far plane of an ordinary view reaches as far as the terrain is streamed, which
// is its own setting rather than the draw range. only the projection moves: what
// else gets drawn is still decided by draw_range. reverse z on a float depth
// buffer keeps the precision this costs negligible
zfar = std::max(zfar, Global.TerrainRange);
}
auto const znear = ( Znear > 1.f ? Znear : Znear > 0.f ? Znear * zfar : 0.1f * Global.ZoomFactor);
// the editor's top-down plan view drops the perspective, so distances read the same wherever
@@ -2467,6 +2475,7 @@ void opengl33_renderer::Render(scene::basic_region *Region)
{
case rendermode::color:
{
Render_terrain();
Render(std::begin(m_sectionqueue), std::end(m_sectionqueue));
// draw queue is filled while rendering sections
if (EditorModeFlag && m_current_viewport->main)
@@ -2851,6 +2860,84 @@ void opengl33_renderer::Render(cell_sequence::iterator First, cell_sequence::ite
m_frame_instance_buckets.clear();
}
// The heightfield draws itself; what it needs from here is the state every other draw in
// this pass gets - the camera-relative modelview in the model block - and the viewpoint
// the world is being rendered around, so it can place its tiles relative to it.
void opengl33_renderer::Render_terrain()
{
// opened on first use rather than at init: a scenery names its heightfields in
// terrain directives, which are not read until the scenario is parsed, long after the
// renderer starts. attempted once either way, so a bad path does not retry per frame
if (false == m_terrainattempted)
{
m_terrainattempted = true;
for (auto const &path : Global.terrain_heightfields)
{
auto field{std::make_unique<terrain_clipmap>()};
if (true == field->open(path))
{
m_terrain.emplace_back(std::move(field));
}
}
// finest first: drawn in this order, the depth test keeps the better surface
// where two fields describe the same ground
std::sort(std::begin(m_terrain), std::end(m_terrain),
[](auto const &Left, auto const &Right) { return Left->gridstep() < Right->gridstep(); });
for (std::size_t index = 0; index < m_terrain.size(); ++index)
{
// every field sits a little under the triangles, so where a baked heightfield and
// the geometry it was baked from are both drawn, the geometry is what shows. a
// coarser field is sunk further still, far enough that rounding cannot let it
// through, and little enough to be invisible at the spacing it is drawn at
m_terrain[index]->depthbias(index == 0 ? 0.02f : 0.1f);
}
}
if (true == m_terrain.empty())
{
return;
}
glm::dvec3 const viewpoint{m_renderpass.pass_camera.position()};
model_ubs.set_modelview(OpenGLMatrices.data(GL_MODELVIEW));
model_ubo->update(model_ubs);
for (auto const &field : m_terrain)
{
// what stays on the gpu is decided by the main view alone. any other colour pass
// draws what is there: letting a second viewport with its own camera and range
// steer residency would evict what the main view is looking at
if (m_current_viewport->main)
{
// the terrain has a range of its own, not the pass draw range
field->range(Global.TerrainRange);
field->update(viewpoint);
}
field->render(viewpoint);
}
if (m_current_viewport->main)
{
TerrainStatus.resize(m_terrain.size());
for (std::size_t index = 0; index < m_terrain.size(); ++index)
{
auto const &field{*m_terrain[index]};
auto &status{TerrainStatus[index]};
if (status.path.empty())
{
status.path = field.path();
status.gridstep = field.gridstep();
status.tilesize = field.tilesize();
status.levels = field.levels();
}
status.range = field.range();
status.backlog = field.backlog();
status.stats = field.stats();
}
}
}
void opengl33_renderer::Draw_Geometry(std::vector<gfx::geometrybank_handle>::iterator begin, std::vector<gfx::geometrybank_handle>::iterator end)
{
m_geometry.draw(begin, end);