18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 08:32:45 +01:00
Files
maszyna/scene/scenenode.h
T
lcddisplay 339d70a6e6 Moths fly about the lit lamps of the scenery
A part of the lamps of the scenery models which shine downwards (moths.share,
picked by the places of the lamps) gets a swarm of moths: an instance of
animals/cmy.t3d (animated by textures, shader vat) put at the light point when
the lamp first comes within moths.range of the camera. The swarm is shown while
its lamp shines (the light points gathered by the local lights in the frame),
not in the winter and not below moths.temperature. The swarms are made by the
game (basic_node::m_generated): not saved with the scenery, not edited.

Settings (eu07.ini): moths yes/no, moths.share 0-1 (0.4), moths.range metres
(80), moths.temperature degrees (8). Also in the debug window of the lights.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GXgG7F4L42c2F9sDSAJbT2
2026-11-29 11:39:09 +01:00

517 lines
16 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_from( 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_from( 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;
};
*/
// changes whenever a node is shown or hidden, or a model instance stops being drawn along with other instances of its
// model. copies of these states (instance records of the renderer) are made anew once it changes
inline unsigned int node_state_version { 1 };
// 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
bool m_catenary { false }; // a part the catenary assembled (world/catenary_scene.cpp): it's changed by the catenary tool, not edited as a node
bool m_generated { false }; // made by the game while it runs (the moths at the lamps, rendering/common/moths.cpp): not edited, not saved
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 ) {
if( Visible != m_visible ) {
++node_state_version;
}
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
//---------------------------------------------------------------------------