diff --git a/tools/quake3/common/inout.cpp b/tools/quake3/common/inout.cpp index 0fd6d20d..4878d299 100644 --- a/tools/quake3/common/inout.cpp +++ b/tools/quake3/common/inout.cpp @@ -325,7 +325,7 @@ void xml_message_push( int flag, const char* characters, size_t length ){ } else { - size_t size = ( space < ( size_t )( end - characters ) ) ? space : ( size_t )( end - characters ); + size_t size = std::min( space, static_cast( end - characters ) ); memcpy( mesege + mesege_len, characters, size ); mesege_len += size; characters += size; diff --git a/tools/quake3/common/polylib.cpp b/tools/quake3/common/polylib.cpp index 85a20fc9..064eaee6 100644 --- a/tools/quake3/common/polylib.cpp +++ b/tools/quake3/common/polylib.cpp @@ -599,7 +599,7 @@ void ChopWindingInPlaceAccu( winding_accu_t **inout, const Plane3f& plane, float // 64-bit land inside of the epsilon for all numbers we're dealing with. static const double smallestEpsilonAllowed = ( (double) VEC_SMALLEST_EPSILON_AROUND_ONE ) * 0.5; - const double fineEpsilon = ( crudeEpsilon < smallestEpsilonAllowed )? smallestEpsilonAllowed : (double) crudeEpsilon; + const double fineEpsilon = std::max( smallestEpsilonAllowed, (double) crudeEpsilon ); in = *inout; counts[0] = counts[1] = counts[2] = 0; @@ -851,7 +851,7 @@ void CheckWinding( winding_t *w ){ Error( "CheckFace: MAX_WORLD_COORD exceeded: %f",p1[j] ); } - j = i + 1 == w->numpoints ? 0 : i + 1; + j = ( i + 1 == w->numpoints )? 0 : i + 1; // check the point is on the face plane d = plane3_distance_to_point( faceplane, p1 ); diff --git a/tools/quake3/common/qmath.h b/tools/quake3/common/qmath.h index d3d71e81..702a37ff 100644 --- a/tools/quake3/common/qmath.h +++ b/tools/quake3/common/qmath.h @@ -11,6 +11,16 @@ #define VectorFastNormalize VectorNormalize +template +inline void value_maximize( T& value, const T& other ){ + value = std::max( value, other ); +} + +template +inline void value_minimize( T& value, const T& other ){ + value = std::min( value, other ); +} + template struct MinMax___ diff --git a/tools/quake3/q3map2/autopk3.cpp b/tools/quake3/q3map2/autopk3.cpp index 586c864a..30108f8b 100644 --- a/tools/quake3/q3map2/autopk3.cpp +++ b/tools/quake3/q3map2/autopk3.cpp @@ -225,10 +225,8 @@ int pk3BSPMain( int argc, char **argv ){ png = true; } else if ( strEqual( argv[ i ], "-complevel" ) ) { - compLevel = atoi( argv[ i + 1 ] ); + compLevel = std::clamp( atoi( argv[ i + 1 ] ), -1, 10 ); i++; - if ( compLevel < -1 ) compLevel = -1; - if ( compLevel > 10 ) compLevel = 10; Sys_Printf( "Compression level set to %i\n", compLevel ); } } @@ -777,10 +775,8 @@ int repackBSPMain( int argc, char **argv ){ analyze = true; } else if ( strEqual( argv[ i ], "-complevel" ) ) { - compLevel = atoi( argv[ i + 1 ] ); + compLevel = std::clamp( atoi( argv[ i + 1 ] ), -1, 10 ); i++; - if ( compLevel < -1 ) compLevel = -1; - if ( compLevel > 10 ) compLevel = 10; Sys_Printf( "Compression level set to %i\n", compLevel ); } } diff --git a/tools/quake3/q3map2/brush.cpp b/tools/quake3/q3map2/brush.cpp index 94f4a425..19344042 100644 --- a/tools/quake3/q3map2/brush.cpp +++ b/tools/quake3/q3map2/brush.cpp @@ -804,7 +804,7 @@ bool WindingIsTiny( winding_t *w ){ for ( i = 0 ; i < w->numpoints ; i++ ) { - j = i == w->numpoints - 1 ? 0 : i + 1; + j = ( i == w->numpoints - 1 )? 0 : i + 1; if ( vector3_length( w->p[j] - w->p[i] ) > EDGE_LENGTH ) { if ( ++edges == 3 ) { return false; @@ -903,12 +903,12 @@ void SplitBrush( brush_t *brush, int planenum, brush_t **front, brush_t **back ) } for ( j = 0 ; j < w->numpoints ; j++ ) { - const double d = plane3_distance_to_point( plane->plane, w->p[j] ); - if ( d > 0 && d > d_front ) { - d_front = d; + const float d = plane3_distance_to_point( plane->plane, w->p[j] ); + if ( d > 0 ) { + value_maximize( d_front, d ); } - if ( d < 0 && d < d_back ) { - d_back = d; + if ( d < 0 ) { + value_minimize( d_back, d ); } } } diff --git a/tools/quake3/q3map2/bsp.cpp b/tools/quake3/q3map2/bsp.cpp index 2ee9bb11..136e2642 100644 --- a/tools/quake3/q3map2/bsp.cpp +++ b/tools/quake3/q3map2/bsp.cpp @@ -762,18 +762,12 @@ int BSPMain( int argc, char **argv ){ fakemap = true; } else if ( strEqual( argv[ i ], "-samplesize" ) ) { - sampleSize = atoi( argv[ i + 1 ] ); - if ( sampleSize < 1 ) { - sampleSize = 1; - } + sampleSize = std::max( 1, atoi( argv[ i + 1 ] ) ); i++; Sys_Printf( "Lightmap sample size set to %dx%d units\n", sampleSize, sampleSize ); } else if ( strEqual( argv[ i ], "-minsamplesize" ) ) { - minSampleSize = atoi( argv[ i + 1 ] ); - if ( minSampleSize < 1 ) { - minSampleSize = 1; - } + minSampleSize = std::max( 1, atoi( argv[ i + 1 ] ) ); i++; Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize ); } @@ -800,49 +794,32 @@ int BSPMain( int argc, char **argv ){ Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon ); } else if ( strEqual( argv[ i ], "-mv" ) ) { - maxLMSurfaceVerts = atoi( argv[ i + 1 ] ); - if ( maxLMSurfaceVerts < 3 ) { - maxLMSurfaceVerts = 3; - } - if ( maxLMSurfaceVerts > maxSurfaceVerts ) { - maxSurfaceVerts = maxLMSurfaceVerts; - } + maxLMSurfaceVerts = std::max( 3, atoi( argv[ i + 1 ] ) ); + value_maximize( maxSurfaceVerts, maxLMSurfaceVerts ); i++; Sys_Printf( "Maximum lightmapped surface vertex count set to %d\n", maxLMSurfaceVerts ); } else if ( strEqual( argv[ i ], "-mi" ) ) { - maxSurfaceIndexes = atoi( argv[ i + 1 ] ); - if ( maxSurfaceIndexes < 3 ) { - maxSurfaceIndexes = 3; - } - i++; + maxSurfaceIndexes = std::max( 3, atoi( argv[ i + 1 ] ) ); Sys_Printf( "Maximum per-surface index count set to %d\n", maxSurfaceIndexes ); + i++; } else if ( strEqual( argv[ i ], "-np" ) ) { - npDegrees = atof( argv[ i + 1 ] ); - if ( npDegrees < 0.0f ) { - npDegrees = 0.0f; - } - else if ( npDegrees > 0.0f ) { + npDegrees = std::max( 0.0, atof( argv[ i + 1 ] ) ); + if ( npDegrees > 0.0f ) { Sys_Printf( "Forcing nonplanar surfaces with a breaking angle of %f degrees\n", npDegrees ); } i++; } else if ( strEqual( argv[ i ], "-snap" ) ) { - bevelSnap = atoi( argv[ i + 1 ] ); - if ( bevelSnap < 0 ) { - bevelSnap = 0; - } - i++; + bevelSnap = std::max( 0, atoi( argv[ i + 1 ] ) ); if ( bevelSnap > 0 ) { Sys_Printf( "Snapping brush bevel planes to %d units\n", bevelSnap ); } + i++; } else if ( strEqual( argv[ i ], "-texrange" ) ) { - texRange = atoi( argv[ i + 1 ] ); - if ( texRange < 0 ) { - texRange = 0; - } + texRange = std::max( 0, atoi( argv[ i + 1 ] ) ); i++; Sys_Printf( "Limiting per-surface texture range to %d texels\n", texRange ); } @@ -869,20 +846,14 @@ int BSPMain( int argc, char **argv ){ meta = true; } else if ( strEqual( argv[ i ], "-metaadequatescore" ) ) { - metaAdequateScore = atoi( argv[ i + 1 ] ); - if ( metaAdequateScore < 0 ) { - metaAdequateScore = -1; - } + metaAdequateScore = std::max( -1, atoi( argv[ i + 1 ] ) ); i++; if ( metaAdequateScore >= 0 ) { Sys_Printf( "Setting ADEQUATE meta score to %d (see surface_meta.c)\n", metaAdequateScore ); } } else if ( strEqual( argv[ i ], "-metagoodscore" ) ) { - metaGoodScore = atoi( argv[ i + 1 ] ); - if ( metaGoodScore < 0 ) { - metaGoodScore = -1; - } + metaGoodScore = std::max( -1, atoi( argv[ i + 1 ] ) ); i++; if ( metaGoodScore >= 0 ) { Sys_Printf( "Setting GOOD meta score to %d (see surface_meta.c)\n", metaGoodScore ); diff --git a/tools/quake3/q3map2/bspfile_rbsp.cpp b/tools/quake3/q3map2/bspfile_rbsp.cpp index 5e583e0c..5bca723d 100644 --- a/tools/quake3/q3map2/bspfile_rbsp.cpp +++ b/tools/quake3/q3map2/bspfile_rbsp.cpp @@ -115,7 +115,7 @@ static void AddLightGridLumps( FILE *file, rbspHeader_t *header ){ /* allocate temporary buffers */ - maxGridPoints = ( numBSPGridPoints < MAX_MAP_GRID ) ? numBSPGridPoints : MAX_MAP_GRID; + maxGridPoints = std::min( numBSPGridPoints, MAX_MAP_GRID ); gridPoints = safe_malloc( maxGridPoints * sizeof( *gridPoints ) ); gridArray = safe_malloc( numBSPGridPoints * sizeof( *gridArray ) ); diff --git a/tools/quake3/q3map2/convert_bsp.cpp b/tools/quake3/q3map2/convert_bsp.cpp index bff45b06..a000fd70 100644 --- a/tools/quake3/q3map2/convert_bsp.cpp +++ b/tools/quake3/q3map2/convert_bsp.cpp @@ -542,12 +542,8 @@ int ScaleBSPMain( int argc, char **argv ){ /* scale drawverts */ for ( i = 0; i < numBSPDrawVerts; i++ ) { - bspDrawVerts[i].xyz[0] *= scale[0]; - bspDrawVerts[i].xyz[1] *= scale[1]; - bspDrawVerts[i].xyz[2] *= scale[2]; - bspDrawVerts[i].normal[0] /= scale[0]; - bspDrawVerts[i].normal[1] /= scale[1]; - bspDrawVerts[i].normal[2] /= scale[2]; + bspDrawVerts[i].xyz *= scale; + bspDrawVerts[i].normal /= scale; VectorNormalize( bspDrawVerts[i].normal ); } diff --git a/tools/quake3/q3map2/convert_map.cpp b/tools/quake3/q3map2/convert_map.cpp index 61445a34..07d270b3 100644 --- a/tools/quake3/q3map2/convert_map.cpp +++ b/tools/quake3/q3map2/convert_map.cpp @@ -76,13 +76,9 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t * vert[1] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 1]]; vert[2] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 2]]; if ( s->surfaceType == MST_PLANAR && VectorCompare( vert[0]->normal, vert[1]->normal ) && VectorCompare( vert[1]->normal, vert[2]->normal ) ) { - if ( vector3_length( vert[0]->normal - buildPlane->normal() ) >= normalEpsilon ) { - continue; - } - if ( vector3_length( vert[1]->normal - buildPlane->normal() ) >= normalEpsilon ) { - continue; - } - if ( vector3_length( vert[2]->normal - buildPlane->normal() ) >= normalEpsilon ) { + if ( vector3_length( vert[0]->normal - buildPlane->normal() ) >= normalEpsilon + || vector3_length( vert[1]->normal - buildPlane->normal() ) >= normalEpsilon + || vector3_length( vert[2]->normal - buildPlane->normal() ) >= normalEpsilon ) { continue; } } @@ -97,13 +93,9 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t * } } // fixme? better distance epsilon - if ( abs( plane3_distance_to_point( buildPlane->plane, vert[0]->xyz ) ) > 1 ) { - continue; - } - if ( abs( plane3_distance_to_point( buildPlane->plane, vert[1]->xyz ) ) > 1 ) { - continue; - } - if ( abs( plane3_distance_to_point( buildPlane->plane, vert[2]->xyz ) ) > 1 ) { + if ( abs( plane3_distance_to_point( buildPlane->plane, vert[0]->xyz ) ) > 1 + || abs( plane3_distance_to_point( buildPlane->plane, vert[1]->xyz ) ) > 1 + || abs( plane3_distance_to_point( buildPlane->plane, vert[2]->xyz ) ) > 1 ) { continue; } // Okay. Correct surface type, correct shader, correct plane. Let's start with the business... diff --git a/tools/quake3/q3map2/decals.cpp b/tools/quake3/q3map2/decals.cpp index 03631038..213714a0 100644 --- a/tools/quake3/q3map2/decals.cpp +++ b/tools/quake3/q3map2/decals.cpp @@ -206,7 +206,7 @@ static bool MakeTextureMatrix( decalProjector_t *dp, const Plane3f& projection, radians_to_degrees( acos( vector3_dot( dp->texMat[ 0 ].vec3(), dp->texMat[ 1 ].vec3() ) ) ), radians_to_degrees( acos( vector3_dot( axis[ 0 ], axis[ 1 ] ) ) ) ); - Sys_Printf( "XYZ: %f %f %f ST: %f %f ST(t): %f %f\n", + Sys_Printf( "XYZ: %f %f %f ST: %f %f ST(t): %lf %lf\n", a->xyz[ 0 ], a->xyz[ 1 ], a->xyz[ 2 ], a->st[ 0 ], a->st[ 1 ], vector3_dot( a->xyz, dp->texMat[ 0 ].vec3() ) + dp->texMat[ 0 ][ 3 ], vector3_dot( a->xyz, dp->texMat[ 1 ].vec3() ) + dp->texMat[ 1 ][ 3 ] ); @@ -498,7 +498,6 @@ void ProcessDecals( void ){ static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds, winding_t *w ){ int i, j; - float d, d2, alpha; winding_t *front, *back; mapDrawSurface_t *ds2; bspDrawVert_t *dv; @@ -577,15 +576,9 @@ static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds, dv = &ds2->verts[ i ]; /* set alpha */ - d = plane3_distance_to_point( dp->planes[ 0 ], w->p[ i ] ); - d2 = plane3_distance_to_point( dp->planes[ 1 ], w->p[ i ] ); - alpha = 255.0f * d2 / ( d + d2 ); - if ( alpha > 255 ) { - alpha = 255; - } - else if ( alpha < 0 ) { - alpha = 0; - } + const float d = plane3_distance_to_point( dp->planes[ 0 ], w->p[ i ] ); + const float d2 = plane3_distance_to_point( dp->planes[ 1 ], w->p[ i ] ); + const float alpha = std::clamp( 255.0f * d2 / ( d + d2 ), 0.f, 255.f ); /* set misc */ dv->xyz = w->p[ i ] - entityOrigin; diff --git a/tools/quake3/q3map2/fog.cpp b/tools/quake3/q3map2/fog.cpp index ec71ddf0..502e5d7e 100644 --- a/tools/quake3/q3map2/fog.cpp +++ b/tools/quake3/q3map2/fog.cpp @@ -338,14 +338,10 @@ winding_t *WindingFromDrawSurf( mapDrawSurface_t *ds ){ // we use the first point of the surface, maybe something more clever would be useful // (actually send the whole draw surface would be cool?) if ( ds->numVerts >= MAX_POINTS_ON_WINDING ) { - int max = ds->numVerts; + const int max = std::min( ds->numVerts, 256 ); Vector3 p[256]; - if ( max > 256 ) { - max = 256; - } - - for ( i = 0 ; i < max ; i++ ) { + for ( i = 0; i < max; i++ ) { p[i] = ds->verts[i].xyz; } diff --git a/tools/quake3/q3map2/light.cpp b/tools/quake3/q3map2/light.cpp index d894d85b..af37431b 100644 --- a/tools/quake3/q3map2/light.cpp +++ b/tools/quake3/q3map2/light.cpp @@ -51,9 +51,7 @@ static void CreateSunLight( sun_t *sun ){ } /* fixup */ - if ( sun->numSamples < 1 ) { - sun->numSamples = 1; - } + value_maximize( sun->numSamples, 1 ); /* set photons */ photons = sun->photons / sun->numSamples; @@ -344,18 +342,13 @@ void CreateEntityLights( void ){ } /* ydnar: get deviance and samples */ - float deviance = e->floatForKey( "_deviance", "_deviation", "_jitter" ); - if ( deviance < 0.f ) - deviance = 0.f; - int numSamples = e->intForKey( "_samples" ); - if ( numSamples < 1 ) - numSamples = 1; + const float deviance = std::max( 0.f, e->floatForKey( "_deviance", "_deviation", "_jitter" ) ); + const int numSamples = std::max( 1, e->intForKey( "_samples" ) ); intensity /= numSamples; { /* ydnar: get filter radius */ - const float filterRadius = e->floatForKey( "_filterradius", "_filteradius", "_filter" ); - light->filterRadius = filterRadius < 0.f? 0.f : filterRadius; + light->filterRadius = std::max( 0.f, e->floatForKey( "_filterradius", "_filteradius", "_filter" ) ); } /* set light color */ @@ -650,7 +643,7 @@ float PointToPolygonFormFactor( const Vector3& point, const Vector3& normal, con int i, j; Vector3 dirs[ MAX_POINTS_ON_WINDING ]; float total; - float dot, angle, facing; + float angle, facing; /* this is expensive */ @@ -669,18 +662,10 @@ float PointToPolygonFormFactor( const Vector3& point, const Vector3& normal, con { /* get a triangle */ j = i + 1; - dot = vector3_dot( dirs[ i ], dirs[ j ] ); - - /* roundoff can cause slight creep, which gives an IND from acos */ - if ( dot > 1.0f ) { - dot = 1.0f; - } - else if ( dot < -1.0f ) { - dot = -1.0f; - } /* get the angle */ - angle = acos( dot ); + /* roundoff can cause slight creep, which gives an IND from acos, thus clamp */ + angle = acos( std::clamp( vector3_dot( dirs[ i ], dirs[ j ] ), -1.0, 1.0 ) ); Vector3 triNormal = vector3_cross( dirs[ i ], dirs[ j ] ); if ( VectorFastNormalize( triNormal ) < 0.0001f ) { @@ -813,10 +798,7 @@ int LightContributionToSample( trace_t *trace ){ } /* clamp the distance to prevent super hot spots */ - dist = sqrt( dist * dist + light->extraDist * light->extraDist ); - if ( dist < 16.0f ) { - dist = 16.0f; - } + dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) ); add = light->photons / ( dist * dist ) * angle; @@ -881,10 +863,7 @@ int LightContributionToSample( trace_t *trace ){ } /* clamp the distance to prevent super hot spots */ - dist = sqrt( dist * dist + light->extraDist * light->extraDist ); - if ( dist < 16.0f ) { - dist = 16.0f; - } + dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) ); /* angle attenuation */ if ( light->flags & LightFlags::AttenAngle ) { @@ -902,9 +881,7 @@ int LightContributionToSample( trace_t *trace ){ /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */ if ( lightAngleHL ) { if ( dot > 0.001f ) { // skip coplanar - if ( dot > 1.0f ) { - dot = 1.0f; - } + value_minimize( dot, 1.0f ); dot = ( dot * 0.5f ) + 0.5f; dot *= dot; } @@ -921,17 +898,12 @@ int LightContributionToSample( trace_t *trace ){ if ( light->angleScale != 0.0f ) { angle /= light->angleScale; - if ( angle > 1.0f ) { - angle = 1.0f; - } + value_minimize( angle, 1.0f ); } /* attenuate */ if ( light->flags & LightFlags::AttenLinear ) { - add = angle * light->photons * linearScale - ( dist * light->fade ); - if ( add < 0.0f ) { - add = 0.0f; - } + add = std::max( 0.f, angle * light->photons * linearScale - ( dist * light->fade ) ); if ( deluxemap ) { if ( angledDeluxe ) { @@ -941,17 +913,12 @@ int LightContributionToSample( trace_t *trace ){ addDeluxe = light->photons * linearScale - ( dist * light->fade ); } - if ( addDeluxe < 0.0f ) { - addDeluxe = 0.0f; - } + value_maximize( addDeluxe, 0.0f ); } } else { - add = ( light->photons / ( dist * dist ) ) * angle; - if ( add < 0.0f ) { - add = 0.0f; - } + add = std::max( 0.f, ( light->photons / ( dist * dist ) ) * angle ); if ( deluxemap ) { if ( angledDeluxe ) { @@ -962,9 +929,7 @@ int LightContributionToSample( trace_t *trace ){ } } - if ( addDeluxe < 0.0f ) { - addDeluxe = 0.0f; - } + value_maximize( addDeluxe, 0.0f ); } /* handle spotlights */ @@ -987,15 +952,10 @@ int LightContributionToSample( trace_t *trace ){ /* attenuate */ if ( sampleRadius > ( radiusAtDist - 32.0f ) ) { add *= ( ( radiusAtDist - sampleRadius ) / 32.0f ); - if ( add < 0.0f ) { - add = 0.0f; - } + value_maximize( add, 0.0f ); addDeluxe *= ( ( radiusAtDist - sampleRadius ) / 32.0f ); - - if ( addDeluxe < 0.0f ) { - addDeluxe = 0.0f; - } + value_maximize( addDeluxe, 0.0f ); } } } @@ -1022,9 +982,7 @@ int LightContributionToSample( trace_t *trace ){ /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */ if ( lightAngleHL ) { if ( dot > 0.001f ) { // skip coplanar - if ( dot > 1.0f ) { - dot = 1.0f; - } + value_minimize( dot, 1.0f ); dot = ( dot * 0.5f ) + 0.5f; dot *= dot; } @@ -1050,9 +1008,7 @@ int LightContributionToSample( trace_t *trace ){ addDeluxe = light->photons; } - if ( addDeluxe < 0.0f ) { - addDeluxe = 0.0f; - } + value_maximize( addDeluxe, 0.0f ); } if ( add <= 0.0f ) { @@ -1066,9 +1022,7 @@ int LightContributionToSample( trace_t *trace ){ if ( bouncing ) { addDeluxe *= addDeluxeBounceScale; - if ( addDeluxe < 0.00390625f ) { - addDeluxe = 0.00390625f; - } + value_maximize( addDeluxe, 0.00390625f ); } trace->directionContribution = trace->direction * addDeluxe; @@ -1287,10 +1241,7 @@ bool LightContributionToPoint( trace_t *trace ){ /* ptpff approximation */ if ( light->type == ELightType::Area && faster ) { /* clamp the distance to prevent super hot spots */ - dist = sqrt( dist * dist + light->extraDist * light->extraDist ); - if ( dist < 16.0f ) { - dist = 16.0f; - } + dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) ); /* attenuate */ add = light->photons / ( dist * dist ); @@ -1338,17 +1289,11 @@ bool LightContributionToPoint( trace_t *trace ){ /* point/spot lights */ else if ( light->type == ELightType::Point || light->type == ELightType::Spot ) { /* clamp the distance to prevent super hot spots */ - dist = sqrt( dist * dist + light->extraDist * light->extraDist ); - if ( dist < 16.0f ) { - dist = 16.0f; - } + dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) ); /* attenuate */ if ( light->flags & LightFlags::AttenLinear ) { - add = light->photons * linearScale - ( dist * light->fade ); - if ( add < 0.0f ) { - add = 0.0f; - } + add = std::max( 0.f, light->photons * linearScale - ( dist * light->fade ) ); } else{ add = light->photons / ( dist * dist ); @@ -1468,20 +1413,10 @@ void TraceGrid( int num ){ mod -= y * gridBounds[ 0 ]; x = mod; - trace.origin[ 0 ] = gridMins[ 0 ] + x * gridSize[ 0 ]; - trace.origin[ 1 ] = gridMins[ 1 ] + y * gridSize[ 1 ]; - trace.origin[ 2 ] = gridMins[ 2 ] + z * gridSize[ 2 ]; + trace.origin = gridMins + Vector3( x, y, z ) * gridSize; /* set inhibit sphere */ - if ( gridSize[ 0 ] > gridSize[ 1 ] && gridSize[ 0 ] > gridSize[ 2 ] ) { - trace.inhibitRadius = gridSize[ 0 ] * 0.5f; - } - else if ( gridSize[ 1 ] > gridSize[ 0 ] && gridSize[ 1 ] > gridSize[ 2 ] ) { - trace.inhibitRadius = gridSize[ 1 ] * 0.5f; - } - else{ - trace.inhibitRadius = gridSize[ 2 ] * 0.5f; - } + trace.inhibitRadius = gridSize[ vector3_max_abs_component_index( gridSize ) ] * 0.5f; /* find point cluster */ trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON ); @@ -1616,10 +1551,7 @@ void TraceGrid( int num ){ /* get relative directed strength */ d = vector3_dot( contributions[ i ].dir, thisdir ); /* we map 1 to gridDirectionality, and 0 to gridAmbientDirectionality */ - d = gridAmbientDirectionality + d * ( gridDirectionality - gridAmbientDirectionality ); - if ( d < 0.0f ) { - d = 0.0f; - } + d = std::max( 0.f, gridAmbientDirectionality + d * ( gridDirectionality - gridAmbientDirectionality ) ); /* find appropriate style */ for ( j = 0; j < numStyles; j++ ) @@ -1686,9 +1618,7 @@ void TraceGrid( int num ){ /* set minimum light and copy off to bytes */ Vector3 color = gp->ambient[ i ]; for ( j = 0; j < 3; j++ ) - if ( color[ j ] < minGridLight[ j ] ) { - color[ j ] = minGridLight[ j ]; - } + value_maximize( color[ j ], minGridLight[ j ] ); /* vortex: apply gridscale and gridambientscale here */ ColorToBytes( color, bgp->ambient[ i ], gridScale * gridAmbientScale ); @@ -1740,7 +1670,7 @@ void SetupGrid( void ){ /* quantize it */ const Vector3 oldGridSize = gridSize; for ( i = 0; i < 3; i++ ) - gridSize[ i ] = gridSize[ i ] >= 8.0f ? floor( gridSize[ i ] ) : 8.0f; + gridSize[ i ] = std::max( 8.0, floor( gridSize[ i ] ) ); /* ydnar: increase gridSize until grid count is smaller than max allowed */ numRawGridPoints = MAX_MAP_LIGHTGRID + 1; @@ -1858,7 +1788,7 @@ void LightWorld( bool fastAllocate ){ /* maxlight */ if ( entities[ 0 ].read_keyvalue( f, "_maxlight" ) ) { - maxLight = f > 255? 255 : f < 0? 0 : f; + maxLight = std::clamp( f, 0.f, 255.f ); } /* determine the number of grid points */ @@ -2163,13 +2093,12 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-backsplash" ) && i < ( argc - 3 ) ) { - f = atof( argv[ i + 1 ] ); - g_backsplashFractionScale = f; - Sys_Printf( "Area lights backsplash fraction scaled by %f\n", f, g_backsplashFractionScale ); + g_backsplashFractionScale = atof( argv[ i + 1 ] ); + Sys_Printf( "Area lights backsplash fraction scaled by %f\n", g_backsplashFractionScale ); f = atof( argv[ i + 2 ] ); if ( f >= -900.0f ){ g_backsplashDistance = f; - Sys_Printf( "Area lights backsplash distance set globally to %f\n", f, g_backsplashDistance ); + Sys_Printf( "Area lights backsplash distance set globally to %f\n", g_backsplashDistance ); } i+=2; } @@ -2180,14 +2109,7 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-bouncecolorratio" ) ) { - f = atof( argv[ i + 1 ] ); - bounceColorRatio *= f; - if ( bounceColorRatio > 1 ) { - bounceColorRatio = 1; - } - if ( bounceColorRatio < 0 ) { - bounceColorRatio = 0; - } + bounceColorRatio = std::clamp( atof( argv[ i + 1 ] ), 0.0, 1.0 ); Sys_Printf( "Bounce color ratio set to %f\n", bounceColorRatio ); i++; } @@ -2225,28 +2147,16 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-griddirectionality" ) ) { - f = atof( argv[ i + 1 ] ); - if ( f > 1 ) { - f = 1; - } - if ( f < gridAmbientDirectionality ) { - gridAmbientDirectionality = f; - } - Sys_Printf( "Grid directionality is %f\n", f ); - gridDirectionality = f; + gridDirectionality = std::min( 1.0, atof( argv[ i + 1 ] ) ); + value_minimize( gridAmbientDirectionality, gridDirectionality ); + Sys_Printf( "Grid directionality is %f\n", gridDirectionality ); i++; } else if ( strEqual( argv[ i ], "-gridambientdirectionality" ) ) { - f = atof( argv[ i + 1 ] ); - if ( f < -1 ) { - f = -1; - } - if ( f > gridDirectionality ) { - gridDirectionality = f; - } - Sys_Printf( "Grid ambient directionality is %f\n", f ); - gridAmbientDirectionality = f; + gridAmbientDirectionality = std::max( -1.0, atof( argv[ i + 1 ] ) ); + value_maximize( gridDirectionality, gridAmbientDirectionality ); + Sys_Printf( "Grid ambient directionality is %f\n", gridAmbientDirectionality ); i++; } @@ -2331,8 +2241,7 @@ int LightMain( int argc, char **argv ){ /* Lighting contrast */ else if( strEqual( argv[ i ], "-contrast" ) ){ - f = atof( argv[ i + 1 ] ); - lightmapContrast = f > 255? 255 : f < -255? -255 : f; + lightmapContrast = std::clamp( atof( argv[ i + 1 ] ), -255.0, 255.0 ); Sys_Printf( "Lighting contrast set to %f\n", lightmapContrast ); i++; /* change to factor in range of 0 to 129.5 */ @@ -2341,22 +2250,16 @@ int LightMain( int argc, char **argv ){ /* ydnar switches */ else if ( strEqual( argv[ i ], "-bounce" ) ) { - bounce = atoi( argv[ i + 1 ] ); - if ( bounce < 0 ) { - bounce = 0; - } - else if ( bounce > 0 ) { + bounce = std::max( 0, atoi( argv[ i + 1 ] ) ); + if ( bounce > 0 ) { Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce ); } i++; } else if ( strEqual( argv[ i ], "-supersample" ) || strEqual( argv[ i ], "-super" ) ) { - superSample = atoi( argv[ i + 1 ] ); - if ( superSample < 1 ) { - superSample = 1; - } - else if ( superSample > 1 ) { + superSample = std::max( 1, atoi( argv[ i + 1 ] ) ); + if ( superSample > 1 ) { Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", ( superSample * superSample ) ); } i++; @@ -2369,22 +2272,15 @@ int LightMain( int argc, char **argv ){ else if ( strEqual( argv[ i ], "-samples" ) ) { lightSamplesInsist = ( *argv[i + 1] == '+' ); - lightSamples = atoi( argv[ i + 1 ] ); - if ( lightSamples < 1 ) { - lightSamples = 1; - } - else if ( lightSamples > 1 ) { + lightSamples = std::max( 1, atoi( argv[ i + 1 ] ) ); + if ( lightSamples > 1 ) { Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples ); } i++; } else if ( strEqual( argv[ i ], "-samplessearchboxsize" ) ) { - lightSamplesSearchBoxSize = atoi( argv[ i + 1 ] ); -// lightSamplesSearchBoxSize = MAX( MIN( lightSamplesSearchBoxSize, 4 ), 1 ); - lightSamplesSearchBoxSize = lightSamplesSearchBoxSize < 1 ? 1 - : lightSamplesSearchBoxSize > 4 ? 4 /* more makes no sense */ - : lightSamplesSearchBoxSize; + lightSamplesSearchBoxSize = std::clamp( atoi( argv[ i + 1 ] ), 1, 4 ); /* more makes no sense */ if ( lightSamplesSearchBoxSize != 1 ) Sys_Printf( "Adaptive supersampling uses %f times the normal search box size\n", lightSamplesSearchBoxSize ); i++; @@ -2401,11 +2297,8 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-shadeangle" ) ) { - shadeAngleDegrees = atof( argv[ i + 1 ] ); - if ( shadeAngleDegrees < 0.0f ) { - shadeAngleDegrees = 0.0f; - } - else if ( shadeAngleDegrees > 0.0f ) { + shadeAngleDegrees = std::max( 0.0, atof( argv[ i + 1 ] ) ); + if ( shadeAngleDegrees > 0.0f ) { shade = true; Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees ); } @@ -2424,11 +2317,8 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-approx" ) ) { - approximateTolerance = atoi( argv[ i + 1 ] ); - if ( approximateTolerance < 0 ) { - approximateTolerance = 0; - } - else if ( approximateTolerance > 0 ) { + approximateTolerance = std::max( 0, atoi( argv[ i + 1 ] ) ); + if ( approximateTolerance > 0 ) { Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance ); } i++; @@ -2439,7 +2329,7 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-deluxemode" ) ) { deluxemode = atoi( argv[ i + 1 ] ); - if ( deluxemode == 0 || deluxemode > 1 || deluxemode < 0 ) { + if ( deluxemode != 1 ) { Sys_Printf( "Generating modelspace deluxemaps\n" ); deluxemode = 0; } @@ -2518,10 +2408,7 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-extradist" ) ) { - extraDist = atof( argv[ i + 1 ] ); - if ( extraDist < 0 ) { - extraDist = 0; - } + extraDist = std::max( 0.0, atof( argv[ i + 1 ] ) ); i++; Sys_Printf( "Default extra radius set to %f units\n", extraDist ); } @@ -2694,18 +2581,12 @@ int LightMain( int argc, char **argv ){ Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n" ); } else if ( strEqual( argv[ i ], "-samplesize" ) ) { - sampleSize = atoi( argv[ i + 1 ] ); - if ( sampleSize < 1 ) { - sampleSize = 1; - } + sampleSize = std::max( 1, atoi( argv[ i + 1 ] ) ); i++; Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize ); } else if ( strEqual( argv[ i ], "-minsamplesize" ) ) { - minSampleSize = atoi( argv[ i + 1 ] ); - if ( minSampleSize < 1 ) { - minSampleSize = 1; - } + minSampleSize = std::max( 1, atoi( argv[ i + 1 ] ) ); i++; Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize ); } @@ -2720,8 +2601,9 @@ int LightMain( int argc, char **argv ){ } else if ( strEqual( argv[ i ], "-novertex" ) ) { noVertexLighting = 1; - if ( ( atof( argv[ i + 1 ] ) != 0 ) && ( atof( argv[ i + 1 ] )) < 1 ) { - noVertexLighting = ( atof( argv[ i + 1 ] ) ); + f = atof( argv[ i + 1 ] ); + if ( f != 0 && f < 1 ) { + noVertexLighting = f; i++; Sys_Printf( "Setting vertex lighting globally to %f\n", noVertexLighting ); } @@ -2890,10 +2772,7 @@ int LightMain( int argc, char **argv ){ /* fix up lightmap search power */ if ( lightmapMergeSize ) { - lightmapSearchBlockSize = ( lightmapMergeSize / lmCustomSizeW ) * ( lightmapMergeSize / lmCustomSizeW ); //? should use min or max( lmCustomSizeW, lmCustomSizeH )? :thinking: - if ( lightmapSearchBlockSize < 1 ) { - lightmapSearchBlockSize = 1; - } + lightmapSearchBlockSize = std::max( 1, ( lightmapMergeSize / lmCustomSizeW ) * ( lightmapMergeSize / lmCustomSizeW ) ); //? should use min or max( lmCustomSizeW, lmCustomSizeH )? :thinking: Sys_Printf( "Restricted lightmap searching enabled - block size adjusted to %d\n", lightmapSearchBlockSize ); } diff --git a/tools/quake3/q3map2/light_bounce.cpp b/tools/quake3/q3map2/light_bounce.cpp index b8223016..172c20cd 100644 --- a/tools/quake3/q3map2/light_bounce.cpp +++ b/tools/quake3/q3map2/light_bounce.cpp @@ -232,7 +232,7 @@ bool RadSampleImage( byte *pixels, int width, int height, const Vector2& st, Col #define SAMPLE_GRANULARITY 6 static void RadSample( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si, radWinding_t *rw, Vector3& average, Vector3& gradient, int *style ){ - int i, j, k, l, v, x, y, samples; + int i, j, k, l, v, samples; Vector3 color; MinMax minmax; Color4f textureColor; @@ -313,20 +313,8 @@ static void RadSample( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, } /* get lightmap xy coords */ - x = lightmap[ 0 ] / (float) superSample; - y = lightmap[ 1 ] / (float) superSample; - if ( x < 0 ) { - x = 0; - } - else if ( x >= lm->w ) { - x = lm->w - 1; - } - if ( y < 0 ) { - y = 0; - } - else if ( y >= lm->h ) { - y = lm->h - 1; - } + const int x = std::clamp( int( lightmap[ 0 ] / superSample ), 0, lm->w - 1 ); + const int y = std::clamp( int( lightmap[ 1 ] / superSample ), 0, lm->h - 1 ); /* get radiosity luxel */ const Vector3& radLuxel = lm->getRadLuxel( lightmapNum, x, y ); diff --git a/tools/quake3/q3map2/light_trace.cpp b/tools/quake3/q3map2/light_trace.cpp index 29d8b921..95eaa5f1 100644 --- a/tools/quake3/q3map2/light_trace.cpp +++ b/tools/quake3/q3map2/light_trace.cpp @@ -638,9 +638,7 @@ static void SubdivideTraceNode_r( int nodeNum, int depth ){ } /* set max trace depth */ - if ( depth > maxTraceDepth ) { - maxTraceDepth = depth; - } + value_maximize( maxTraceDepth, depth ); /* snap the average */ dist = floor( average[ type ] / count ); @@ -1431,20 +1429,8 @@ bool TraceTriangle( traceInfo_t *ti, traceTriangle_t *tt, trace_t *trace ){ t = w * tt->v[ 0 ].st[ 1 ] + u * tt->v[ 1 ].st[ 1 ] + v * tt->v[ 2 ].st[ 1 ]; s = s - floor( s ); t = t - floor( t ); - is = s * si->lightImage->width; - it = t * si->lightImage->height; - if ( is < 0 ) { - is = 0; - } - if ( is > si->lightImage->width - 1 ) { - is = si->lightImage->width - 1; - } - if ( it < 0 ) { - it = 0; - } - if ( it > si->lightImage->height - 1 ) { - it = si->lightImage->height - 1; - } + is = std::clamp( int( s * si->lightImage->width ), 0, si->lightImage->width - 1 ); + it = std::clamp( int( t * si->lightImage->height ), 0, si->lightImage->height - 1 ); /* get pixel */ pixel = si->lightImage->pixels + 4 * ( it * si->lightImage->width + is ); diff --git a/tools/quake3/q3map2/light_ydnar.cpp b/tools/quake3/q3map2/light_ydnar.cpp index fc660c0d..78f5a581 100644 --- a/tools/quake3/q3map2/light_ydnar.cpp +++ b/tools/quake3/q3map2/light_ydnar.cpp @@ -101,10 +101,7 @@ void ColorToBytes( const Vector3& color, Vector3b& colorBytes, float scale ){ /* contrast */ if ( lightmapContrast != 1.0f ){ for ( i = 0; i < 3; i++ ){ - sample[i] = lightmapContrast * ( sample[i] - 128 ) + 128; - if ( sample[i] < 0 ){ - sample[i] = 0; - } + sample[i] = std::max( 0.f, lightmapContrast * ( sample[i] - 128 ) + 128 ); } /* clamp with color normalization */ max = VectorMax( sample ); @@ -143,7 +140,7 @@ void ColorToBytes( const Vector3& color, Vector3b& colorBytes, float scale ){ void SmoothNormals( void ){ int i, j, k, f, numVerts, numVotes, fOld, start; - float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle; + float shadeAngle, defaultShadeAngle, maxShadeAngle; bspDrawSurface_t *ds; shaderInfo_t *si; float *shadeAngles; @@ -178,9 +175,7 @@ void SmoothNormals( void ){ else{ shadeAngle = defaultShadeAngle; } - if ( shadeAngle > maxShadeAngle ) { - maxShadeAngle = shadeAngle; - } + value_maximize( maxShadeAngle, shadeAngle ); /* flag its verts */ for ( j = 0; j < ds->numVerts; j++ ) @@ -244,18 +239,11 @@ void SmoothNormals( void ){ } /* use smallest shade angle */ - shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] ); + shadeAngle = std::min( shadeAngles[ i ], shadeAngles[ j ] ); /* check shade angle */ - dot = vector3_dot( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal ); - if ( dot > 1.0 ) { - dot = 1.0; - } - else if ( dot < -1.0 ) { - dot = -1.0; - } - testAngle = acos( dot ) + THETA_EPSILON; - if ( testAngle >= shadeAngle ) { + const double dot = std::clamp( vector3_dot( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal ), -1.0, 1.0 ); + if ( acos( dot ) + THETA_EPSILON >= shadeAngle ) { //Sys_Printf( "F(%3.3f >= %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) ); continue; } @@ -410,7 +398,7 @@ static void PerturbNormal( bspDrawVert_t *dv, shaderInfo_t *si, Vector3& pNormal #define BOGUS_NUDGE -99999.0f static int MapSingleLuxel( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv, const Plane3f* plane, float pass, const Vector3 stv[ 3 ], const Vector3 ttv[ 3 ], const Vector3 worldverts[ 3 ] ){ - int i, x, y, numClusters, *clusters, pointCluster; + int i, numClusters, *clusters, pointCluster; float lightmapSampleOffset; shaderInfo_t *si; Vector3 pNormal; @@ -435,20 +423,8 @@ static int MapSingleLuxel( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t /* find luxel xy coords (fixme: subtract 0.5?) */ - x = dv->lightmap[ 0 ][ 0 ]; - y = dv->lightmap[ 0 ][ 1 ]; - if ( x < 0 ) { - x = 0; - } - else if ( x >= lm->sw ) { - x = lm->sw - 1; - } - if ( y < 0 ) { - y = 0; - } - else if ( y >= lm->sh ) { - y = lm->sh - 1; - } + const int x = std::clamp( int( dv->lightmap[ 0 ][ 0 ] ), 0, lm->sw - 1 ); + const int y = std::clamp( int( dv->lightmap[ 0 ][ 1 ] ), 0, lm->sh - 1 ); /* set shader and cluster list */ if ( info != NULL ) { @@ -1153,9 +1129,7 @@ void MapRawLightmap( int rawLightmapNum ){ ----------------------------------------------------------------- */ /* walk the luxels */ - radius = floor( superSample / 2 ); - radius = radius > 0 ? radius : 1.0f; - radius += 1.0f; + radius = std::max( 1, superSample / 2 ) + 1; for ( pass = 2.0f; pass <= radius; pass += 1.0f ) { for ( y = 0; y < lm->sh; y++ ) @@ -1443,16 +1417,11 @@ float DirtForSample( trace_t *trace ){ } /* apply gain (does this even do much? heh) */ - outDirt = pow( gatherDirt / ( numDirtVectors + 1 ), dirtGain ); - if ( outDirt > 1.0f ) { - outDirt = 1.0f; - } + outDirt = std::min( 1.0, pow( gatherDirt / ( numDirtVectors + 1 ), dirtGain ) ); /* apply scale */ outDirt *= dirtScale; - if ( outDirt > 1.0f ) { - outDirt = 1.0f; - } + value_minimize( outDirt, 1.0f ); /* return to sender */ return 1.0f - outDirt; @@ -1863,7 +1832,6 @@ void IlluminateRawLightmap( int rawLightmapNum ){ rawLightmap_t *lm; surfaceInfo_t *info; bool filterColor, filterDir; - float brightness; float samples, filterRadius, weight; Vector3 averageColor, averageDir; float tests[ 4 ][ 2 ] = { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }; @@ -2005,12 +1973,8 @@ void IlluminateRawLightmap( int rawLightmapNum ){ { luxel.value = ambientColor; if ( deluxemap ) { - brightness = RGBTOGRAY( ambientColor ) * ( 1.0f / 255.0f ); - // use AT LEAST this amount of contribution from ambient for the deluxemap, fixes points that receive ZERO light - if ( brightness < 0.00390625f ) { - brightness = 0.00390625f; - } + const float brightness = std::max( 0.00390625f, RGBTOGRAY( ambientColor ) * ( 1.0f / 255.0f ) ); lm->getSuperDeluxel( x, y ) = lm->getSuperNormal( x, y ) * brightness; } @@ -2061,12 +2025,7 @@ void IlluminateRawLightmap( int rawLightmapNum ){ totalLighted = 0; /* determine filter radius */ - filterRadius = lm->filterRadius > trace.light->filterRadius - ? lm->filterRadius - : trace.light->filterRadius; - if ( filterRadius < 0.0f ) { - filterRadius = 0.0f; - } + filterRadius = std::max( { 0.f, lm->filterRadius, trace.light->filterRadius } ); /* set luxel filter radius */ luxelFilterRadius = lm->sampleSize != 0 ? superSample * filterRadius / lm->sampleSize : 0; @@ -2739,9 +2698,7 @@ void IlluminateVertexes( int num ){ z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 ); /* nudge origin */ - trace.origin[ 0 ] = verts[ i ].xyz[ 0 ] + ( VERTEX_NUDGE * x1 ); - trace.origin[ 1 ] = verts[ i ].xyz[ 1 ] + ( VERTEX_NUDGE * y1 ); - trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + ( VERTEX_NUDGE * z1 ); + trace.origin = verts[ i ].xyz + Vector3( x1, y1, z1 ) * Vector3().set( VERTEX_NUDGE ); /* try at nudged origin */ trace.cluster = ClusterForPointExtFilter( origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] ); @@ -2862,8 +2819,7 @@ void IlluminateVertexes( int num ){ ds->vertexStyles[ lightmapNum ] = lm->styles[ lightmapNum ]; /* get max search radius */ - maxRadius = lm->sw; - maxRadius = maxRadius > lm->sh ? maxRadius : lm->sh; + maxRadius = std::max( lm->sw, lm->sh ); /* walk the surface verts */ verts = yDrawVerts + ds->firstVert; @@ -2878,20 +2834,8 @@ void IlluminateVertexes( int num ){ } /* get luxel coords */ - x = verts[ i ].lightmap[ lightmapNum ][ 0 ]; - y = verts[ i ].lightmap[ lightmapNum ][ 1 ]; - if ( x < 0 ) { - x = 0; - } - else if ( x >= lm->sw ) { - x = lm->sw - 1; - } - if ( y < 0 ) { - y = 0; - } - else if ( y >= lm->sh ) { - y = lm->sh - 1; - } + x = std::clamp( int( verts[ i ].lightmap[ lightmapNum ][ 0 ] ), 0, lm->sw - 1 ); + y = std::clamp( int( verts[ i ].lightmap[ lightmapNum ][ 1 ] ), 0, lm->sh - 1 ); /* get vertex luxels */ Vector3& vertLuxel = getVertexLuxel( lightmapNum, ds->firstVert + i ); @@ -3489,13 +3433,8 @@ void SetupEnvelopes( bool forGrid, bool fastFlag ){ const Vector3 dir = -light->normal; for ( radius = 100.0f; radius < MAX_WORLD_COORD * 8.0f; radius += 10.0f ) { - float factor; - origin = light->origin + light->normal * radius; - factor = PointToPolygonFormFactor( origin, dir, light->w ); - if ( factor < 0.0f ) { - factor *= -1.0f; - } + const float factor = std::abs( PointToPolygonFormFactor( origin, dir, light->w ) ); if ( ( factor * light->add ) <= light->falloffTolerance ) { light->envelope = radius; break; @@ -3608,12 +3547,9 @@ void SetupEnvelopes( bool forGrid, bool fastFlag ){ radius = vector3_length( minmax.maxs - light->origin ); /* if this radius is smaller than the envelope, then set the envelope to it */ - if ( radius < light->envelope ) { - light->envelope = radius; - //% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights ); - } - //% else - //% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope ); + //% if ( radius < light->envelope ) Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights ); + //% else Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope ); + value_minimize( light->envelope, radius ); } /* add grid/surface only check */ @@ -3944,7 +3880,6 @@ void SetupFloodLight( void ){ float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodLightLowQuality ){ int i; float contribution; - int sub = 0; float gatherLight, outLight; Vector3 myUp, myRt; int vecs = 0; @@ -4016,10 +3951,7 @@ float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodL // d=trace->distance; //if (d>256) gatherDirt+=1; - contribution = d / dd; - if ( contribution > 1 ) { - contribution = 1.0f; - } + contribution = std::min( 1.f, d / dd ); //gatherDirt += 1.0f - ooDepth * VectorLength( displacement ); } @@ -4033,17 +3965,9 @@ float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodL return 0.0f; } - sub = vecs; + gatherLight /= std::max( 1, vecs ); - if ( sub < 1 ) { - sub = 1; - } - gatherLight /= ( sub ); - - outLight = gatherLight; - if ( outLight > 1.0f ) { - outLight = 1.0f; - } + outLight = std::min( 1.f, gatherLight ); /* return to sender */ return outLight; @@ -4256,12 +4180,8 @@ void FloodlightIlluminateLightmap( rawLightmap_t *lm ){ /* add to deluxemap */ if ( deluxemap && floodlight.scale > 0 ) { - float brightness = RGBTOGRAY( floodlight.value ) * ( 1.0f / 255.0f ) * floodlight.scale; - // use AT LEAST this amount of contribution from ambient for the deluxemap, fixes points that receive ZERO light - if ( brightness < 0.00390625f ) { - brightness = 0.00390625f; - } + const float brightness = std::max( 0.00390625f, RGBTOGRAY( floodlight.value ) * ( 1.0f / 255.0f ) * floodlight.scale ); const Vector3 lightvector = lm->getSuperNormal( x, y ) * brightness; lm->getSuperDeluxel( x, y ) += lightvector; diff --git a/tools/quake3/q3map2/lightmaps.cpp b/tools/quake3/q3map2/lightmaps.cpp deleted file mode 100644 index c5d2d65b..00000000 --- a/tools/quake3/q3map2/lightmaps.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - Copyright (C) 1999-2007 id Software, Inc. and contributors. - For a list of contributors, see the accompanying CONTRIBUTORS file. - - This file is part of GtkRadiant. - - GtkRadiant is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - GtkRadiant is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GtkRadiant; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "qbsp.h" - - -/* - - Lightmap allocation has to be done after all flood filling and - visible surface determination. - - */ - -int numSortShaders; -mapDrawSurface_t **surfsOnShader; -int allocatedSurfsOnShader; - - -int allocated[ LIGHTMAP_WIDTH ]; - - -void PrepareNewLightmap( void ) { - memset( allocated, 0, sizeof( allocated ) ); -} - -/* - =============== - AllocLMBlock - - returns a texture number and the position inside it - =============== - */ -bool AllocLMBlock( int w, int h, int *x, int *y ){ - int i, j; - int best, best2; - - best = LIGHTMAP_HEIGHT; - - for ( i = 0 ; i <= LIGHTMAP_WIDTH - w ; i++ ) { - best2 = 0; - - for ( j = 0 ; j < w ; j++ ) { - if ( allocated[i + j] >= best ) { - break; - } - if ( allocated[i + j] > best2 ) { - best2 = allocated[i + j]; - } - } - if ( j == w ) { // this is a valid spot - *x = i; - *y = best = best2; - } - } - - if ( best + h > LIGHTMAP_HEIGHT ) { - return false; - } - - for ( i = 0 ; i < w ; i++ ) { - allocated[*x + i] = best + h; - } - - return true; -} - - -/* - =================== - AllocateLightmapForPatch - =================== - */ -//#define LIGHTMAP_PATCHSHIFT - - - -/* - =================== - AllocateLightmapForSurface - =================== - */ - -//#define LIGHTMAP_BLOCK 16 - - - -/* - =================== - AllocateLightmaps - =================== - */ diff --git a/tools/quake3/q3map2/lightmaps_ydnar.cpp b/tools/quake3/q3map2/lightmaps_ydnar.cpp index 486c0458..1e01a899 100644 --- a/tools/quake3/q3map2/lightmaps_ydnar.cpp +++ b/tools/quake3/q3map2/lightmaps_ydnar.cpp @@ -493,20 +493,14 @@ bool AddPatchToRawLightmap( int num, rawLightmap_t *lm ){ if ( x + 1 < mesh->width ) { a = &verts[ ( y * mesh->width ) + x ]; b = &verts[ ( y * mesh->width ) + x + 1 ]; - length = vector3_length( a->xyz - b->xyz ); - if ( length > widthTable[ x ] ) { - widthTable[ x ] = length; - } + value_maximize( widthTable[ x ], (float)vector3_length( a->xyz - b->xyz ) ); } /* get height */ if ( y + 1 < mesh->height ) { a = &verts[ ( y * mesh->width ) + x ]; b = &verts[ ( ( y + 1 ) * mesh->width ) + x ]; - length = vector3_length( a->xyz - b->xyz ); - if ( length > heightTable[ y ] ) { - heightTable[ y ] = length; - } + value_maximize( heightTable[ y ], (float)vector3_length( a->xyz - b->xyz ) ); } } } @@ -516,12 +510,8 @@ bool AddPatchToRawLightmap( int num, rawLightmap_t *lm ){ for ( x = 0; x < ( mesh->width - 1 ); x++ ) length += widthTable[ x ]; lm->w = lm->sampleSize != 0 ? ceil( length / lm->sampleSize ) + 1 : 0; - if ( lm->w < ds->patchWidth ) { - lm->w = ds->patchWidth; - } - if ( lm->w > lm->customWidth ) { - lm->w = lm->customWidth; - } + value_maximize( lm->w, ds->patchWidth ); + value_minimize( lm->w, lm->customWidth ); sBasis = (float) ( lm->w - 1 ) / (float) ( ds->patchWidth - 1 ); /* determine lightmap height */ @@ -529,12 +519,8 @@ bool AddPatchToRawLightmap( int num, rawLightmap_t *lm ){ for ( y = 0; y < ( mesh->height - 1 ); y++ ) length += heightTable[ y ]; lm->h = lm->sampleSize != 0 ? ceil( length / lm->sampleSize ) + 1 : 0; - if ( lm->h < ds->patchHeight ) { - lm->h = ds->patchHeight; - } - if ( lm->h > lm->customHeight ) { - lm->h = lm->customHeight; - } + value_maximize( lm->h, ds->patchHeight ); + value_minimize( lm->h, lm->customHeight ); tBasis = (float) ( lm->h - 1 ) / (float) ( ds->patchHeight - 1 ); /* free the temporary mesh */ @@ -967,10 +953,8 @@ void SetupSurfaceLightmaps( void ){ Sys_FPrintf( SYS_VRB, "--- SetupSurfaceLightmaps ---\n" ); /* determine supersample amount */ - if ( superSample < 1 ) { - superSample = 1; - } - else if ( superSample > 8 ) { + value_maximize( superSample, 1 ); + if ( superSample > 8 ) { Sys_Warning( "Insane supersampling amount (%d) detected.\n", superSample ); superSample = 8; } @@ -1295,9 +1279,7 @@ void StitchSurfaceLightmaps( void ){ sampleSize = 0.5f * std::min( a->actualSampleSize, b->actualSampleSize ); /* test bounding box */ - if ( origin[ 0 ] < ( b->minmax.mins[ 0 ] - sampleSize ) || ( origin[ 0 ] > b->minmax.maxs[ 0 ] + sampleSize ) || - origin[ 1 ] < ( b->minmax.mins[ 1 ] - sampleSize ) || ( origin[ 1 ] > b->minmax.maxs[ 1 ] + sampleSize ) || - origin[ 2 ] < ( b->minmax.mins[ 2 ] - sampleSize ) || ( origin[ 2 ] > b->minmax.maxs[ 2 ] + sampleSize ) ) { + if ( !MinMax( b->minmax.mins - Vector3().set( sampleSize ), b->minmax.maxs + Vector3().set( sampleSize ) ).test( origin ) ) { continue; } @@ -1541,27 +1523,12 @@ static bool MergeBSPLuxels( rawLightmap_t *a, int aNum, rawLightmap_t *b, int bN */ static bool ApproximateLuxel( rawLightmap_t *lm, bspDrawVert_t *dv ){ - int i, x, y, d, lightmapNum; - - /* find luxel xy coords */ - x = dv->lightmap[ 0 ][ 0 ] / superSample; - y = dv->lightmap[ 0 ][ 1 ] / superSample; - if ( x < 0 ) { - x = 0; - } - else if ( x >= lm->w ) { - x = lm->w - 1; - } - if ( y < 0 ) { - y = 0; - } - else if ( y >= lm->h ) { - y = lm->h - 1; - } + const int x = std::clamp( int( dv->lightmap[ 0 ][ 0 ] / superSample ), 0, lm->w - 1 ); + const int y = std::clamp( int( dv->lightmap[ 0 ][ 1 ] / superSample ), 0, lm->h - 1 ); /* walk list */ - for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + for ( int lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) { /* early out */ if ( lm->styles[ lightmapNum ] == LS_NONE ) { @@ -1582,15 +1549,11 @@ static bool ApproximateLuxel( rawLightmap_t *lm, bspDrawVert_t *dv ){ /* styles are not affected by minlight */ if ( lightmapNum == 0 ) { - for ( i = 0; i < 3; i++ ) + for ( int i = 0; i < 3; i++ ) { /* set min color */ - if ( color[ i ] < minLight[ i ] ) { - color[ i ] = minLight[ i ]; - } - if ( vertexColor[ i ] < minLight[ i ] ) { /* note NOT minVertexLight */ - vertexColor[ i ] = minLight[ i ]; - } + value_maximize( color[ i ], minLight[ i ] ); + value_maximize( vertexColor[ i ], minLight[ i ] ); /* note NOT minVertexLight */ } } @@ -1600,13 +1563,9 @@ static bool ApproximateLuxel( rawLightmap_t *lm, bspDrawVert_t *dv ){ ColorToBytes( vertexColor, vcb.rgb(), 1.0f ); /* compare */ - for ( i = 0; i < 3; i++ ) + for ( int i = 0; i < 3; i++ ) { - d = cb[ i ] - vcb[ i ]; - if ( d < 0 ) { - d *= -1; - } - if ( d > approximateTolerance ) { + if ( abs( cb[ i ] - vcb[ i ] ) > approximateTolerance ) { return false; } } @@ -2081,8 +2040,8 @@ static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){ /* if fast allocation, do not test allocation on every pixels, especially for large lightmaps */ if ( fastAllocate ) { - xIncrement = MAX(1, lm->w / 15); - yIncrement = MAX(1, lm->h / 15); + xIncrement = std::max( 1, lm->w / 15 ); + yIncrement = std::max( 1, lm->h / 15 ); } else { xIncrement = 1; @@ -2217,9 +2176,7 @@ static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){ if ( lightmapNum == 0 ) { for ( i = 0; i < 3; i++ ) { - if ( color[ i ] < minLight[ i ] ) { - color[ i ] = minLight[ i ]; - } + value_maximize( color[ i ], minLight[ i ] ); } } @@ -2264,7 +2221,7 @@ static int CompareRawLightmap( const void *a, const void *b ){ blm = &rawLightmaps[ *( (const int*) b ) ]; /* get min number of surfaces */ - min = ( alm->numLightSurfaces < blm->numLightSurfaces ? alm->numLightSurfaces : blm->numLightSurfaces ); + min = std::min( alm->numLightSurfaces, blm->numLightSurfaces ); //#define allocate_bigger_first #ifdef allocate_bigger_first @@ -2644,9 +2601,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){ /* fix negative samples */ for ( j = 0; j < 3; j++ ) { - if ( sample[ j ] < 0.0f ) { - sample[ j ] = 0.0f; - } + value_maximize( sample[ j ], 0.0f ); } } else @@ -2692,9 +2647,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){ /* fix negative samples */ for ( j = 0; j < 3; j++ ) { - if ( sample[ j ] < 0.0f ) { - sample[ j ] = 0.0f; - } + value_maximize( sample[ j ], 0.0f ); } } } @@ -3280,9 +3233,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){ /* set minimum light */ if ( lightmapNum == 0 ) { for ( k = 0; k < 3; k++ ) - if ( color[ k ] < minVertexLight[ k ] ) { - color[ k ] = minVertexLight[ k ]; - } + value_maximize( color[ k ], minVertexLight[ k ] ); } } diff --git a/tools/quake3/q3map2/map.cpp b/tools/quake3/q3map2/map.cpp index 53da1bb5..c5eec734 100644 --- a/tools/quake3/q3map2/map.cpp +++ b/tools/quake3/q3map2/map.cpp @@ -745,9 +745,7 @@ void AddBrushBevels( void ){ if ( d > 0.1f ) { break; // point in front } - if ( d < minBack ) { - minBack = d; - } + value_minimize( minBack, d ); } // if some point was at the front if ( l != w2->numpoints ) { @@ -1699,10 +1697,8 @@ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){ GetEntityShadowFlags( mapEnt, NULL, &castShadows, &recvShadows ); /* ydnar: get lightmap scaling value for this entity */ - float lightmapScale = mapEnt->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ); - if ( lightmapScale < 0.0f ) - lightmapScale = 0.0f; - else if ( lightmapScale > 0.0f ) + const float lightmapScale = std::max( 0.f, mapEnt->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ) ); + if ( lightmapScale != 0 ) Sys_Printf( "Entity %d (%s) has lightmap scale of %.4f\n", mapEnt->mapEntityNum, classname, lightmapScale ); /* ydnar: get cel shader :) for this entity */ @@ -1718,18 +1714,14 @@ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){ } /* jal : entity based _shadeangle */ - float shadeAngle = mapEnt->floatForKey( "_shadeangle", - "_smoothnormals", "_sn", "_sa", "_smooth" ); /* vortex' aliases */ - if ( shadeAngle < 0.0f ) - shadeAngle = 0.0f; - else if ( shadeAngle > 0.0f ) + const float shadeAngle = std::max( 0.f, mapEnt->floatForKey( "_shadeangle", + "_smoothnormals", "_sn", "_sa", "_smooth" ) ); /* vortex' aliases */ + if ( shadeAngle != 0 ) Sys_Printf( "Entity %d (%s) has shading angle of %.4f\n", mapEnt->mapEntityNum, classname, shadeAngle ); /* jal : entity based _samplesize */ - int lightmapSampleSize = mapEnt->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ); - if ( lightmapSampleSize < 0 ) - lightmapSampleSize = 0; - else if ( lightmapSampleSize > 0 ) + const int lightmapSampleSize = std::max( 0, mapEnt->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ) ); + if ( lightmapSampleSize != 0 ) Sys_Printf( "Entity %d (%s) has lightmap sample size of %d\n", mapEnt->mapEntityNum, classname, lightmapSampleSize ); /* attach stuff to everything in the entity */ diff --git a/tools/quake3/q3map2/mesh.cpp b/tools/quake3/q3map2/mesh.cpp index d1c94536..b32c6bde 100644 --- a/tools/quake3/q3map2/mesh.cpp +++ b/tools/quake3/q3map2/mesh.cpp @@ -581,7 +581,6 @@ Vector3 ProjectPointOntoVector( const Vector3& point, const Vector3& vStart, con */ mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) { int i, j, k; - float len, maxLength; mesh_t out; bspDrawVert_t expand[MAX_EXPANDED_AXIS][MAX_EXPANDED_AXIS]; @@ -597,12 +596,9 @@ mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) { } for ( j = 1 ; j < out.width - 1; j++ ) { - maxLength = 0; + double maxLength = 0; for ( i = 0 ; i < out.height ; i++ ) { - len = vector3_length( expand[i][j].xyz - ProjectPointOntoVector( expand[i][j].xyz, expand[i][j - 1].xyz, expand[i][j + 1].xyz ) ); - if ( len > maxLength ) { - maxLength = len; - } + value_maximize( maxLength, vector3_length( expand[i][j].xyz - ProjectPointOntoVector( expand[i][j].xyz, expand[i][j - 1].xyz, expand[i][j + 1].xyz ) ) ); } if ( maxLength < 0.1 ) { out.width--; @@ -615,12 +611,9 @@ mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) { } } for ( j = 1 ; j < out.height - 1; j++ ) { - maxLength = 0; + double maxLength = 0; for ( i = 0 ; i < out.width ; i++ ) { - len = vector3_length( expand[j][i].xyz - ProjectPointOntoVector( expand[j][i].xyz, expand[j - 1][i].xyz, expand[j + 1][i].xyz ) ); - if ( len > maxLength ) { - maxLength = len; - } + value_maximize( maxLength, vector3_length( expand[j][i].xyz - ProjectPointOntoVector( expand[j][i].xyz, expand[j - 1][i].xyz, expand[j + 1][i].xyz ) ) ); } if ( maxLength < 0.1 ) { out.height--; @@ -650,7 +643,6 @@ mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) { */ mesh_t *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *widthtable, int *heighttable ){ int i, j, k, w, h, maxsubdivisions, subdivisions; - float length, maxLength, amount; mesh_t out; bspDrawVert_t expand[MAX_EXPANDED_AXIS][MAX_EXPANDED_AXIS]; @@ -672,18 +664,12 @@ mesh_t *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *width maxsubdivisions = ( maxsize - in->width ) / ( in->width - 1 ); for ( w = 0, j = 0 ; w < in->width - 1; w++, j += subdivisions + 1 ) { - maxLength = 0; + double maxLength = 0; for ( i = 0 ; i < out.height ; i++ ) { - length = vector3_length( expand[i][j + 1].xyz - expand[i][j].xyz ); - if ( length > maxLength ) { - maxLength = length; - } + value_maximize( maxLength, vector3_length( expand[i][j + 1].xyz - expand[i][j].xyz ) ); } - subdivisions = (int) ( maxLength / minLength ); - if ( subdivisions > maxsubdivisions ) { - subdivisions = maxsubdivisions; - } + subdivisions = std::min( (int) ( maxLength / minLength ), maxsubdivisions ); widthtable[w] = subdivisions + 1; if ( subdivisions <= 0 ) { @@ -698,7 +684,7 @@ mesh_t *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *width } for ( k = 1; k <= subdivisions; k++ ) { - amount = (float) k / ( subdivisions + 1 ); + const float amount = (float) k / ( subdivisions + 1 ); LerpDrawVertAmount( &expand[i][j], &expand[i][j + subdivisions + 1], amount, &expand[i][j + k] ); } } @@ -707,18 +693,12 @@ mesh_t *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *width maxsubdivisions = ( maxsize - in->height ) / ( in->height - 1 ); for ( h = 0, j = 0 ; h < in->height - 1; h++, j += subdivisions + 1 ) { - maxLength = 0; + double maxLength = 0; for ( i = 0 ; i < out.width ; i++ ) { - length = vector3_length( expand[j + 1][i].xyz - expand[j][i].xyz ); - if ( length > maxLength ) { - maxLength = length; - } + value_maximize( maxLength, vector3_length( expand[j + 1][i].xyz - expand[j][i].xyz ) ); } - subdivisions = (int) ( maxLength / minLength ); - if ( subdivisions > maxsubdivisions ) { - subdivisions = maxsubdivisions; - } + subdivisions = std::min( (int) ( maxLength / minLength ), maxsubdivisions ); heighttable[h] = subdivisions + 1; if ( subdivisions <= 0 ) { @@ -733,7 +713,7 @@ mesh_t *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *width } for ( k = 1; k <= subdivisions; k++ ) { - amount = (float) k / ( subdivisions + 1 ); + const float amount = (float) k / ( subdivisions + 1 ); LerpDrawVertAmount( &expand[j][i], &expand[j + subdivisions + 1][i], amount, &expand[j + k][i] ); } } diff --git a/tools/quake3/q3map2/minimap.cpp b/tools/quake3/q3map2/minimap.cpp index cb6b6e99..dcc94995 100644 --- a/tools/quake3/q3map2/minimap.cpp +++ b/tools/quake3/q3map2/minimap.cpp @@ -647,12 +647,8 @@ int MiniMapBSPMain( int argc, char **argv ){ for ( x = 0; x < minimap.width; ++x ) { float v = *q++; - if ( v < mi ) { - mi = v; - } - if ( v > ma ) { - ma = v; - } + value_minimize( mi, v ); + value_maximize( ma, v ); } if ( ma > mi ) { s = 1 / ( ma - mi ); @@ -699,16 +695,7 @@ int MiniMapBSPMain( int argc, char **argv ){ for ( y = 0; y < minimap.height; ++y ) for ( x = 0; x < minimap.width; ++x ) { - byte b; - float v = *q++; - if ( v < 0 ) { - v = 0; - } - if ( v > 255.0 / 256.0 ) { - v = 255.0 / 256.0; - } - b = v * 256; - *p++ = b; + *p++ = std::clamp( *q++, 0.f, 255.f / 256.f ) * 256; } Sys_Printf( " writing to %s...", minimapFilename ); WriteTGAGray( minimapFilename, data4b, minimap.width, minimap.height ); @@ -718,19 +705,10 @@ int MiniMapBSPMain( int argc, char **argv ){ for ( y = 0; y < minimap.height; ++y ) for ( x = 0; x < minimap.width; ++x ) { - byte b; - float v = *q++; - if ( v < 0 ) { - v = 0; - } - if ( v > 255.0 / 256.0 ) { - v = 255.0 / 256.0; - } - b = v * 256; *p++ = 0; *p++ = 0; *p++ = 0; - *p++ = b; + *p++ = std::clamp( *q++, 0.f, 255.f / 256.f ) * 256; } Sys_Printf( " writing to %s...", minimapFilename ); WriteTGA( minimapFilename, data4b, minimap.width, minimap.height ); @@ -740,19 +718,10 @@ int MiniMapBSPMain( int argc, char **argv ){ for ( y = 0; y < minimap.height; ++y ) for ( x = 0; x < minimap.width; ++x ) { - byte b; - float v = *q++; - if ( v < 0 ) { - v = 0; - } - if ( v > 255.0 / 256.0 ) { - v = 255.0 / 256.0; - } - b = v * 256; *p++ = 255; *p++ = 255; *p++ = 255; - *p++ = b; + *p++ = std::clamp( *q++, 0.f, 255.f / 256.f ) * 256; } Sys_Printf( " writing to %s...", minimapFilename ); WriteTGA( minimapFilename, data4b, minimap.width, minimap.height ); diff --git a/tools/quake3/q3map2/model.cpp b/tools/quake3/q3map2/model.cpp index f071f28b..b6813378 100644 --- a/tools/quake3/q3map2/model.cpp +++ b/tools/quake3/q3map2/model.cpp @@ -212,7 +212,6 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor picoSurface_t *surface; shaderInfo_t *si; mapDrawSurface_t *ds; - bspDrawVert_t *dv; const char *picoShaderName; byte *color; picoIndex_t *indexes; @@ -423,46 +422,45 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor for ( i = 0; i < ds->numVerts; i++ ) { /* get vertex */ - dv = &ds->verts[ i ]; + bspDrawVert_t& dv = ds->verts[ i ]; /* xyz and normal */ - dv->xyz = vector3_from_array( PicoGetSurfaceXYZ( surface, i ) ); - matrix4_transform_point( transform, dv->xyz ); + dv.xyz = vector3_from_array( PicoGetSurfaceXYZ( surface, i ) ); + matrix4_transform_point( transform, dv.xyz ); - dv->normal = vector3_from_array( PicoGetSurfaceNormal( surface, i ) ); - matrix4_transform_direction( nTransform, dv->normal ); - VectorNormalize( dv->normal ); + dv.normal = vector3_from_array( PicoGetSurfaceNormal( surface, i ) ); + matrix4_transform_direction( nTransform, dv.normal ); + VectorNormalize( dv.normal ); /* ydnar: tek-fu celshading support for flat shaded shit */ if ( flat ) { - dv->st = si->stFlat; + dv.st = si->stFlat; } /* ydnar: gs mods: added support for explicit shader texcoord generation */ else if ( si->tcGen ) { /* project the texture */ - dv->st[ 0 ] = vector3_dot( si->vecs[ 0 ], dv->xyz ); - dv->st[ 1 ] = vector3_dot( si->vecs[ 1 ], dv->xyz ); + dv.st[ 0 ] = vector3_dot( si->vecs[ 0 ], dv.xyz ); + dv.st[ 1 ] = vector3_dot( si->vecs[ 1 ], dv.xyz ); } /* normal texture coordinates */ else { - dv->st = vector2_from_array( PicoGetSurfaceST( surface, 0, i ) ); + dv.st = vector2_from_array( PicoGetSurfaceST( surface, 0, i ) ); } /* set lightmap/color bits */ color = PicoGetSurfaceColor( surface, 0, i ); for ( j = 0; j < MAX_LIGHTMAPS; j++ ) { - dv->lightmap[ j ][ 0 ] = 0.0f; - dv->lightmap[ j ][ 1 ] = 0.0f; + dv.lightmap[ j ] = { 0, 0 }; if ( spawnFlags & 32 ) { // spawnflag 32: model color -> alpha hack - dv->color[ j ] = { 255, 255, 255, RGBTOGRAY( color ) }; + dv.color[ j ] = { 255, 255, 255, RGBTOGRAY( color ) }; } else { - dv->color[ j ] = { color[0], color[1], color[2], color[3] }; + dv.color[ j ] = { color[0], color[1], color[2], color[3] }; } } } @@ -523,8 +521,7 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor { for ( j = 0; j < 3; j++ ) { - dv = &ds->verts[ ds->indexes[ i + j ] ]; - points[ j ] = dv->xyz; + points[ j ] = ds->verts[ ds->indexes[ i + j ] ].xyz; } if ( PlaneFromPoints( plane, points ) ){ if ( spawnFlags & 16 ) @@ -557,11 +554,8 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor /* make points */ for ( j = 0; j < 3; j++ ) { - /* get vertex */ - dv = &ds->verts[ ds->indexes[ i + j ] ]; - /* copy xyz */ - points[ j ] = dv->xyz; + points[ j ] = ds->verts[ ds->indexes[ i + j ] ].xyz; } /* make plane for triangle */ @@ -726,7 +720,7 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor } //if ( bestdist == 999999 && bestangle == 1 ) Sys_Printf("default_CLIPMODEL\n"); if ( bestdist == 999999 && bestangle == 1 ) goto default_CLIPMODEL; - if ( bestdist < mindist ) mindist = bestdist; + value_minimize( mindist, bestdist ); } if ( (limDepth != 0.0) && (mindist > limDepth) ) goto default_CLIPMODEL; @@ -834,17 +828,13 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor reverse.dist() = points[0][axis]; if ( bestNormal[axis] > 0 ){ for ( j = 1; j < 3; j++ ){ - if ( points[j][axis] < reverse.dist() ){ - reverse.dist() = points[j][axis]; - } + value_minimize( reverse.dist(), points[j][axis] ); } reverse.dist() = -reverse.dist() + clipDepth; } else{ for ( j = 1; j < 3; j++ ){ - if ( points[j][axis] > reverse.dist() ){ - reverse.dist() = points[j][axis]; - } + value_maximize( reverse.dist(), points[j][axis] ); } reverse.dist() += clipDepth; } @@ -968,10 +958,8 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor /* get vertex normals */ for ( j = 0; j < 3; j++ ) { - /* get vertex */ - dv = &ds->verts[ ds->indexes[ i + j ] ]; /* copy normal */ - Vnorm[ j ] = dv->normal; + Vnorm[ j ] = ds->verts[ ds->indexes[ i + j ] ].normal; } //avg normals for side planes @@ -1396,25 +1384,19 @@ void AddTriangleModels( entity_t *eparent ){ } /* jal : entity based _samplesize */ - int lightmapSampleSize = e->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ); - if ( lightmapSampleSize < 0 ) - lightmapSampleSize = 0; - if ( lightmapSampleSize > 0 ) + const int lightmapSampleSize = std::max( 0, e->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ) ); + if ( lightmapSampleSize != 0 ) Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize ); /* get lightmap scale */ - float lightmapScale = e->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ); - if ( lightmapScale < 0.0f ) - lightmapScale = 0.0f; - else if ( lightmapScale > 0.0f ) + const float lightmapScale = std::max( 0.f, e->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ) ); + if ( lightmapScale != 0 ) Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale ); /* jal : entity based _shadeangle */ - float shadeAngle = e->floatForKey( "_shadeangle", - "_smoothnormals", "_sn", "_sa", "_smooth" ); /* vortex' aliases */ - if ( shadeAngle < 0.0f ) - shadeAngle = 0.0f; - else if ( shadeAngle > 0.0f ) + const float shadeAngle = std::max( 0.f, e->floatForKey( "_shadeangle", + "_smoothnormals", "_sn", "_sa", "_smooth" ) ); /* vortex' aliases */ + if ( shadeAngle != 0 ) Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle ); const int skin = e->intForKey( "_skin", "skin" ); diff --git a/tools/quake3/q3map2/patch.cpp b/tools/quake3/q3map2/patch.cpp index 9070a189..26dbfa25 100644 --- a/tools/quake3/q3map2/patch.cpp +++ b/tools/quake3/q3map2/patch.cpp @@ -86,9 +86,7 @@ static void ExpandLongestCurve( float *longestCurve, const Vector3& a, const Vec } /* longer? */ - if ( len > *longestCurve ) { - *longestCurve = len; - } + value_maximize( *longestCurve, len ); } @@ -186,9 +184,7 @@ static void ExpandMaxIterations( int *maxIterations, int maxError, const Vector3 } /* more? */ - if ( iterations > *maxIterations ) { - *maxIterations = iterations; - } + value_maximize( *maxIterations, iterations ); } @@ -362,12 +358,8 @@ static void GrowGroup_r( parseMesh_t *pm, int patchNum, int patchCount, parseMes row = bordering + patchNum * patchCount; /* check maximums */ - if ( meshes[ patchNum ]->longestCurve > pm->longestCurve ) { - pm->longestCurve = meshes[ patchNum ]->longestCurve; - } - if ( meshes[ patchNum ]->maxIterations > pm->maxIterations ) { - pm->maxIterations = meshes[ patchNum ]->maxIterations; - } + value_maximize( pm->longestCurve, meshes[ patchNum ]->longestCurve ); + value_maximize( pm->maxIterations, meshes[ patchNum ]->maxIterations ); /* walk other patches */ for ( i = 0; i < patchCount; i++ ) @@ -429,9 +421,7 @@ void PatchMapDrawSurfs( entity_t *e ){ c2 = check->mesh.width * check->mesh.height; v2 = check->mesh.verts; for ( j = 0 ; j < c2 ; j++, v2++ ) { - if ( fabs( v1->xyz[0] - v2->xyz[0] ) < 1.0 - && fabs( v1->xyz[1] - v2->xyz[1] ) < 1.0 - && fabs( v1->xyz[2] - v2->xyz[2] ) < 1.0 ) { + if ( vector3_equal_epsilon( v1->xyz, v2->xyz, 1.f ) ) { break; } } diff --git a/tools/quake3/q3map2/path_init.cpp b/tools/quake3/q3map2/path_init.cpp index 1768d23f..fc4d06bc 100644 --- a/tools/quake3/q3map2/path_init.cpp +++ b/tools/quake3/q3map2/path_init.cpp @@ -532,12 +532,8 @@ void InitPaths( int *argc, char **argv ){ } /* initialize vfs paths */ - if ( numBasePaths > MAX_BASE_PATHS ) { - numBasePaths = MAX_BASE_PATHS; - } - if ( numGamePaths > MAX_GAME_PATHS ) { - numGamePaths = MAX_GAME_PATHS; - } + value_minimize( numBasePaths, MAX_BASE_PATHS ); + value_minimize( numGamePaths, MAX_GAME_PATHS ); /* walk the list of game paths */ for ( j = 0; j < numGamePaths; j++ ) @@ -552,9 +548,7 @@ void InitPaths( int *argc, char **argv ){ } /* initialize vfs paths */ - if ( numPakPaths > MAX_PAK_PATHS ) { - numPakPaths = MAX_PAK_PATHS; - } + value_minimize( numPakPaths, MAX_PAK_PATHS ); /* walk the list of pak paths */ for ( i = 0; i < numPakPaths; i++ ) diff --git a/tools/quake3/q3map2/shaders.cpp b/tools/quake3/q3map2/shaders.cpp index da316e14..c75f3762 100644 --- a/tools/quake3/q3map2/shaders.cpp +++ b/tools/quake3/q3map2/shaders.cpp @@ -142,14 +142,7 @@ void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts ){ { for ( k = 0; k < 4; k++ ) { - c = ( mult[ k ] * dv->color[ j ][ k ] ) + add[ k ]; - if ( c < 0 ) { - c = 0; - } - else if ( c > 255 ) { - c = 255; - } - dv->color[ j ][ k ] = c; + dv->color[ j ][ k ] = std::clamp( mult[ k ] * dv->color[ j ][ k ] + add[ k ], 0.f, 255.f ); } } } @@ -933,7 +926,7 @@ void Parse1DMatrixAppend( char *buffer, int x, float *m ){ */ static void ParseShaderFile( const char *filename ){ - int i, val; + int i; shaderInfo_t *si; char shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */ @@ -1383,12 +1376,8 @@ static void ParseShaderFile( const char *filename ){ si->skyLightIterations = atoi( token ); /* clamp */ - if ( si->skyLightValue < 0.0f ) { - si->skyLightValue = 0.0f; - } - if ( si->skyLightIterations < 2 ) { - si->skyLightIterations = 2; - } + value_maximize( si->skyLightValue, 0.0f ); + value_maximize( si->skyLightIterations, 2 ); } /* q3map_surfacelight */ @@ -1400,14 +1389,7 @@ static void ParseShaderFile( const char *filename ){ /* q3map_lightStyle (sof2/jk2 lightstyle) */ else if ( striEqual( token, "q3map_lightStyle" ) ) { GetTokenAppend( shaderText, false ); - val = atoi( token ); - if ( val < 0 ) { - val = 0; - } - else if ( val > LS_NONE ) { - val = LS_NONE; - } - si->lightStyle = val; + si->lightStyle = std::clamp( atoi( token ), 0, LS_NONE ); } /* wolf: q3map_lightRGB */ diff --git a/tools/quake3/q3map2/surface.cpp b/tools/quake3/q3map2/surface.cpp index 44d4afc9..550226b9 100644 --- a/tools/quake3/q3map2/surface.cpp +++ b/tools/quake3/q3map2/surface.cpp @@ -324,7 +324,6 @@ void TidyEntitySurfaces( entity_t *e ){ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){ int i, j, v, size[ 2 ]; - float mins[ 2 ], maxs[ 2 ]; /* try to early out */ @@ -333,20 +332,13 @@ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){ } /* walk the verts and determine min/max st values */ - mins[ 0 ] = 999999; - mins[ 1 ] = 999999; - maxs[ 0 ] = -999999; - maxs[ 1 ] = -999999; + Vector2 mins( 999999, 999999 ), maxs( -999999, -999999 ); for ( i = 0; i < ds->numVerts; i++ ) { for ( j = 0; j < 2; j++ ) { - if ( ds->verts[ i ].st[ j ] < mins[ j ] ) { - mins[ j ] = ds->verts[ i ].st[ j ]; - } - if ( ds->verts[ i ].st[ j ] > maxs[ j ] ) { - maxs[ j ] = ds->verts[ i ].st[ j ]; - } + value_minimize( mins[ j ], ds->verts[ i ].st[ j ] ); + value_maximize( maxs[ j ], ds->verts[ i ].st[ j ] ); } } @@ -366,12 +358,8 @@ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){ for ( j = 0; j < 2; j++ ) { v = ( ds->verts[ i ].st[ j ] + ds->bias[ j ] ) * size[ j ]; - if ( v < ds->texMins[ j ] ) { - ds->texMins[ j ] = v; - } - if ( v > ds->texMaxs[ j ] ) { - ds->texMaxs[ j ] = v; - } + value_minimize( ds->texMins[ j ], v ); + value_maximize( ds->texMaxs[ j ], v ); } } @@ -514,7 +502,7 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ plane = { 0, 0, 0, 0 }; for ( i = 0; i < ds->numVerts; i++ ) { - if ( ds->verts[ i ].normal[ 0 ] != 0.0f && ds->verts[ i ].normal[ 1 ] != 0.0f && ds->verts[ i ].normal[ 2 ] != 0.0f ) { + if ( ds->verts[ i ].normal != g_vector3_identity ) { plane.normal() = ds->verts[ i ].normal; plane.dist() = vector3_dot( ds->verts[ i ].xyz, plane.normal() ); break; @@ -630,17 +618,7 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ ds->lightmapScale = 0; /* applied */ } - if ( ds->sampleSize < minSampleSize ) { - ds->sampleSize = minSampleSize; - } - - if ( ds->sampleSize < 1 ) { - ds->sampleSize = 1; - } - - if ( ds->sampleSize > 16384 ) { /* powers of 2 are preferred */ - ds->sampleSize = 16384; - } + ds->sampleSize = std::clamp( ds->sampleSize, std::max( minSampleSize, 1 ), 16384 ); /* powers of 2 are preferred */ } } @@ -677,9 +655,6 @@ void ClassifyEntitySurfaces( entity_t *e ){ */ byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const Vector3& point ){ - int x, y; - - /* early out if no indexmap */ if ( im == NULL ) { return 0; @@ -697,24 +672,12 @@ byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const const Vector3 size = maxs - mins; /* find st (fixme: support more than just z-axis projection) */ - float s = floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ]; - float t = floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ]; - if ( s < 0.0f ) { - s = 0.0f; - } - else if ( s > 1.0f ) { - s = 1.0f; - } - if ( t < 0.0f ) { - t = 0.0f; - } - else if ( t > 1.0f ) { - t = 1.0f; - } + const float s = std::clamp( floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ], 0.0, 1.0 ); + const float t = std::clamp( floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ], 0.0, 1.0 ); /* make xy */ - x = ( im->w - 1 ) * s; - y = ( im->h - 1 ) * t; + const int x = ( im->w - 1 ) * s; + const int y = ( im->h - 1 ) * t; #else /* get size */ const Vector3 size = eMinmax.maxs - eMinmax.mins; @@ -724,20 +687,8 @@ byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const const float t = ( eMinmax.maxs[ 1 ] - point[ 1 ] ) / size[ 1 ]; /* calc xy */ - x = s * im->w; - y = t * im->h; - if ( x < 0 ) { - x = 0; - } - else if ( x > ( im->w - 1 ) ) { - x = ( im->w - 1 ); - } - if ( y < 0 ) { - y = 0; - } - else if ( y > ( im->h - 1 ) ) { - y = ( im->h - 1 ); - } + const int x = std::clamp( int( s * im->w ), 0, im->w - 1 ); + const int y = std::clamp( int( t * im->h ), 0, im->h - 1 ); #endif /* return index */ @@ -753,31 +704,22 @@ byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const */ shaderInfo_t *GetIndexedShader( const shaderInfo_t *parent, const indexMap_t *im, int numPoints, byte *shaderIndexes ){ - int i; - byte minShaderIndex, maxShaderIndex; - shaderInfo_t *si; - - /* early out if bad data */ if ( im == NULL || numPoints <= 0 || shaderIndexes == NULL ) { return ShaderInfoForShader( "default" ); } /* determine min/max index */ - minShaderIndex = 255; - maxShaderIndex = 0; - for ( i = 0; i < numPoints; i++ ) + byte minShaderIndex = 255; + byte maxShaderIndex = 0; + for ( int i = 0; i < numPoints; i++ ) { - if ( shaderIndexes[ i ] < minShaderIndex ) { - minShaderIndex = shaderIndexes[ i ]; - } - if ( shaderIndexes[ i ] > maxShaderIndex ) { - maxShaderIndex = shaderIndexes[ i ]; - } + value_minimize( minShaderIndex, shaderIndexes[ i ] ); + value_maximize( maxShaderIndex, shaderIndexes[ i ] ); } /* set alpha inline */ - for ( i = 0; i < numPoints; i++ ) + for ( int i = 0; i < numPoints; i++ ) { /* straight rip from terrain.c */ if ( shaderIndexes[ i ] < maxShaderIndex ) { @@ -789,7 +731,7 @@ shaderInfo_t *GetIndexedShader( const shaderInfo_t *parent, const indexMap_t *im } /* get the shader */ - si = ShaderInfoForShader( ( minShaderIndex == maxShaderIndex )? + shaderInfo_t *si = ShaderInfoForShader( ( minShaderIndex == maxShaderIndex )? String64()( "textures/", im->shader.c_str(), '_', int(maxShaderIndex) ): String64()( "textures/", im->shader.c_str(), '_', int(minShaderIndex), "to", int(maxShaderIndex) ) ); @@ -829,8 +771,8 @@ shaderInfo_t *GetIndexedShader( const shaderInfo_t *parent, const indexMap_t *im creates a SURF_FACE drawsurface from a given brush side and winding */ -#define SNAP_FLOAT_TO_INT 8 -#define SNAP_INT_TO_FLOAT ( 1.0 / SNAP_FLOAT_TO_INT ) +const double SNAP_FLOAT_TO_INT = 8.0; +const double SNAP_INT_TO_FLOAT = ( 1.0 / SNAP_FLOAT_TO_INT ); mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w ){ int i, j, k; @@ -922,7 +864,7 @@ mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, windin /* round the xyz to a given precision and translate by origin */ for ( i = 0 ; i < 3 ; i++ ) - dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5f ); + dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5 ); vTranslated = dv->xyz + e->origin; /* ydnar: tek-fu celshading support for flat shaded shit */ @@ -1249,11 +1191,8 @@ mapDrawSurface_t *DrawSurfaceForShader( const char *shader ){ static void AddSurfaceFlare( mapDrawSurface_t *ds, const Vector3& entityOrigin ){ Vector3 origin( 0, 0, 0 ); - int i; - - /* find centroid */ - for ( i = 0; i < ds->numVerts; i++ ) + for ( int i = 0; i < ds->numVerts; i++ ) origin += ds->verts[ i ].xyz; origin /= ds->numVerts; origin += entityOrigin; @@ -1408,8 +1347,8 @@ void SubdivideFaceSurfaces( entity_t *e, tree_t *tree ){ } /* get subdivisions from shader */ - if ( si->subdivisions > 0 && si->subdivisions < subdivisions ) { - subdivisions = si->subdivisions; + if ( si->subdivisions > 0 ) { + value_minimize( subdivisions, si->subdivisions ); } if ( subdivisions < 1.0f ) { continue; @@ -1941,10 +1880,6 @@ int FilterPointIntoTree_r( const Vector3& point, mapDrawSurface_t *ds, node_t *n */ int FilterPointConvexHullIntoTree_r( Vector3 *points[], const int npoints, mapDrawSurface_t *ds, node_t *node ){ - float d, dmin, dmax; - int refs = 0; - int i; - if ( !points ) { return 0; } @@ -1954,20 +1889,17 @@ int FilterPointConvexHullIntoTree_r( Vector3 *points[], const int npoints, mapDr /* classify the point in relation to the plane */ const Plane3f& plane = mapplanes[ node->planenum ].plane; + float dmin, dmax; dmin = dmax = plane3_distance_to_point( plane, *points[0] ); - for ( i = 1; i < npoints; ++i ) + for ( int i = 1; i < npoints; ++i ) { - d = plane3_distance_to_point( plane, *points[i] ); - if ( d > dmax ) { - dmax = d; - } - if ( d < dmin ) { - dmin = d; - } + const float d = plane3_distance_to_point( plane, *points[i] ); + value_maximize( dmax, d ); + value_minimize( dmin, d ); } /* filter by this plane */ - refs = 0; + int refs = 0; if ( dmax >= -ON_EPSILON ) { refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 0 ] ); } diff --git a/tools/quake3/q3map2/surface_foliage.cpp b/tools/quake3/q3map2/surface_foliage.cpp index e8bb10cb..6b1fb8c1 100644 --- a/tools/quake3/q3map2/surface_foliage.cpp +++ b/tools/quake3/q3map2/surface_foliage.cpp @@ -72,30 +72,17 @@ static void SubdivideFoliageTriangle_r( mapDrawSurface_t *ds, const foliage_t& f /* subdivide calc */ { - int i; - float dx, dy, dz, dist, maxDist; - foliageInstance_t *fi; - - /* get instance */ - fi = &foliageInstances[ numFoliageInstances ]; + foliageInstance_t *fi = &foliageInstances[ numFoliageInstances ]; /* find the longest edge and split it */ max = -1; - maxDist = 0.0f; + float maxDist = 0.0f; fi->xyz.set( 0 ); fi->normal.set( 0 ); - for ( i = 0; i < 3; i++ ) + for ( int i = 0; i < 3; i++ ) { - /* get verts */ - const Vector3& a = tri[ i ]->xyz; - const Vector3& b = tri[ ( i + 1 ) % 3 ]->xyz; - - /* get dists */ - dx = a[ 0 ] - b[ 0 ]; - dy = a[ 1 ] - b[ 1 ]; - dz = a[ 2 ] - b[ 2 ]; - dist = ( dx * dx ) + ( dy * dy ) + ( dz * dz ); + const float dist = vector3_length_squared( tri[ i ]->xyz - tri[ ( i + 1 ) % 3 ]->xyz ); /* longer? */ if ( dist > maxDist ) { diff --git a/tools/quake3/q3map2/surface_meta.cpp b/tools/quake3/q3map2/surface_meta.cpp index c2a5f367..30248e23 100644 --- a/tools/quake3/q3map2/surface_meta.cpp +++ b/tools/quake3/q3map2/surface_meta.cpp @@ -125,13 +125,9 @@ int FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, b int triIndex; /* detect degenerate triangles fixme: do something proper here */ - if ( vector3_length( a->xyz - b->xyz ) < 0.125f ) { - return -1; - } - if ( vector3_length( b->xyz - c->xyz ) < 0.125f ) { - return -1; - } - if ( vector3_length( c->xyz - a->xyz ) < 0.125f ) { + if ( vector3_length( a->xyz - b->xyz ) < 0.125f + || vector3_length( b->xyz - c->xyz ) < 0.125f + || vector3_length( c->xyz - a->xyz ) < 0.125f ) { return -1; } @@ -989,9 +985,7 @@ void FixMetaTJunctions( void ){ /* skip this point if it already exists in the triangle */ for ( k = 0; k < 3; k++ ) { - if ( fabs( pt[ 0 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 0 ] ) <= TJ_POINT_EPSILON && - fabs( pt[ 1 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 1 ] ) <= TJ_POINT_EPSILON && - fabs( pt[ 2 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 2 ] ) <= TJ_POINT_EPSILON ) { + if ( vector3_equal_epsilon( pt, metaVerts[ tri->indexes[ k ] ].xyz, TJ_POINT_EPSILON ) ) { break; } } @@ -1106,7 +1100,7 @@ void FixMetaTJunctions( void ){ void SmoothMetaTriangles( void ){ int i, j, k, f, fOld, start, numVerts, numVotes, numSmoothed; - float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle; + float shadeAngle, defaultShadeAngle, maxShadeAngle; metaTriangle_t *tri; float *shadeAngles; byte *smoothed; @@ -1146,9 +1140,7 @@ void SmoothMetaTriangles( void ){ shadeAngle = defaultShadeAngle; } - if ( shadeAngle > maxShadeAngle ) { - maxShadeAngle = shadeAngle; - } + value_maximize( maxShadeAngle, shadeAngle ); /* flag its verts */ for ( j = 0; j < 3; j++ ) @@ -1207,18 +1199,11 @@ void SmoothMetaTriangles( void ){ } /* use smallest shade angle */ - shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] ); + shadeAngle = std::min( shadeAngles[ i ], shadeAngles[ j ] ); /* check shade angle */ - dot = vector3_dot( metaVerts[ i ].normal, metaVerts[ j ].normal ); - if ( dot > 1.0 ) { - dot = 1.0; - } - else if ( dot < -1.0 ) { - dot = -1.0; - } - testAngle = acos( dot ) + THETA_EPSILON; - if ( testAngle >= shadeAngle ) { + const double dot = std::clamp( vector3_dot( metaVerts[ i ].normal, metaVerts[ j ].normal ), -1.0, 1.0 ); + if ( acos( dot ) + THETA_EPSILON >= shadeAngle ) { continue; } @@ -1388,25 +1373,13 @@ static int AddMetaTriangleToSurface( mapDrawSurface_t *ds, metaTriangle_t *tri, - if ( metaMaxBBoxDistance >= 0 ) { - if ( ds->numIndexes > 0 ) { - const Vector3 mins = ds->minmax.mins - Vector3( metaMaxBBoxDistance, metaMaxBBoxDistance, metaMaxBBoxDistance ); - const Vector3 maxs = ds->minmax.maxs + Vector3( metaMaxBBoxDistance, metaMaxBBoxDistance, metaMaxBBoxDistance ); -#define CHECK_1D( mins, v, maxs ) ( ( mins ) <= ( v ) && ( v ) <= ( maxs ) ) -#define CHECK_3D( mins, v, maxs ) ( CHECK_1D( ( mins )[0], ( v )[0], ( maxs )[0] ) && CHECK_1D( ( mins )[1], ( v )[1], ( maxs )[1] ) && CHECK_1D( ( mins )[2], ( v )[2], ( maxs )[2] ) ) - Vector3 p = metaVerts[ tri->indexes[ 0 ] ].xyz; - if ( !CHECK_3D( mins, p, maxs ) ) { - p = metaVerts[ tri->indexes[ 1 ] ].xyz; - if ( !CHECK_3D( mins, p, maxs ) ) { - p = metaVerts[ tri->indexes[ 2 ] ].xyz; - if ( !CHECK_3D( mins, p, maxs ) ) { - return 0; - } - } - } -#undef CHECK_3D -#undef CHECK_1D - } + if ( metaMaxBBoxDistance >= 0 && ds->numIndexes > 0 ) { + const MinMax minmax( ds->minmax.mins - Vector3().set( metaMaxBBoxDistance ), + ds->minmax.maxs + Vector3().set( metaMaxBBoxDistance ) ); + if( !minmax.test( metaVerts[ tri->indexes[ 0 ] ].xyz ) + && !minmax.test( metaVerts[ tri->indexes[ 1 ] ].xyz ) + && !minmax.test( metaVerts[ tri->indexes[ 2 ] ].xyz ) ) + return 0; } /* set initial score */ @@ -1707,51 +1680,52 @@ static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles, compare function for qsort() */ -static int CompareMetaTriangles( const void *a, const void *b ){ - int i, j, av, bv; +static int CompareMetaTriangles( const void *a_, const void *b_ ){ + auto a = reinterpret_cast( a_ ); + auto b = reinterpret_cast( b_ ); /* shader first */ - if ( ( (const metaTriangle_t*) a )->si < ( (const metaTriangle_t*) b )->si ) { + if ( a->si < b->si ) { return 1; } - else if ( ( (const metaTriangle_t*) a )->si > ( (const metaTriangle_t*) b )->si ) { + else if ( a->si > b->si ) { return -1; } /* then fog */ - else if ( ( (const metaTriangle_t*) a )->fogNum < ( (const metaTriangle_t*) b )->fogNum ) { + else if ( a->fogNum < b->fogNum ) { return 1; } - else if ( ( (const metaTriangle_t*) a )->fogNum > ( (const metaTriangle_t*) b )->fogNum ) { + else if ( a->fogNum > b->fogNum ) { return -1; } /* then plane */ #if 0 - else if ( npDegrees == 0.0f && !( (const metaTriangle_t*) a )->si->nonplanar && - ( (const metaTriangle_t*) a )->planeNum >= 0 && ( (const metaTriangle_t*) a )->planeNum >= 0 ) { - if ( ( (const metaTriangle_t*) a )->plane[ 3 ] < ( (const metaTriangle_t*) b )->plane[ 3 ] ) { + else if ( npDegrees == 0.0f && !a->si->nonplanar && + a->planeNum >= 0 && a->planeNum >= 0 ) { + if ( a->plane.dist() < b->plane.dist() ) { return 1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 3 ] > ( (const metaTriangle_t*) b )->plane[ 3 ] ) { + else if ( a->plane.dist() > b->plane.dist() ) { return -1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 0 ] < ( (const metaTriangle_t*) b )->plane[ 0 ] ) { + else if ( a->plane.normal()[ 0 ] < b->plane.normal()[ 0 ] ) { return 1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 0 ] > ( (const metaTriangle_t*) b )->plane[ 0 ] ) { + else if ( a->plane.normal()[ 0 ] > b->plane.normal()[ 0 ] ) { return -1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 1 ] < ( (const metaTriangle_t*) b )->plane[ 1 ] ) { + else if ( a->plane.normal()[ 1 ] < b->plane.normal()[ 1 ] ) { return 1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 1 ] > ( (const metaTriangle_t*) b )->plane[ 1 ] ) { + else if ( a->plane.normal()[ 1 ] > b->plane.normal()[ 1 ] ) { return -1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 2 ] < ( (const metaTriangle_t*) b )->plane[ 2 ] ) { + else if ( a->plane.normal()[ 2 ] < b->plane.normal()[ 2 ] ) { return 1; } - else if ( ( (const metaTriangle_t*) a )->plane[ 2 ] > ( (const metaTriangle_t*) b )->plane[ 2 ] ) { + else if ( a->plane.normal()[ 2 ] > b->plane.normal()[ 2 ] ) { return -1; } } @@ -1762,23 +1736,19 @@ static int CompareMetaTriangles( const void *a, const void *b ){ /* find mins */ Vector3 aMins( 999999, 999999, 999999 ); Vector3 bMins( 999999, 999999, 999999 ); - for ( i = 0; i < 3; i++ ) + for ( int i = 0; i < 3; i++ ) { - av = ( (const metaTriangle_t*) a )->indexes[ i ]; - bv = ( (const metaTriangle_t*) b )->indexes[ i ]; - for ( j = 0; j < 3; j++ ) + const int av = a->indexes[ i ]; + const int bv = b->indexes[ i ]; + for ( int j = 0; j < 3; j++ ) { - if ( metaVerts[ av ].xyz[ j ] < aMins[ j ] ) { - aMins[ j ] = metaVerts[ av ].xyz[ j ]; - } - if ( metaVerts[ bv ].xyz[ j ] < bMins[ j ] ) { - bMins[ j ] = metaVerts[ bv ].xyz[ j ]; - } + value_minimize( aMins[ j ], metaVerts[ av ].xyz[ j ] ); + value_minimize( bMins[ j ], metaVerts[ bv ].xyz[ j ] ); } } /* test it */ - for ( i = 0; i < 3; i++ ) + for ( int i = 0; i < 3; i++ ) { if ( aMins[ i ] < bMins[ i ] ) { return 1; diff --git a/tools/quake3/q3map2/tjunction.cpp b/tools/quake3/q3map2/tjunction.cpp index b577eb32..000d63cb 100644 --- a/tools/quake3/q3map2/tjunction.cpp +++ b/tools/quake3/q3map2/tjunction.cpp @@ -78,7 +78,7 @@ int c_natural, c_rotate, c_cant; // these should be whatever epsilon we actually expect, // plus SNAP_INT_TO_FLOAT -#define LINE_POSITION_EPSILON 0.25 +#define LINE_POSITION_EPSILON 0.25f #define POINT_ON_LINE_EPSILON 0.25 /* @@ -87,13 +87,10 @@ int c_natural, c_rotate, c_cant; ==================== */ void InsertPointOnEdge( const Vector3 &v, edgeLine_t *e ) { - float d; edgePoint_t *p, *scan; - d = vector3_dot( v - e->origin, e->dir ); - p = safe_malloc( sizeof( edgePoint_t ) ); - p->intercept = d; + p->intercept = vector3_dot( v - e->origin, e->dir ); p->xyz = v; if ( e->chain->next == e->chain ) { @@ -103,9 +100,8 @@ void InsertPointOnEdge( const Vector3 &v, edgeLine_t *e ) { } scan = e->chain->next; - for ( ; scan != e->chain ; scan = scan->next ) { - d = p->intercept - scan->intercept; - if ( d > -LINE_POSITION_EPSILON && d < LINE_POSITION_EPSILON ) { + for ( ; scan != e->chain; scan = scan->next ) { + if ( float_equal_epsilon( p->intercept, scan->intercept, LINE_POSITION_EPSILON ) ) { free( p ); return; // the point is already set } @@ -163,21 +159,10 @@ int AddEdge( bspDrawVert_t& dv1, bspDrawVert_t& dv2, bool createNonAxial ) { for ( i = 0 ; i < numEdgeLines ; i++ ) { e = &edgeLines[i]; - d = vector3_dot( v1, e->normal1 ) - e->dist1; - if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) { - continue; - } - d = vector3_dot( v1, e->normal2 ) - e->dist2; - if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) { - continue; - } - - d = vector3_dot( v2, e->normal1 ) - e->dist1; - if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) { - continue; - } - d = vector3_dot( v2, e->normal2 ) - e->dist2; - if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) { + if ( !float_equal_epsilon( vector3_dot( v1, e->normal1 ), e->dist1, POINT_ON_LINE_EPSILON ) + || !float_equal_epsilon( vector3_dot( v1, e->normal2 ), e->dist2, POINT_ON_LINE_EPSILON ) + || !float_equal_epsilon( vector3_dot( v2, e->normal1 ), e->dist1, POINT_ON_LINE_EPSILON ) + || !float_equal_epsilon( vector3_dot( v2, e->normal2 ), e->dist2, POINT_ON_LINE_EPSILON ) ) { continue; } @@ -581,10 +566,8 @@ bool FixBrokenSurface( mapDrawSurface_t *ds ){ ================ */ int EdgeCompare( const void *elem1, const void *elem2 ) { - float d1, d2; - - d1 = ( (const originalEdge_t *)elem1 )->length; - d2 = ( (const originalEdge_t *)elem2 )->length; + const float d1 = reinterpret_cast( elem1 )->length; + const float d2 = reinterpret_cast( elem2 )->length; if ( d1 < d2 ) { return -1; diff --git a/tools/quake3/q3map2/vis.cpp b/tools/quake3/q3map2/vis.cpp index 507a71eb..da1e5166 100644 --- a/tools/quake3/q3map2/vis.cpp +++ b/tools/quake3/q3map2/vis.cpp @@ -369,26 +369,21 @@ void CalcVis( void ){ ================== */ void SetPortalSphere( vportal_t *p ){ - int i; Vector3 total( 0, 0, 0 ); fixedWinding_t *w; - float r, bestr; w = p->winding; - for ( i = 0 ; i < w->numpoints ; i++ ) + for ( int i = 0; i < w->numpoints; i++ ) { total += w->points[i]; } total /= w->numpoints; - bestr = 0; - for ( i = 0 ; i < w->numpoints ; i++ ) + double bestr = 0; + for ( int i = 0; i < w->numpoints; i++ ) { - r = vector3_length( w->points[i] - total ); - if ( r > bestr ) { - bestr = r; - } + value_maximize( bestr, vector3_length( w->points[i] - total ) ); } p->origin = total; p->radius = bestr; diff --git a/tools/quake3/q3map2/visflow.cpp b/tools/quake3/q3map2/visflow.cpp index 3916290b..98b93ac0 100644 --- a/tools/quake3/q3map2/visflow.cpp +++ b/tools/quake3/q3map2/visflow.cpp @@ -102,9 +102,7 @@ fixedWinding_t *AllocStackWinding( pstack_t *stack ){ } void FreeStackWinding( fixedWinding_t *w, pstack_t *stack ){ - int i; - - i = w - stack->windings; + const int i = w - stack->windings; if ( i < 0 || i > 2 ) { return; // not from local @@ -1345,10 +1343,8 @@ void CreatePassages( int portalnum ){ for ( k = 0; k < numseperators; k++ ) { /* ydnar: this is a shitty crutch */ - if ( in.numpoints > MAX_POINTS_ON_FIXED_WINDING ) { - //% Sys_Printf( "[%d]", p->winding->numpoints ); - in.numpoints = MAX_POINTS_ON_FIXED_WINDING; - } + //% if ( in.numpoints > MAX_POINTS_ON_FIXED_WINDING ) Sys_Printf( "[%d]", p->winding->numpoints ); + value_minimize( in.numpoints, MAX_POINTS_ON_FIXED_WINDING ); res = PassageChopWinding( &in, &out, seperators[ k ] ); if ( res == &out ) {