18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 00:22:42 +01:00

fix(render): the lamps of small scenery models are seen from afar

This commit is contained in:
2026-11-30 18:48:43 +01:00
parent cabd53a01c
commit 269591fc35
14 changed files with 66 additions and 13 deletions
+5
View File
@@ -180,6 +180,11 @@ scales can also be changed in the **Light clusters** debug window.
shines always). Their colour, power, cone and the optional `Radius:`/`SoftFalloff:`/`CastShadows:` are those of the light
point (brightness `gfx.lights.freespot.intensity`, the self-illumination threshold doesn't matter), and they fade in the
daylight as the cab lamp does. The dimming of the cab lamp applies only to the lamp under the top.
- **Lamps seen from afar** (`gfx.lights.freespot.far`, default `yes`): a scenery model with light points (not `LightOnly`) is
drawn however small it is in the view, so its lamps are seen as far as the ranges of their submodels (`MaxDistance` of the
light point and of every submodel above it) and the draw range reach. Before, a model smaller than 0.3% of the view was left
out whole, lamps too: a dwarf signal at a few hundred metres, a signal head at a kilometre or two. `no` brings that back. It
holds in the colour pass only; shadows and reflections leave small models out as before.
- **`LightOnly: true`** (an optional entry of any `FreeSpotLight`, after `HotspotAngle:` with the others; in e3d the flag 0x8
after the radius) makes the light point only a light: it lights its surroundings, but its glowing point and its glare,
seen from afar, aren't drawn. For lights hidden in fittings, the backlight of a desk and the like.
+13
View File
@@ -1843,6 +1843,17 @@ void assign_lod_ranges(TSubModel *Root, std::string const &Filename)
phase.submodel->fSquareMaxDist = farlimit * farlimit;
}
}
// whether the submodel, the submodels under it or the ones after it hold a light point seen from afar
bool has_light_points(TSubModel const *Submodel)
{
for (auto const *submodel = Submodel; submodel != nullptr; submodel = submodel->Next)
{
if (((submodel->eType == TP_FREESPOTLIGHT) && (false == submodel->bLightOnly)) || has_light_points(submodel->Child))
return true;
}
return false;
}
} // namespace
bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic)
@@ -1890,6 +1901,8 @@ bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic)
assign_lod_ranges(Root, name);
}
m_lightpoints = has_light_points(Root);
bool const result = Root ? iSubModelsCount > 0 : false; // brak pliku albo problem z wczytaniem
if (false == result)
{
+4
View File
@@ -307,6 +307,7 @@ private:
bool m_instancesubmodelslocated { false };
std::vector<float> m_lodbounds; // filled on first use, see lod_bounds()
bool m_lodboundsfound { false };
bool m_lightpoints { false }; // see HasLightPoints()
std::uint32_t m_stateversion { 0 }; // see state_version()
public:
@@ -322,6 +323,9 @@ public:
// returns the distinct bounds of the visibility ranges of the submodels (fSquareMinDist and fSquareMaxDist, squared), sorted:
// they split the distance into bands in which the same submodels are drawn. found once for the model, like instance_parts()
std::vector<float> const & lod_bounds();
// whether the model holds light points seen from afar: freespot lights which aren't only lights (lightonly:). found as it loads
bool HasLightPoints() const {
return m_lightpoints; }
// the submodels of the model are shared by everything drawn with it, which sets their state (lights, animations, visibility)
// before it's drawn. the version changes when something which may set it differently than the last time did, or the submodels
// changed, so the renderer can tell whether what it worked out from them still holds
+1
View File
@@ -65,6 +65,7 @@ class instance_cull
float distancerange{0.f}; // squared range of the scaled distance before the offset
float drawrange{0.f}; // squared draw range
float zoom{1.f};
bool lightpoints{false}; // instances of models with light points are kept however small in the view (scene_walk::Keeps_Light_Points())
bool drawing{false}; // the instances are drawn from the result (Global.gfx_gpucull 1), not compared
};
// a group of instances (model and skins) of the pass, for the renderer to make the draw commands of
+20 -7
View File
@@ -936,9 +936,10 @@ void scene_walk::Render(TAnimModel *Instance)
if( distancesquared > sq(drawdistancethreshold) ) {
return;
}
// second stage visibility cull, reject modelstoo far away to be noticeable
// second stage visibility cull, reject modelstoo far away to be noticeable; a model with light points is kept for its lamps
auto const radiussquared { Instance->radius() * Instance->radius() };
if( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 ) {
if( ( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 )
&& ( false == Keeps_Light_Points( ( Instance->pModel != nullptr ) && Instance->pModel->HasLightPoints() ) ) ) {
return;
}
if( m_modelcomparekey != nullptr ) {
@@ -1085,6 +1086,9 @@ void scene_walk::Update_Instance_Records( TModel3d const *Model, scene::basic_ce
Bucket.records.clear();
Bucket.records.reserve( Bucket.instances.size() );
// the light points of the model are seen from afar, however small its instances are in the view (Keeps_Light_Points())
auto const lightpoints { ( Model != nullptr ) && Model->HasLightPoints() };
Bucket.recordlightpoints = lightpoints;
// the range of the bucket: past it none of its instances is drawn, by its own range or by the submodels of the model
auto rangesquared { 0.f };
for( auto *instance : Bucket.instances ) {
@@ -1097,7 +1101,8 @@ void scene_walk::Update_Instance_Records( TModel3d const *Model, scene::basic_ce
rangesquared = std::max( rangesquared, record.rangesquaredmax );
record.flags =
( instance->visible() ? scene::basic_cell::instance_visible : 0u )
| ( instance->m_instanceable ? scene::basic_cell::instance_instanceable : 0u );
| ( instance->m_instanceable ? scene::basic_cell::instance_instanceable : 0u )
| ( lightpoints ? scene::basic_cell::instance_lightpoints : 0u );
auto const &rotationscale { instance->rotation_scale() };
for( int column = 0; column < 3; ++column ) {
record.rotationscale[ column ] = rotationscale[ column ];
@@ -1170,6 +1175,12 @@ scene::basic_cell::instance_sequence const &scene_walk::Single_Instances( scene:
return Cell.m_instancessingle;
}
bool scene_walk::Keeps_Light_Points( bool const Lightpoints ) const
{
// only the colour pass shows the light points: in the shadows and the reflections a small model is left out as before
return Lightpoints && Global.gfx_lights_freespot_far && ( m_renderpass.draw_mode == rendermode::color );
}
// whether none of the instances of a bucket can pass the tests of the culling of the pass (Render_Instanced(), on the gpu
// instance_cull.comp): by the bounds of their records the visible ones lie wholly out of the frustum of the pass, beyond their
// range or the draw range, or are too small on the screen. the tests are those of the culling with the nearest location and
@@ -1202,7 +1213,7 @@ bool scene_walk::Bucket_Culled( scene::basic_cell::instance_bucket const &Bucket
if( distancesquared > slack * sq( static_cast<double>( m_renderpass.draw_range ) + 250.0 ) ) { return true; }
// the largest instance at the nearest location takes the largest part of the view
auto const radiusmax { static_cast<double>( Bucket.recordradiusmax ) };
if( radiusmax * radiusmax * zoom * slack < 0.003 * 0.003 * distancesquared ) { return true; }
if( ( radiusmax * radiusmax * zoom * slack < 0.003 * 0.003 * distancesquared ) && ( false == Keeps_Light_Points( Bucket.recordlightpoints ) ) ) { return true; }
return false;
}
@@ -1313,7 +1324,8 @@ void scene_walk::Render_Instanced( TModel3d *Model, std::vector<queued_instance>
if( distancesquared > sq( drawdistancethreshold ) ) { continue; }
auto const radiussquared { record.radius * record.radius };
if( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 ) { continue; }
if( ( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 )
&& ( false == Keeps_Light_Points( ( record.flags & scene::basic_cell::instance_lightpoints ) != 0 ) ) ) { continue; }
if( batch_instance == nullptr ) {
batch_instance = queued.bucket->instances[ queued.index ];
@@ -2884,9 +2896,10 @@ void scene_walk::Render_Alpha(TAnimModel *Instance)
if( distancesquared > sq(drawdistancethreshold) ) {
return;
}
// second stage visibility cull, reject modelstoo far away to be noticeable
// second stage visibility cull, reject modelstoo far away to be noticeable; a model with light points is kept for its lamps
auto const radiussquared { Instance->radius() * Instance->radius() };
if( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 ) {
if( ( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 )
&& ( false == Keeps_Light_Points( ( Instance->pModel != nullptr ) && Instance->pModel->HasLightPoints() ) ) ) {
return;
}
+3
View File
@@ -158,6 +158,9 @@ class scene_walk
scene::basic_cell::instance_sequence const &Single_Instances(scene::basic_cell const &Cell) const;
// whether none of the instances of a bucket can pass the culling of the current pass, by the bounds of their records
bool Bucket_Culled(scene::basic_cell::instance_bucket const &Bucket) const;
// whether the current pass draws an instance whose model has light points (Lightpoints) however small it is in the view,
// so that its lamps are seen from afar (Global.gfx_lights_freespot_far)
bool Keeps_Light_Points(bool const Lightpoints) const;
// the sphere around the locations of the visible instances of a bucket, with a margin for the precision of the records
static scene::bounding_area Bucket_Area(scene::basic_cell::instance_bucket const &Bucket);
bool Render(TDynamicObject *Dynamic);
+1
View File
@@ -59,6 +59,7 @@ void scene_walk::Begin_Pass_Cull(gfx::instance_cull *Cull, bool const Mainview)
setup.distancerange = (reflections ? static_cast<float>(sq(Global.reflectiontune.range_instances)) : std::numeric_limits<float>::max());
setup.drawrange = static_cast<float>(sq(m_renderpass.draw_range + 250));
setup.zoom = Global.ZoomFactor;
setup.lightpoints = Keeps_Light_Points(true);
setup.drawing = (Global.gfx_gpucull == 1);
m_passcull->begin_pass(setup);
}
+1 -1
View File
@@ -105,7 +105,7 @@ void opengl46_instance_cull::dispatch(glm::mat4 const &View, occlusion_setup con
glProgramUniformMatrix4fv(*m_shared->cull_program, 0, 1, GL_FALSE, glm::value_ptr(View));
glProgramUniform4fv(*m_shared->cull_program, 1, 6, glm::value_ptr(planes[0]));
glProgramUniform4f(*m_shared->cull_program, 7, m_setup.distancescale, m_setup.distanceoffset, m_setup.distancerange, m_setup.drawrange);
glProgramUniform4f(*m_shared->cull_program, 8, m_setup.zoom, 0.f, 0.f, 0.f);
glProgramUniform4f(*m_shared->cull_program, 8, m_setup.zoom, (m_setup.lightpoints ? 1.f : 0.f), 0.f, 0.f);
glProgramUniform4ui(*m_shared->offsets_program, 0, m_entries, 0, 0, 0);
// the occlusion test only when the instances are drawn from the result, the cpu doesn't make it
m_occlusion = (m_setup.drawing && (Occlusion != nullptr) && (Occlusion->pyramid != 0));
+1 -1
View File
@@ -282,7 +282,7 @@ void instance_cull::dispatch(command_stream *Commands, glm::mat4 const &View, cu
block.view = View;
frustum_planes(block.planes);
block.distance_params = glm::vec4{m_setup.distancescale, m_setup.distanceoffset, m_setup.distancerange, m_setup.drawrange};
block.size_params = glm::vec4{m_setup.zoom, 0.f, 0.f, 0.f};
block.size_params = glm::vec4{m_setup.zoom, (m_setup.lightpoints ? 1.f : 0.f), 0.f, 0.f};
// the occlusion test only when the instances are drawn from the result, the cpu doesn't make it
m_occlusion = (m_setup.drawing && (Occlusion != nullptr) && (Occlusion->pyramid != VK_NULL_HANDLE));
if (m_occlusion)
+2
View File
@@ -272,6 +272,7 @@ public:
enum instance_record_flags : std::uint32_t {
instance_visible = 1 << 0,
instance_instanceable = 1 << 1, // false once an event gave the instance animations or a skin of its own
instance_lightpoints = 1 << 2, // the model has light points seen from afar (TModel3d::HasLightPoints())
};
struct instance_bucket {
std::vector<TAnimModel *> instances;
@@ -287,6 +288,7 @@ public:
mutable float recordlocationradius { 0.f };
mutable float recordradius { -1.f };
mutable float recordradiusmax { 0.f };
mutable bool recordlightpoints { false }; // the model of the bucket has light points seen from afar
mutable unsigned int recordsversion { 0 };
// place of the records in the record buffer of the gpu culling: first record, room, and the generation of
// the buffer it is good for. gpustale: the records changed since they were sent
+5 -2
View File
@@ -82,7 +82,7 @@ layout(location = 1) uniform vec4 planes[6];
// x: scale of the squared distance, y: added to it past the range test, z: range of the scaled distance before the
// addition (reflections), w: squared draw range
layout(location = 7) uniform vec4 distance_params;
// x: zoom factor
// x: zoom factor, y: 1 when instances of models with light points are kept however small in the view
layout(location = 8) uniform vec4 size_params;
// x: jobs, y: records in all jobs, z: entries, w: stage
layout(location = 9) uniform uvec4 counts;
@@ -99,6 +99,7 @@ layout(binding = 0) uniform sampler2D hiz;
const uint INSTANCE_VISIBLE = 1u;
const uint INSTANCE_INSTANCEABLE = 2u;
const uint INSTANCE_LIGHTPOINTS = 4u;
// smallest part of the view an instance has to take
const float MIN_SCREEN_SIZE = 0.003;
// header of the counters, see gl::INSTANCE_COUNTER_HEADER
@@ -198,7 +199,9 @@ bool passes(instance_cull_job job, instance_record record, out vec3 location, ou
return false;
if (distancesquared > distance_params.w)
return false;
if (record.radius * record.radius * size_params.x / distancesquared < MIN_SCREEN_SIZE * MIN_SCREEN_SIZE)
// a model with light points stays for its lamps to be seen from afar
bool lightpoints = ((record.flags & INSTANCE_LIGHTPOINTS) != 0u) && (size_params.y != 0.0);
if ((record.radius * record.radius * size_params.x / distancesquared < MIN_SCREEN_SIZE * MIN_SCREEN_SIZE) && (!lightpoints))
return false;
}
+5 -2
View File
@@ -86,7 +86,7 @@ layout(std140, set = COMPUTE_SET, binding = 7) uniform kernel_block
// x: scale of the squared distance, y: added to it past the range test, z: range of the scaled distance before the
// addition (reflections), w: squared draw range
vec4 distance_params;
// x: zoom factor
// x: zoom factor, y: 1 when instances of models with light points are kept however small in the view
vec4 size_params;
// occlusion test: projection of the pass, x: on, y: levels of the pyramid, zw: size of its first level
mat4 hiz_projection;
@@ -107,6 +107,7 @@ layout(set = COMPUTE_SET, binding = 6) uniform sampler2D hiz;
const uint INSTANCE_VISIBLE = 1u;
const uint INSTANCE_INSTANCEABLE = 2u;
const uint INSTANCE_LIGHTPOINTS = 4u;
// smallest part of the view an instance has to take
const float MIN_SCREEN_SIZE = 0.003;
// header of the counters, see gl::INSTANCE_COUNTER_HEADER
@@ -206,7 +207,9 @@ bool passes(instance_cull_job job, instance_record record, out vec3 location, ou
return false;
if (distancesquared > distance_params.w)
return false;
if (record.radius * record.radius * size_params.x / distancesquared < MIN_SCREEN_SIZE * MIN_SCREEN_SIZE)
// a model with light points stays for its lamps to be seen from afar
bool lightpoints = ((record.flags & INSTANCE_LIGHTPOINTS) != 0u) && (size_params.y != 0.0);
if ((record.radius * record.radius * size_params.x / distancesquared < MIN_SCREEN_SIZE * MIN_SCREEN_SIZE) && (!lightpoints))
return false;
}
+2
View File
@@ -2250,6 +2250,7 @@ global_settings::ConfigParse_gfx_sky( cParser &Parser, std::string_view const To
else if( Token == "gfx.lights.headlights" ) { readswitch( gfx_lights_headlights ); }
else if( Token == "gfx.lights.cab" ) { readswitch( gfx_lights_cab ); }
else if( Token == "gfx.lights.freespot.intensity" ) { readvalue( gfx_lights_freespot_intensity, 0.f, 1000.f ); }
else if( Token == "gfx.lights.freespot.far" ) { readswitch( gfx_lights_freespot_far ); }
else if( Token == "gfx.lights.compartment.intensity" ) { readvalue( gfx_lights_compartment_intensity, 0.f, 100.f ); }
else if( Token == "gfx.lights.cab.intensity" ) { readvalue( gfx_lights_cab_intensity, 0.f, 100.f ); }
else if( Token == "gfx.lights.cab.bounce" ) { readvalue( gfx_lights_cab_bounce, 0.f, 1.f ); }
@@ -2854,6 +2855,7 @@ global_settings::export_as_text( std::ostream &Output ) const {
export_as_text( Output, "gfx.lights.headlights", gfx_lights_headlights );
export_as_text( Output, "gfx.lights.cab", gfx_lights_cab );
export_as_text( Output, "gfx.lights.freespot.intensity", gfx_lights_freespot_intensity );
export_as_text( Output, "gfx.lights.freespot.far", gfx_lights_freespot_far );
export_as_text( Output, "gfx.lights.compartment.intensity", gfx_lights_compartment_intensity );
export_as_text( Output, "gfx.lights.cab.intensity", gfx_lights_cab_intensity );
export_as_text( Output, "gfx.lights.cab.bounce", gfx_lights_cab_bounce );
+3
View File
@@ -597,6 +597,9 @@ struct global_settings {
bool gfx_lights_headlights { true };
bool gfx_lights_cab { true }; // the lamp of the occupied cab is a point light instead of an addition to the ambient light
float gfx_lights_freespot_intensity { 10.f }; // brightness of a light point of a model
// a scenery model with light points is drawn however small it is in the view, so its lamps are seen from afar as far as the ranges
// of their submodels reach. no: as before, a model smaller than 0.3% of the view is left out with its lamps
bool gfx_lights_freespot_far { true };
float gfx_lights_compartment_intensity { 1.f }; // brightness of the light of a lit vehicle compartment
float gfx_lights_cab_intensity { 1.5f }; // brightness of the cab lamp
float gfx_lights_cab_bounce { 0.3f }; // share of the former ambient light of the cab lamp kept, for the light which bounces around the cab