From 269591fc357bdec0806cd386949bc1e7c85b0511 Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Mon, 30 Nov 2026 18:48:43 +0100 Subject: [PATCH] fix(render): the lamps of small scenery models are seen from afar --- docs/opengl46-renderer.md | 5 ++++ model/Model3d.cpp | 13 ++++++++++ model/Model3d.h | 4 +++ rendering/common/instancecull.h | 1 + rendering/common/scenewalk.cpp | 27 +++++++++++++++------ rendering/common/scenewalk.h | 3 +++ rendering/common/scenewalkindirect.cpp | 1 + rendering/opengl46/opengl46instancecull.cpp | 2 +- rendering/vulkan14/vulkan14cull.cpp | 2 +- scene/scene.h | 2 ++ shaders/ogl46/instance_cull.comp | 7 ++++-- shaders/vk14/instance_cull.comp | 7 ++++-- utilities/Globals.cpp | 2 ++ utilities/Globals.h | 3 +++ 14 files changed, 66 insertions(+), 13 deletions(-) diff --git a/docs/opengl46-renderer.md b/docs/opengl46-renderer.md index 2491ceec6..39045ca63 100644 --- a/docs/opengl46-renderer.md +++ b/docs/opengl46-renderer.md @@ -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. diff --git a/model/Model3d.cpp b/model/Model3d.cpp index f3ddbc4a5..e7247b3b0 100644 --- a/model/Model3d.cpp +++ b/model/Model3d.cpp @@ -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) { diff --git a/model/Model3d.h b/model/Model3d.h index f7a09dc03..67486802e 100644 --- a/model/Model3d.h +++ b/model/Model3d.h @@ -307,6 +307,7 @@ private: bool m_instancesubmodelslocated { false }; std::vector 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 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 diff --git a/rendering/common/instancecull.h b/rendering/common/instancecull.h index 1661bf933..5328016fe 100644 --- a/rendering/common/instancecull.h +++ b/rendering/common/instancecull.h @@ -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 diff --git a/rendering/common/scenewalk.cpp b/rendering/common/scenewalk.cpp index ad0dafae0..7ea2c2bad 100644 --- a/rendering/common/scenewalk.cpp +++ b/rendering/common/scenewalk.cpp @@ -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( 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( 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 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; } diff --git a/rendering/common/scenewalk.h b/rendering/common/scenewalk.h index 0bdfa63e9..8b1f18550 100644 --- a/rendering/common/scenewalk.h +++ b/rendering/common/scenewalk.h @@ -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); diff --git a/rendering/common/scenewalkindirect.cpp b/rendering/common/scenewalkindirect.cpp index 661986bec..8b5bf96df 100644 --- a/rendering/common/scenewalkindirect.cpp +++ b/rendering/common/scenewalkindirect.cpp @@ -59,6 +59,7 @@ void scene_walk::Begin_Pass_Cull(gfx::instance_cull *Cull, bool const Mainview) setup.distancerange = (reflections ? static_cast(sq(Global.reflectiontune.range_instances)) : std::numeric_limits::max()); setup.drawrange = static_cast(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); } diff --git a/rendering/opengl46/opengl46instancecull.cpp b/rendering/opengl46/opengl46instancecull.cpp index a18b1fe23..4556d75b5 100644 --- a/rendering/opengl46/opengl46instancecull.cpp +++ b/rendering/opengl46/opengl46instancecull.cpp @@ -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)); diff --git a/rendering/vulkan14/vulkan14cull.cpp b/rendering/vulkan14/vulkan14cull.cpp index 0e33cd871..e90720553 100644 --- a/rendering/vulkan14/vulkan14cull.cpp +++ b/rendering/vulkan14/vulkan14cull.cpp @@ -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) diff --git a/scene/scene.h b/scene/scene.h index e92b11988..3c6f9f0e6 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -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 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 diff --git a/shaders/ogl46/instance_cull.comp b/shaders/ogl46/instance_cull.comp index 5a6fd6ca4..7e7bb3015 100644 --- a/shaders/ogl46/instance_cull.comp +++ b/shaders/ogl46/instance_cull.comp @@ -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; } diff --git a/shaders/vk14/instance_cull.comp b/shaders/vk14/instance_cull.comp index c6b8e0983..b7ac25928 100644 --- a/shaders/vk14/instance_cull.comp +++ b/shaders/vk14/instance_cull.comp @@ -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; } diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 4461e8278..ccd0672c8 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -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 ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 0a47cd13d..322bd54bb 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -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