16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-20 09:09:19 +02:00

Replace clamp with std::clamp

This commit is contained in:
docentYT
2026-05-01 22:14:29 +02:00
parent 1bdd000443
commit d1f16411a1
36 changed files with 271 additions and 287 deletions

View File

@@ -67,7 +67,7 @@ opengl33_particles::update( opengl_camera const &Camera ) {
vertex.color[ 0 ] = particlecolor.r;
vertex.color[ 1 ] = particlecolor.g;
vertex.color[ 2 ] = particlecolor.b;
vertex.color.a = clamp(particle.opacity, 0.0f, 1.0f);
vertex.color.a = std::clamp(particle.opacity, 0.0f, 1.0f);
auto const offset { glm::vec3{ particle.position - Camera.position() } };
auto const rotation { glm::angleAxis( particle.rotation, glm::vec3{ 0.f, 0.f, 1.f } ) };

View File

@@ -1293,7 +1293,7 @@ bool opengl33_renderer::Render_lowpoly( TDynamicObject *Dynamic, float const Squ
Dynamic->fShade :
1.0 ) ) ) ) };
setup_sunlight_intensity(
clamp( (
std::clamp( (
Dynamic->fShade > 0.f ?
Dynamic->fShade :
1.f )
@@ -1913,20 +1913,20 @@ bool opengl33_renderer::Render(world_environment *Environment)
auto const &modelview = OpenGLMatrices.data(GL_MODELVIEW);
auto const fogfactor{clamp(Global.fFogEnd / 2000.f, 0.f, 1.f)}; // stronger fog reduces opacity of the celestial bodies
float const duskfactor = 1.0f - clamp(std::abs(Environment->m_sun.getAngle()), 0.0f, 12.0f) / 12.0f;
auto const fogfactor{std::clamp(Global.fFogEnd / 2000.f, 0.f, 1.f)}; // stronger fog reduces opacity of the celestial bodies
float const duskfactor = 1.0f - std::clamp(std::abs(Environment->m_sun.getAngle()), 0.0f, 12.0f) / 12.0f;
glm::vec3 suncolor = interpolate(glm::vec3(255.0f / 255.0f, 242.0f / 255.0f, 231.0f / 255.0f), glm::vec3(235.0f / 255.0f, 140.0f / 255.0f, 36.0f / 255.0f), duskfactor);
// sun
{
Bind_Texture(0, m_suntexture);
glm::vec4 color(suncolor.x, suncolor.y, suncolor.z, clamp(1.5f - Global.Overcast, 0.f, 1.f) * fogfactor);
glm::vec4 color(suncolor.x, suncolor.y, suncolor.z, std::clamp(1.5f - Global.Overcast, 0.f, 1.f) * fogfactor);
auto const sunvector = Environment->m_sun.getDirection();
/*float const size = interpolate( // TODO: expose distance/scale factor from the moon object
0.0325f,
0.0275f,
clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f );*/
std::clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f );*/
model_ubs.param[0] = color;
model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(sunvector, 1.0f)), 0.00463f /* size */);
model_ubs.param[2] = glm::vec4(0.0f, 1.0f, 1.0f, 0.0f);
@@ -2003,7 +2003,7 @@ bool opengl33_renderer::Render(world_environment *Environment)
float const size = interpolate( // TODO: expose distance/scale factor from the moon object
0.0160f,
0.0135f,
clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f );*/
std::clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f );*/
model_ubs.param[0] = color;
model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(moonvector, 1.0f)), 0.00451f /* size */);
@@ -2044,7 +2044,7 @@ bool opengl33_renderer::Render(world_environment *Environment)
m_sunlight.apply_intensity();
// calculate shadow tone, based on positions of celestial bodies
m_shadowcolor = interpolate(glm::vec4{colors::shadow}, glm::vec4{colors::white}, clamp(-Environment->m_sun.getAngle(), 0.f, 6.f) / 6.f);
m_shadowcolor = interpolate(glm::vec4{colors::shadow}, glm::vec4{colors::white}, std::clamp(-Environment->m_sun.getAngle(), 0.f, 6.f) / 6.f);
if ((Environment->m_sun.getAngle() < -18.f) && (Environment->m_moon.getAngle() > 0.f))
{
// turn on moon shadows after nautical twilight, if the moon is actually up
@@ -3102,7 +3102,7 @@ bool opengl33_renderer::Render_cab(TDynamicObject const *Dynamic, float const Li
auto const luminance { Global.fLuminance * ( Dynamic->fShade > 0.0f ? Dynamic->fShade : 1.0f ) };
if( Lightlevel > 0.f ) {
// crude way to light the cabin, until we have something more complete in place
light_ubs.ambient += ( Dynamic->InteriorLight * Lightlevel ) * static_cast<float>( clamp( 1.25 - luminance, 0.0, 1.0 ) );
light_ubs.ambient += ( Dynamic->InteriorLight * Lightlevel ) * std::clamp( 1.25f - (float)luminance, 0.f, 1.f );
light_ubo->update( light_ubs );
}
@@ -3715,7 +3715,7 @@ void opengl33_renderer::Render_precipitation()
::glRotated(m_precipitationrotation, 0.0, 1.0, 0.0);
model_ubs.set_modelview(OpenGLMatrices.data(GL_MODELVIEW));
model_ubs.param[0] = interpolate(0.5f * (Global.DayLight.diffuse + Global.DayLight.ambient), colors::white, 0.5f * clamp<float>(Global.fLuminance, 0.f, 1.f));
model_ubs.param[0] = interpolate(0.5f * (Global.DayLight.diffuse + Global.DayLight.ambient), colors::white, 0.5f * std::clamp((float)Global.fLuminance, 0.f, 1.f));
model_ubs.param[1].x = simulation::Environment.m_precipitation.get_textureoffset();
model_ubo->update(model_ubs);
@@ -3891,7 +3891,7 @@ void opengl33_renderer::Render_Alpha(TTraction *Traction)
auto const distance{static_cast<float>(std::sqrt(distancesquared))};
auto const linealpha = 20.f * Traction->WireThickness / std::max(0.5f * Traction->radius() + 1.f, distance - (0.5f * Traction->radius()));
if (m_widelines_supported)
glLineWidth(clamp(0.5f * linealpha + Traction->WireThickness * Traction->radius() / 1000.f, 1.f, 1.75f));
glLineWidth(std::clamp(0.5f * linealpha + Traction->WireThickness * Traction->radius() / 1000.f, 1.f, 1.75f));
// render
@@ -3929,7 +3929,7 @@ void opengl33_renderer::Render_Alpha(scene::lines_node const &Lines)
auto const linealpha =
(data.line_width > 0.f ? 10.f * data.line_width / std::max(0.5f * data.area.radius + 1.f, distance - (0.5f * data.area.radius)) : 1.f); // negative width means the lines are always opague
if (m_widelines_supported)
glLineWidth(clamp(0.5f * linealpha + data.line_width * data.area.radius / 1000.f, 1.f, 8.f));
glLineWidth(std::clamp(0.5f * linealpha + data.line_width * data.area.radius / 1000.f, 1.f, 8.f));
model_ubs.param[0] = glm::vec4(glm::vec3(data.lighting.diffuse * m_sunlight.ambient), glm::min(1.0f, linealpha));
@@ -4171,11 +4171,11 @@ void opengl33_renderer::Render_Alpha(TSubModel *Submodel)
{
// only bother if the viewer is inside the visibility cone
// luminosity at night is at level of ~0.1, so the overall resulting transparency in clear conditions is ~0.5 at full 'brightness'
auto glarelevel{clamp(std::max<float>(0.6f - Global.fLuminance, // reduce the glare in bright daylight
auto glarelevel{std::clamp(std::max<float>(0.6f - Global.fLuminance, // reduce the glare in bright daylight
Global.Overcast - 1.f), // ensure some glare in rainy/foggy conditions
0.f, 1.f)};
// view angle attenuation
float const anglefactor{clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f)};
float const anglefactor{std::clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f)};
glarelevel *= anglefactor;
@@ -4226,7 +4226,7 @@ void opengl33_renderer::Render_Alpha(TSubModel *Submodel)
// kąt większy niż maksymalny stożek swiatła
float lightlevel = 1.f; // TODO, TBD: parameter to control light strength
// view angle attenuation
float const anglefactor = clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f);
float const anglefactor = std::clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f);
lightlevel *= anglefactor;
// distance attenuation. NOTE: since it's fixed pipeline with built-in gamma correction we're using linear attenuation
@@ -4236,7 +4236,7 @@ void opengl33_renderer::Render_Alpha(TSubModel *Submodel)
// additionally reduce light strength for farther sources in rain or snow
if (Global.Overcast > 0.75f)
{
float const precipitationfactor{interpolate(interpolate(1.f, 0.25f, clamp(Global.Overcast * 0.75f - 0.5f, 0.f, 1.f)), 1.f, distancefactor)};
float const precipitationfactor{interpolate(interpolate(1.f, 0.25f, std::clamp(Global.Overcast * 0.75f - 0.5f, 0.f, 1.f)), 1.f, distancefactor)};
lightlevel *= precipitationfactor;
}
@@ -4263,7 +4263,7 @@ void opengl33_renderer::Render_Alpha(TSubModel *Submodel)
if (Global.Overcast > 1.0f)
{
// fake fog halo
float const fogfactor{interpolate(1.5f, 1.f, clamp(Global.fFogEnd / 2000, 0.f, 1.f)) * std::max(1.f, Global.Overcast)};
float const fogfactor{interpolate(1.5f, 1.f, std::clamp(Global.fFogEnd / 2000, 0.f, 1.f)) * std::max(1.f, Global.Overcast)};
model_ubs.param[1].x = pointsize * fogfactor * 4.0f;
model_ubs.param[0] = glm::vec4(glm::vec3(lightcolor), Submodel->fVisible * std::min(1.f, lightlevel) * 0.5f);

View File

@@ -22,7 +22,7 @@ opengl_light::apply_intensity( float const Factor ) {
::glLightfv( id, GL_SPECULAR, glm::value_ptr( specular ) );
}
else {
auto const factor{ clamp( Factor, 0.05f, 1.f ) };
auto const factor{ std::clamp( Factor, 0.05f, 1.f ) };
// temporary light scaling mechanics (ultimately this work will be left to the shaders
glm::vec4 scaledambient( ambient.r * factor, ambient.g * factor, ambient.b * factor, ambient.a );
glm::vec4 scaleddiffuse( diffuse.r * factor, diffuse.g * factor, diffuse.b * factor, diffuse.a );

View File

@@ -65,7 +65,7 @@ opengl_particles::update( opengl_camera const &Camera ) {
vertex.color[ 0 ] = static_cast<std::uint_fast8_t>( particlecolor.r );
vertex.color[ 1 ] = static_cast<std::uint_fast8_t>( particlecolor.g );
vertex.color[ 2 ] = static_cast<std::uint_fast8_t>( particlecolor.b );
vertex.color[ 3 ] = clamp<std::uint8_t>( particle.opacity * 255, 0, 255 );
vertex.color[ 3 ] = static_cast<std::uint_fast8_t>( std::clamp( particle.opacity * 255, 0.f, 255.f ) );
auto const offset { glm::vec3{ particle.position - Camera.position() } };
auto const rotation { glm::angleAxis( particle.rotation, glm::vec3{ 0.f, 0.f, 1.f } ) };

View File

@@ -821,7 +821,7 @@ bool opengl_renderer::Render_lowpoly( TDynamicObject *Dynamic, float const Squar
Dynamic->fShade :
1.0 ) ) ) ) };
m_sunlight.apply_intensity(
clamp( (
std::clamp( (
Dynamic->fShade > 0.f ?
Dynamic->fShade :
1.f )
@@ -923,7 +923,7 @@ opengl_renderer::setup_pass( renderpass_config &Config, rendermode const Mode, f
switch( Mode ) {
case rendermode::color: { Config.draw_range = Global.BaseDrawRange * Global.fDistanceFactor; break; }
case rendermode::shadows: { Config.draw_range = Global.shadowtune.range; break; }
case rendermode::cabshadows: { Config.draw_range = ( Global.RenderCabShadowsRange > 0 ? clamp( Global.RenderCabShadowsRange, 5, 100 ) : simulation::Train->Occupied()->Dim.L ); break; }
case rendermode::cabshadows: { Config.draw_range = ( Global.RenderCabShadowsRange > 0 ? std::clamp( Global.RenderCabShadowsRange, 5, 100 ) : simulation::Train->Occupied()->Dim.L ); break; }
case rendermode::reflections: { Config.draw_range = Global.BaseDrawRange; break; }
case rendermode::pickcontrols: { Config.draw_range = 50.f; break; }
case rendermode::pickscenery: { Config.draw_range = Global.BaseDrawRange * 0.5f; break; }
@@ -1510,7 +1510,7 @@ opengl_renderer::Render( world_environment *Environment ) {
m_shadowcolor = interpolate(
glm::vec4{ colors::shadow },
glm::vec4{ colors::white },
clamp( -Environment->m_sun.getAngle(), 0.f, 6.f ) / 6.f );
std::clamp( -Environment->m_sun.getAngle(), 0.f, 6.f ) / 6.f );
if( ( Environment->m_sun.getAngle() < -18.f )
&& ( Environment->m_moon.getAngle() > 0.f ) ) {
// turn on moon shadows after nautical twilight, if the moon is actually up
@@ -1568,8 +1568,8 @@ opengl_renderer::Render( world_environment *Environment ) {
auto const &modelview = OpenGLMatrices.data( GL_MODELVIEW );
auto const fogfactor { clamp( Global.fFogEnd / 2000.f, 0.f, 1.f ) }; // closer/denser fog reduces opacity of the celestial bodies
float const duskfactor = 1.0f - clamp( std::abs( Environment->m_sun.getAngle() ), 0.0f, 12.0f ) / 12.0f;
auto const fogfactor { std::clamp( Global.fFogEnd / 2000.f, 0.f, 1.f ) }; // closer/denser fog reduces opacity of the celestial bodies
float const duskfactor = 1.0f - std::clamp( std::abs( Environment->m_sun.getAngle() ), 0.0f, 12.0f ) / 12.0f;
glm::vec3 suncolor = interpolate(
glm::vec3( 255.0f / 255.0f, 242.0f / 255.0f, 231.0f / 255.0f ),
glm::vec3( 235.0f / 255.0f, 140.0f / 255.0f, 36.0f / 255.0f ),
@@ -1578,7 +1578,7 @@ opengl_renderer::Render( world_environment *Environment ) {
if (!m_isATI)
{
Bind_Texture( m_suntexture );
::glColor4f( suncolor.x, suncolor.y, suncolor.z, clamp( 1.5f - Global.Overcast, 0.f, 1.f ) * fogfactor );
::glColor4f( suncolor.x, suncolor.y, suncolor.z, std::clamp( 1.5f - Global.Overcast, 0.f, 1.f ) * fogfactor );
auto const sunvector = Environment->m_sun.getDirection();
auto const sunposition = modelview * glm::vec4( sunvector.x, sunvector.y, sunvector.z, 1.0f );
@@ -1589,7 +1589,7 @@ opengl_renderer::Render( world_environment *Environment ) {
float const size = interpolate( // TODO: expose distance/scale factor from the moon object
0.0325f,
0.0275f,
clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f );
std::clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f );
::glBegin( GL_TRIANGLE_STRIP );
::glMultiTexCoord2f( m_diffusetextureunit, 1.f, 1.f ); ::glVertex3f( -size, size, 0.f );
::glMultiTexCoord2f( m_diffusetextureunit, 1.f, 0.f ); ::glVertex3f( -size, -size, 0.f );
@@ -1622,7 +1622,7 @@ opengl_renderer::Render( world_environment *Environment ) {
float const size = interpolate( // TODO: expose distance/scale factor from the moon object
0.0160f,
0.0135f,
clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f );
std::clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f );
// choose the moon appearance variant, based on current moon phase
// NOTE: implementation specific, 8 variants are laid out in 3x3 arrangement
// from new moon onwards, top left to right bottom (last spot is left for future use, if any)
@@ -2609,7 +2609,7 @@ opengl_renderer::Render_cab( TDynamicObject const *Dynamic, float const Lightlev
GL_LIGHT_MODEL_AMBIENT,
glm::value_ptr(
glm::vec3( m_baseambient )
+ ( Dynamic->InteriorLight * Lightlevel ) * static_cast<float>( clamp( 1.25 - luminance, 0.0, 1.0 ) ) ) );
+ ( Dynamic->InteriorLight * Lightlevel ) * static_cast<float>( std::clamp( 1.25 - luminance, 0.0, 1.0 ) ) ) );
}
// render
if( true == Alpha ) {
@@ -2765,7 +2765,7 @@ opengl_renderer::Render( TSubModel *Submodel ) {
// textures...
Bind_Material( material );
// ...colors and opacity...
auto const opacity { clamp( Material( material )->get_or_guess_opacity(), 0.f, 1.f ) };
auto const opacity { std::clamp( Material( material )->get_or_guess_opacity(), 0.f, 1.f ) };
if( Submodel->fVisible < 1.f ) {
// setup
::glAlphaFunc( GL_GREATER, 0.f );
@@ -2921,7 +2921,7 @@ opengl_renderer::Render( TSubModel *Submodel ) {
// kąt większy niż maksymalny stożek swiatła
float lightlevel = 1.f; // TODO, TBD: parameter to control light strength
// view angle attenuation
float const anglefactor = clamp(
float const anglefactor = std::clamp(
( Submodel->fCosViewAngle - Submodel->fCosFalloffAngle ) / ( Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle ),
0.f, 1.f );
lightlevel *= anglefactor;
@@ -2934,7 +2934,7 @@ opengl_renderer::Render( TSubModel *Submodel ) {
if( Global.Overcast > 0.75f ) {
float const precipitationfactor{
interpolate(
interpolate( 1.f, 0.25f, clamp( Global.Overcast * 0.75f - 0.5f, 0.f, 1.f ) ),
interpolate( 1.f, 0.25f, std::clamp( Global.Overcast * 0.75f - 0.5f, 0.f, 1.f ) ),
1.f,
distancefactor ) };
lightlevel *= precipitationfactor;
@@ -2970,7 +2970,7 @@ opengl_renderer::Render( TSubModel *Submodel ) {
float const fogfactor {
interpolate(
2.f, 1.f,
clamp( Global.fFogEnd / 2000, 0.f, 1.f ) )
std::clamp( Global.fFogEnd / 2000, 0.f, 1.f ) )
* std::max( 1.f, Global.Overcast ) };
::glPointSize( pointsize * fogfactor );
@@ -3346,7 +3346,7 @@ opengl_renderer::Render_precipitation() {
interpolate(
0.5f * ( Global.DayLight.diffuse + Global.DayLight.ambient ),
colors::white,
0.5f * clamp<float>( Global.fLuminance, 0.f, 1.f ) ) ) );
0.5f * std::clamp( (float)Global.fLuminance, 0.f, 1.f ) ) ) );
::glPushMatrix();
// tilt the precipitation cone against the camera movement vector for crude motion blur
// include current wind vector while at it
@@ -3587,14 +3587,14 @@ opengl_renderer::Render_Alpha( TTraction *Traction ) {
0.5f * Traction->radius() + 1.f,
distance - ( 0.5f * Traction->radius() ) ) };
::glLineWidth(
clamp(
std::clamp(
0.5f * linealpha + Traction->WireThickness * Traction->radius() / 1000.f,
1.f, 1.75f ) );
// McZapkie-261102: kolor zalezy od materialu i zasniedzenia
::glColor4fv(
glm::value_ptr(
glm::vec4{
Traction->wire_color() /* * ( DebugModeFlag ? 1.f : clamp( m_sunandviewangle, 0.25f, 1.f ) ) */,
Traction->wire_color() /* * ( DebugModeFlag ? 1.f : std::clamp( m_sunandviewangle, 0.25f, 1.f ) ) */,
linealpha } ) );
// render
m_geometry.draw( Traction->m_geometry );
@@ -3637,7 +3637,7 @@ opengl_renderer::Render_Alpha( scene::lines_node const &Lines ) {
distance - ( 0.5f * data.area.radius ) ) :
1.f ); // negative width means the lines are always opague
::glLineWidth(
clamp(
std::clamp(
0.5f * linealpha + data.line_width * data.area.radius / 1000.f,
1.f, 8.f ) );
::glColor4fv(
@@ -3839,7 +3839,7 @@ opengl_renderer::Render_Alpha( TSubModel *Submodel ) {
// textures...
Bind_Material( material );
// ...colors and opacity...
auto const opacity { clamp( Material( material )->get_or_guess_opacity(), 0.f, 1.f ) };
auto const opacity { std::clamp( Material( material )->get_or_guess_opacity(), 0.f, 1.f ) };
if( Submodel->fVisible < 1.f ) {
::glColor4f(
Submodel->f4Diffuse.r,
@@ -3944,13 +3944,13 @@ opengl_renderer::Render_Alpha( TSubModel *Submodel ) {
if( Submodel->fCosViewAngle > Submodel->fCosFalloffAngle ) {
// only bother if the viewer is inside the visibility cone
// luminosity at night is at level of ~0.1, so the overall resulting transparency in clear conditions is ~0.5 at full 'brightness'
auto glarelevel { clamp(
auto glarelevel { std::clamp(
std::max<float>(
0.6f - Global.fLuminance, // reduce the glare in bright daylight
Global.Overcast - 1.f ), // ensure some glare in rainy/foggy conditions
0.f, 1.f ) };
// view angle attenuation
float const anglefactor { clamp(
float const anglefactor { std::clamp(
( Submodel->fCosViewAngle - Submodel->fCosFalloffAngle ) / ( Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle ),
0.f, 1.f ) };
glarelevel *= anglefactor;
@@ -4146,8 +4146,8 @@ glm::dvec3
opengl_renderer::Update_Mouse_Position() {
glm::ivec2 mousepos = Global.cursor_pos * Global.fb_size / Global.window_size;
mousepos.x = clamp<int>( mousepos.x, 0, Global.fb_size.x - 1 );
mousepos.y = clamp<int>( Global.fb_size.y - mousepos.y, 0, Global.fb_size.y - 1 ) ;
mousepos.x = std::clamp( mousepos.x, 0, Global.fb_size.x - 1 );
mousepos.y = std::clamp(Global.fb_size.y - mousepos.y, 0, Global.fb_size.y - 1);
GLfloat pointdepth;
::glReadPixels( mousepos.x, mousepos.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &pointdepth );

View File

@@ -59,7 +59,7 @@ void opengl_skydome::update() {
::glBindBuffer( GL_ARRAY_BUFFER, m_coloursbuffer );
auto &colors{ skydome.colors() };
/*
float twilightfactor = clamp( -simulation::Environment.sun().getAngle(), 0.0f, 18.0f ) / 18.0f;
float twilightfactor = std::clamp( -simulation::Environment.sun().getAngle(), 0.0f, 18.0f ) / 18.0f;
auto gamma = interpolate( glm::vec3( 0.45f ), glm::vec3( 1.0f ), twilightfactor );
for( auto & color : colors ) {
color = glm::pow( color, gamma );