From 3710f87fc5b9338cbecbbc090c24ecdc161295d2 Mon Sep 17 00:00:00 2001 From: pawelw Date: Fri, 20 Nov 2026 04:25:18 +0100 Subject: [PATCH] feat(gl46): street lamps of the scenery light their surroundings (#371) * Local lights: street lamps of the scenery light their surroundings Optional settings, all off by default (nothing changes without them): - gfx.lights.stars: Stars submodels (e.g. "lamp" of the SGP340 street lights) whose glowing points light downwards as lamps; signal LEDs, chimneys and the sky only glow - gfx.lights.points.near: scenery lamps shining downwards light also closer than their glare is seen from - gfx.lights.freespot.mincone: wider cones for scenery lamps shining downwards (many have the narrow cone of their glare) - gfx.lights.lamps.gain: brightness of the scenery lamps shining downwards - gfx.lights.hideglow: textures of the planes faking the light of lamps (pools of light, glows), hidden while the lamps light Lights shining sideways (signals) keep their cones, brightness and distances. The lamps made of glowing points are gathered also from the opaque instances of a cell. * Local lights: the lamp settings in a parser of their own, the draw phase test in a helper The entries of the lamps are read by ConfigParse_gfx_lamps, which the parser of the sky hands the tokens it doesn't know, and the opaque and translucent draws ask drawn_in_phase whether a submodel has a part in them, so the long functions keep their complexity. The fake glow cache is private, the colour of a light point is lightcolor (no shadowing of rendermode::color), the minimal distance of a submodel comes from walk_min_distance and the lit instances of a cell from add_lit_instances. * Settings: the lamp entries are tried by the main loop of the parser, the parser of the sky stays as it was * Local lights: the fake glows are left out only within the reach the lamps are gathered from Beyond gfx.lights.range nothing lights the ground in place of the pools of light and glows of the lamps, so a scenery model keeps them there. Render_Alpha(TAnimModel *) tells hide_fake_glows() which model it draws, and is_fake_glow() leaves out the glows of that model only, when it is within the range (and its radius) of the camera the lamps are gathered around. The glow planes are translucent, so the opaque draws are as they were. * Local lights: hide_fake_glows() takes the model as it is drawn (radius() isn't const) * Local lights: the fake glows hidden at any distance again, gfx.lights.lamps.range to see the lamps from afar Glow planes kept beyond the reach of the lamps don't light a distant station either, they only float above it, so the glows listed in gfx.lights.hideglow are left out at any distance again. A lit station is seen from afar with gfx.lights.lamps.range instead: the lamps are gathered from that far when it's above gfx.lights.range (0, the default: from gfx.lights.range). * Local lights: gfx.lights.lamps.range for the lamps of the scenery only, the vehicles keep gfx.lights.range * Local lights: the fake glows are kept with gfx.lights.range 0, the limits of gfx.lights.hideglow in the docs --- docs/opengl46-renderer.md | 6 + rendering/common/locallights.cpp | 187 +++++++++++++++++++++++++++---- rendering/common/locallights.h | 15 ++- rendering/common/scenewalk.cpp | 65 ++++++++++- rendering/common/scenewalk.h | 8 ++ utilities/Globals.cpp | 35 ++++++ utilities/Globals.h | 21 ++++ 7 files changed, 313 insertions(+), 24 deletions(-) diff --git a/docs/opengl46-renderer.md b/docs/opengl46-renderer.md index 01e8f8f30..2491ceec6 100644 --- a/docs/opengl46-renderer.md +++ b/docs/opengl46-renderer.md @@ -250,6 +250,12 @@ shadows of the sun, see `headlight shadows:` in Shift+F12); with the vehicle on | `gfx.lights.compartment.intensity` | `1.0` | brightness of the light of a lit compartment | | `gfx.lights.cab.intensity` | `1.5` | brightness of the cab lamp | | `gfx.lights.cab.bounce` | `0.3` | 0..1: share of the former ambient light of the cab lamp kept with the lamp on | +| `gfx.lights.stars` | `no` | names of the `Stars` submodels of scenery models whose glowing points light their surroundings, each one downwards as the lamp of a street light, separated with `\|` (the lamps of many models, e.g. the SGP340 street lights, are named `lamp`); other glowing points (signal LEDs, chimneys, the sky) only glow. `no`: none | +| `gfx.lights.points.near yes/no` | `no` | a light point of a scenery lamp shining downwards lights also closer than its glare is seen from (many lamps have a minimal distance set for the glare), so the lamp doesn't go out as the camera comes near it | +| `gfx.lights.freespot.mincone` | `0` | 0..89: the half angle of the cone of the light of a scenery lamp shining downwards is at least this, degrees (many lamps have the narrow cone of their glare); `0`: the cone of the model. Lights shining sideways (signals) keep their cones | +| `gfx.lights.lamps.range` | `0` | 0..10000: the lamps of scenery models are gathered from this far from the camera when it is above `gfx.lights.range`, metres, so lit stations are seen from afar; the lights of vehicles keep `gfx.lights.range` (at the cost of the time of the gathering, see `local lights:` in Shift+F12); `0`: from `gfx.lights.range` | +| `gfx.lights.lamps.gain` | `1.0` | 0.1..10: the brightness of the light of the scenery lamps shining downwards and of their glowing points is multiplied by this; signals keep theirs | +| `gfx.lights.hideglow` | `no` | textures of the planes models fake the light of their lamps with (pools of light on the ground, glows), separated with `\|`, e.g. `elektryczne/light1\|elektryczne/light2\|elektryczne/poswiata`: they aren't drawn while the scenery lamps light their surroundings (`gfx.lights.clustered` and `gfx.lights.scenery` on, `gfx.lights.range` above 0). They're left out at any distance, also past the reach the lamps are gathered from, where nothing lights in their place: set it together with `gfx.lights.lamps.range`, or distant stations stay dark at night. List only the textures of models whose lamps light (light points shining downwards, or `Stars` submodels named in `gfx.lights.stars`); a model with a listed texture and no such lamp loses its glow. In the game data `elektryczne/light1`, `light2` and `poswiata` are used by such lamps only. `no`: they're drawn | | `gfx.lights.range` | `300` | distance from the camera the lamps of models and vehicles are gathered from, metres | | `gfx.lights.shadows` | `medium` | `no`/`low`/`medium`/`high` (or 0..3): shadows of the cab lamp and the scenery lamps, see above | | `gfx.lights.shadows.cab` | `8` | 0..16: how many lights of the cab (the brightest; the lamp or the light points of the cab model) have a cube shadow map of their own; the others take the map of the closest light with one (within 1 m); 0: none | diff --git a/rendering/common/locallights.cpp b/rendering/common/locallights.cpp index 60f62f4e8..9f042afe0 100644 --- a/rendering/common/locallights.cpp +++ b/rendering/common/locallights.cpp @@ -14,7 +14,10 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include +#include +#include #include "utilities/Globals.h" #include "rendering/common/skyocclusion.h" @@ -40,6 +43,14 @@ float const LIGHT_DUSK_LUMINANCE{0.325f}; float const LIGHT_DAY_LUMINANCE{1.0f}; // lights of the occupied cab, at most std::size_t const CAB_LIGHTS_MAX{16}; +// the cone of the light of a glowing point of a street lamp (gfx_lights_stars), shining downwards: the half angles where it ends +// and where it is at its full brightness, degrees, and the radius of the sphere it shines from, metres +float const LAMP_POINT_OUTER_ANGLE{80.f}; +float const LAMP_POINT_INNER_ANGLE{55.f}; +float const LAMP_POINT_RADIUS{0.2f}; +// a light point whose axis is at most this far from straight down (the cosine of the angle) is a lamp lighting the ground under it; +// one shining more sideways is a signal or a headlight, left as its model has it +float const LAMP_DOWN_COS{0.7f}; glm::vec3 to_linear(glm::vec3 const &Color) { @@ -57,6 +68,69 @@ float light_range(glm::vec3 const &Color) return std::min(std::sqrt(max_component(Color) / LIGHT_THRESHOLD), LIGHT_RANGE_MAX); } +// how far from the camera the lights are gathered from: gfx_lights_range, or gfx_lights_lamps_range when it's farther, which +// only the lamps of the scenery go as far as (gather_vehicle()). none with gfx_lights_range 0 +float gather_range() +{ + return (Global.gfx_lights_range > 0.f ? std::max(Global.gfx_lights_range, Global.gfx_lights_lamps_range) : 0.f); +} + +// whether Name is one of the names separated with | in List +bool is_listed(std::string_view const Name, std::string_view const List) +{ + std::size_t start{0}; + while (start <= List.size()) + { + auto end{List.find('|', start)}; + if (end == std::string_view::npos) + { + end = List.size(); + } + if (List.substr(start, end - start) == Name) + { + return true; + } + start = end + 1; + } + return false; +} + +// a smt_Stars submodel whose glowing points are the lamp of a street light (gfx_lights_stars) +bool is_lamp_points(TSubModel const &Submodel) +{ + return (Submodel.eType == TP_STARS) && (false == Global.gfx_lights_stars.empty()) && is_listed(ToLower(Submodel.pName), Global.gfx_lights_stars); +} + +// the axis of the cone of a light point, in the world +glm::vec3 light_axis(glm::mat4 const &Transform, glm::dmat4 const &Placement) +{ + auto const axis{glm::vec3{glm::dmat3{Placement} * glm::dvec3{-glm::vec3{Transform[2]}}}}; + return (glm::length2(axis) > 0.f ? glm::normalize(axis) : glm::vec3{0.f, -1.f, 0.f}); +} + +// whether a light with the axis shines downwards, the way a lamp lights the ground under it +bool shines_down(glm::vec3 const &Axis) +{ + return (Axis.y <= -LAMP_DOWN_COS); +} + +// the cosines of the outer and inner half angles of the cone of a light, the outer one widened to gfx_lights_freespot_mincone when +// Widen. the inner one is above the outer one, as some models have them the wrong way round +glm::vec2 light_cone(float const Cosouter, float const Cosinner, bool const Widen) +{ + auto cosouter{std::clamp(Cosouter, -1.f, 0.999f)}; + auto cosinner{std::clamp(std::max(Cosinner, cosouter + 0.05f), cosouter + 0.001f, 1.f)}; + if (Widen && (Global.gfx_lights_freespot_mincone > 0.f)) + { + if (auto const minouter{std::cos(glm::radians(Global.gfx_lights_freespot_mincone))}; minouter < cosouter) + { + cosouter = minouter; + cosinner = std::min(cosinner, std::cos(glm::radians(Global.gfx_lights_freespot_mincone * 0.6f))); + } + } + return {cosouter, cosinner}; +} + // model space transform of the parent of the submodel, from the static transforms of the submodels above it; false if the // submodel is not in the tree bool find_parent_transform(TSubModel const *Submodel, TSubModel const *Target, glm::mat4 const &Parent, glm::mat4 &Result) @@ -118,6 +192,7 @@ void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Re m_cablights.clear(); m_cablampon = false; m_scenerycount = m_vehiclecount = m_compartmentcount = 0; + m_walkgeometry = &Geometry; // the editor adds, moves and removes instances in the cells. it can also do it in the frame it is turned off in auto const rebuild{EditorModeFlag || m_editorseen}; @@ -132,7 +207,7 @@ void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Re // lamps are lost in the daylight; this also spares the work of gathering them in the middle of the day auto const darkness{1.f - glm::smoothstep(LIGHT_DUSK_LUMINANCE, LIGHT_DAY_LUMINANCE, static_cast(Global.fLuminance))}; - auto const range{Global.gfx_lights_range}; + auto const range{gather_range()}; if ((darkness > 0.f) && (range > 0.f) && Global.gfx_lights_scenery) { @@ -208,7 +283,7 @@ void gfx::local_lights::gather_instance(TAnimModel *Instance, glm::dvec3 const & return; } auto const offset{Instance->location() - Camera}; - if (glm::length(offset) > Global.gfx_lights_range + Instance->radius()) + if (glm::length(offset) > gather_range() + Instance->radius()) { return; } @@ -234,6 +309,11 @@ void gfx::local_lights::gather_instance(TAnimModel *Instance, glm::dvec3 const & void gfx::local_lights::gather_vehicle(TDynamicObject *Vehicle, glm::dvec3 const &Camera, gfx::geometrybank_manager const &Geometry, float const Freespotbrightness, float const Compartmentbrightness) { + // the vehicles light within gfx_lights_range, only the lamps of the scenery may be gathered from farther + if (glm::length(Vehicle->GetPosition() - Camera) > Global.gfx_lights_range + Vehicle->radius()) + { + return; + } if ((false == Global.render_cab) && (Global.pCamera.m_owner == Vehicle)) { // the render leaves the vehicle out as well @@ -389,12 +469,19 @@ void gfx::local_lights::gather_cab_lights(gfx::geometrybank_manager const &Geome m_cablampon = (false == m_cablights.empty()); } +float gfx::local_lights::walk_min_distance(TSubModel const &Submodel) const +{ + // a lamp of the scenery lights also closer than its glare is seen from (gfx_lights_points_near) + auto const lampnear{(m_walkowner != nullptr) && Global.gfx_lights_points_near && ((Submodel.eType == TP_FREESPOTLIGHT) || is_lamp_points(Submodel))}; + return (lampnear ? 0.f : Submodel.fSquareMinDist); +} + void gfx::local_lights::walk(TSubModel *Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float const Brightness) { // visibility, lod ranges, transforms and animations as in opengl46_renderer::Render_Alpha(TSubModel *) for (auto *submodel{Submodel}; submodel != nullptr; submodel = submodel->Next) { - if ((submodel->iVisible == 0) || (TSubModel::fSquareDist < submodel->fSquareMinDist) || (TSubModel::fSquareDist >= submodel->fSquareMaxDist)) + if (auto const mindistance{walk_min_distance(*submodel)}; (submodel->iVisible == 0) || (TSubModel::fSquareDist < mindistance) || (TSubModel::fSquareDist >= submodel->fSquareMaxDist)) { continue; } @@ -410,10 +497,19 @@ void gfx::local_lights::walk(TSubModel *Submodel, glm::mat4 const &Transform, gl submodel->RaAnimation(transform, submodel->b_aAnim); } } + // closer than its glare is seen from only a lamp shining downwards lights, not a signal + if ((TSubModel::fSquareDist < submodel->fSquareMinDist) && (submodel->eType == TP_FREESPOTLIGHT) && (false == shines_down(light_axis(transform, Placement)))) + { + continue; + } if (submodel->eType == TP_FREESPOTLIGHT) { add_light_point(*submodel, transform, Placement, Brightness); } + else if ((m_walkowner != nullptr) && is_lamp_points(*submodel)) + { + add_lamp_points(*submodel, transform, Placement, Brightness); + } if (submodel->Child != nullptr) { walk(submodel->Child, transform, Placement, Brightness); @@ -439,22 +535,26 @@ void gfx::local_lights::add_light_point(TSubModel const &Submodel, glm::mat4 con auto const diffuse{Submodel.DiffuseOverride.r < 0.f ? // -1 indicates no override glm::vec3{Submodel.f4Diffuse} : Submodel.DiffuseOverride}; - auto const color{to_linear(diffuse) * (Brightness * Submodel.fHotspotPower * Submodel.fVisible * std::min(Submodel.f4Emision.a, 1.f))}; - if (max_component(color) <= LIGHT_THRESHOLD) + auto const direction{light_axis(Transform, Placement)}; + // a lamp of the scenery shining downwards: brighter by gfx_lights_lamps_gain, its cone widened to gfx_lights_freespot_mincone + auto const lamp{(m_walkowner != nullptr) && shines_down(direction)}; + auto const gain{lamp ? Global.gfx_lights_lamps_gain : 1.f}; + auto const lightcolor{to_linear(diffuse) * (Brightness * gain * Submodel.fHotspotPower * Submodel.fVisible * std::min(Submodel.f4Emision.a, 1.f))}; + if (max_component(lightcolor) <= LIGHT_THRESHOLD) { return; } light lightpoint; lightpoint.position = glm::dvec3{Placement * glm::dvec4{glm::dvec3{Transform[3]}, 1.0}}; - auto const axis{glm::vec3{glm::dmat3{Placement} * glm::dvec3{-glm::vec3{Transform[2]}}}}; - lightpoint.direction = (glm::length2(axis) > 0.f ? glm::normalize(axis) : glm::vec3{0.f, -1.f, 0.f}); - lightpoint.color = color; + lightpoint.direction = direction; + lightpoint.color = lightcolor; // the cone of the glare: where the point is seen from is where it shines. the glare goes from no brightness at the falloff // angle to full at the hotspot angle, which some models have the wrong way round - lightpoint.cos_outer = std::clamp(Submodel.fCosFalloffAngle, -1.f, 0.999f); - lightpoint.cos_inner = std::clamp(std::max(Submodel.fCosHotspotAngle, lightpoint.cos_outer + 0.05f), lightpoint.cos_outer + 0.001f, 1.f); - lightpoint.range = light_range(color); + auto const cone{light_cone(Submodel.fCosFalloffAngle, Submodel.fCosHotspotAngle, lamp)}; + lightpoint.cos_outer = cone.x; + lightpoint.cos_inner = cone.y; + lightpoint.range = light_range(lightcolor); lightpoint.radius = Submodel.fLightRadius; lightpoint.soft_falloff = Submodel.bSoftFalloff; lightpoint.cast_shadows = Submodel.bCastShadows; @@ -472,6 +572,39 @@ void gfx::local_lights::add_light_point(TSubModel const &Submodel, glm::mat4 con m_lights.emplace_back(lightpoint); } +void gfx::local_lights::add_lamp_points(TSubModel const &Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float const Brightness) +{ + // lit as the render draws them: past the self-illumination threshold + if ((m_walkgeometry == nullptr) || (Submodel.m_geometry.handle == null_handle) || (Global.fLuminance >= Submodel.fLight)) + { + return; + } + auto const cone{light_cone(std::cos(glm::radians(LAMP_POINT_OUTER_ANGLE)), std::cos(glm::radians(LAMP_POINT_INNER_ANGLE)), true)}; + for (auto const &vertex : m_walkgeometry->vertices(Submodel.m_geometry.handle)) + { + // a glowing point keeps its colour in its normal; one without any takes the colour of the submodel + auto const pointcolor{max_component(vertex.normal) > 0.f ? vertex.normal : glm::vec3{Submodel.f4Diffuse}}; + auto const lightcolor{to_linear(pointcolor) * (Brightness * Global.gfx_lights_lamps_gain)}; + if (max_component(lightcolor) <= LIGHT_THRESHOLD) + { + continue; + } + light lightpoint; + lightpoint.position = glm::dvec3{Placement * glm::dvec4{glm::dvec3{Transform * glm::vec4{vertex.position, 1.f}}, 1.0}}; + lightpoint.direction = glm::vec3{0.f, -1.f, 0.f}; + lightpoint.color = lightcolor; + lightpoint.cos_outer = cone.x; + lightpoint.cos_inner = cone.y; + lightpoint.range = light_range(lightcolor); + lightpoint.radius = LAMP_POINT_RADIUS; + lightpoint.soft_falloff = true; + // the point names the lamp from frame to frame (the shadow maps of the lamps) + lightpoint.owner = m_walkowner; + lightpoint.part = &vertex; + m_lights.emplace_back(lightpoint); + } +} + bool gfx::local_lights::has_light_points(TModel3d *Model) { auto lookup{m_modellights.find(Model)}; @@ -487,7 +620,7 @@ bool gfx::local_lights::has_light_points(TModel3d *Model) pending.pop_back(); for (; submodel != nullptr; submodel = submodel->Next) { - if (submodel->eType == TP_FREESPOTLIGHT) + if ((submodel->eType == TP_FREESPOTLIGHT) || is_lamp_points(*submodel)) { found = true; break; @@ -502,23 +635,35 @@ bool gfx::local_lights::has_light_points(TModel3d *Model) return found; } +void gfx::local_lights::add_lit_instances(std::vector const &Instances, std::unordered_set &Added, std::vector &Lit) +{ + for (auto *instance : Instances) + { + if ((instance->pModel != nullptr) && has_light_points(instance->pModel) && Added.insert(instance).second) + { + Lit.emplace_back(instance); + } + } +} + gfx::local_lights::cell_data const &gfx::local_lights::cell_instances(scene::basic_cell &Cell, bool const Rebuild) { - // light points are drawn in the translucent phase, so the instances which have them are on the translucent list of the cell + // light points are drawn in the translucent phase, so the instances which have them are on the translucent list of the cell. + // the glowing points of the lamps (smt_Stars) are drawn with the opaque ones, so a lamp made of them can be on the opaque list only auto &instances{Cell.m_instancetranslucent}; + auto &opaque{Cell.m_instancesopaque}; auto &data{m_cells[&Cell]}; - if (Rebuild || (data.source_size != instances.size()) || (data.source_data != static_cast(instances.data()))) + if (Rebuild || (data.source_size != instances.size()) || (data.source_data != static_cast(instances.data())) || (data.opaque_size != opaque.size()) || + (data.opaque_data != static_cast(opaque.data()))) { data.instances.clear(); - for (auto *instance : instances) - { - if ((instance->pModel != nullptr) && has_light_points(instance->pModel)) - { - data.instances.emplace_back(instance); - } - } + std::unordered_set added; + add_lit_instances(instances, added, data.instances); + add_lit_instances(opaque, added, data.instances); data.source_size = instances.size(); data.source_data = instances.data(); + data.opaque_size = opaque.size(); + data.opaque_data = opaque.data(); } return data; } diff --git a/rendering/common/locallights.h b/rendering/common/locallights.h index fa0390c60..5cfd6247f 100644 --- a/rendering/common/locallights.h +++ b/rendering/common/locallights.h @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include namespace scene @@ -70,7 +71,8 @@ class local_lights local_lights(const local_lights &) = delete; local_lights &operator=(const local_lights &) = delete; - // collects the lights within Global.gfx_lights_range of Camera, for the frame about to be drawn. it sets the state of the + // collects the lights within Global.gfx_lights_range of Camera (the lamps of the scenery within Global.gfx_lights_lamps_range, + // when it's farther), for the frame about to be drawn. it sets the state of the // shared models of the instances and vehicles it goes through, as the render does before it draws each of them. // Geometry: the geometry the models were loaded to, for the bounds of compartments and cabs; Framestamp: the number of the frame void gather(glm::dvec3 const &Camera, scene::basic_region &Region, gfx::geometrybank_manager const &Geometry, unsigned int const Framestamp); @@ -122,20 +124,28 @@ class local_lights struct cell_data { std::vector instances; // instances of models with light points - // what the list was made from: when the instance list of the cell changes, the list is made anew + // what the list was made from: when the instance lists of the cell change, the list is made anew std::size_t source_size{0}; void const *source_data{nullptr}; + std::size_t opaque_size{0}; + void const *opaque_data{nullptr}; }; // lights of the light points of the submodel tree, gone through the way the render goes through it. Transform: model space of // Submodel's parent, Placement: model space to world space, Brightness: of a light point of white colour void walk(TSubModel *Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float const Brightness); + // the square of the distance from the camera the submodel lights from at the least + float walk_min_distance(TSubModel const &Submodel) const; void add_light_point(TSubModel const &Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float Brightness); + // lights of the glowing points of a smt_Stars submodel of a scenery lamp (gfx_lights_stars), each one shining downwards + void add_lamp_points(TSubModel const &Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float Brightness); void gather_instance(TAnimModel *Instance, glm::dvec3 const &Camera, float const Brightness, unsigned int const Framestamp); void gather_vehicle(TDynamicObject *Vehicle, glm::dvec3 const &Camera, gfx::geometrybank_manager const &Geometry, float const Freespotbrightness, float const Compartmentbrightness); void gather_cab_lights(gfx::geometrybank_manager const &Geometry); bool has_light_points(TModel3d *Model); cell_data const &cell_instances(scene::basic_cell &Cell, bool const Rebuild); + // the instances of the list with light points not yet in Added, appended to Lit + void add_lit_instances(std::vector const &Instances, std::unordered_set &Added, std::vector &Lit); section_data const §ion(TModel3d *Model, TSubModel *Section, gfx::geometrybank_manager const &Geometry); section_data const &model_bounds(TModel3d *Model, gfx::geometrybank_manager const &Geometry); @@ -150,6 +160,7 @@ class local_lights std::unordered_map m_sections; // compartments, by their submodel std::unordered_map m_models; // bounds of cab models TAnimModel const *m_walkowner{nullptr}; // scenery instance whose light points walk() goes through; nullptr for vehicles + gfx::geometrybank_manager const *m_walkgeometry{nullptr}; // geometry of the models, for the glowing points of the lamps bool m_editorseen{false}; // the editor was on in the last frame: the instance lists of the cells may have changed // statistics std::uint32_t m_scenerycount{0}; diff --git a/rendering/common/scenewalk.cpp b/rendering/common/scenewalk.cpp index 967e615cd..2e27b081f 100644 --- a/rendering/common/scenewalk.cpp +++ b/rendering/common/scenewalk.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/common/walksync.h" #include "utilities/color.h" #include "utilities/Globals.h" +#include "utilities/utilities.h" #include "vehicle/Train.h" #include "vehicle/Camera.h" #include "vehicle/DynObj.h" @@ -29,7 +30,10 @@ http://mozilla.org/MPL/2.0/. #include "model/AnimModel.h" #include "model/Model3d.h" #include "rendering/common/skyocclusion.h" +#include #include +#include +#include namespace gfx { @@ -44,8 +48,67 @@ TEnvironmentType track_environment(TTrack const *Track) return (Global.gfx_tunnelflags ? Track->eEnvironment : e_flat); } +// the name of a texture or a material the way gfx_lights_hideglow lists it: lower case, with /, without the folder of the textures +// and without the extension +std::string glow_name(std::string_view const Name) +{ + auto name{ToLower(std::string{Name})}; + std::ranges::replace(name, '\\', '/'); + if (name.starts_with("textures/")) + { + name.erase(0, std::string_view{"textures/"}.size()); + } + if (auto const dot{name.rfind('.')}; (dot != std::string::npos) && (name.find('/', dot) == std::string::npos)) + { + name.erase(dot); + } + return name; +} + +// whether the material named Name is one of the textures separated with | in List: the same name, or the end of it after a / +bool is_listed_glow(std::string_view const Name, std::string_view const List) +{ + std::size_t start{0}; + while (start <= List.size()) + { + auto end{List.find('|', start)}; + if (end == std::string_view::npos) + { + end = List.size(); + } + if (auto const entry{glow_name(List.substr(start, end - start))}; (false == entry.empty()) && ((Name == entry) || Name.ends_with("/" + entry))) + { + return true; + } + start = end + 1; + } + return false; +} + } // namespace +bool scene_walk::drawn_in_phase(TSubModel const *Submodel, int const Mask) +{ + return ((Submodel->iAlpha & Submodel->iFlags & Mask) != 0) && (false == is_fake_glow(Submodel)); +} + +bool scene_walk::is_fake_glow(TSubModel const *Submodel) +{ + // kept when no lamp lights in their place: the local lights off, or gathered from nowhere (gfx_lights_range 0) + if (Global.gfx_lights_hideglow.empty() || (false == Global.gfx_lights_clustered) || (false == Global.gfx_lights_scenery) || (Global.gfx_lights_range <= 0.f) || + (Submodel->m_material <= 0)) + { + return false; + } + auto lookup{m_fakeglows.find(Submodel->m_material)}; + if (lookup == m_fakeglows.end()) + { + auto const fake{is_listed_glow(glow_name(walk_material_data(Submodel->m_material).GetName()), Global.gfx_lights_hideglow)}; + lookup = m_fakeglows.emplace(Submodel->m_material, fake).first; + } + return lookup->second; +} + // the top view of the editor shows everything this close to the camera, the corners of the screen included float scene_walk::editor_ortho_reach() { @@ -3142,7 +3205,7 @@ void scene_walk::Render_Alpha(TSubModel *Submodel) if (Submodel->eType < TP_ROTATOR) { // renderowanie obiektów OpenGL - if (Submodel->iAlpha & Submodel->iFlags & 0x2F) + if (drawn_in_phase(Submodel, 0x2F)) { // rysuj gdy element przezroczysty diff --git a/rendering/common/scenewalk.h b/rendering/common/scenewalk.h index 982d58e37..1668d74e9 100644 --- a/rendering/common/scenewalk.h +++ b/rendering/common/scenewalk.h @@ -635,6 +635,14 @@ class scene_walk private: static float editor_ortho_reach(); + // a plane faking the light of a lamp (its texture is one of gfx_lights_hideglow), left out while the lamps of the scenery + // light their surroundings + bool is_fake_glow(TSubModel const *Submodel); + // whether the submodel is drawn in the phase of the mask (0x2F translucent): it has a part of it, and isn't a fake glow (the + // glow planes are translucent) + bool drawn_in_phase(TSubModel const *Submodel, int Mask); + + std::unordered_map m_fakeglows; // whether the material is one of gfx_lights_hideglow }; } // namespace gfx diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 346676c05..71ab7987c 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -1552,6 +1552,9 @@ void global_settings::ConfigParse(cParser& Parser) if (ConfigParse_gfx(Parser, token)) continue; + if (ConfigParse_gfx_lamps(Parser, token)) + continue; + if (ConfigParseGeneral(Parser, token)) continue; @@ -2276,6 +2279,32 @@ global_settings::ConfigParse_gfx_sky( cParser &Parser, std::string_view const To return true; } +bool +global_settings::ConfigParse_gfx_lamps( cParser &Parser, std::string_view const Token ) { + + // a list of names separated with |, or "no" for none + auto const readlist = [&Parser]( std::string &Value ) { + Parser.getTokens( 1 ); + Parser >> Value; + if( Value == "no" ) { Value.clear(); } }; + auto const readvalue = [&Parser]( float &Value, float const Min, float const Max ) { + Parser.getTokens( 1 ); + Parser >> Value; + Value = std::clamp( Value, Min, Max ); }; + + if( Token == "gfx.lights.points.near" ) { + Parser.getTokens( 1 ); + Parser >> gfx_lights_points_near; } + else if( Token == "gfx.lights.stars" ) { readlist( gfx_lights_stars ); } + else if( Token == "gfx.lights.freespot.mincone" ) { readvalue( gfx_lights_freespot_mincone, 0.f, 89.f ); } + else if( Token == "gfx.lights.lamps.gain" ) { readvalue( gfx_lights_lamps_gain, 0.1f, 10.f ); } + else if( Token == "gfx.lights.lamps.range" ) { readvalue( gfx_lights_lamps_range, 0.f, 10000.f ); } + else if( Token == "gfx.lights.hideglow" ) { readlist( gfx_lights_hideglow ); } + else { return false; } + + return true; +} + void global_settings::export_as_text( std::ostream &Output ) const { @@ -2633,6 +2662,12 @@ global_settings::export_as_text( std::ostream &Output ) const { 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 ); + export_as_text( Output, "gfx.lights.points.near", gfx_lights_points_near ); + export_as_text( Output, "gfx.lights.stars", gfx_lights_stars.empty() ? std::string{ "no" } : gfx_lights_stars ); + export_as_text( Output, "gfx.lights.freespot.mincone", gfx_lights_freespot_mincone ); + export_as_text( Output, "gfx.lights.lamps.gain", gfx_lights_lamps_gain ); + export_as_text( Output, "gfx.lights.lamps.range", gfx_lights_lamps_range ); + export_as_text( Output, "gfx.lights.hideglow", gfx_lights_hideglow.empty() ? std::string{ "no" } : gfx_lights_hideglow ); export_as_text( Output, "gfx.cabprobe", std::string{ gfx_cabprobe == 3 ? "high" : gfx_cabprobe == 2 ? "medium" : gfx_cabprobe == 1 ? "low" : "no" } ); export_as_text( Output, "gfx.cabprobe.interval", gfx_cabprobe_interval ); export_as_text( Output, "gfx.cabprobe.bounce", gfx_cabprobe_bounce ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 709594ad9..a05e364da 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -537,6 +537,26 @@ struct global_settings { 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 + // a light point of a scenery lamp shining downwards lights its surroundings also closer than its glare is seen from (the + // minimal distance of its submodel, which many lamps have set for the glare seen from afar), so the lamp doesn't go out as the + // camera comes near it. off: as before + bool gfx_lights_points_near { false }; + // names of the smt_Stars submodels of scenery models (many street lamps have nothing else) whose glowing points light their + // surroundings, each one downwards as the lamp of a street light, separated with |. other glowing points (lights of signals, + // of chimneys, the stars of the sky) only glow. empty (as before): none. the lamps of many models are named "lamp" + std::string gfx_lights_stars {}; + // the half angle of the cone of the light of a light point of a scenery lamp shining downwards is at least this, degrees. + // 0: the cone of its glare, which many lamps have narrow. lights shining sideways (signals) keep their cones + float gfx_lights_freespot_mincone { 0.f }; + // the brightness of the light the lamps of the scenery shining downwards (and their glowing points, gfx_lights_stars) light + // their surroundings with is multiplied by this. lights shining sideways (signals) keep theirs + float gfx_lights_lamps_gain { 1.f }; + // the lamps of the scenery are gathered from this far from the camera when it's above gfx_lights_range, metres, so the lit + // stations are seen from afar; the vehicles keep gfx_lights_range. 0: from gfx_lights_range + float gfx_lights_lamps_range { 0.f }; + // textures of the planes models fake the light of their lamps with (pools of light on the ground, glows), separated with |: + // they aren't drawn while the light points of scenery models light their surroundings. empty: they're drawn + std::string gfx_lights_hideglow; // the probe of the occupied cab (opengl 4.6 and vulkan, gfx::cab_probe): cube maps of the cab lit by its lamp (and what glows in // it) and of its albedo, drawn every few frames: the light of the lamp bounces around the cab from it (in place of // gfx_lights_cab_bounce) and the cab reflects itself in its glasses and glossy parts. 0 off, 1 low, 2 medium, 3 high (faces of @@ -718,6 +738,7 @@ struct global_settings { bool ConfigParse_gfx( cParser &parser, std::string_view const Token ); // sky light and exposure switches of the opengl 4.6 renderer (gfx.ambient.*, gfx.envmap.ggx, gfx.atmosphere*, gfx.postfx.autoexposure*, gfx.postfx.bloom*, gfx.postfx.whitebalance*, gfx.postfx.ao*) bool ConfigParse_gfx_sky( cParser &parser, std::string_view const Token ); + bool ConfigParse_gfx_lamps( cParser &parser, std::string_view const Token ); // sends basic content of the class in legacy (text) format to provided stream void export_as_text( std::ostream &Output ) const;