17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 21:29:19 +02:00
Files
maszyna/scene/scenenode.cppm
maj00r 2660d14565 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.
2026-09-16 23:18:09 +02:00

485 lines
14 KiB
C++

module;
#include <array>
#include <chrono>
#include <deque>
#include <mutex>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <sstream>
#include <cstddef>
#include <limits>
#include "global_include/interfaces/ITexture_macros.h"
#include <istream>
#include <ostream>
#include <string>
#include <vector>
export module eu07.simcore:scenenode;
import eu07.glm;
import :fwd;
import :model3d;
import eu07.rendering.geometrybank;
import eu07.utilities.parser;
import eu07.utilities.classes;
import eu07.model.vertex;
import eu07.utilities.uuid;
export {
/*
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/.
*/
struct lighting_data {
glm::vec4 diffuse { 0.8f, 0.8f, 0.8f, 1.0f };
glm::vec4 ambient { 0.2f, 0.2f, 0.2f, 1.0f };
glm::vec4 specular { 0.0f, 0.0f, 0.0f, 1.0f };
// stores content of the struct in provided output stream
void
serialize( std::ostream &Output ) const;
// restores content of the struct from provided input stream
void
deserialize( std::istream &Input );
};
inline
bool
operator==( lighting_data const &Left, lighting_data const &Right ) {
return Left.diffuse == Right.diffuse && Left.ambient == Right.ambient && Left.specular == Right.specular;
}
inline
bool
operator!=( lighting_data const &Left, lighting_data const &Right ) {
return !( Left == Right );
}
namespace scene {
struct bounding_area {
glm::dvec3 center; // mid point of the rectangle
float radius { -1.0f }; // radius of the bounding sphere
bounding_area() = default;
bounding_area( glm::dvec3 Center, float Radius ) :
center( Center ),
radius( Radius )
{}
// stores content of the struct in provided output stream
void
serialize( std::ostream &Output ) const;
// restores content of the struct from provided input stream.
void
deserialize( std::istream &Input, bool const Preserveradius = true );
};
//using group_handle = std::size_t;
struct node_data {
double range_min { 0.0 };
double range_max { std::numeric_limits<double>::max() };
std::string name;
std::string type;
};
// holds unique piece of geometry, covered with single material
class shape_node
{
friend class basic_region; // region might want to modify node content when it's being inserted
public:
// types
struct shapenode_data {
// members:
// placement and visibility
scene::bounding_area area; // bounding area, in world coordinates
double rangesquared_min { 0.0 }; // visibility range, min
double rangesquared_max { 0.0 }; // visibility range, max
bool visible { true }; // visibility flag
// material data
bool translucent { false }; // whether opaque or translucent
material_handle material { null_handle };
lighting_data lighting;
// geometry data
glm::dvec3 origin; // world position of the relative coordinate system origin
gfx::geometry_handle geometry { 0, 0 }; // relative origin-centered chunk of geometry held by gfx renderer
std::vector<world_vertex> vertices; // world space source data of the geometry
gfx::userdata_array userdata;
// methods:
// sends content of the struct to provided stream
void
serialize( std::ostream &Output ) const;
// restores content of the struct from provided input stream
void
deserialize( std::istream &Input );
};
// methods
// sends content of the class to provided stream
void
serialize( std::ostream &Output ) const;
// restores content of the node from provided input stream
shape_node &
deserialize( std::istream &Input );
// restores content of the node from provided input stream
shape_node &
import( cParser &Input, scene::node_data const &Nodedata );
// imports data from provided submodel
shape_node &
convert( TSubModel const *Submodel );
// builds an opaque, always-visible shape from a world-space GL_TRIANGLES vertex list, stored
// relative to Origin. used by the editor's terrain to own editable geometry it can re-upload.
shape_node &
make_terrain( material_handle const Material, std::vector<world_vertex> Vertices, glm::dvec3 const Origin );
// adds content of provided node to already enclosed geometry. returns: true if merge could be performed
bool
merge( shape_node &Shape );
// generates renderable version of held non-instanced geometry in specified geometry bank
void
create_geometry( gfx::geometrybank_handle const &Bank );
// calculates shape's bounding radius
void
compute_radius();
// invalidates shape's bounding radius
void
invalidate_radius();
// set visibility
void
visible( bool State );
// set origin point
void
origin( glm::dvec3 Origin );
// replaces the renderable geometry handle (used by the editor when it re-uploads terrain geometry)
void
geometry( gfx::geometry_handle const &Handle );
// data access
shapenode_data const &
data() const;
// keeps only the triangles a predicate accepts, by their position in the vertex list.
// used to leave out what a baked heightfield already shows
template <typename Predicate_>
void
keep_triangles( Predicate_ &&Keep ) {
std::vector<world_vertex> kept;
kept.reserve( m_data.vertices.size() );
for( std::size_t index = 0; index + 2 < m_data.vertices.size(); index += 3 ) {
if( true == Keep( index / 3 ) ) {
kept.insert( kept.end(), m_data.vertices.begin() + index, m_data.vertices.begin() + index + 3 );
}
}
m_data.vertices = std::move( kept ); }
// get bounding radius
// NOTE: use this method instead of direct access to the data member, due to lazy radius evaluation
float radius();
private:
// members
std::string m_name;
shapenode_data m_data;
};
// set visibility
inline
void
shape_node::visible( bool State ) {
m_data.visible = State;
}
// set origin point
inline
void
shape_node::origin( glm::dvec3 Origin ) {
m_data.origin = Origin;
}
// replaces the renderable geometry handle
inline
void
shape_node::geometry( gfx::geometry_handle const &Handle ) {
m_data.geometry = Handle;
}
// data access
inline
shape_node::shapenode_data const &
shape_node::data() const {
return m_data;
}
// holds a group of untextured lines
class lines_node
{
friend class basic_region; // region might want to modify node content when it's being inserted
public:
// types
struct linesnode_data {
// members:
// placement and visibility
scene::bounding_area area; // bounding area, in world coordinates
double rangesquared_min { 0.0 }; // visibility range, min
double rangesquared_max { 0.0 }; // visibility range, max
bool visible { true }; // visibility flag
// material data
float line_width { 1.f }; // thickness of stored lines
lighting_data lighting;
// geometry data
glm::dvec3 origin; // world position of the relative coordinate system origin
gfx::geometry_handle geometry { 0, 0 }; // relative origin-centered chunk of geometry held by gfx renderer
std::vector<world_vertex> vertices; // world space source data of the geometry
gfx::userdata_array userdata;
// methods:
// sends content of the struct to provided stream
void
serialize( std::ostream &Output ) const;
// restores content of the struct from provided input stream
void
deserialize( std::istream &Input );
};
// methods
// sends content of the class to provided stream
void
serialize( std::ostream &Output ) const;
// restores content of the node from provided input stream
lines_node &
deserialize( std::istream &Input );
// restores content of the node from provided input stream
lines_node &
import( cParser &Input, scene::node_data const &Nodedata );
// adds content of provided node to already enclosed geometry. returns: true if merge could be performed
bool
merge( lines_node &Lines );
// generates renderable version of held non-instanced geometry in specified geometry bank
void
create_geometry( gfx::geometrybank_handle const &Bank );
// calculates shape's bounding radius
void
compute_radius();
// set visibility
void
visible( bool State );
// set origin point
void
origin( glm::dvec3 Origin );
// data access
linesnode_data const &
data() const;
private:
// members
std::string m_name;
linesnode_data m_data;
};
// set visibility
inline
void
lines_node::visible( bool State ) {
m_data.visible = State;
}
// set origin point
inline
void
lines_node::origin( glm::dvec3 Origin ) {
m_data.origin = Origin;
}
// data access
inline
lines_node::linesnode_data const &
lines_node::data() const {
return m_data;
}
/*
// holds geometry for specific piece of track/road/waterway
class path_node {
friend class basic_region; // region might want to modify node content when it's being inserted
public:
// types
// TODO: enable after track class refactoring
struct pathnode_data {
// placement and visibility
bounding_area area; // bounding area, in world coordinates
bool visible { true }; // visibility flag
// material data
material_handle material_1 { 0 };
material_handle material_2 { 0 };
lighting_data lighting;
TEnvironmentType environment { e_flat };
// geometry data
std::vector<world_vertex> vertices; // world space source data of the geometry
glm::dvec3 origin; // world position of the relative coordinate system origin
using geometryhandle_sequence = std::vector<geometry_handle>;
geometryhandle_sequence geometry_1; // geometry chunks textured with texture 1
geometryhandle_sequence geometry_2; // geometry chunks textured with texture 2
};
// methods
// restores content of the node from provded input stream
// TODO: implement
path_node &
deserialize( cParser &Input, node_data const &Nodedata );
// binds specified track to the node
// TODO: remove after track class refactoring
void
path( TTrack *Path ) {
m_path = Path; }
TTrack *
path() {
return m_path; }
private:
// members
// // TODO: enable after track class refactoring
// pathnode_data m_data;
TTrack * m_path;
};
*/
// base interface for nodes which can be actvated in scenario editor
class basic_node {
public:
// constructor
explicit basic_node( scene::node_data const &Nodedata );
// destructor
virtual ~basic_node() = default;
// methods
// sends content of the class to provided stream
void
serialize( std::ostream &Output ) const;
// restores content of the class from provided stream
void
deserialize( std::istream &Input );
// sends basic content of the class in legacy (text) format to provided stream
void
export_as_text( std::ostream &Output ) const;
void
export_as_text( std::string &Output ) const;
std::string const &
name() const;
virtual std::string tooltip() const;
void
location( glm::dvec3 const Location );
glm::dvec3 const &
location() const;
glm::dvec3 &
location();
float const &
radius();
void
visible( bool const Visible );
bool
visible() const;
void
group( scene::group_handle Group );
scene::group_handle
group() const;
void
mark_dirty() { m_dirty = true; }
bool
dirty() const { return m_dirty; }
std::string node_type;
public:
// members
scene::group_handle m_group { null_handle }; // group this node belongs to, if any
scene::bounding_area m_area;
double m_rangesquaredmin { 0.0 }; // visibility range, min
double m_rangesquaredmax { 0.0 }; // visibility range, max
bool m_visible { true }; // visibility flag
std::string m_name;
bool m_dirty { false };
UID uuid;
private:
// methods
// radius() subclass details, calculates node's bounding radius
virtual float radius_();
// serialize() subclass details, sends content of the subclass to provided stream
virtual void serialize_( std::ostream &Output ) const = 0;
// deserialize() subclass details, restores content of the subclass from provided stream
virtual void deserialize_( std::istream &Input ) = 0;
// export() subclass details, sends basic content of the class in legacy (text) format to provided stream
virtual void export_as_text_( std::ostream &Output ) const = 0;
};
inline
std::string const &
basic_node::name() const {
return m_name;
}
// Returns the tooltip of this Node when hovered with the mouse cursor.
inline
std::string basic_node::tooltip() const
{
return m_name;
}
inline
void
basic_node::location( glm::dvec3 const Location ) {
m_area.center = Location;
}
inline
glm::dvec3 const &
basic_node::location() const {
return m_area.center;
}
inline
glm::dvec3 &
basic_node::location() {
return m_area.center;
}
inline
void
basic_node::visible( bool const Visible ) {
m_visible = Visible;
}
inline
bool
basic_node::visible() const {
return m_visible;
}
inline
void
basic_node::group( scene::group_handle Group ) {
m_group = Group;
}
inline
scene::group_handle
basic_node::group() const {
return m_group;
}
} // scene
//---------------------------------------------------------------------------
} // export