mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 05:57:08 +01:00
4ae7226cfa
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
757 lines
30 KiB
C++
757 lines
30 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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "rendering/common/locallights.h"
|
|
#include "rendering/common/moths.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <string_view>
|
|
#include <unordered_set>
|
|
|
|
#include "utilities/Globals.h"
|
|
#include "rendering/common/skyocclusion.h"
|
|
#include "utilities/utilities.h"
|
|
#include "scene/scene.h"
|
|
#include "model/AnimModel.h"
|
|
#include "model/Model3d.h"
|
|
#include "vehicle/DynObj.h"
|
|
#include "vehicle/Train.h"
|
|
#include "simulation/simulation.h"
|
|
#include "rendering/geometrybank.h"
|
|
|
|
namespace
|
|
{
|
|
// a light ends where its contribution falls under this. it is above the limit of the culling of the clusters, as there are
|
|
// many more of these lights than headlights, and each of them lights only its surroundings
|
|
float const LIGHT_THRESHOLD{0.01f};
|
|
// no light of a model reaches farther than this, metres
|
|
float const LIGHT_RANGE_MAX{60.f};
|
|
// the self-illumination threshold of the lamps of scenery models (Light_On, ls_Dark) and full darkness: between them
|
|
// the lights of models and compartments fade in, as they would be lost in the daylight anyway
|
|
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)
|
|
{
|
|
return glm::pow(glm::max(Color, glm::vec3{0.f}), glm::vec3{2.2f});
|
|
}
|
|
|
|
float max_component(glm::vec3 const &Color)
|
|
{
|
|
return std::max(Color.r, std::max(Color.g, Color.b));
|
|
}
|
|
|
|
// distance at which a light of the colour falls under the threshold with the square of the distance
|
|
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)
|
|
{
|
|
for (auto const *submodel{Submodel}; submodel != nullptr; submodel = submodel->Next)
|
|
{
|
|
if (submodel == Target)
|
|
{
|
|
Result = Parent;
|
|
return true;
|
|
}
|
|
auto transform{Parent};
|
|
if ((submodel->iFlags & 0xC000) && (submodel->fMatrix != nullptr))
|
|
{
|
|
transform *= glm::make_mat4(submodel->fMatrix->readArray());
|
|
}
|
|
if (find_parent_transform(submodel->Child, Target, transform, Result))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// bounds of the geometry of the submodel, of its children and, with Siblings, of the submodels after it, in the space of Transform
|
|
void extend_bounds(TSubModel const *Submodel, glm::mat4 const &Transform, gfx::geometrybank_manager const &Geometry, bool const Siblings, glm::vec3 &Min, glm::vec3 &Max,
|
|
bool &Lightpoints)
|
|
{
|
|
for (auto const *submodel{Submodel}; submodel != nullptr; submodel = (Siblings ? submodel->Next : nullptr))
|
|
{
|
|
auto transform{Transform};
|
|
if ((submodel->iFlags & 0xC000) && (submodel->fMatrix != nullptr))
|
|
{
|
|
transform *= glm::make_mat4(submodel->fMatrix->readArray());
|
|
}
|
|
if (submodel->eType == TP_FREESPOTLIGHT)
|
|
{
|
|
Lightpoints = true;
|
|
}
|
|
else if ((submodel->eType < TP_ROTATOR) && (submodel->m_geometry.handle != null_handle))
|
|
{
|
|
for (auto const &vertex : Geometry.vertices(submodel->m_geometry.handle))
|
|
{
|
|
auto const point{glm::vec3{transform * glm::vec4{vertex.position, 1.f}}};
|
|
Min = glm::min(Min, point);
|
|
Max = glm::max(Max, point);
|
|
}
|
|
}
|
|
extend_bounds(submodel->Child, transform, Geometry, true, Min, Max, Lightpoints);
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Region, gfx::geometrybank_manager const &Geometry, unsigned int const Framestamp)
|
|
{
|
|
auto const start{std::chrono::steady_clock::now()};
|
|
|
|
m_lights.clear();
|
|
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};
|
|
m_editorseen = EditorModeFlag;
|
|
|
|
if (Global.gfx_lights_clustered)
|
|
{
|
|
if (Global.gfx_lights_cab)
|
|
{
|
|
gather_cab_lights(Geometry);
|
|
}
|
|
|
|
// 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<float>(Global.fLuminance))};
|
|
auto const range{gather_range()};
|
|
|
|
if ((darkness > 0.f) && (range > 0.f) && Global.gfx_lights_scenery)
|
|
{
|
|
auto const freespotbrightness{Global.gfx_lights_freespot_intensity * darkness};
|
|
// sections of the region within the range, the same way the render picks them
|
|
auto const camerax{static_cast<int>(std::floor(Camera.x / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))};
|
|
auto const cameraz{static_cast<int>(std::floor(Camera.z / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))};
|
|
auto const reach{static_cast<int>(std::ceil(range / scene::EU07_SECTIONSIZE))};
|
|
for (int row = std::max(0, cameraz - reach); row <= std::min(scene::EU07_REGIONSIDESECTIONCOUNT - 1, cameraz + reach); ++row)
|
|
{
|
|
for (int column = std::max(0, camerax - reach); column <= std::min(scene::EU07_REGIONSIDESECTIONCOUNT - 1, camerax + reach); ++column)
|
|
{
|
|
auto *section{Region.m_sections[row * scene::EU07_REGIONSIDESECTIONCOUNT + column]};
|
|
if (section == nullptr)
|
|
{
|
|
continue;
|
|
}
|
|
for (auto &cell : section->m_cells)
|
|
{
|
|
if ((false == cell.m_active) || (glm::length(cell.m_area.center - Camera) > range + cell.m_area.radius))
|
|
{
|
|
continue;
|
|
}
|
|
for (auto *instance : cell_instances(cell, rebuild).instances)
|
|
{
|
|
gather_instance(instance, Camera, freespotbrightness, Framestamp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ((darkness > 0.f) && (range > 0.f) && Global.gfx_lights_vehicles)
|
|
{
|
|
auto const freespotbrightness{Global.gfx_lights_freespot_intensity * darkness};
|
|
auto const compartmentbrightness{Global.gfx_lights_compartment_intensity * darkness};
|
|
for (auto *vehicle : simulation::Vehicles.sequence())
|
|
{
|
|
if (glm::length(vehicle->GetPosition() - Camera) > range + vehicle->radius())
|
|
{
|
|
continue;
|
|
}
|
|
gather_vehicle(vehicle, Camera, Geometry, freespotbrightness, compartmentbrightness);
|
|
}
|
|
}
|
|
|
|
// the most important lights first: when the light buffer or the list of a cluster fills up, the others are left out
|
|
std::sort(std::begin(m_lights), std::end(m_lights), [&Camera](light const &Left, light const &Right) {
|
|
return glm::length2(Left.position - Camera) / std::max(max_component(Left.color), 1e-6f) < glm::length2(Right.position - Camera) / std::max(max_component(Right.color), 1e-6f);
|
|
});
|
|
}
|
|
|
|
// the moths fly about the lamps of the scenery which shine
|
|
Moths.update(m_lights, Camera, Framestamp);
|
|
|
|
auto const elapsed{std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count()};
|
|
m_gathertime = (m_gathertime == 0.0 ? elapsed : m_gathertime * 0.95 + elapsed * 0.05);
|
|
}
|
|
|
|
void gfx::local_lights::reset()
|
|
{
|
|
m_lights.clear();
|
|
m_cablights.clear();
|
|
m_cablampon = false;
|
|
m_modellights.clear();
|
|
m_cells.clear();
|
|
m_sections.clear();
|
|
m_models.clear();
|
|
m_scenerycount = m_vehiclecount = m_compartmentcount = m_placed = m_dropped = 0;
|
|
Moths.reset();
|
|
}
|
|
|
|
void gfx::local_lights::gather_instance(TAnimModel *Instance, glm::dvec3 const &Camera, float const Brightness, unsigned int const Framestamp)
|
|
{
|
|
if ((false == Instance->m_visible) || (Instance->pModel == nullptr))
|
|
{
|
|
return;
|
|
}
|
|
auto const offset{Instance->location() - Camera};
|
|
if (glm::length(offset) > gather_range() + Instance->radius())
|
|
{
|
|
return;
|
|
}
|
|
// the same distance the render picks the lods and the range of the instance by
|
|
auto const distancesquared{glm::length2(offset / static_cast<double>(Global.ZoomFactor)) / Global.fDistanceFactor};
|
|
if ((distancesquared < Instance->m_rangesquaredmin) || (distancesquared >= Instance->m_rangesquaredmax))
|
|
{
|
|
return;
|
|
}
|
|
|
|
Instance->RaAnimate(Framestamp);
|
|
Instance->RaPrepare();
|
|
TSubModel::fSquareDist = static_cast<float>(distancesquared);
|
|
|
|
auto const count{m_lights.size()};
|
|
auto const placement{glm::translate(glm::dmat4{1.0}, Instance->location()) * glm::dmat4{Instance->rotation_scale()}};
|
|
m_walkowner = Instance;
|
|
walk(Instance->pModel->Root, glm::mat4{1.f}, placement, Brightness);
|
|
m_walkowner = nullptr;
|
|
m_scenerycount += static_cast<std::uint32_t>(m_lights.size() - count);
|
|
}
|
|
|
|
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
|
|
return;
|
|
}
|
|
// light points: the models with any are gone through after the setup the render makes before it draws the vehicle
|
|
std::array<TModel3d *, 2> models{Vehicle->mdModel, Vehicle->mdLowPolyInt};
|
|
auto const haslightpoints = [this](TModel3d *Model) { return (Model != nullptr) && has_light_points(Model); };
|
|
if (std::any_of(std::begin(models), std::end(models), haslightpoints) || std::any_of(std::begin(Vehicle->mdAttachments), std::end(Vehicle->mdAttachments), haslightpoints))
|
|
{
|
|
TSubModel::iInstance = reinterpret_cast<std::uintptr_t>(Vehicle);
|
|
TSubModel::iVariantSeed = Vehicle->variant_seed();
|
|
auto const squaredistance{glm::length2(glm::vec3{Vehicle->vPosition - Camera} / Global.ZoomFactor)};
|
|
Vehicle->ABuLittleUpdate(squaredistance);
|
|
|
|
auto const placement{glm::translate(glm::dmat4{1.0}, Vehicle->vPosition) * Vehicle->mMatrix};
|
|
auto const count{m_lights.size()};
|
|
auto const walkmodel = [&](TModel3d *Model) {
|
|
if (haslightpoints(Model))
|
|
{
|
|
TSubModel::fSquareDist = squaredistance;
|
|
walk(Model->Root, glm::mat4{1.f}, placement, Freespotbrightness);
|
|
}
|
|
};
|
|
walkmodel(Vehicle->mdModel);
|
|
for (auto *attachment : Vehicle->mdAttachments)
|
|
{
|
|
walkmodel(attachment);
|
|
}
|
|
walkmodel(Vehicle->mdLowPolyInt);
|
|
m_vehiclecount += static_cast<std::uint32_t>(m_lights.size() - count);
|
|
|
|
if (Vehicle->btnOn)
|
|
{
|
|
Vehicle->TurnOff(); // the state of the shared model goes back to the default, as after the render
|
|
}
|
|
}
|
|
|
|
// lit compartments, the way TDynamicObject::Update() works out their self-illumination: cab sections come first and are lit at
|
|
// the level of their lamp, the others at the level of the compartment lights
|
|
if ((Vehicle->mdLowPolyInt != nullptr) && (Compartmentbrightness > 0.f))
|
|
{
|
|
auto const owncab{m_cablampon && (simulation::Train != nullptr) && (simulation::Train->Dynamic() == Vehicle)};
|
|
auto cabsection{true};
|
|
for (auto const §ion : Vehicle->Sections)
|
|
{
|
|
auto const &name{section.compartment->pName};
|
|
cabsection &= (name.size() >= 4) && name.starts_with("cab");
|
|
auto const level{section.light_level * (cabsection ? 1.f : Vehicle->MoverParameters->CompartmentLights.intensity)};
|
|
if ((level <= 0.f) || (Global.fLuminance > section.compartment->fLight) || (section.compartment->iVisible == 0))
|
|
{
|
|
continue;
|
|
}
|
|
if (cabsection && owncab)
|
|
{
|
|
// the lamp of the occupied cab is a light of its own
|
|
continue;
|
|
}
|
|
auto const &data{this->section(Vehicle->mdLowPolyInt, section.compartment, Geometry)};
|
|
if (data.has_light_points || (data.extent == glm::vec3{0.f}))
|
|
{
|
|
continue;
|
|
}
|
|
light compartmentlight;
|
|
compartmentlight.position = Vehicle->GetWorldPosition(glm::dvec3{data.center});
|
|
compartmentlight.color = to_linear(Vehicle->InteriorLight) * (level * Compartmentbrightness);
|
|
// the light goes out of the windows a little; there are no shadows to keep it in, so it doesn't reach far
|
|
compartmentlight.range = std::clamp(glm::length(data.extent) * 1.5f, 2.f, std::min(light_range(compartmentlight.color), 15.f));
|
|
m_lights.emplace_back(compartmentlight);
|
|
++m_compartmentcount;
|
|
}
|
|
}
|
|
}
|
|
|
|
void gfx::local_lights::gather_cab_lights(gfx::geometrybank_manager const &Geometry)
|
|
{
|
|
// the cab is drawn, Render_cab() conditions
|
|
if (FreeFlyModeFlag || (false == Global.render_cab) || (simulation::Train == nullptr))
|
|
{
|
|
return;
|
|
}
|
|
auto const *train{simulation::Train};
|
|
auto const *vehicle{train->Dynamic()};
|
|
if ((vehicle == nullptr) || (false == vehicle->bDisplayCab) || (vehicle->mdKabina == nullptr) || (vehicle->mdKabina == vehicle->mdModel))
|
|
{
|
|
return;
|
|
}
|
|
auto const &bounds{model_bounds(vehicle->mdKabina, Geometry)};
|
|
if (bounds.extent == glm::vec3{0.f})
|
|
{
|
|
return;
|
|
}
|
|
// as bright as the ambient light the lamp used to add to the cab: lost in the daylight
|
|
auto const luminance{Global.fLuminance * gfx::vehicle_light_level(vehicle)};
|
|
auto const daylight{std::clamp(1.25f - static_cast<float>(luminance), 0.f, 1.f)};
|
|
auto const lightpoints{has_light_points(vehicle->mdKabina)};
|
|
|
|
if (lightpoints && (daylight > 0.f))
|
|
{
|
|
// the light points of the cab model, in its space, the way Render_cab() sets it up. they are switched by the visibility of
|
|
// the submodels they're in (the lamp of the cab under i-cablight:, the lights of the desk under i-dashboardlight:, ...)
|
|
TSubModel::iInstance = reinterpret_cast<std::uintptr_t>(vehicle);
|
|
TSubModel::iVariantSeed = vehicle->variant_seed();
|
|
TSubModel::fSquareDist = 0.f;
|
|
auto const count{m_lights.size()};
|
|
m_walkcab = true;
|
|
walk(vehicle->mdKabina->Root, glm::mat4{1.f}, glm::dmat4{1.0}, Global.gfx_lights_freespot_intensity * daylight);
|
|
m_walkcab = false;
|
|
// within the cab: without shadows (gfx.lights.shadows) the light goes through its walls
|
|
auto const reach{std::max(2.f * glm::length(bounds.extent), 2.5f)};
|
|
auto const rotation{glm::dmat3{vehicle->mMatrix}};
|
|
for (auto idx{count}; idx < m_lights.size(); ++idx)
|
|
{
|
|
cab_light cablight;
|
|
cablight.source = m_lights[idx];
|
|
cablight.local = glm::vec3{m_lights[idx].position};
|
|
cablight.local_direction = m_lights[idx].direction;
|
|
cablight.source.position = vehicle->GetWorldPosition(m_lights[idx].position);
|
|
auto const direction{glm::vec3{rotation * glm::dvec3{m_lights[idx].direction}}};
|
|
cablight.source.direction = (glm::length2(direction) > 0.f ? glm::normalize(direction) : glm::vec3{0.f, -1.f, 0.f});
|
|
cablight.source.range = std::min(cablight.source.range, reach);
|
|
m_cablights.emplace_back(cablight);
|
|
}
|
|
m_lights.resize(count);
|
|
}
|
|
|
|
// the lamp of a cab model without light points of its own: under the ceiling, in the middle of the cab, at the level of its
|
|
// switch (off, dimmed, with or without the 110 V)
|
|
auto const cablevel{std::max(vehicle->InteriorLightLevel, 0.f) * daylight};
|
|
if ((false == lightpoints) && (cablevel > 0.f))
|
|
{
|
|
auto const lamp{glm::vec3{bounds.center.x, std::max(bounds.center.y, bounds.center.y + bounds.extent.y - 0.25f), bounds.center.z}};
|
|
cab_light cablight;
|
|
cablight.local = lamp;
|
|
cablight.source.position = vehicle->GetWorldPosition(glm::dvec3{lamp});
|
|
cablight.source.color = to_linear(vehicle->InteriorLight) * (cablevel * Global.gfx_lights_cab_intensity);
|
|
// within the cab: without shadows (gfx.lights.shadows) the lamp would light the track below and the body of the vehicle around it
|
|
cablight.source.range = std::clamp(glm::length(bounds.extent) * 1.25f, 2.5f, 8.f);
|
|
cablight.source.part = vehicle;
|
|
if (max_component(cablight.source.color) > 0.f)
|
|
{
|
|
m_cablights.emplace_back(cablight);
|
|
}
|
|
}
|
|
|
|
// the brightest first: the first ones get the shadow maps, the brightest lights the probe of the cab
|
|
std::stable_sort(std::begin(m_cablights), std::end(m_cablights), [](cab_light const &Left, cab_light const &Right) { return max_component(Left.source.color) > max_component(Right.source.color); });
|
|
if (m_cablights.size() > CAB_LIGHTS_MAX)
|
|
{
|
|
m_cablights.resize(CAB_LIGHTS_MAX);
|
|
}
|
|
m_cablampvehicle = vehicle;
|
|
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 (auto const mindistance{walk_min_distance(*submodel)}; (submodel->iVisible == 0) || (TSubModel::fSquareDist < mindistance) || (TSubModel::fSquareDist >= submodel->fSquareMaxDist))
|
|
{
|
|
continue;
|
|
}
|
|
auto transform{Transform};
|
|
if (submodel->iFlags & 0xC000)
|
|
{
|
|
if (submodel->fMatrix != nullptr)
|
|
{
|
|
transform *= glm::make_mat4(submodel->fMatrix->readArray());
|
|
}
|
|
if (submodel->b_aAnim != TAnimType::at_None)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
void gfx::local_lights::add_light_point(TSubModel const &Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float Brightness)
|
|
{
|
|
if (m_walkcab)
|
|
{
|
|
// the lights of the cab: switched with the submodels they're in, whatever the self-illumination threshold
|
|
if ((Submodel.f4Emision.a <= 0.f) || (Submodel.fVisible <= 0.f))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
// lit points: with self-illumination, past its threshold. the compartments of vehicles set it with their lights
|
|
else if ((Submodel.f4Emision.a <= 0.f) || (Global.fLuminance >= Submodel.fLight) || (Submodel.fVisible <= 0.f))
|
|
{
|
|
return;
|
|
}
|
|
auto const diffuse{Submodel.DiffuseOverride.r < 0.f ? // -1 indicates no override
|
|
glm::vec3{Submodel.f4Diffuse} :
|
|
Submodel.DiffuseOverride};
|
|
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}};
|
|
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
|
|
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;
|
|
lightpoint.shade_self = Submodel.bShadeSelf;
|
|
if (m_walkowner != nullptr)
|
|
{
|
|
lightpoint.owner = m_walkowner;
|
|
lightpoint.part = &Submodel;
|
|
}
|
|
else if (m_walkcab)
|
|
{
|
|
// the light of the cab, its shadow map kept by it
|
|
lightpoint.part = &Submodel;
|
|
}
|
|
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)};
|
|
if (lookup != m_modellights.end())
|
|
{
|
|
return lookup->second;
|
|
}
|
|
auto found{false};
|
|
std::vector<TSubModel const *> pending{Model->Root};
|
|
while ((false == found) && (false == pending.empty()))
|
|
{
|
|
auto const *submodel{pending.back()};
|
|
pending.pop_back();
|
|
for (; submodel != nullptr; submodel = submodel->Next)
|
|
{
|
|
if ((submodel->eType == TP_FREESPOTLIGHT) || is_lamp_points(*submodel))
|
|
{
|
|
found = true;
|
|
break;
|
|
}
|
|
if (submodel->Child != nullptr)
|
|
{
|
|
pending.emplace_back(submodel->Child);
|
|
}
|
|
}
|
|
}
|
|
m_modellights.emplace(Model, found);
|
|
return found;
|
|
}
|
|
|
|
void gfx::local_lights::add_lit_instances(std::vector<TAnimModel *> const &Instances, std::unordered_set<TAnimModel const *> &Added, std::vector<TAnimModel *> &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.
|
|
// 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<void const *>(instances.data())) || (data.opaque_size != opaque.size()) ||
|
|
(data.opaque_data != static_cast<void const *>(opaque.data())))
|
|
{
|
|
data.instances.clear();
|
|
std::unordered_set<TAnimModel const *> 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;
|
|
}
|
|
|
|
gfx::local_lights::section_data const &gfx::local_lights::section(TModel3d *Model, TSubModel *Section, gfx::geometrybank_manager const &Geometry)
|
|
{
|
|
auto lookup{m_sections.find(Section)};
|
|
if (lookup != m_sections.end())
|
|
{
|
|
return lookup->second;
|
|
}
|
|
section_data data;
|
|
glm::mat4 transform{1.f};
|
|
if (find_parent_transform(Model->Root, Section, glm::mat4{1.f}, transform))
|
|
{
|
|
glm::vec3 min{std::numeric_limits<float>::max()};
|
|
glm::vec3 max{std::numeric_limits<float>::lowest()};
|
|
extend_bounds(Section, transform, Geometry, false, min, max, data.has_light_points);
|
|
if (min.x <= max.x)
|
|
{
|
|
data.center = 0.5f * (min + max);
|
|
data.extent = 0.5f * (max - min);
|
|
}
|
|
}
|
|
return m_sections.emplace(Section, data).first->second;
|
|
}
|
|
|
|
gfx::local_lights::section_data const &gfx::local_lights::model_bounds(TModel3d *Model, gfx::geometrybank_manager const &Geometry)
|
|
{
|
|
auto lookup{m_models.find(Model)};
|
|
if (lookup != m_models.end())
|
|
{
|
|
return lookup->second;
|
|
}
|
|
section_data data;
|
|
glm::vec3 min{std::numeric_limits<float>::max()};
|
|
glm::vec3 max{std::numeric_limits<float>::lowest()};
|
|
extend_bounds(Model->Root, glm::mat4{1.f}, Geometry, true, min, max, data.has_light_points);
|
|
if (min.x <= max.x)
|
|
{
|
|
data.center = 0.5f * (min + max);
|
|
data.extent = 0.5f * (max - min);
|
|
}
|
|
return m_models.emplace(Model, data).first->second;
|
|
}
|
|
|
|
bool gfx::local_lights::cab_bounds(TModel3d *Cab, gfx::geometrybank_manager const &Geometry, glm::vec3 &Center, glm::vec3 &Extent)
|
|
{
|
|
if (Cab == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
auto const &bounds{model_bounds(Cab, Geometry)};
|
|
Center = bounds.center;
|
|
Extent = bounds.extent;
|
|
return (bounds.extent != glm::vec3{0.f});
|
|
}
|
|
|
|
std::string gfx::local_lights::info() const
|
|
{
|
|
return "local lights: " + std::to_string(m_lights.size()) + " (scenery " + std::to_string(m_scenerycount) + ", vehicles " + std::to_string(m_vehiclecount) + ", compartments " +
|
|
std::to_string(m_compartmentcount) + ")" + (m_cablampon ? " + cab lights " + std::to_string(m_cablights.size()) : "") + ", placed " + std::to_string(m_placed) + ", over capacity " + std::to_string(m_dropped) +
|
|
", gather " + to_string(m_gathertime, 2) + " ms, moth swarms " + std::to_string(Moths.shown()) + "/" + std::to_string(Moths.count());
|
|
}
|
|
|
|
void gfx::local_lights::draw_debug_ui()
|
|
{
|
|
ImGui::TextUnformatted("local lights (need clustered lights)");
|
|
ImGui::Checkbox("light points of scenery models", &Global.gfx_lights_scenery);
|
|
ImGui::Checkbox("light points and compartments of vehicles", &Global.gfx_lights_vehicles);
|
|
ImGui::Checkbox("cab lamp as a light (or the light points of the cab model)", &Global.gfx_lights_cab);
|
|
ImGui::SliderFloat("light point brightness", &Global.gfx_lights_freespot_intensity, 0.f, 50.f);
|
|
ImGui::SliderFloat("compartment brightness", &Global.gfx_lights_compartment_intensity, 0.f, 10.f);
|
|
ImGui::SliderFloat("cab lamp brightness", &Global.gfx_lights_cab_intensity, 0.f, 10.f);
|
|
ImGui::SliderFloat("cab ambient kept", &Global.gfx_lights_cab_bounce, 0.f, 1.f);
|
|
ImGui::SliderFloat("range, m", &Global.gfx_lights_range, 0.f, 1000.f);
|
|
ImGui::Checkbox("moths at the lamps of the scenery", &Global.moths);
|
|
ImGui::SliderFloat("moths: part of the lamps", &Global.moths_share, 0.f, 1.f);
|
|
ImGui::SliderFloat("moths: range, m", &Global.moths_range, 0.f, 300.f);
|
|
ImGui::Text("moth swarms shown %u, made %u", static_cast<unsigned>(Moths.shown()), static_cast<unsigned>(Moths.count()));
|
|
ImGui::Text("gathered %u: scenery %u, vehicles %u, compartments %u, cab %u", static_cast<unsigned>(m_lights.size()), m_scenerycount, m_vehiclecount, m_compartmentcount,
|
|
static_cast<unsigned>(m_cablights.size()));
|
|
ImGui::Text("in the light buffer %u, over capacity %u", m_placed, m_dropped);
|
|
ImGui::Text("gather %.2f ms (cpu)", m_gathertime);
|
|
}
|