18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 02:27:06 +01:00
Files
maszyna/rendering/lightarray.cpp
Hirek d28b908ca4 refactor(render): the renderer reads a vehicle through TDynamicObject, not its physics
No file of rendering/ includes physics/vehicle.h any more. What the renderers
read of a vehicle comes through a few queries of TDynamicObject
(vehicle/dynamic/lights.cpp): its dimensions, its speed, its active cab, the
light of its compartments, the adapters of its couplers, the lights switched
on at an end, the low voltage for them, and the multiplier and the colour of
its headlights. The shares of the speed and the opacity of the exhaust smoke
(ExhaustScales()) move out of the particles into the vehicle, as they are.

The headlights pick their multiplier once, by the dimming and the high beam
switches, where the light array repeated the same four branches twice; the
light of a switched off end still goes to zero as before.
2026-11-29 10:43:31 +01:00

99 lines
4.1 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/.
*/
/*
MaSzyna EU07 locomotive simulator
Copyright (C) 2001-2004 Marcin Wozniak and others
*/
#include "stdafx.h"
#include "rendering/lightarray.h"
#include "vehicle/DynObj.h"
void
light_array::insert( TDynamicObject const *Owner ) {
// we're only storing lights for locos, which have two sets of lights, front and rear
// for a more generic role this function would have to be tweaked to add vehicle type-specific light combinations
data.emplace_back( Owner, NumberOf( VehicleEnd::front ) );
data.emplace_back( Owner, NumberOf( VehicleEnd::rear ) );
}
void
light_array::remove( TDynamicObject const *Owner ) {
std::erase_if( data, [=]( light_record const &light ){ return light.owner == Owner; } );
}
// updates records in the collection
void
light_array::update() {
for( auto &light : data ) {
// update light parameters to match current data of the owner
if( EndOf( light.index ) == VehicleEnd::front ) {
// front light set
light.position = light.owner->GetPosition() + ( light.owner->VectorFront() * ( std::max( 0.0, light.owner->GetLength() * 0.5 - 2.0 ) ) );// +( light.owner->VectorUp() * 0.25 );
light.direction = glm::make_vec3( glm::value_ptr(light.owner->VectorFront()) ); // TODO: It is needed to get value_ptr and then make_vec3?
}
else {
// rear light set
light.position = light.owner->GetPosition() - ( light.owner->VectorFront() * ( std::max( 0.0, light.owner->GetLength() * 0.5 - 2.0 ) ) );// +( light.owner->VectorUp() * 0.25 );
light.direction = glm::make_vec3( glm::value_ptr(light.owner->VectorFront()) ); // TODO: It is needed to get value_ptr and then make_vec3?
light.direction.x = -light.direction.x;
light.direction.z = -light.direction.z;
}
// determine intensity of this light set
if( true == light.owner->LowVoltagePowered() ) {
// with power on, the intensity depends on the state of activated switches
// first we cross-check the list of enabled lights with the lights installed in the vehicle...
auto const lights { light.owner->LightsSwitchedOn( static_cast<VehicleEnd>( light.index ) ) & light.owner->LightList( static_cast<VehicleEnd>( light.index ) ) };
// ...then check their individual state
light.count = 0
+ ( ( lights & light::headlight_left ) ? 1 : 0 )
+ ( ( lights & light::headlight_right ) ? 1 : 0 )
+ ( ( lights & light::headlight_upper ) ? 1 : 0 )
+ ( ( lights & light::highbeamlight_left ) ? 1: 0)
+ ( ( lights & light::highbeamlight_right ) ? 1 : 0);
// set intensity
if( light.count > 0 ) {
bool isEnabled = EndOf( light.index ) == VehicleEnd::front ? !light.owner->HeadlightsAoff : !light.owner->HeadlightsBoff;
light.intensity = std::max(0.0f, std::log((float)light.count + 1.0f));
auto const multiplier{light.owner->HeadlightsMultiplier()};
if (isEnabled)
light.intensity *= static_cast<float>(multiplier);
else
light.intensity = 0.0f;
// TBD, TODO: intensity can be affected further by other factors
light.state = {
( ( lights & light::headlight_left | light::highbeamlight_left ) ? 1.f : 0.f ),
( ( lights & light::headlight_upper ) ? 1.f : 0.f ),
( ( lights & light::headlight_right | light::highbeamlight_right) ? 1.f : 0.f ) };
light.color = light.owner->HeadlightsColor();
light.state *= multiplier;
}
else {
light.intensity = 0.0f;
light.state = glm::vec3{ 0.f };
}
}
else {
// with battery off the lights are off
light.intensity = 0.0f;
light.count = 0;
}
}
}