18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 22:44:59 +01:00
Files
maszyna/scene/scenenode.h
maj00r 39e8bcc59d fix(editor): the orthophoto shown one of three ways, the terrain made from the NMT under one rule and around the camera (#334)
* fix(editor): the orthophoto shown one of three ways, chosen in one place, and laid on the terrain without flicker

The settings of the orthophoto had two checkboxes ("Fit to terrain", "Covered by
objects and terrain") and three heights greyed out by turns, and the user couldn't
tell what they did. They're one choice now, each way told under it:

- over the view: drawn over everything, see-through, to compare with the map
- on the terrain, in the scene: tracks and models cover it, lit, the mouse
  places objects on it
- a flat sheet in the scene: for a scenery with no terrain yet

The lift is shown only where it matters (on the terrain, in the scene), the
height is named for what it is in each way ("where there's no ground"), and the
ways in the scene are greyed out with the Better Renderer, which doesn't show
them.

On the terrain in the scene the photos are a copy of the ground's triangles 5 cm
up, which the depth buffer lost further away: they flickered with the terrain
textures. Their shapes are decals now, drawn with a polygon offset towards the
camera (the depth is reversed, so a positive one) in every pass but the shadows.

The translations hold the strings of the terrain of the download area as well.

* fix(editor): the terrain made from the NMT under one rule, also around the camera within the download area

The download area didn't bring the terrain: the NMT squares in it were fetched
only once the model had been placed (its panel opened) and the scenery had
terrain open, and nothing told why. They're fetched from the area as it is now,
in PUWG 1992 already.

Where a chunk is made from the model where there's no terrain, the rule is one
for the track laid, the area and the buttons of the panel (nmt_chunk_offset):
beside terrain made from the model it goes on at its height over the model,
otherwise (terrain made by hand, or none) at the offset of the scenery; no chunk
is left out for what lies beside it. Before, the terrain under the track laid
needed terrain in the scenery and a chunk of it beside, gave up chunks for the
session otherwise, and the model was placed only by its panel.

The terrain of the area: "The terrain of the scenery around the camera" (on by
default, kept with the editor) makes the chunks missing in the area in a square
of 9 x 9 around the camera, the nearest first, as the camera goes. Light: a chunk
at most every 0.1 s and three times as long as it took left to the editor, after
the work with the model the user asked for; the step of the terrain history the
editor keeps open is closed around each chunk, as for the terrain grown while
laying. Not the whole area at once: the chunks made wait in memory to be saved.
The log tells what holds it.

The download of the area gives way to the squares asked for: a square of the
area being downloaded is broken off as one is asked for (the terrain under the
track laid) and taken up again later.

The map no longer covers the area with green over the tiles in the cache, only
the failed ones are red.

* fix(editor): the terrain under the tracks follows them again, a brush stroke keeps its undo, the area terrain timed

From the review of the terrain made from the NMT under one rule:

- The terrain drawn to the tracks place by place (under the tracks) took the
  height of the chunk beside it, carrying one offset along a run of chunks,
  metres off the tracks: nmt_chunk_offset gives the offsets of the tracks there
  again. The height the track laid goes on at (state.ahead) comes from the
  terrain grown under it only, not from the chunks of the buttons.
- A chunk of the area made during a stroke of the terrain brush closed the
  stroke's step of the history, and its undo lost the part brushed before: the
  area terrain waits for the stroke to end.
- In a scenery with no name yet the terrain under the track laid was asked for
  and dropped every half a second, the log filled: nothing is asked for there.

The log tells how long the chunks of the square around the camera took, and the
self-test sets the area terrain on or off for its run (nmtsettings area 0|1).
Measured on lukow.scn: 1.3-1.5 ms a chunk, 2.2 ms the longest; the longest frame
17 ms against 10 ms without it, none over 33 ms.

* fix(editor): the selftest turns the area terrain on or off for its run only, the setting kept

---------

Co-authored-by: maj00r <maj00r@host>
2026-11-12 05:04:14 +01:00

508 lines
15 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/.
*/
#pragma once
#include <vector>
#include "utilities/Classes.h"
#include "model/material.h"
#include "model/vertex.h"
#include "rendering/geometrybank.h"
#include "../utilities/uuid.hpp"
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;
layer_handle layer { null_handle }; // scenery layer the node is defined in, if any
instance_handle instance { 0 }; // include of an *.inc template which defines the node, if it isn't defined directly by a scenery layer file
};
// 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
friend class terrain_file; // binary terrain files hold complete content of the nodes
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;
// terrain file the shape comes from, see scene::terrain_file::reference_of(); 0: none, or not known (only the editor asks)
std::uint16_t terrainfile { 0 };
// drawn pulled towards the camera in the depth buffer, over the surface it lies on: the photos of the editor laid on the
// terrain. not kept by serialize()
bool decal { false };
// 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 );
// see shapenode_data::decal
void
decal( bool const Decal ) { m_data.decal = Decal; }
// 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 );
// set lighting (used by generated geometry painted with plain colour)
void
lighting( lighting_data const &Lighting );
// sets whether the shape is drawn with the translucent geometry (used by generated geometry)
void
translucent( bool const Translucent );
// notes the terrain file the shape comes from
void
source_file( std::uint16_t const Reference ) { m_data.terrainfile = Reference; }
// data access
shapenode_data const &
data() const;
// 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;
}
// set lighting
inline
void
shape_node::lighting( lighting_data const &Lighting ) {
m_data.lighting = Lighting;
}
inline
void
shape_node::translucent( bool const Translucent ) {
m_data.translucent = Translucent;
}
// 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();
// the radius as worked out already, or worked out anew and not kept
float
radius() const;
void
visible( bool const Visible );
bool
visible() const;
void
group( scene::group_handle Group );
scene::group_handle
group() const;
void
layer( scene::layer_handle Layer );
scene::layer_handle
layer() const;
// true if the node is defined by an *.inc template
bool
from_template() const { return m_instance != 0; }
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 };
bool m_layerhidden { false }; // the node was visible until the editor hid its scenery layer, or the include which defines it
bool m_preview { false }; // the node was made by the editor to show an include it placed or changed, and is replaced when the include changes
scene::layer_handle m_layer { null_handle }; // scenery layer this node belongs to, if any
// include of an *.inc template which defines the node; 0 if the node is defined directly by a scenery layer file.
// such nodes can't be rewritten one by one on scenery save. set only for scenery opened for editing
scene::instance_handle m_instance { 0 };
UID uuid;
private:
// methods
// radius() subclass details, calculates node's bounding radius
virtual float radius_() const;
// 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;
}
inline
void
basic_node::layer( scene::layer_handle Layer ) {
m_layer = Layer;
}
inline
scene::layer_handle
basic_node::layer() const {
return m_layer;
}
} // scene
//---------------------------------------------------------------------------