This commit is contained in:
Garux 2021-03-02 22:01:09 +03:00
parent 9446aeca8e
commit f9a424b6c8
30 changed files with 321 additions and 1000 deletions

View File

@ -325,7 +325,7 @@ void xml_message_push( int flag, const char* characters, size_t length ){
} }
else else
{ {
size_t size = ( space < ( size_t )( end - characters ) ) ? space : ( size_t )( end - characters ); size_t size = std::min( space, static_cast<size_t>( end - characters ) );
memcpy( mesege + mesege_len, characters, size ); memcpy( mesege + mesege_len, characters, size );
mesege_len += size; mesege_len += size;
characters += size; characters += size;

View File

@ -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. // 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; 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; in = *inout;
counts[0] = counts[1] = counts[2] = 0; 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] ); 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 // check the point is on the face plane
d = plane3_distance_to_point( faceplane, p1 ); d = plane3_distance_to_point( faceplane, p1 );

View File

@ -11,6 +11,16 @@
#define VectorFastNormalize VectorNormalize #define VectorFastNormalize VectorNormalize
template<typename T>
inline void value_maximize( T& value, const T& other ){
value = std::max( value, other );
}
template<typename T>
inline void value_minimize( T& value, const T& other ){
value = std::min( value, other );
}
template<typename T> template<typename T>
struct MinMax___ struct MinMax___

View File

@ -225,10 +225,8 @@ int pk3BSPMain( int argc, char **argv ){
png = true; png = true;
} }
else if ( strEqual( argv[ i ], "-complevel" ) ) { else if ( strEqual( argv[ i ], "-complevel" ) ) {
compLevel = atoi( argv[ i + 1 ] ); compLevel = std::clamp( atoi( argv[ i + 1 ] ), -1, 10 );
i++; i++;
if ( compLevel < -1 ) compLevel = -1;
if ( compLevel > 10 ) compLevel = 10;
Sys_Printf( "Compression level set to %i\n", compLevel ); Sys_Printf( "Compression level set to %i\n", compLevel );
} }
} }
@ -777,10 +775,8 @@ int repackBSPMain( int argc, char **argv ){
analyze = true; analyze = true;
} }
else if ( strEqual( argv[ i ], "-complevel" ) ) { else if ( strEqual( argv[ i ], "-complevel" ) ) {
compLevel = atoi( argv[ i + 1 ] ); compLevel = std::clamp( atoi( argv[ i + 1 ] ), -1, 10 );
i++; i++;
if ( compLevel < -1 ) compLevel = -1;
if ( compLevel > 10 ) compLevel = 10;
Sys_Printf( "Compression level set to %i\n", compLevel ); Sys_Printf( "Compression level set to %i\n", compLevel );
} }
} }

View File

@ -804,7 +804,7 @@ bool WindingIsTiny( winding_t *w ){
for ( i = 0 ; i < w->numpoints ; i++ ) 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 ( vector3_length( w->p[j] - w->p[i] ) > EDGE_LENGTH ) {
if ( ++edges == 3 ) { if ( ++edges == 3 ) {
return false; 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++ ) for ( j = 0 ; j < w->numpoints ; j++ )
{ {
const double d = plane3_distance_to_point( plane->plane, w->p[j] ); const float d = plane3_distance_to_point( plane->plane, w->p[j] );
if ( d > 0 && d > d_front ) { if ( d > 0 ) {
d_front = d; value_maximize( d_front, d );
} }
if ( d < 0 && d < d_back ) { if ( d < 0 ) {
d_back = d; value_minimize( d_back, d );
} }
} }
} }

View File

@ -762,18 +762,12 @@ int BSPMain( int argc, char **argv ){
fakemap = true; fakemap = true;
} }
else if ( strEqual( argv[ i ], "-samplesize" ) ) { else if ( strEqual( argv[ i ], "-samplesize" ) ) {
sampleSize = atoi( argv[ i + 1 ] ); sampleSize = std::max( 1, atoi( argv[ i + 1 ] ) );
if ( sampleSize < 1 ) {
sampleSize = 1;
}
i++; i++;
Sys_Printf( "Lightmap sample size set to %dx%d units\n", sampleSize, sampleSize ); Sys_Printf( "Lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
} }
else if ( strEqual( argv[ i ], "-minsamplesize" ) ) { else if ( strEqual( argv[ i ], "-minsamplesize" ) ) {
minSampleSize = atoi( argv[ i + 1 ] ); minSampleSize = std::max( 1, atoi( argv[ i + 1 ] ) );
if ( minSampleSize < 1 ) {
minSampleSize = 1;
}
i++; i++;
Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize ); 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 ); Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
} }
else if ( strEqual( argv[ i ], "-mv" ) ) { else if ( strEqual( argv[ i ], "-mv" ) ) {
maxLMSurfaceVerts = atoi( argv[ i + 1 ] ); maxLMSurfaceVerts = std::max( 3, atoi( argv[ i + 1 ] ) );
if ( maxLMSurfaceVerts < 3 ) { value_maximize( maxSurfaceVerts, maxLMSurfaceVerts );
maxLMSurfaceVerts = 3;
}
if ( maxLMSurfaceVerts > maxSurfaceVerts ) {
maxSurfaceVerts = maxLMSurfaceVerts;
}
i++; i++;
Sys_Printf( "Maximum lightmapped surface vertex count set to %d\n", maxLMSurfaceVerts ); Sys_Printf( "Maximum lightmapped surface vertex count set to %d\n", maxLMSurfaceVerts );
} }
else if ( strEqual( argv[ i ], "-mi" ) ) { else if ( strEqual( argv[ i ], "-mi" ) ) {
maxSurfaceIndexes = atoi( argv[ i + 1 ] ); maxSurfaceIndexes = std::max( 3, atoi( argv[ i + 1 ] ) );
if ( maxSurfaceIndexes < 3 ) {
maxSurfaceIndexes = 3;
}
i++;
Sys_Printf( "Maximum per-surface index count set to %d\n", maxSurfaceIndexes ); Sys_Printf( "Maximum per-surface index count set to %d\n", maxSurfaceIndexes );
i++;
} }
else if ( strEqual( argv[ i ], "-np" ) ) { else if ( strEqual( argv[ i ], "-np" ) ) {
npDegrees = atof( argv[ i + 1 ] ); npDegrees = std::max( 0.0, atof( argv[ i + 1 ] ) );
if ( npDegrees < 0.0f ) { if ( npDegrees > 0.0f ) {
npDegrees = 0.0f;
}
else if ( npDegrees > 0.0f ) {
Sys_Printf( "Forcing nonplanar surfaces with a breaking angle of %f degrees\n", npDegrees ); Sys_Printf( "Forcing nonplanar surfaces with a breaking angle of %f degrees\n", npDegrees );
} }
i++; i++;
} }
else if ( strEqual( argv[ i ], "-snap" ) ) { else if ( strEqual( argv[ i ], "-snap" ) ) {
bevelSnap = atoi( argv[ i + 1 ] ); bevelSnap = std::max( 0, atoi( argv[ i + 1 ] ) );
if ( bevelSnap < 0 ) {
bevelSnap = 0;
}
i++;
if ( bevelSnap > 0 ) { if ( bevelSnap > 0 ) {
Sys_Printf( "Snapping brush bevel planes to %d units\n", bevelSnap ); Sys_Printf( "Snapping brush bevel planes to %d units\n", bevelSnap );
} }
i++;
} }
else if ( strEqual( argv[ i ], "-texrange" ) ) { else if ( strEqual( argv[ i ], "-texrange" ) ) {
texRange = atoi( argv[ i + 1 ] ); texRange = std::max( 0, atoi( argv[ i + 1 ] ) );
if ( texRange < 0 ) {
texRange = 0;
}
i++; i++;
Sys_Printf( "Limiting per-surface texture range to %d texels\n", texRange ); Sys_Printf( "Limiting per-surface texture range to %d texels\n", texRange );
} }
@ -869,20 +846,14 @@ int BSPMain( int argc, char **argv ){
meta = true; meta = true;
} }
else if ( strEqual( argv[ i ], "-metaadequatescore" ) ) { else if ( strEqual( argv[ i ], "-metaadequatescore" ) ) {
metaAdequateScore = atoi( argv[ i + 1 ] ); metaAdequateScore = std::max( -1, atoi( argv[ i + 1 ] ) );
if ( metaAdequateScore < 0 ) {
metaAdequateScore = -1;
}
i++; i++;
if ( metaAdequateScore >= 0 ) { if ( metaAdequateScore >= 0 ) {
Sys_Printf( "Setting ADEQUATE meta score to %d (see surface_meta.c)\n", metaAdequateScore ); Sys_Printf( "Setting ADEQUATE meta score to %d (see surface_meta.c)\n", metaAdequateScore );
} }
} }
else if ( strEqual( argv[ i ], "-metagoodscore" ) ) { else if ( strEqual( argv[ i ], "-metagoodscore" ) ) {
metaGoodScore = atoi( argv[ i + 1 ] ); metaGoodScore = std::max( -1, atoi( argv[ i + 1 ] ) );
if ( metaGoodScore < 0 ) {
metaGoodScore = -1;
}
i++; i++;
if ( metaGoodScore >= 0 ) { if ( metaGoodScore >= 0 ) {
Sys_Printf( "Setting GOOD meta score to %d (see surface_meta.c)\n", metaGoodScore ); Sys_Printf( "Setting GOOD meta score to %d (see surface_meta.c)\n", metaGoodScore );

View File

@ -115,7 +115,7 @@ static void AddLightGridLumps( FILE *file, rbspHeader_t *header ){
/* allocate temporary buffers */ /* 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 ) ); gridPoints = safe_malloc( maxGridPoints * sizeof( *gridPoints ) );
gridArray = safe_malloc( numBSPGridPoints * sizeof( *gridArray ) ); gridArray = safe_malloc( numBSPGridPoints * sizeof( *gridArray ) );

View File

@ -542,12 +542,8 @@ int ScaleBSPMain( int argc, char **argv ){
/* scale drawverts */ /* scale drawverts */
for ( i = 0; i < numBSPDrawVerts; i++ ) for ( i = 0; i < numBSPDrawVerts; i++ )
{ {
bspDrawVerts[i].xyz[0] *= scale[0]; bspDrawVerts[i].xyz *= scale;
bspDrawVerts[i].xyz[1] *= scale[1]; bspDrawVerts[i].normal /= scale;
bspDrawVerts[i].xyz[2] *= scale[2];
bspDrawVerts[i].normal[0] /= scale[0];
bspDrawVerts[i].normal[1] /= scale[1];
bspDrawVerts[i].normal[2] /= scale[2];
VectorNormalize( bspDrawVerts[i].normal ); VectorNormalize( bspDrawVerts[i].normal );
} }

View File

@ -76,13 +76,9 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t *
vert[1] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 1]]; vert[1] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 1]];
vert[2] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 2]]; 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 ( 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 ) { if ( vector3_length( vert[0]->normal - buildPlane->normal() ) >= normalEpsilon
continue; || vector3_length( vert[1]->normal - buildPlane->normal() ) >= normalEpsilon
} || vector3_length( vert[2]->normal - buildPlane->normal() ) >= normalEpsilon ) {
if ( vector3_length( vert[1]->normal - buildPlane->normal() ) >= normalEpsilon ) {
continue;
}
if ( vector3_length( vert[2]->normal - buildPlane->normal() ) >= normalEpsilon ) {
continue; continue;
} }
} }
@ -97,13 +93,9 @@ void GetBestSurfaceTriangleMatchForBrushside( side_t *buildSide, bspDrawVert_t *
} }
} }
// fixme? better distance epsilon // fixme? better distance epsilon
if ( abs( plane3_distance_to_point( buildPlane->plane, vert[0]->xyz ) ) > 1 ) { if ( abs( plane3_distance_to_point( buildPlane->plane, vert[0]->xyz ) ) > 1
continue; || abs( plane3_distance_to_point( buildPlane->plane, vert[1]->xyz ) ) > 1
} || abs( plane3_distance_to_point( buildPlane->plane, vert[2]->xyz ) ) > 1 ) {
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 ) {
continue; continue;
} }
// Okay. Correct surface type, correct shader, correct plane. Let's start with the business... // Okay. Correct surface type, correct shader, correct plane. Let's start with the business...

View File

@ -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( dp->texMat[ 0 ].vec3(), dp->texMat[ 1 ].vec3() ) ) ),
radians_to_degrees( acos( vector3_dot( axis[ 0 ], axis[ 1 ] ) ) ) ); 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->xyz[ 0 ], a->xyz[ 1 ], a->xyz[ 2 ],
a->st[ 0 ], a->st[ 1 ], 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 ] ); 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 ){ static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds, winding_t *w ){
int i, j; int i, j;
float d, d2, alpha;
winding_t *front, *back; winding_t *front, *back;
mapDrawSurface_t *ds2; mapDrawSurface_t *ds2;
bspDrawVert_t *dv; bspDrawVert_t *dv;
@ -577,15 +576,9 @@ static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds,
dv = &ds2->verts[ i ]; dv = &ds2->verts[ i ];
/* set alpha */ /* set alpha */
d = plane3_distance_to_point( dp->planes[ 0 ], w->p[ i ] ); const float d = plane3_distance_to_point( dp->planes[ 0 ], w->p[ i ] );
d2 = plane3_distance_to_point( dp->planes[ 1 ], w->p[ i ] ); const float d2 = plane3_distance_to_point( dp->planes[ 1 ], w->p[ i ] );
alpha = 255.0f * d2 / ( d + d2 ); const float alpha = std::clamp( 255.0f * d2 / ( d + d2 ), 0.f, 255.f );
if ( alpha > 255 ) {
alpha = 255;
}
else if ( alpha < 0 ) {
alpha = 0;
}
/* set misc */ /* set misc */
dv->xyz = w->p[ i ] - entityOrigin; dv->xyz = w->p[ i ] - entityOrigin;

View File

@ -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 // we use the first point of the surface, maybe something more clever would be useful
// (actually send the whole draw surface would be cool?) // (actually send the whole draw surface would be cool?)
if ( ds->numVerts >= MAX_POINTS_ON_WINDING ) { if ( ds->numVerts >= MAX_POINTS_ON_WINDING ) {
int max = ds->numVerts; const int max = std::min( ds->numVerts, 256 );
Vector3 p[256]; Vector3 p[256];
if ( max > 256 ) { for ( i = 0; i < max; i++ ) {
max = 256;
}
for ( i = 0 ; i < max ; i++ ) {
p[i] = ds->verts[i].xyz; p[i] = ds->verts[i].xyz;
} }

View File

@ -51,9 +51,7 @@ static void CreateSunLight( sun_t *sun ){
} }
/* fixup */ /* fixup */
if ( sun->numSamples < 1 ) { value_maximize( sun->numSamples, 1 );
sun->numSamples = 1;
}
/* set photons */ /* set photons */
photons = sun->photons / sun->numSamples; photons = sun->photons / sun->numSamples;
@ -344,18 +342,13 @@ void CreateEntityLights( void ){
} }
/* ydnar: get deviance and samples */ /* ydnar: get deviance and samples */
float deviance = e->floatForKey( "_deviance", "_deviation", "_jitter" ); const float deviance = std::max( 0.f, e->floatForKey( "_deviance", "_deviation", "_jitter" ) );
if ( deviance < 0.f ) const int numSamples = std::max( 1, e->intForKey( "_samples" ) );
deviance = 0.f;
int numSamples = e->intForKey( "_samples" );
if ( numSamples < 1 )
numSamples = 1;
intensity /= numSamples; intensity /= numSamples;
{ /* ydnar: get filter radius */ { /* ydnar: get filter radius */
const float filterRadius = e->floatForKey( "_filterradius", "_filteradius", "_filter" ); light->filterRadius = std::max( 0.f, e->floatForKey( "_filterradius", "_filteradius", "_filter" ) );
light->filterRadius = filterRadius < 0.f? 0.f : filterRadius;
} }
/* set light color */ /* set light color */
@ -650,7 +643,7 @@ float PointToPolygonFormFactor( const Vector3& point, const Vector3& normal, con
int i, j; int i, j;
Vector3 dirs[ MAX_POINTS_ON_WINDING ]; Vector3 dirs[ MAX_POINTS_ON_WINDING ];
float total; float total;
float dot, angle, facing; float angle, facing;
/* this is expensive */ /* this is expensive */
@ -669,18 +662,10 @@ float PointToPolygonFormFactor( const Vector3& point, const Vector3& normal, con
{ {
/* get a triangle */ /* get a triangle */
j = i + 1; 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 */ /* 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 ] ); Vector3 triNormal = vector3_cross( dirs[ i ], dirs[ j ] );
if ( VectorFastNormalize( triNormal ) < 0.0001f ) { if ( VectorFastNormalize( triNormal ) < 0.0001f ) {
@ -813,10 +798,7 @@ int LightContributionToSample( trace_t *trace ){
} }
/* clamp the distance to prevent super hot spots */ /* clamp the distance to prevent super hot spots */
dist = sqrt( dist * dist + light->extraDist * light->extraDist ); dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) );
if ( dist < 16.0f ) {
dist = 16.0f;
}
add = light->photons / ( dist * dist ) * angle; add = light->photons / ( dist * dist ) * angle;
@ -881,10 +863,7 @@ int LightContributionToSample( trace_t *trace ){
} }
/* clamp the distance to prevent super hot spots */ /* clamp the distance to prevent super hot spots */
dist = sqrt( dist * dist + light->extraDist * light->extraDist ); dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) );
if ( dist < 16.0f ) {
dist = 16.0f;
}
/* angle attenuation */ /* angle attenuation */
if ( light->flags & LightFlags::AttenAngle ) { 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) */ /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
if ( lightAngleHL ) { if ( lightAngleHL ) {
if ( dot > 0.001f ) { // skip coplanar if ( dot > 0.001f ) { // skip coplanar
if ( dot > 1.0f ) { value_minimize( dot, 1.0f );
dot = 1.0f;
}
dot = ( dot * 0.5f ) + 0.5f; dot = ( dot * 0.5f ) + 0.5f;
dot *= dot; dot *= dot;
} }
@ -921,17 +898,12 @@ int LightContributionToSample( trace_t *trace ){
if ( light->angleScale != 0.0f ) { if ( light->angleScale != 0.0f ) {
angle /= light->angleScale; angle /= light->angleScale;
if ( angle > 1.0f ) { value_minimize( angle, 1.0f );
angle = 1.0f;
}
} }
/* attenuate */ /* attenuate */
if ( light->flags & LightFlags::AttenLinear ) { if ( light->flags & LightFlags::AttenLinear ) {
add = angle * light->photons * linearScale - ( dist * light->fade ); add = std::max( 0.f, angle * light->photons * linearScale - ( dist * light->fade ) );
if ( add < 0.0f ) {
add = 0.0f;
}
if ( deluxemap ) { if ( deluxemap ) {
if ( angledDeluxe ) { if ( angledDeluxe ) {
@ -941,17 +913,12 @@ int LightContributionToSample( trace_t *trace ){
addDeluxe = light->photons * linearScale - ( dist * light->fade ); addDeluxe = light->photons * linearScale - ( dist * light->fade );
} }
if ( addDeluxe < 0.0f ) { value_maximize( addDeluxe, 0.0f );
addDeluxe = 0.0f;
}
} }
} }
else else
{ {
add = ( light->photons / ( dist * dist ) ) * angle; add = std::max( 0.f, ( light->photons / ( dist * dist ) ) * angle );
if ( add < 0.0f ) {
add = 0.0f;
}
if ( deluxemap ) { if ( deluxemap ) {
if ( angledDeluxe ) { if ( angledDeluxe ) {
@ -962,9 +929,7 @@ int LightContributionToSample( trace_t *trace ){
} }
} }
if ( addDeluxe < 0.0f ) { value_maximize( addDeluxe, 0.0f );
addDeluxe = 0.0f;
}
} }
/* handle spotlights */ /* handle spotlights */
@ -987,15 +952,10 @@ int LightContributionToSample( trace_t *trace ){
/* attenuate */ /* attenuate */
if ( sampleRadius > ( radiusAtDist - 32.0f ) ) { if ( sampleRadius > ( radiusAtDist - 32.0f ) ) {
add *= ( ( radiusAtDist - sampleRadius ) / 32.0f ); add *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
if ( add < 0.0f ) { value_maximize( add, 0.0f );
add = 0.0f;
}
addDeluxe *= ( ( radiusAtDist - sampleRadius ) / 32.0f ); addDeluxe *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
value_maximize( addDeluxe, 0.0f );
if ( addDeluxe < 0.0f ) {
addDeluxe = 0.0f;
}
} }
} }
} }
@ -1022,9 +982,7 @@ int LightContributionToSample( trace_t *trace ){
/* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */ /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
if ( lightAngleHL ) { if ( lightAngleHL ) {
if ( dot > 0.001f ) { // skip coplanar if ( dot > 0.001f ) { // skip coplanar
if ( dot > 1.0f ) { value_minimize( dot, 1.0f );
dot = 1.0f;
}
dot = ( dot * 0.5f ) + 0.5f; dot = ( dot * 0.5f ) + 0.5f;
dot *= dot; dot *= dot;
} }
@ -1050,9 +1008,7 @@ int LightContributionToSample( trace_t *trace ){
addDeluxe = light->photons; addDeluxe = light->photons;
} }
if ( addDeluxe < 0.0f ) { value_maximize( addDeluxe, 0.0f );
addDeluxe = 0.0f;
}
} }
if ( add <= 0.0f ) { if ( add <= 0.0f ) {
@ -1066,9 +1022,7 @@ int LightContributionToSample( trace_t *trace ){
if ( bouncing ) { if ( bouncing ) {
addDeluxe *= addDeluxeBounceScale; addDeluxe *= addDeluxeBounceScale;
if ( addDeluxe < 0.00390625f ) { value_maximize( addDeluxe, 0.00390625f );
addDeluxe = 0.00390625f;
}
} }
trace->directionContribution = trace->direction * addDeluxe; trace->directionContribution = trace->direction * addDeluxe;
@ -1287,10 +1241,7 @@ bool LightContributionToPoint( trace_t *trace ){
/* ptpff approximation */ /* ptpff approximation */
if ( light->type == ELightType::Area && faster ) { if ( light->type == ELightType::Area && faster ) {
/* clamp the distance to prevent super hot spots */ /* clamp the distance to prevent super hot spots */
dist = sqrt( dist * dist + light->extraDist * light->extraDist ); dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) );
if ( dist < 16.0f ) {
dist = 16.0f;
}
/* attenuate */ /* attenuate */
add = light->photons / ( dist * dist ); add = light->photons / ( dist * dist );
@ -1338,17 +1289,11 @@ bool LightContributionToPoint( trace_t *trace ){
/* point/spot lights */ /* point/spot lights */
else if ( light->type == ELightType::Point || light->type == ELightType::Spot ) { else if ( light->type == ELightType::Point || light->type == ELightType::Spot ) {
/* clamp the distance to prevent super hot spots */ /* clamp the distance to prevent super hot spots */
dist = sqrt( dist * dist + light->extraDist * light->extraDist ); dist = std::max( 16.0, sqrt( dist * dist + light->extraDist * light->extraDist ) );
if ( dist < 16.0f ) {
dist = 16.0f;
}
/* attenuate */ /* attenuate */
if ( light->flags & LightFlags::AttenLinear ) { if ( light->flags & LightFlags::AttenLinear ) {
add = light->photons * linearScale - ( dist * light->fade ); add = std::max( 0.f, light->photons * linearScale - ( dist * light->fade ) );
if ( add < 0.0f ) {
add = 0.0f;
}
} }
else{ else{
add = light->photons / ( dist * dist ); add = light->photons / ( dist * dist );
@ -1468,20 +1413,10 @@ void TraceGrid( int num ){
mod -= y * gridBounds[ 0 ]; mod -= y * gridBounds[ 0 ];
x = mod; x = mod;
trace.origin[ 0 ] = gridMins[ 0 ] + x * gridSize[ 0 ]; trace.origin = gridMins + Vector3( x, y, z ) * gridSize;
trace.origin[ 1 ] = gridMins[ 1 ] + y * gridSize[ 1 ];
trace.origin[ 2 ] = gridMins[ 2 ] + z * gridSize[ 2 ];
/* set inhibit sphere */ /* set inhibit sphere */
if ( gridSize[ 0 ] > gridSize[ 1 ] && gridSize[ 0 ] > gridSize[ 2 ] ) { trace.inhibitRadius = gridSize[ vector3_max_abs_component_index( gridSize ) ] * 0.5f;
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;
}
/* find point cluster */ /* find point cluster */
trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON ); trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON );
@ -1616,10 +1551,7 @@ void TraceGrid( int num ){
/* get relative directed strength */ /* get relative directed strength */
d = vector3_dot( contributions[ i ].dir, thisdir ); d = vector3_dot( contributions[ i ].dir, thisdir );
/* we map 1 to gridDirectionality, and 0 to gridAmbientDirectionality */ /* we map 1 to gridDirectionality, and 0 to gridAmbientDirectionality */
d = gridAmbientDirectionality + d * ( gridDirectionality - gridAmbientDirectionality ); d = std::max( 0.f, gridAmbientDirectionality + d * ( gridDirectionality - gridAmbientDirectionality ) );
if ( d < 0.0f ) {
d = 0.0f;
}
/* find appropriate style */ /* find appropriate style */
for ( j = 0; j < numStyles; j++ ) for ( j = 0; j < numStyles; j++ )
@ -1686,9 +1618,7 @@ void TraceGrid( int num ){
/* set minimum light and copy off to bytes */ /* set minimum light and copy off to bytes */
Vector3 color = gp->ambient[ i ]; Vector3 color = gp->ambient[ i ];
for ( j = 0; j < 3; j++ ) for ( j = 0; j < 3; j++ )
if ( color[ j ] < minGridLight[ j ] ) { value_maximize( color[ j ], minGridLight[ j ] );
color[ j ] = minGridLight[ j ];
}
/* vortex: apply gridscale and gridambientscale here */ /* vortex: apply gridscale and gridambientscale here */
ColorToBytes( color, bgp->ambient[ i ], gridScale * gridAmbientScale ); ColorToBytes( color, bgp->ambient[ i ], gridScale * gridAmbientScale );
@ -1740,7 +1670,7 @@ void SetupGrid( void ){
/* quantize it */ /* quantize it */
const Vector3 oldGridSize = gridSize; const Vector3 oldGridSize = gridSize;
for ( i = 0; i < 3; i++ ) 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 */ /* ydnar: increase gridSize until grid count is smaller than max allowed */
numRawGridPoints = MAX_MAP_LIGHTGRID + 1; numRawGridPoints = MAX_MAP_LIGHTGRID + 1;
@ -1858,7 +1788,7 @@ void LightWorld( bool fastAllocate ){
/* maxlight */ /* maxlight */
if ( entities[ 0 ].read_keyvalue( f, "_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 */ /* 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 ) ) { else if ( strEqual( argv[ i ], "-backsplash" ) && i < ( argc - 3 ) ) {
f = atof( argv[ i + 1 ] ); g_backsplashFractionScale = atof( argv[ i + 1 ] );
g_backsplashFractionScale = f; Sys_Printf( "Area lights backsplash fraction scaled by %f\n", g_backsplashFractionScale );
Sys_Printf( "Area lights backsplash fraction scaled by %f\n", f, g_backsplashFractionScale );
f = atof( argv[ i + 2 ] ); f = atof( argv[ i + 2 ] );
if ( f >= -900.0f ){ if ( f >= -900.0f ){
g_backsplashDistance = f; 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; i+=2;
} }
@ -2180,14 +2109,7 @@ int LightMain( int argc, char **argv ){
} }
else if ( strEqual( argv[ i ], "-bouncecolorratio" ) ) { else if ( strEqual( argv[ i ], "-bouncecolorratio" ) ) {
f = atof( argv[ i + 1 ] ); bounceColorRatio = std::clamp( atof( argv[ i + 1 ] ), 0.0, 1.0 );
bounceColorRatio *= f;
if ( bounceColorRatio > 1 ) {
bounceColorRatio = 1;
}
if ( bounceColorRatio < 0 ) {
bounceColorRatio = 0;
}
Sys_Printf( "Bounce color ratio set to %f\n", bounceColorRatio ); Sys_Printf( "Bounce color ratio set to %f\n", bounceColorRatio );
i++; i++;
} }
@ -2225,28 +2147,16 @@ int LightMain( int argc, char **argv ){
} }
else if ( strEqual( argv[ i ], "-griddirectionality" ) ) { else if ( strEqual( argv[ i ], "-griddirectionality" ) ) {
f = atof( argv[ i + 1 ] ); gridDirectionality = std::min( 1.0, atof( argv[ i + 1 ] ) );
if ( f > 1 ) { value_minimize( gridAmbientDirectionality, gridDirectionality );
f = 1; Sys_Printf( "Grid directionality is %f\n", gridDirectionality );
}
if ( f < gridAmbientDirectionality ) {
gridAmbientDirectionality = f;
}
Sys_Printf( "Grid directionality is %f\n", f );
gridDirectionality = f;
i++; i++;
} }
else if ( strEqual( argv[ i ], "-gridambientdirectionality" ) ) { else if ( strEqual( argv[ i ], "-gridambientdirectionality" ) ) {
f = atof( argv[ i + 1 ] ); gridAmbientDirectionality = std::max( -1.0, atof( argv[ i + 1 ] ) );
if ( f < -1 ) { value_maximize( gridDirectionality, gridAmbientDirectionality );
f = -1; Sys_Printf( "Grid ambient directionality is %f\n", gridAmbientDirectionality );
}
if ( f > gridDirectionality ) {
gridDirectionality = f;
}
Sys_Printf( "Grid ambient directionality is %f\n", f );
gridAmbientDirectionality = f;
i++; i++;
} }
@ -2331,8 +2241,7 @@ int LightMain( int argc, char **argv ){
/* Lighting contrast */ /* Lighting contrast */
else if( strEqual( argv[ i ], "-contrast" ) ){ else if( strEqual( argv[ i ], "-contrast" ) ){
f = atof( argv[ i + 1 ] ); lightmapContrast = std::clamp( atof( argv[ i + 1 ] ), -255.0, 255.0 );
lightmapContrast = f > 255? 255 : f < -255? -255 : f;
Sys_Printf( "Lighting contrast set to %f\n", lightmapContrast ); Sys_Printf( "Lighting contrast set to %f\n", lightmapContrast );
i++; i++;
/* change to factor in range of 0 to 129.5 */ /* change to factor in range of 0 to 129.5 */
@ -2341,22 +2250,16 @@ int LightMain( int argc, char **argv ){
/* ydnar switches */ /* ydnar switches */
else if ( strEqual( argv[ i ], "-bounce" ) ) { else if ( strEqual( argv[ i ], "-bounce" ) ) {
bounce = atoi( argv[ i + 1 ] ); bounce = std::max( 0, atoi( argv[ i + 1 ] ) );
if ( bounce < 0 ) { if ( bounce > 0 ) {
bounce = 0;
}
else if ( bounce > 0 ) {
Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce ); Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce );
} }
i++; i++;
} }
else if ( strEqual( argv[ i ], "-supersample" ) || strEqual( argv[ i ], "-super" ) ) { else if ( strEqual( argv[ i ], "-supersample" ) || strEqual( argv[ i ], "-super" ) ) {
superSample = atoi( argv[ i + 1 ] ); superSample = std::max( 1, atoi( argv[ i + 1 ] ) );
if ( superSample < 1 ) { if ( superSample > 1 ) {
superSample = 1;
}
else if ( superSample > 1 ) {
Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", ( superSample * superSample ) ); Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", ( superSample * superSample ) );
} }
i++; i++;
@ -2369,22 +2272,15 @@ int LightMain( int argc, char **argv ){
else if ( strEqual( argv[ i ], "-samples" ) ) { else if ( strEqual( argv[ i ], "-samples" ) ) {
lightSamplesInsist = ( *argv[i + 1] == '+' ); lightSamplesInsist = ( *argv[i + 1] == '+' );
lightSamples = atoi( argv[ i + 1 ] ); lightSamples = std::max( 1, atoi( argv[ i + 1 ] ) );
if ( lightSamples < 1 ) { if ( lightSamples > 1 ) {
lightSamples = 1;
}
else if ( lightSamples > 1 ) {
Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples ); Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples );
} }
i++; i++;
} }
else if ( strEqual( argv[ i ], "-samplessearchboxsize" ) ) { else if ( strEqual( argv[ i ], "-samplessearchboxsize" ) ) {
lightSamplesSearchBoxSize = atoi( argv[ i + 1 ] ); lightSamplesSearchBoxSize = std::clamp( atoi( argv[ i + 1 ] ), 1, 4 ); /* more makes no sense */
// lightSamplesSearchBoxSize = MAX( MIN( lightSamplesSearchBoxSize, 4 ), 1 );
lightSamplesSearchBoxSize = lightSamplesSearchBoxSize < 1 ? 1
: lightSamplesSearchBoxSize > 4 ? 4 /* more makes no sense */
: lightSamplesSearchBoxSize;
if ( lightSamplesSearchBoxSize != 1 ) if ( lightSamplesSearchBoxSize != 1 )
Sys_Printf( "Adaptive supersampling uses %f times the normal search box size\n", lightSamplesSearchBoxSize ); Sys_Printf( "Adaptive supersampling uses %f times the normal search box size\n", lightSamplesSearchBoxSize );
i++; i++;
@ -2401,11 +2297,8 @@ int LightMain( int argc, char **argv ){
} }
else if ( strEqual( argv[ i ], "-shadeangle" ) ) { else if ( strEqual( argv[ i ], "-shadeangle" ) ) {
shadeAngleDegrees = atof( argv[ i + 1 ] ); shadeAngleDegrees = std::max( 0.0, atof( argv[ i + 1 ] ) );
if ( shadeAngleDegrees < 0.0f ) { if ( shadeAngleDegrees > 0.0f ) {
shadeAngleDegrees = 0.0f;
}
else if ( shadeAngleDegrees > 0.0f ) {
shade = true; shade = true;
Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees ); 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" ) ) { else if ( strEqual( argv[ i ], "-approx" ) ) {
approximateTolerance = atoi( argv[ i + 1 ] ); approximateTolerance = std::max( 0, atoi( argv[ i + 1 ] ) );
if ( approximateTolerance < 0 ) { if ( approximateTolerance > 0 ) {
approximateTolerance = 0;
}
else if ( approximateTolerance > 0 ) {
Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance ); Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance );
} }
i++; i++;
@ -2439,7 +2329,7 @@ int LightMain( int argc, char **argv ){
} }
else if ( strEqual( argv[ i ], "-deluxemode" ) ) { else if ( strEqual( argv[ i ], "-deluxemode" ) ) {
deluxemode = atoi( argv[ i + 1 ] ); deluxemode = atoi( argv[ i + 1 ] );
if ( deluxemode == 0 || deluxemode > 1 || deluxemode < 0 ) { if ( deluxemode != 1 ) {
Sys_Printf( "Generating modelspace deluxemaps\n" ); Sys_Printf( "Generating modelspace deluxemaps\n" );
deluxemode = 0; deluxemode = 0;
} }
@ -2518,10 +2408,7 @@ int LightMain( int argc, char **argv ){
} }
else if ( strEqual( argv[ i ], "-extradist" ) ) { else if ( strEqual( argv[ i ], "-extradist" ) ) {
extraDist = atof( argv[ i + 1 ] ); extraDist = std::max( 0.0, atof( argv[ i + 1 ] ) );
if ( extraDist < 0 ) {
extraDist = 0;
}
i++; i++;
Sys_Printf( "Default extra radius set to %f units\n", extraDist ); 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" ); Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n" );
} }
else if ( strEqual( argv[ i ], "-samplesize" ) ) { else if ( strEqual( argv[ i ], "-samplesize" ) ) {
sampleSize = atoi( argv[ i + 1 ] ); sampleSize = std::max( 1, atoi( argv[ i + 1 ] ) );
if ( sampleSize < 1 ) {
sampleSize = 1;
}
i++; i++;
Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize ); Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
} }
else if ( strEqual( argv[ i ], "-minsamplesize" ) ) { else if ( strEqual( argv[ i ], "-minsamplesize" ) ) {
minSampleSize = atoi( argv[ i + 1 ] ); minSampleSize = std::max( 1, atoi( argv[ i + 1 ] ) );
if ( minSampleSize < 1 ) {
minSampleSize = 1;
}
i++; i++;
Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize ); 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" ) ) { else if ( strEqual( argv[ i ], "-novertex" ) ) {
noVertexLighting = 1; noVertexLighting = 1;
if ( ( atof( argv[ i + 1 ] ) != 0 ) && ( atof( argv[ i + 1 ] )) < 1 ) { f = atof( argv[ i + 1 ] );
noVertexLighting = ( atof( argv[ i + 1 ] ) ); if ( f != 0 && f < 1 ) {
noVertexLighting = f;
i++; i++;
Sys_Printf( "Setting vertex lighting globally to %f\n", noVertexLighting ); 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 */ /* fix up lightmap search power */
if ( lightmapMergeSize ) { if ( lightmapMergeSize ) {
lightmapSearchBlockSize = ( lightmapMergeSize / lmCustomSizeW ) * ( lightmapMergeSize / lmCustomSizeW ); //? should use min or max( lmCustomSizeW, lmCustomSizeH )? :thinking: lightmapSearchBlockSize = std::max( 1, ( lightmapMergeSize / lmCustomSizeW ) * ( lightmapMergeSize / lmCustomSizeW ) ); //? should use min or max( lmCustomSizeW, lmCustomSizeH )? :thinking:
if ( lightmapSearchBlockSize < 1 ) {
lightmapSearchBlockSize = 1;
}
Sys_Printf( "Restricted lightmap searching enabled - block size adjusted to %d\n", lightmapSearchBlockSize ); Sys_Printf( "Restricted lightmap searching enabled - block size adjusted to %d\n", lightmapSearchBlockSize );
} }

View File

@ -232,7 +232,7 @@ bool RadSampleImage( byte *pixels, int width, int height, const Vector2& st, Col
#define SAMPLE_GRANULARITY 6 #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 ){ 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; Vector3 color;
MinMax minmax; MinMax minmax;
Color4f textureColor; Color4f textureColor;
@ -313,20 +313,8 @@ static void RadSample( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm,
} }
/* get lightmap xy coords */ /* get lightmap xy coords */
x = lightmap[ 0 ] / (float) superSample; const int x = std::clamp( int( lightmap[ 0 ] / superSample ), 0, lm->w - 1 );
y = lightmap[ 1 ] / (float) superSample; const int y = std::clamp( int( lightmap[ 1 ] / superSample ), 0, lm->h - 1 );
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;
}
/* get radiosity luxel */ /* get radiosity luxel */
const Vector3& radLuxel = lm->getRadLuxel( lightmapNum, x, y ); const Vector3& radLuxel = lm->getRadLuxel( lightmapNum, x, y );

View File

@ -638,9 +638,7 @@ static void SubdivideTraceNode_r( int nodeNum, int depth ){
} }
/* set max trace depth */ /* set max trace depth */
if ( depth > maxTraceDepth ) { value_maximize( maxTraceDepth, depth );
maxTraceDepth = depth;
}
/* snap the average */ /* snap the average */
dist = floor( average[ type ] / count ); 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 ]; t = w * tt->v[ 0 ].st[ 1 ] + u * tt->v[ 1 ].st[ 1 ] + v * tt->v[ 2 ].st[ 1 ];
s = s - floor( s ); s = s - floor( s );
t = t - floor( t ); t = t - floor( t );
is = s * si->lightImage->width; is = std::clamp( int( s * si->lightImage->width ), 0, si->lightImage->width - 1 );
it = t * si->lightImage->height; it = std::clamp( int( t * si->lightImage->height ), 0, si->lightImage->height - 1 );
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;
}
/* get pixel */ /* get pixel */
pixel = si->lightImage->pixels + 4 * ( it * si->lightImage->width + is ); pixel = si->lightImage->pixels + 4 * ( it * si->lightImage->width + is );

View File

@ -101,10 +101,7 @@ void ColorToBytes( const Vector3& color, Vector3b& colorBytes, float scale ){
/* contrast */ /* contrast */
if ( lightmapContrast != 1.0f ){ if ( lightmapContrast != 1.0f ){
for ( i = 0; i < 3; i++ ){ for ( i = 0; i < 3; i++ ){
sample[i] = lightmapContrast * ( sample[i] - 128 ) + 128; sample[i] = std::max( 0.f, lightmapContrast * ( sample[i] - 128 ) + 128 );
if ( sample[i] < 0 ){
sample[i] = 0;
}
} }
/* clamp with color normalization */ /* clamp with color normalization */
max = VectorMax( sample ); max = VectorMax( sample );
@ -143,7 +140,7 @@ void ColorToBytes( const Vector3& color, Vector3b& colorBytes, float scale ){
void SmoothNormals( void ){ void SmoothNormals( void ){
int i, j, k, f, numVerts, numVotes, fOld, start; int i, j, k, f, numVerts, numVotes, fOld, start;
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle; float shadeAngle, defaultShadeAngle, maxShadeAngle;
bspDrawSurface_t *ds; bspDrawSurface_t *ds;
shaderInfo_t *si; shaderInfo_t *si;
float *shadeAngles; float *shadeAngles;
@ -178,9 +175,7 @@ void SmoothNormals( void ){
else{ else{
shadeAngle = defaultShadeAngle; shadeAngle = defaultShadeAngle;
} }
if ( shadeAngle > maxShadeAngle ) { value_maximize( maxShadeAngle, shadeAngle );
maxShadeAngle = shadeAngle;
}
/* flag its verts */ /* flag its verts */
for ( j = 0; j < ds->numVerts; j++ ) for ( j = 0; j < ds->numVerts; j++ )
@ -244,18 +239,11 @@ void SmoothNormals( void ){
} }
/* use smallest shade angle */ /* use smallest shade angle */
shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] ); shadeAngle = std::min( shadeAngles[ i ], shadeAngles[ j ] );
/* check shade angle */ /* check shade angle */
dot = vector3_dot( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal ); const double dot = std::clamp( vector3_dot( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal ), -1.0, 1.0 );
if ( dot > 1.0 ) { if ( acos( dot ) + THETA_EPSILON >= shadeAngle ) {
dot = 1.0;
}
else if ( dot < -1.0 ) {
dot = -1.0;
}
testAngle = acos( dot ) + THETA_EPSILON;
if ( testAngle >= shadeAngle ) {
//Sys_Printf( "F(%3.3f >= %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) ); //Sys_Printf( "F(%3.3f >= %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) );
continue; continue;
} }
@ -410,7 +398,7 @@ static void PerturbNormal( bspDrawVert_t *dv, shaderInfo_t *si, Vector3& pNormal
#define BOGUS_NUDGE -99999.0f #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 ] ){ 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; float lightmapSampleOffset;
shaderInfo_t *si; shaderInfo_t *si;
Vector3 pNormal; 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?) */ /* find luxel xy coords (fixme: subtract 0.5?) */
x = dv->lightmap[ 0 ][ 0 ]; const int x = std::clamp( int( dv->lightmap[ 0 ][ 0 ] ), 0, lm->sw - 1 );
y = dv->lightmap[ 0 ][ 1 ]; const int y = std::clamp( int( dv->lightmap[ 0 ][ 1 ] ), 0, lm->sh - 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;
}
/* set shader and cluster list */ /* set shader and cluster list */
if ( info != NULL ) { if ( info != NULL ) {
@ -1153,9 +1129,7 @@ void MapRawLightmap( int rawLightmapNum ){
----------------------------------------------------------------- */ ----------------------------------------------------------------- */
/* walk the luxels */ /* walk the luxels */
radius = floor( superSample / 2 ); radius = std::max( 1, superSample / 2 ) + 1;
radius = radius > 0 ? radius : 1.0f;
radius += 1.0f;
for ( pass = 2.0f; pass <= radius; pass += 1.0f ) for ( pass = 2.0f; pass <= radius; pass += 1.0f )
{ {
for ( y = 0; y < lm->sh; y++ ) for ( y = 0; y < lm->sh; y++ )
@ -1443,16 +1417,11 @@ float DirtForSample( trace_t *trace ){
} }
/* apply gain (does this even do much? heh) */ /* apply gain (does this even do much? heh) */
outDirt = pow( gatherDirt / ( numDirtVectors + 1 ), dirtGain ); outDirt = std::min( 1.0, pow( gatherDirt / ( numDirtVectors + 1 ), dirtGain ) );
if ( outDirt > 1.0f ) {
outDirt = 1.0f;
}
/* apply scale */ /* apply scale */
outDirt *= dirtScale; outDirt *= dirtScale;
if ( outDirt > 1.0f ) { value_minimize( outDirt, 1.0f );
outDirt = 1.0f;
}
/* return to sender */ /* return to sender */
return 1.0f - outDirt; return 1.0f - outDirt;
@ -1863,7 +1832,6 @@ void IlluminateRawLightmap( int rawLightmapNum ){
rawLightmap_t *lm; rawLightmap_t *lm;
surfaceInfo_t *info; surfaceInfo_t *info;
bool filterColor, filterDir; bool filterColor, filterDir;
float brightness;
float samples, filterRadius, weight; float samples, filterRadius, weight;
Vector3 averageColor, averageDir; Vector3 averageColor, averageDir;
float tests[ 4 ][ 2 ] = { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }; float tests[ 4 ][ 2 ] = { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } };
@ -2005,12 +1973,8 @@ void IlluminateRawLightmap( int rawLightmapNum ){
{ {
luxel.value = ambientColor; luxel.value = ambientColor;
if ( deluxemap ) { 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 // use AT LEAST this amount of contribution from ambient for the deluxemap, fixes points that receive ZERO light
if ( brightness < 0.00390625f ) { const float brightness = std::max( 0.00390625f, RGBTOGRAY( ambientColor ) * ( 1.0f / 255.0f ) );
brightness = 0.00390625f;
}
lm->getSuperDeluxel( x, y ) = lm->getSuperNormal( x, y ) * brightness; lm->getSuperDeluxel( x, y ) = lm->getSuperNormal( x, y ) * brightness;
} }
@ -2061,12 +2025,7 @@ void IlluminateRawLightmap( int rawLightmapNum ){
totalLighted = 0; totalLighted = 0;
/* determine filter radius */ /* determine filter radius */
filterRadius = lm->filterRadius > trace.light->filterRadius filterRadius = std::max( { 0.f, lm->filterRadius, trace.light->filterRadius } );
? lm->filterRadius
: trace.light->filterRadius;
if ( filterRadius < 0.0f ) {
filterRadius = 0.0f;
}
/* set luxel filter radius */ /* set luxel filter radius */
luxelFilterRadius = lm->sampleSize != 0 ? superSample * filterRadius / lm->sampleSize : 0; 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 ); z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 );
/* nudge origin */ /* nudge origin */
trace.origin[ 0 ] = verts[ i ].xyz[ 0 ] + ( VERTEX_NUDGE * x1 ); trace.origin = verts[ i ].xyz + Vector3( x1, y1, z1 ) * Vector3().set( VERTEX_NUDGE );
trace.origin[ 1 ] = verts[ i ].xyz[ 1 ] + ( VERTEX_NUDGE * y1 );
trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + ( VERTEX_NUDGE * z1 );
/* try at nudged origin */ /* try at nudged origin */
trace.cluster = ClusterForPointExtFilter( origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] ); 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 ]; ds->vertexStyles[ lightmapNum ] = lm->styles[ lightmapNum ];
/* get max search radius */ /* get max search radius */
maxRadius = lm->sw; maxRadius = std::max( lm->sw, lm->sh );
maxRadius = maxRadius > lm->sh ? maxRadius : lm->sh;
/* walk the surface verts */ /* walk the surface verts */
verts = yDrawVerts + ds->firstVert; verts = yDrawVerts + ds->firstVert;
@ -2878,20 +2834,8 @@ void IlluminateVertexes( int num ){
} }
/* get luxel coords */ /* get luxel coords */
x = verts[ i ].lightmap[ lightmapNum ][ 0 ]; x = std::clamp( int( verts[ i ].lightmap[ lightmapNum ][ 0 ] ), 0, lm->sw - 1 );
y = verts[ i ].lightmap[ lightmapNum ][ 1 ]; y = std::clamp( int( verts[ i ].lightmap[ lightmapNum ][ 1 ] ), 0, lm->sh - 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;
}
/* get vertex luxels */ /* get vertex luxels */
Vector3& vertLuxel = getVertexLuxel( lightmapNum, ds->firstVert + i ); Vector3& vertLuxel = getVertexLuxel( lightmapNum, ds->firstVert + i );
@ -3489,13 +3433,8 @@ void SetupEnvelopes( bool forGrid, bool fastFlag ){
const Vector3 dir = -light->normal; const Vector3 dir = -light->normal;
for ( radius = 100.0f; radius < MAX_WORLD_COORD * 8.0f; radius += 10.0f ) for ( radius = 100.0f; radius < MAX_WORLD_COORD * 8.0f; radius += 10.0f )
{ {
float factor;
origin = light->origin + light->normal * radius; origin = light->origin + light->normal * radius;
factor = PointToPolygonFormFactor( origin, dir, light->w ); const float factor = std::abs( PointToPolygonFormFactor( origin, dir, light->w ) );
if ( factor < 0.0f ) {
factor *= -1.0f;
}
if ( ( factor * light->add ) <= light->falloffTolerance ) { if ( ( factor * light->add ) <= light->falloffTolerance ) {
light->envelope = radius; light->envelope = radius;
break; break;
@ -3608,12 +3547,9 @@ void SetupEnvelopes( bool forGrid, bool fastFlag ){
radius = vector3_length( minmax.maxs - light->origin ); radius = vector3_length( minmax.maxs - light->origin );
/* if this radius is smaller than the envelope, then set the envelope to it */ /* if this radius is smaller than the envelope, then set the envelope to it */
if ( radius < light->envelope ) { //% if ( radius < light->envelope ) Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights );
light->envelope = radius; //% else Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope );
//% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights ); value_minimize( light->envelope, radius );
}
//% else
//% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope );
} }
/* add grid/surface only check */ /* add grid/surface only check */
@ -3944,7 +3880,6 @@ void SetupFloodLight( void ){
float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodLightLowQuality ){ float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodLightLowQuality ){
int i; int i;
float contribution; float contribution;
int sub = 0;
float gatherLight, outLight; float gatherLight, outLight;
Vector3 myUp, myRt; Vector3 myUp, myRt;
int vecs = 0; int vecs = 0;
@ -4016,10 +3951,7 @@ float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodL
// d=trace->distance; // d=trace->distance;
//if (d>256) gatherDirt+=1; //if (d>256) gatherDirt+=1;
contribution = d / dd; contribution = std::min( 1.f, d / dd );
if ( contribution > 1 ) {
contribution = 1.0f;
}
//gatherDirt += 1.0f - ooDepth * VectorLength( displacement ); //gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
} }
@ -4033,17 +3965,9 @@ float FloodLightForSample( trace_t *trace, float floodLightDistance, bool floodL
return 0.0f; return 0.0f;
} }
sub = vecs; gatherLight /= std::max( 1, vecs );
if ( sub < 1 ) { outLight = std::min( 1.f, gatherLight );
sub = 1;
}
gatherLight /= ( sub );
outLight = gatherLight;
if ( outLight > 1.0f ) {
outLight = 1.0f;
}
/* return to sender */ /* return to sender */
return outLight; return outLight;
@ -4256,12 +4180,8 @@ void FloodlightIlluminateLightmap( rawLightmap_t *lm ){
/* add to deluxemap */ /* add to deluxemap */
if ( deluxemap && floodlight.scale > 0 ) { 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 // use AT LEAST this amount of contribution from ambient for the deluxemap, fixes points that receive ZERO light
if ( brightness < 0.00390625f ) { const float brightness = std::max( 0.00390625f, RGBTOGRAY( floodlight.value ) * ( 1.0f / 255.0f ) * floodlight.scale );
brightness = 0.00390625f;
}
const Vector3 lightvector = lm->getSuperNormal( x, y ) * brightness; const Vector3 lightvector = lm->getSuperNormal( x, y ) * brightness;
lm->getSuperDeluxel( x, y ) += lightvector; lm->getSuperDeluxel( x, y ) += lightvector;

View File

@ -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
===================
*/

View File

@ -493,20 +493,14 @@ bool AddPatchToRawLightmap( int num, rawLightmap_t *lm ){
if ( x + 1 < mesh->width ) { if ( x + 1 < mesh->width ) {
a = &verts[ ( y * mesh->width ) + x ]; a = &verts[ ( y * mesh->width ) + x ];
b = &verts[ ( y * mesh->width ) + x + 1 ]; b = &verts[ ( y * mesh->width ) + x + 1 ];
length = vector3_length( a->xyz - b->xyz ); value_maximize( widthTable[ x ], (float)vector3_length( a->xyz - b->xyz ) );
if ( length > widthTable[ x ] ) {
widthTable[ x ] = length;
}
} }
/* get height */ /* get height */
if ( y + 1 < mesh->height ) { if ( y + 1 < mesh->height ) {
a = &verts[ ( y * mesh->width ) + x ]; a = &verts[ ( y * mesh->width ) + x ];
b = &verts[ ( ( y + 1 ) * mesh->width ) + x ]; b = &verts[ ( ( y + 1 ) * mesh->width ) + x ];
length = vector3_length( a->xyz - b->xyz ); value_maximize( heightTable[ y ], (float)vector3_length( a->xyz - b->xyz ) );
if ( length > heightTable[ y ] ) {
heightTable[ y ] = length;
}
} }
} }
} }
@ -516,12 +510,8 @@ bool AddPatchToRawLightmap( int num, rawLightmap_t *lm ){
for ( x = 0; x < ( mesh->width - 1 ); x++ ) for ( x = 0; x < ( mesh->width - 1 ); x++ )
length += widthTable[ x ]; length += widthTable[ x ];
lm->w = lm->sampleSize != 0 ? ceil( length / lm->sampleSize ) + 1 : 0; lm->w = lm->sampleSize != 0 ? ceil( length / lm->sampleSize ) + 1 : 0;
if ( lm->w < ds->patchWidth ) { value_maximize( lm->w, ds->patchWidth );
lm->w = ds->patchWidth; value_minimize( lm->w, lm->customWidth );
}
if ( lm->w > lm->customWidth ) {
lm->w = lm->customWidth;
}
sBasis = (float) ( lm->w - 1 ) / (float) ( ds->patchWidth - 1 ); sBasis = (float) ( lm->w - 1 ) / (float) ( ds->patchWidth - 1 );
/* determine lightmap height */ /* determine lightmap height */
@ -529,12 +519,8 @@ bool AddPatchToRawLightmap( int num, rawLightmap_t *lm ){
for ( y = 0; y < ( mesh->height - 1 ); y++ ) for ( y = 0; y < ( mesh->height - 1 ); y++ )
length += heightTable[ y ]; length += heightTable[ y ];
lm->h = lm->sampleSize != 0 ? ceil( length / lm->sampleSize ) + 1 : 0; lm->h = lm->sampleSize != 0 ? ceil( length / lm->sampleSize ) + 1 : 0;
if ( lm->h < ds->patchHeight ) { value_maximize( lm->h, ds->patchHeight );
lm->h = ds->patchHeight; value_minimize( lm->h, lm->customHeight );
}
if ( lm->h > lm->customHeight ) {
lm->h = lm->customHeight;
}
tBasis = (float) ( lm->h - 1 ) / (float) ( ds->patchHeight - 1 ); tBasis = (float) ( lm->h - 1 ) / (float) ( ds->patchHeight - 1 );
/* free the temporary mesh */ /* free the temporary mesh */
@ -967,10 +953,8 @@ void SetupSurfaceLightmaps( void ){
Sys_FPrintf( SYS_VRB, "--- SetupSurfaceLightmaps ---\n" ); Sys_FPrintf( SYS_VRB, "--- SetupSurfaceLightmaps ---\n" );
/* determine supersample amount */ /* determine supersample amount */
if ( superSample < 1 ) { value_maximize( superSample, 1 );
superSample = 1; if ( superSample > 8 ) {
}
else if ( superSample > 8 ) {
Sys_Warning( "Insane supersampling amount (%d) detected.\n", superSample ); Sys_Warning( "Insane supersampling amount (%d) detected.\n", superSample );
superSample = 8; superSample = 8;
} }
@ -1295,9 +1279,7 @@ void StitchSurfaceLightmaps( void ){
sampleSize = 0.5f * std::min( a->actualSampleSize, b->actualSampleSize ); sampleSize = 0.5f * std::min( a->actualSampleSize, b->actualSampleSize );
/* test bounding box */ /* test bounding box */
if ( origin[ 0 ] < ( b->minmax.mins[ 0 ] - sampleSize ) || ( origin[ 0 ] > b->minmax.maxs[ 0 ] + sampleSize ) || if ( !MinMax( b->minmax.mins - Vector3().set( sampleSize ), b->minmax.maxs + Vector3().set( sampleSize ) ).test( origin ) ) {
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 ) ) {
continue; 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 ){ static bool ApproximateLuxel( rawLightmap_t *lm, bspDrawVert_t *dv ){
int i, x, y, d, lightmapNum;
/* find luxel xy coords */ /* find luxel xy coords */
x = dv->lightmap[ 0 ][ 0 ] / superSample; const int x = std::clamp( int( dv->lightmap[ 0 ][ 0 ] / superSample ), 0, lm->w - 1 );
y = dv->lightmap[ 0 ][ 1 ] / superSample; const int y = std::clamp( int( dv->lightmap[ 0 ][ 1 ] / superSample ), 0, lm->h - 1 );
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;
}
/* walk list */ /* walk list */
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) for ( int lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
{ {
/* early out */ /* early out */
if ( lm->styles[ lightmapNum ] == LS_NONE ) { 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 */ /* styles are not affected by minlight */
if ( lightmapNum == 0 ) { if ( lightmapNum == 0 ) {
for ( i = 0; i < 3; i++ ) for ( int i = 0; i < 3; i++ )
{ {
/* set min color */ /* set min color */
if ( color[ i ] < minLight[ i ] ) { value_maximize( color[ i ], minLight[ i ] );
color[ i ] = minLight[ i ]; value_maximize( vertexColor[ i ], minLight[ i ] ); /* note NOT minVertexLight */
}
if ( vertexColor[ i ] < minLight[ i ] ) { /* note NOT minVertexLight */
vertexColor[ i ] = minLight[ i ];
}
} }
} }
@ -1600,13 +1563,9 @@ static bool ApproximateLuxel( rawLightmap_t *lm, bspDrawVert_t *dv ){
ColorToBytes( vertexColor, vcb.rgb(), 1.0f ); ColorToBytes( vertexColor, vcb.rgb(), 1.0f );
/* compare */ /* compare */
for ( i = 0; i < 3; i++ ) for ( int i = 0; i < 3; i++ )
{ {
d = cb[ i ] - vcb[ i ]; if ( abs( cb[ i ] - vcb[ i ] ) > approximateTolerance ) {
if ( d < 0 ) {
d *= -1;
}
if ( d > approximateTolerance ) {
return false; 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 fast allocation, do not test allocation on every pixels, especially for large lightmaps */
if ( fastAllocate ) { if ( fastAllocate ) {
xIncrement = MAX(1, lm->w / 15); xIncrement = std::max( 1, lm->w / 15 );
yIncrement = MAX(1, lm->h / 15); yIncrement = std::max( 1, lm->h / 15 );
} }
else { else {
xIncrement = 1; xIncrement = 1;
@ -2217,9 +2176,7 @@ static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){
if ( lightmapNum == 0 ) { if ( lightmapNum == 0 ) {
for ( i = 0; i < 3; i++ ) for ( i = 0; i < 3; i++ )
{ {
if ( color[ i ] < minLight[ i ] ) { value_maximize( color[ i ], minLight[ i ] );
color[ i ] = minLight[ i ];
}
} }
} }
@ -2264,7 +2221,7 @@ static int CompareRawLightmap( const void *a, const void *b ){
blm = &rawLightmaps[ *( (const int*) b ) ]; blm = &rawLightmaps[ *( (const int*) b ) ];
/* get min number of surfaces */ /* 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 //#define allocate_bigger_first
#ifdef allocate_bigger_first #ifdef allocate_bigger_first
@ -2644,9 +2601,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){
/* fix negative samples */ /* fix negative samples */
for ( j = 0; j < 3; j++ ) for ( j = 0; j < 3; j++ )
{ {
if ( sample[ j ] < 0.0f ) { value_maximize( sample[ j ], 0.0f );
sample[ j ] = 0.0f;
}
} }
} }
else else
@ -2692,9 +2647,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){
/* fix negative samples */ /* fix negative samples */
for ( j = 0; j < 3; j++ ) for ( j = 0; j < 3; j++ )
{ {
if ( sample[ j ] < 0.0f ) { value_maximize( sample[ j ], 0.0f );
sample[ j ] = 0.0f;
}
} }
} }
} }
@ -3280,9 +3233,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){
/* set minimum light */ /* set minimum light */
if ( lightmapNum == 0 ) { if ( lightmapNum == 0 ) {
for ( k = 0; k < 3; k++ ) for ( k = 0; k < 3; k++ )
if ( color[ k ] < minVertexLight[ k ] ) { value_maximize( color[ k ], minVertexLight[ k ] );
color[ k ] = minVertexLight[ k ];
}
} }
} }

View File

@ -745,9 +745,7 @@ void AddBrushBevels( void ){
if ( d > 0.1f ) { if ( d > 0.1f ) {
break; // point in front break; // point in front
} }
if ( d < minBack ) { value_minimize( minBack, d );
minBack = d;
}
} }
// if some point was at the front // if some point was at the front
if ( l != w2->numpoints ) { if ( l != w2->numpoints ) {
@ -1699,10 +1697,8 @@ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){
GetEntityShadowFlags( mapEnt, NULL, &castShadows, &recvShadows ); GetEntityShadowFlags( mapEnt, NULL, &castShadows, &recvShadows );
/* ydnar: get lightmap scaling value for this entity */ /* ydnar: get lightmap scaling value for this entity */
float lightmapScale = mapEnt->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ); const float lightmapScale = std::max( 0.f, mapEnt->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ) );
if ( lightmapScale < 0.0f ) if ( lightmapScale != 0 )
lightmapScale = 0.0f;
else if ( lightmapScale > 0.0f )
Sys_Printf( "Entity %d (%s) has lightmap scale of %.4f\n", mapEnt->mapEntityNum, classname, lightmapScale ); Sys_Printf( "Entity %d (%s) has lightmap scale of %.4f\n", mapEnt->mapEntityNum, classname, lightmapScale );
/* ydnar: get cel shader :) for this entity */ /* ydnar: get cel shader :) for this entity */
@ -1718,18 +1714,14 @@ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){
} }
/* jal : entity based _shadeangle */ /* jal : entity based _shadeangle */
float shadeAngle = mapEnt->floatForKey( "_shadeangle", const float shadeAngle = std::max( 0.f, mapEnt->floatForKey( "_shadeangle",
"_smoothnormals", "_sn", "_sa", "_smooth" ); /* vortex' aliases */ "_smoothnormals", "_sn", "_sa", "_smooth" ) ); /* vortex' aliases */
if ( shadeAngle < 0.0f ) if ( shadeAngle != 0 )
shadeAngle = 0.0f;
else if ( shadeAngle > 0.0f )
Sys_Printf( "Entity %d (%s) has shading angle of %.4f\n", mapEnt->mapEntityNum, classname, shadeAngle ); Sys_Printf( "Entity %d (%s) has shading angle of %.4f\n", mapEnt->mapEntityNum, classname, shadeAngle );
/* jal : entity based _samplesize */ /* jal : entity based _samplesize */
int lightmapSampleSize = mapEnt->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ); const int lightmapSampleSize = std::max( 0, mapEnt->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ) );
if ( lightmapSampleSize < 0 ) if ( lightmapSampleSize != 0 )
lightmapSampleSize = 0;
else if ( lightmapSampleSize > 0 )
Sys_Printf( "Entity %d (%s) has lightmap sample size of %d\n", mapEnt->mapEntityNum, classname, lightmapSampleSize ); Sys_Printf( "Entity %d (%s) has lightmap sample size of %d\n", mapEnt->mapEntityNum, classname, lightmapSampleSize );
/* attach stuff to everything in the entity */ /* attach stuff to everything in the entity */

View File

@ -581,7 +581,6 @@ Vector3 ProjectPointOntoVector( const Vector3& point, const Vector3& vStart, con
*/ */
mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) { mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) {
int i, j, k; int i, j, k;
float len, maxLength;
mesh_t out; mesh_t out;
bspDrawVert_t expand[MAX_EXPANDED_AXIS][MAX_EXPANDED_AXIS]; 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++ ) { for ( j = 1 ; j < out.width - 1; j++ ) {
maxLength = 0; double maxLength = 0;
for ( i = 0 ; i < out.height ; i++ ) { 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 ) ); 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 ( len > maxLength ) {
maxLength = len;
}
} }
if ( maxLength < 0.1 ) { if ( maxLength < 0.1 ) {
out.width--; out.width--;
@ -615,12 +611,9 @@ mesh_t *RemoveLinearMeshColumnsRows( mesh_t *in ) {
} }
} }
for ( j = 1 ; j < out.height - 1; j++ ) { for ( j = 1 ; j < out.height - 1; j++ ) {
maxLength = 0; double maxLength = 0;
for ( i = 0 ; i < out.width ; i++ ) { 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 ) ); 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 ( len > maxLength ) {
maxLength = len;
}
} }
if ( maxLength < 0.1 ) { if ( maxLength < 0.1 ) {
out.height--; 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 ){ mesh_t *SubdivideMeshQuads( mesh_t *in, float minLength, int maxsize, int *widthtable, int *heighttable ){
int i, j, k, w, h, maxsubdivisions, subdivisions; int i, j, k, w, h, maxsubdivisions, subdivisions;
float length, maxLength, amount;
mesh_t out; mesh_t out;
bspDrawVert_t expand[MAX_EXPANDED_AXIS][MAX_EXPANDED_AXIS]; 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 ); maxsubdivisions = ( maxsize - in->width ) / ( in->width - 1 );
for ( w = 0, j = 0 ; w < in->width - 1; w++, j += subdivisions + 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++ ) { for ( i = 0 ; i < out.height ; i++ ) {
length = vector3_length( expand[i][j + 1].xyz - expand[i][j].xyz ); value_maximize( maxLength, vector3_length( expand[i][j + 1].xyz - expand[i][j].xyz ) );
if ( length > maxLength ) {
maxLength = length;
}
} }
subdivisions = (int) ( maxLength / minLength ); subdivisions = std::min( (int) ( maxLength / minLength ), maxsubdivisions );
if ( subdivisions > maxsubdivisions ) {
subdivisions = maxsubdivisions;
}
widthtable[w] = subdivisions + 1; widthtable[w] = subdivisions + 1;
if ( subdivisions <= 0 ) { 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++ ) 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] ); 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 ); maxsubdivisions = ( maxsize - in->height ) / ( in->height - 1 );
for ( h = 0, j = 0 ; h < in->height - 1; h++, j += subdivisions + 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++ ) { for ( i = 0 ; i < out.width ; i++ ) {
length = vector3_length( expand[j + 1][i].xyz - expand[j][i].xyz ); value_maximize( maxLength, vector3_length( expand[j + 1][i].xyz - expand[j][i].xyz ) );
if ( length > maxLength ) {
maxLength = length;
}
} }
subdivisions = (int) ( maxLength / minLength ); subdivisions = std::min( (int) ( maxLength / minLength ), maxsubdivisions );
if ( subdivisions > maxsubdivisions ) {
subdivisions = maxsubdivisions;
}
heighttable[h] = subdivisions + 1; heighttable[h] = subdivisions + 1;
if ( subdivisions <= 0 ) { 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++ ) 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] ); LerpDrawVertAmount( &expand[j][i], &expand[j + subdivisions + 1][i], amount, &expand[j + k][i] );
} }
} }

View File

@ -647,12 +647,8 @@ int MiniMapBSPMain( int argc, char **argv ){
for ( x = 0; x < minimap.width; ++x ) for ( x = 0; x < minimap.width; ++x )
{ {
float v = *q++; float v = *q++;
if ( v < mi ) { value_minimize( mi, v );
mi = v; value_maximize( ma, v );
}
if ( v > ma ) {
ma = v;
}
} }
if ( ma > mi ) { if ( ma > mi ) {
s = 1 / ( ma - mi ); s = 1 / ( ma - mi );
@ -699,16 +695,7 @@ int MiniMapBSPMain( int argc, char **argv ){
for ( y = 0; y < minimap.height; ++y ) for ( y = 0; y < minimap.height; ++y )
for ( x = 0; x < minimap.width; ++x ) for ( x = 0; x < minimap.width; ++x )
{ {
byte b; *p++ = std::clamp( *q++, 0.f, 255.f / 256.f ) * 256;
float v = *q++;
if ( v < 0 ) {
v = 0;
}
if ( v > 255.0 / 256.0 ) {
v = 255.0 / 256.0;
}
b = v * 256;
*p++ = b;
} }
Sys_Printf( " writing to %s...", minimapFilename ); Sys_Printf( " writing to %s...", minimapFilename );
WriteTGAGray( minimapFilename, data4b, minimap.width, minimap.height ); 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 ( y = 0; y < minimap.height; ++y )
for ( x = 0; x < minimap.width; ++x ) 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++ = 0;
*p++ = 0; *p++ = 0;
*p++ = b; *p++ = std::clamp( *q++, 0.f, 255.f / 256.f ) * 256;
} }
Sys_Printf( " writing to %s...", minimapFilename ); Sys_Printf( " writing to %s...", minimapFilename );
WriteTGA( minimapFilename, data4b, minimap.width, minimap.height ); 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 ( y = 0; y < minimap.height; ++y )
for ( x = 0; x < minimap.width; ++x ) 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++ = 255;
*p++ = 255; *p++ = 255;
*p++ = b; *p++ = std::clamp( *q++, 0.f, 255.f / 256.f ) * 256;
} }
Sys_Printf( " writing to %s...", minimapFilename ); Sys_Printf( " writing to %s...", minimapFilename );
WriteTGA( minimapFilename, data4b, minimap.width, minimap.height ); WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );

View File

@ -212,7 +212,6 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor
picoSurface_t *surface; picoSurface_t *surface;
shaderInfo_t *si; shaderInfo_t *si;
mapDrawSurface_t *ds; mapDrawSurface_t *ds;
bspDrawVert_t *dv;
const char *picoShaderName; const char *picoShaderName;
byte *color; byte *color;
picoIndex_t *indexes; 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++ ) for ( i = 0; i < ds->numVerts; i++ )
{ {
/* get vertex */ /* get vertex */
dv = &ds->verts[ i ]; bspDrawVert_t& dv = ds->verts[ i ];
/* xyz and normal */ /* xyz and normal */
dv->xyz = vector3_from_array( PicoGetSurfaceXYZ( surface, i ) ); dv.xyz = vector3_from_array( PicoGetSurfaceXYZ( surface, i ) );
matrix4_transform_point( transform, dv->xyz ); matrix4_transform_point( transform, dv.xyz );
dv->normal = vector3_from_array( PicoGetSurfaceNormal( surface, i ) ); dv.normal = vector3_from_array( PicoGetSurfaceNormal( surface, i ) );
matrix4_transform_direction( nTransform, dv->normal ); matrix4_transform_direction( nTransform, dv.normal );
VectorNormalize( dv->normal ); VectorNormalize( dv.normal );
/* ydnar: tek-fu celshading support for flat shaded shit */ /* ydnar: tek-fu celshading support for flat shaded shit */
if ( flat ) { if ( flat ) {
dv->st = si->stFlat; dv.st = si->stFlat;
} }
/* ydnar: gs mods: added support for explicit shader texcoord generation */ /* ydnar: gs mods: added support for explicit shader texcoord generation */
else if ( si->tcGen ) { else if ( si->tcGen ) {
/* project the texture */ /* project the texture */
dv->st[ 0 ] = vector3_dot( si->vecs[ 0 ], dv->xyz ); dv.st[ 0 ] = vector3_dot( si->vecs[ 0 ], dv.xyz );
dv->st[ 1 ] = vector3_dot( si->vecs[ 1 ], dv->xyz ); dv.st[ 1 ] = vector3_dot( si->vecs[ 1 ], dv.xyz );
} }
/* normal texture coordinates */ /* normal texture coordinates */
else else
{ {
dv->st = vector2_from_array( PicoGetSurfaceST( surface, 0, i ) ); dv.st = vector2_from_array( PicoGetSurfaceST( surface, 0, i ) );
} }
/* set lightmap/color bits */ /* set lightmap/color bits */
color = PicoGetSurfaceColor( surface, 0, i ); color = PicoGetSurfaceColor( surface, 0, i );
for ( j = 0; j < MAX_LIGHTMAPS; j++ ) for ( j = 0; j < MAX_LIGHTMAPS; j++ )
{ {
dv->lightmap[ j ][ 0 ] = 0.0f; dv.lightmap[ j ] = { 0, 0 };
dv->lightmap[ j ][ 1 ] = 0.0f;
if ( spawnFlags & 32 ) { // spawnflag 32: model color -> alpha hack 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 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++ ) for ( j = 0; j < 3; j++ )
{ {
dv = &ds->verts[ ds->indexes[ i + j ] ]; points[ j ] = ds->verts[ ds->indexes[ i + j ] ].xyz;
points[ j ] = dv->xyz;
} }
if ( PlaneFromPoints( plane, points ) ){ if ( PlaneFromPoints( plane, points ) ){
if ( spawnFlags & 16 ) if ( spawnFlags & 16 )
@ -557,11 +554,8 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor
/* make points */ /* make points */
for ( j = 0; j < 3; j++ ) for ( j = 0; j < 3; j++ )
{ {
/* get vertex */
dv = &ds->verts[ ds->indexes[ i + j ] ];
/* copy xyz */ /* copy xyz */
points[ j ] = dv->xyz; points[ j ] = ds->verts[ ds->indexes[ i + j ] ].xyz;
} }
/* make plane for triangle */ /* 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 ) Sys_Printf("default_CLIPMODEL\n");
if ( bestdist == 999999 && bestangle == 1 ) goto default_CLIPMODEL; 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; 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]; reverse.dist() = points[0][axis];
if ( bestNormal[axis] > 0 ){ if ( bestNormal[axis] > 0 ){
for ( j = 1; j < 3; j++ ){ for ( j = 1; j < 3; j++ ){
if ( points[j][axis] < reverse.dist() ){ value_minimize( reverse.dist(), points[j][axis] );
reverse.dist() = points[j][axis];
}
} }
reverse.dist() = -reverse.dist() + clipDepth; reverse.dist() = -reverse.dist() + clipDepth;
} }
else{ else{
for ( j = 1; j < 3; j++ ){ for ( j = 1; j < 3; j++ ){
if ( points[j][axis] > reverse.dist() ){ value_maximize( reverse.dist(), points[j][axis] );
reverse.dist() = points[j][axis];
}
} }
reverse.dist() += clipDepth; reverse.dist() += clipDepth;
} }
@ -968,10 +958,8 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor
/* get vertex normals */ /* get vertex normals */
for ( j = 0; j < 3; j++ ) for ( j = 0; j < 3; j++ )
{ {
/* get vertex */
dv = &ds->verts[ ds->indexes[ i + j ] ];
/* copy normal */ /* copy normal */
Vnorm[ j ] = dv->normal; Vnorm[ j ] = ds->verts[ ds->indexes[ i + j ] ].normal;
} }
//avg normals for side planes //avg normals for side planes
@ -1396,25 +1384,19 @@ void AddTriangleModels( entity_t *eparent ){
} }
/* jal : entity based _samplesize */ /* jal : entity based _samplesize */
int lightmapSampleSize = e->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ); const int lightmapSampleSize = std::max( 0, e->intForKey( "_lightmapsamplesize", "_samplesize", "_ss" ) );
if ( lightmapSampleSize < 0 ) if ( lightmapSampleSize != 0 )
lightmapSampleSize = 0;
if ( lightmapSampleSize > 0 )
Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize ); Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
/* get lightmap scale */ /* get lightmap scale */
float lightmapScale = e->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ); const float lightmapScale = std::max( 0.f, e->floatForKey( "lightmapscale", "_lightmapscale", "_ls" ) );
if ( lightmapScale < 0.0f ) if ( lightmapScale != 0 )
lightmapScale = 0.0f;
else if ( lightmapScale > 0.0f )
Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale ); Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
/* jal : entity based _shadeangle */ /* jal : entity based _shadeangle */
float shadeAngle = e->floatForKey( "_shadeangle", const float shadeAngle = std::max( 0.f, e->floatForKey( "_shadeangle",
"_smoothnormals", "_sn", "_sa", "_smooth" ); /* vortex' aliases */ "_smoothnormals", "_sn", "_sa", "_smooth" ) ); /* vortex' aliases */
if ( shadeAngle < 0.0f ) if ( shadeAngle != 0 )
shadeAngle = 0.0f;
else if ( shadeAngle > 0.0f )
Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle ); Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
const int skin = e->intForKey( "_skin", "skin" ); const int skin = e->intForKey( "_skin", "skin" );

View File

@ -86,9 +86,7 @@ static void ExpandLongestCurve( float *longestCurve, const Vector3& a, const Vec
} }
/* longer? */ /* longer? */
if ( len > *longestCurve ) { value_maximize( *longestCurve, len );
*longestCurve = len;
}
} }
@ -186,9 +184,7 @@ static void ExpandMaxIterations( int *maxIterations, int maxError, const Vector3
} }
/* more? */ /* more? */
if ( iterations > *maxIterations ) { value_maximize( *maxIterations, iterations );
*maxIterations = iterations;
}
} }
@ -362,12 +358,8 @@ static void GrowGroup_r( parseMesh_t *pm, int patchNum, int patchCount, parseMes
row = bordering + patchNum * patchCount; row = bordering + patchNum * patchCount;
/* check maximums */ /* check maximums */
if ( meshes[ patchNum ]->longestCurve > pm->longestCurve ) { value_maximize( pm->longestCurve, meshes[ patchNum ]->longestCurve );
pm->longestCurve = meshes[ patchNum ]->longestCurve; value_maximize( pm->maxIterations, meshes[ patchNum ]->maxIterations );
}
if ( meshes[ patchNum ]->maxIterations > pm->maxIterations ) {
pm->maxIterations = meshes[ patchNum ]->maxIterations;
}
/* walk other patches */ /* walk other patches */
for ( i = 0; i < patchCount; i++ ) for ( i = 0; i < patchCount; i++ )
@ -429,9 +421,7 @@ void PatchMapDrawSurfs( entity_t *e ){
c2 = check->mesh.width * check->mesh.height; c2 = check->mesh.width * check->mesh.height;
v2 = check->mesh.verts; v2 = check->mesh.verts;
for ( j = 0 ; j < c2 ; j++, v2++ ) { for ( j = 0 ; j < c2 ; j++, v2++ ) {
if ( fabs( v1->xyz[0] - v2->xyz[0] ) < 1.0 if ( vector3_equal_epsilon( v1->xyz, v2->xyz, 1.f ) ) {
&& fabs( v1->xyz[1] - v2->xyz[1] ) < 1.0
&& fabs( v1->xyz[2] - v2->xyz[2] ) < 1.0 ) {
break; break;
} }
} }

View File

@ -532,12 +532,8 @@ void InitPaths( int *argc, char **argv ){
} }
/* initialize vfs paths */ /* initialize vfs paths */
if ( numBasePaths > MAX_BASE_PATHS ) { value_minimize( numBasePaths, MAX_BASE_PATHS );
numBasePaths = MAX_BASE_PATHS; value_minimize( numGamePaths, MAX_GAME_PATHS );
}
if ( numGamePaths > MAX_GAME_PATHS ) {
numGamePaths = MAX_GAME_PATHS;
}
/* walk the list of game paths */ /* walk the list of game paths */
for ( j = 0; j < numGamePaths; j++ ) for ( j = 0; j < numGamePaths; j++ )
@ -552,9 +548,7 @@ void InitPaths( int *argc, char **argv ){
} }
/* initialize vfs paths */ /* initialize vfs paths */
if ( numPakPaths > MAX_PAK_PATHS ) { value_minimize( numPakPaths, MAX_PAK_PATHS );
numPakPaths = MAX_PAK_PATHS;
}
/* walk the list of pak paths */ /* walk the list of pak paths */
for ( i = 0; i < numPakPaths; i++ ) for ( i = 0; i < numPakPaths; i++ )

View File

@ -142,14 +142,7 @@ void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts ){
{ {
for ( k = 0; k < 4; k++ ) for ( k = 0; k < 4; k++ )
{ {
c = ( mult[ k ] * dv->color[ j ][ k ] ) + add[ k ]; dv->color[ j ][ k ] = std::clamp( mult[ k ] * dv->color[ j ][ k ] + add[ k ], 0.f, 255.f );
if ( c < 0 ) {
c = 0;
}
else if ( c > 255 ) {
c = 255;
}
dv->color[ j ][ k ] = c;
} }
} }
} }
@ -933,7 +926,7 @@ void Parse1DMatrixAppend( char *buffer, int x, float *m ){
*/ */
static void ParseShaderFile( const char *filename ){ static void ParseShaderFile( const char *filename ){
int i, val; int i;
shaderInfo_t *si; shaderInfo_t *si;
char shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */ char shaderText[ 8192 ]; /* ydnar: fixme (make this bigger?) */
@ -1383,12 +1376,8 @@ static void ParseShaderFile( const char *filename ){
si->skyLightIterations = atoi( token ); si->skyLightIterations = atoi( token );
/* clamp */ /* clamp */
if ( si->skyLightValue < 0.0f ) { value_maximize( si->skyLightValue, 0.0f );
si->skyLightValue = 0.0f; value_maximize( si->skyLightIterations, 2 );
}
if ( si->skyLightIterations < 2 ) {
si->skyLightIterations = 2;
}
} }
/* q3map_surfacelight <value> */ /* q3map_surfacelight <value> */
@ -1400,14 +1389,7 @@ static void ParseShaderFile( const char *filename ){
/* q3map_lightStyle (sof2/jk2 lightstyle) */ /* q3map_lightStyle (sof2/jk2 lightstyle) */
else if ( striEqual( token, "q3map_lightStyle" ) ) { else if ( striEqual( token, "q3map_lightStyle" ) ) {
GetTokenAppend( shaderText, false ); GetTokenAppend( shaderText, false );
val = atoi( token ); si->lightStyle = std::clamp( atoi( token ), 0, LS_NONE );
if ( val < 0 ) {
val = 0;
}
else if ( val > LS_NONE ) {
val = LS_NONE;
}
si->lightStyle = val;
} }
/* wolf: q3map_lightRGB <red> <green> <blue> */ /* wolf: q3map_lightRGB <red> <green> <blue> */

View File

@ -324,7 +324,6 @@ void TidyEntitySurfaces( entity_t *e ){
bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){
int i, j, v, size[ 2 ]; int i, j, v, size[ 2 ];
float mins[ 2 ], maxs[ 2 ];
/* try to early out */ /* try to early out */
@ -333,20 +332,13 @@ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){
} }
/* walk the verts and determine min/max st values */ /* walk the verts and determine min/max st values */
mins[ 0 ] = 999999; Vector2 mins( 999999, 999999 ), maxs( -999999, -999999 );
mins[ 1 ] = 999999;
maxs[ 0 ] = -999999;
maxs[ 1 ] = -999999;
for ( i = 0; i < ds->numVerts; i++ ) for ( i = 0; i < ds->numVerts; i++ )
{ {
for ( j = 0; j < 2; j++ ) for ( j = 0; j < 2; j++ )
{ {
if ( ds->verts[ i ].st[ j ] < mins[ j ] ) { value_minimize( mins[ j ], ds->verts[ i ].st[ j ] );
mins[ j ] = ds->verts[ i ].st[ j ]; value_maximize( maxs[ j ], ds->verts[ i ].st[ j ] );
}
if ( ds->verts[ i ].st[ j ] > maxs[ j ] ) {
maxs[ j ] = ds->verts[ i ].st[ j ];
}
} }
} }
@ -366,12 +358,8 @@ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){
for ( j = 0; j < 2; j++ ) for ( j = 0; j < 2; j++ )
{ {
v = ( ds->verts[ i ].st[ j ] + ds->bias[ j ] ) * size[ j ]; v = ( ds->verts[ i ].st[ j ] + ds->bias[ j ] ) * size[ j ];
if ( v < ds->texMins[ j ] ) { value_minimize( ds->texMins[ j ], v );
ds->texMins[ j ] = v; value_maximize( ds->texMaxs[ j ], v );
}
if ( v > ds->texMaxs[ j ] ) {
ds->texMaxs[ j ] = v;
}
} }
} }
@ -514,7 +502,7 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){
plane = { 0, 0, 0, 0 }; plane = { 0, 0, 0, 0 };
for ( i = 0; i < ds->numVerts; i++ ) 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.normal() = ds->verts[ i ].normal;
plane.dist() = vector3_dot( ds->verts[ i ].xyz, plane.normal() ); plane.dist() = vector3_dot( ds->verts[ i ].xyz, plane.normal() );
break; break;
@ -630,17 +618,7 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){
ds->lightmapScale = 0; /* applied */ ds->lightmapScale = 0; /* applied */
} }
if ( ds->sampleSize < minSampleSize ) { ds->sampleSize = std::clamp( ds->sampleSize, std::max( minSampleSize, 1 ), 16384 ); /* powers of 2 are preferred */
ds->sampleSize = minSampleSize;
}
if ( ds->sampleSize < 1 ) {
ds->sampleSize = 1;
}
if ( ds->sampleSize > 16384 ) { /* powers of 2 are preferred */
ds->sampleSize = 16384;
}
} }
} }
@ -677,9 +655,6 @@ void ClassifyEntitySurfaces( entity_t *e ){
*/ */
byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const Vector3& point ){ byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const Vector3& point ){
int x, y;
/* early out if no indexmap */ /* early out if no indexmap */
if ( im == NULL ) { if ( im == NULL ) {
return 0; return 0;
@ -697,24 +672,12 @@ byte GetShaderIndexForPoint( const indexMap_t *im, const MinMax& eMinmax, const
const Vector3 size = maxs - mins; const Vector3 size = maxs - mins;
/* find st (fixme: support more than just z-axis projection) */ /* find st (fixme: support more than just z-axis projection) */
float s = floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ]; const float s = std::clamp( floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ], 0.0, 1.0 );
float t = floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ]; const float t = std::clamp( floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ], 0.0, 1.0 );
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;
}
/* make xy */ /* make xy */
x = ( im->w - 1 ) * s; const int x = ( im->w - 1 ) * s;
y = ( im->h - 1 ) * t; const int y = ( im->h - 1 ) * t;
#else #else
/* get size */ /* get size */
const Vector3 size = eMinmax.maxs - eMinmax.mins; 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 ]; const float t = ( eMinmax.maxs[ 1 ] - point[ 1 ] ) / size[ 1 ];
/* calc xy */ /* calc xy */
x = s * im->w; const int x = std::clamp( int( s * im->w ), 0, im->w - 1 );
y = t * im->h; const int y = std::clamp( int( t * im->h ), 0, im->h - 1 );
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 );
}
#endif #endif
/* return index */ /* 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 ){ 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 */ /* early out if bad data */
if ( im == NULL || numPoints <= 0 || shaderIndexes == NULL ) { if ( im == NULL || numPoints <= 0 || shaderIndexes == NULL ) {
return ShaderInfoForShader( "default" ); return ShaderInfoForShader( "default" );
} }
/* determine min/max index */ /* determine min/max index */
minShaderIndex = 255; byte minShaderIndex = 255;
maxShaderIndex = 0; byte maxShaderIndex = 0;
for ( i = 0; i < numPoints; i++ ) for ( int i = 0; i < numPoints; i++ )
{ {
if ( shaderIndexes[ i ] < minShaderIndex ) { value_minimize( minShaderIndex, shaderIndexes[ i ] );
minShaderIndex = shaderIndexes[ i ]; value_maximize( maxShaderIndex, shaderIndexes[ i ] );
}
if ( shaderIndexes[ i ] > maxShaderIndex ) {
maxShaderIndex = shaderIndexes[ i ];
}
} }
/* set alpha inline */ /* set alpha inline */
for ( i = 0; i < numPoints; i++ ) for ( int i = 0; i < numPoints; i++ )
{ {
/* straight rip from terrain.c */ /* straight rip from terrain.c */
if ( shaderIndexes[ i ] < maxShaderIndex ) { if ( shaderIndexes[ i ] < maxShaderIndex ) {
@ -789,7 +731,7 @@ shaderInfo_t *GetIndexedShader( const shaderInfo_t *parent, const indexMap_t *im
} }
/* get the shader */ /* 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(maxShaderIndex) ):
String64()( "textures/", im->shader.c_str(), '_', int(minShaderIndex), "to", 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 creates a SURF_FACE drawsurface from a given brush side and winding
*/ */
#define SNAP_FLOAT_TO_INT 8 const double SNAP_FLOAT_TO_INT = 8.0;
#define SNAP_INT_TO_FLOAT ( 1.0 / SNAP_FLOAT_TO_INT ) 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 ){ mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w ){
int i, j, k; 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 */ /* round the xyz to a given precision and translate by origin */
for ( i = 0 ; i < 3 ; i++ ) 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; vTranslated = dv->xyz + e->origin;
/* ydnar: tek-fu celshading support for flat shaded shit */ /* 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 ){ static void AddSurfaceFlare( mapDrawSurface_t *ds, const Vector3& entityOrigin ){
Vector3 origin( 0, 0, 0 ); Vector3 origin( 0, 0, 0 );
int i;
/* find centroid */ /* find centroid */
for ( i = 0; i < ds->numVerts; i++ ) for ( int i = 0; i < ds->numVerts; i++ )
origin += ds->verts[ i ].xyz; origin += ds->verts[ i ].xyz;
origin /= ds->numVerts; origin /= ds->numVerts;
origin += entityOrigin; origin += entityOrigin;
@ -1408,8 +1347,8 @@ void SubdivideFaceSurfaces( entity_t *e, tree_t *tree ){
} }
/* get subdivisions from shader */ /* get subdivisions from shader */
if ( si->subdivisions > 0 && si->subdivisions < subdivisions ) { if ( si->subdivisions > 0 ) {
subdivisions = si->subdivisions; value_minimize( subdivisions, si->subdivisions );
} }
if ( subdivisions < 1.0f ) { if ( subdivisions < 1.0f ) {
continue; 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 ){ 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 ) { if ( !points ) {
return 0; return 0;
} }
@ -1954,20 +1889,17 @@ int FilterPointConvexHullIntoTree_r( Vector3 *points[], const int npoints, mapDr
/* classify the point in relation to the plane */ /* classify the point in relation to the plane */
const Plane3f& plane = mapplanes[ node->planenum ].plane; const Plane3f& plane = mapplanes[ node->planenum ].plane;
float dmin, dmax;
dmin = dmax = plane3_distance_to_point( plane, *points[0] ); 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] ); const float d = plane3_distance_to_point( plane, *points[i] );
if ( d > dmax ) { value_maximize( dmax, d );
dmax = d; value_minimize( dmin, d );
}
if ( d < dmin ) {
dmin = d;
}
} }
/* filter by this plane */ /* filter by this plane */
refs = 0; int refs = 0;
if ( dmax >= -ON_EPSILON ) { if ( dmax >= -ON_EPSILON ) {
refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 0 ] ); refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 0 ] );
} }

View File

@ -72,30 +72,17 @@ static void SubdivideFoliageTriangle_r( mapDrawSurface_t *ds, const foliage_t& f
/* subdivide calc */ /* subdivide calc */
{ {
int i;
float dx, dy, dz, dist, maxDist;
foliageInstance_t *fi;
/* get instance */ /* get instance */
fi = &foliageInstances[ numFoliageInstances ]; foliageInstance_t *fi = &foliageInstances[ numFoliageInstances ];
/* find the longest edge and split it */ /* find the longest edge and split it */
max = -1; max = -1;
maxDist = 0.0f; float maxDist = 0.0f;
fi->xyz.set( 0 ); fi->xyz.set( 0 );
fi->normal.set( 0 ); fi->normal.set( 0 );
for ( i = 0; i < 3; i++ ) for ( int i = 0; i < 3; i++ )
{ {
/* get verts */ const float dist = vector3_length_squared( tri[ i ]->xyz - tri[ ( i + 1 ) % 3 ]->xyz );
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 );
/* longer? */ /* longer? */
if ( dist > maxDist ) { if ( dist > maxDist ) {

View File

@ -125,13 +125,9 @@ int FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, b
int triIndex; int triIndex;
/* detect degenerate triangles fixme: do something proper here */ /* detect degenerate triangles fixme: do something proper here */
if ( vector3_length( a->xyz - b->xyz ) < 0.125f ) { if ( vector3_length( a->xyz - b->xyz ) < 0.125f
return -1; || vector3_length( b->xyz - c->xyz ) < 0.125f
} || vector3_length( c->xyz - a->xyz ) < 0.125f ) {
if ( vector3_length( b->xyz - c->xyz ) < 0.125f ) {
return -1;
}
if ( vector3_length( c->xyz - a->xyz ) < 0.125f ) {
return -1; return -1;
} }
@ -989,9 +985,7 @@ void FixMetaTJunctions( void ){
/* skip this point if it already exists in the triangle */ /* skip this point if it already exists in the triangle */
for ( k = 0; k < 3; k++ ) for ( k = 0; k < 3; k++ )
{ {
if ( fabs( pt[ 0 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 0 ] ) <= TJ_POINT_EPSILON && if ( vector3_equal_epsilon( pt, metaVerts[ tri->indexes[ k ] ].xyz, 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 ) {
break; break;
} }
} }
@ -1106,7 +1100,7 @@ void FixMetaTJunctions( void ){
void SmoothMetaTriangles( void ){ void SmoothMetaTriangles( void ){
int i, j, k, f, fOld, start, numVerts, numVotes, numSmoothed; int i, j, k, f, fOld, start, numVerts, numVotes, numSmoothed;
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle; float shadeAngle, defaultShadeAngle, maxShadeAngle;
metaTriangle_t *tri; metaTriangle_t *tri;
float *shadeAngles; float *shadeAngles;
byte *smoothed; byte *smoothed;
@ -1146,9 +1140,7 @@ void SmoothMetaTriangles( void ){
shadeAngle = defaultShadeAngle; shadeAngle = defaultShadeAngle;
} }
if ( shadeAngle > maxShadeAngle ) { value_maximize( maxShadeAngle, shadeAngle );
maxShadeAngle = shadeAngle;
}
/* flag its verts */ /* flag its verts */
for ( j = 0; j < 3; j++ ) for ( j = 0; j < 3; j++ )
@ -1207,18 +1199,11 @@ void SmoothMetaTriangles( void ){
} }
/* use smallest shade angle */ /* use smallest shade angle */
shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] ); shadeAngle = std::min( shadeAngles[ i ], shadeAngles[ j ] );
/* check shade angle */ /* check shade angle */
dot = vector3_dot( metaVerts[ i ].normal, metaVerts[ j ].normal ); const double dot = std::clamp( vector3_dot( metaVerts[ i ].normal, metaVerts[ j ].normal ), -1.0, 1.0 );
if ( dot > 1.0 ) { if ( acos( dot ) + THETA_EPSILON >= shadeAngle ) {
dot = 1.0;
}
else if ( dot < -1.0 ) {
dot = -1.0;
}
testAngle = acos( dot ) + THETA_EPSILON;
if ( testAngle >= shadeAngle ) {
continue; continue;
} }
@ -1388,25 +1373,13 @@ static int AddMetaTriangleToSurface( mapDrawSurface_t *ds, metaTriangle_t *tri,
if ( metaMaxBBoxDistance >= 0 ) { if ( metaMaxBBoxDistance >= 0 && ds->numIndexes > 0 ) {
if ( ds->numIndexes > 0 ) { const MinMax minmax( ds->minmax.mins - Vector3().set( metaMaxBBoxDistance ),
const Vector3 mins = ds->minmax.mins - Vector3( metaMaxBBoxDistance, metaMaxBBoxDistance, metaMaxBBoxDistance ); ds->minmax.maxs + Vector3().set( metaMaxBBoxDistance ) );
const Vector3 maxs = ds->minmax.maxs + Vector3( metaMaxBBoxDistance, metaMaxBBoxDistance, metaMaxBBoxDistance ); if( !minmax.test( metaVerts[ tri->indexes[ 0 ] ].xyz )
#define CHECK_1D( mins, v, maxs ) ( ( mins ) <= ( v ) && ( v ) <= ( maxs ) ) && !minmax.test( metaVerts[ tri->indexes[ 1 ] ].xyz )
#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] ) ) && !minmax.test( metaVerts[ tri->indexes[ 2 ] ].xyz ) )
Vector3 p = metaVerts[ tri->indexes[ 0 ] ].xyz; return 0;
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
}
} }
/* set initial score */ /* set initial score */
@ -1707,51 +1680,52 @@ static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles,
compare function for qsort() compare function for qsort()
*/ */
static int CompareMetaTriangles( const void *a, const void *b ){ static int CompareMetaTriangles( const void *a_, const void *b_ ){
int i, j, av, bv; auto a = reinterpret_cast<const metaTriangle_t *>( a_ );
auto b = reinterpret_cast<const metaTriangle_t *>( b_ );
/* shader first */ /* shader first */
if ( ( (const metaTriangle_t*) a )->si < ( (const metaTriangle_t*) b )->si ) { if ( a->si < b->si ) {
return 1; return 1;
} }
else if ( ( (const metaTriangle_t*) a )->si > ( (const metaTriangle_t*) b )->si ) { else if ( a->si > b->si ) {
return -1; return -1;
} }
/* then fog */ /* then fog */
else if ( ( (const metaTriangle_t*) a )->fogNum < ( (const metaTriangle_t*) b )->fogNum ) { else if ( a->fogNum < b->fogNum ) {
return 1; return 1;
} }
else if ( ( (const metaTriangle_t*) a )->fogNum > ( (const metaTriangle_t*) b )->fogNum ) { else if ( a->fogNum > b->fogNum ) {
return -1; return -1;
} }
/* then plane */ /* then plane */
#if 0 #if 0
else if ( npDegrees == 0.0f && !( (const metaTriangle_t*) a )->si->nonplanar && else if ( npDegrees == 0.0f && !a->si->nonplanar &&
( (const metaTriangle_t*) a )->planeNum >= 0 && ( (const metaTriangle_t*) a )->planeNum >= 0 ) { a->planeNum >= 0 && a->planeNum >= 0 ) {
if ( ( (const metaTriangle_t*) a )->plane[ 3 ] < ( (const metaTriangle_t*) b )->plane[ 3 ] ) { if ( a->plane.dist() < b->plane.dist() ) {
return 1; 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; 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; 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; 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; 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; 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; 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; return -1;
} }
} }
@ -1762,23 +1736,19 @@ static int CompareMetaTriangles( const void *a, const void *b ){
/* find mins */ /* find mins */
Vector3 aMins( 999999, 999999, 999999 ); Vector3 aMins( 999999, 999999, 999999 );
Vector3 bMins( 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 ]; const int av = a->indexes[ i ];
bv = ( (const metaTriangle_t*) b )->indexes[ i ]; const int bv = b->indexes[ i ];
for ( j = 0; j < 3; j++ ) for ( int j = 0; j < 3; j++ )
{ {
if ( metaVerts[ av ].xyz[ j ] < aMins[ j ] ) { value_minimize( aMins[ j ], metaVerts[ av ].xyz[ j ] );
aMins[ j ] = metaVerts[ av ].xyz[ j ]; value_minimize( bMins[ j ], metaVerts[ bv ].xyz[ j ] );
}
if ( metaVerts[ bv ].xyz[ j ] < bMins[ j ] ) {
bMins[ j ] = metaVerts[ bv ].xyz[ j ];
}
} }
} }
/* test it */ /* test it */
for ( i = 0; i < 3; i++ ) for ( int i = 0; i < 3; i++ )
{ {
if ( aMins[ i ] < bMins[ i ] ) { if ( aMins[ i ] < bMins[ i ] ) {
return 1; return 1;

View File

@ -78,7 +78,7 @@ int c_natural, c_rotate, c_cant;
// these should be whatever epsilon we actually expect, // these should be whatever epsilon we actually expect,
// plus SNAP_INT_TO_FLOAT // plus SNAP_INT_TO_FLOAT
#define LINE_POSITION_EPSILON 0.25 #define LINE_POSITION_EPSILON 0.25f
#define POINT_ON_LINE_EPSILON 0.25 #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 ) { void InsertPointOnEdge( const Vector3 &v, edgeLine_t *e ) {
float d;
edgePoint_t *p, *scan; edgePoint_t *p, *scan;
d = vector3_dot( v - e->origin, e->dir );
p = safe_malloc( sizeof( edgePoint_t ) ); p = safe_malloc( sizeof( edgePoint_t ) );
p->intercept = d; p->intercept = vector3_dot( v - e->origin, e->dir );
p->xyz = v; p->xyz = v;
if ( e->chain->next == e->chain ) { if ( e->chain->next == e->chain ) {
@ -103,9 +100,8 @@ void InsertPointOnEdge( const Vector3 &v, edgeLine_t *e ) {
} }
scan = e->chain->next; scan = e->chain->next;
for ( ; scan != e->chain ; scan = scan->next ) { for ( ; scan != e->chain; scan = scan->next ) {
d = p->intercept - scan->intercept; if ( float_equal_epsilon( p->intercept, scan->intercept, LINE_POSITION_EPSILON ) ) {
if ( d > -LINE_POSITION_EPSILON && d < LINE_POSITION_EPSILON ) {
free( p ); free( p );
return; // the point is already set 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++ ) { for ( i = 0 ; i < numEdgeLines ; i++ ) {
e = &edgeLines[i]; e = &edgeLines[i];
d = vector3_dot( v1, e->normal1 ) - e->dist1; if ( !float_equal_epsilon( vector3_dot( v1, e->normal1 ), e->dist1, POINT_ON_LINE_EPSILON )
if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) { || !float_equal_epsilon( vector3_dot( v1, e->normal2 ), e->dist2, POINT_ON_LINE_EPSILON )
continue; || !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 ) ) {
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 ) {
continue; continue;
} }
@ -581,10 +566,8 @@ bool FixBrokenSurface( mapDrawSurface_t *ds ){
================ ================
*/ */
int EdgeCompare( const void *elem1, const void *elem2 ) { int EdgeCompare( const void *elem1, const void *elem2 ) {
float d1, d2; const float d1 = reinterpret_cast<const originalEdge_t *>( elem1 )->length;
const float d2 = reinterpret_cast<const originalEdge_t *>( elem2 )->length;
d1 = ( (const originalEdge_t *)elem1 )->length;
d2 = ( (const originalEdge_t *)elem2 )->length;
if ( d1 < d2 ) { if ( d1 < d2 ) {
return -1; return -1;

View File

@ -369,26 +369,21 @@ void CalcVis( void ){
================== ==================
*/ */
void SetPortalSphere( vportal_t *p ){ void SetPortalSphere( vportal_t *p ){
int i;
Vector3 total( 0, 0, 0 ); Vector3 total( 0, 0, 0 );
fixedWinding_t *w; fixedWinding_t *w;
float r, bestr;
w = p->winding; w = p->winding;
for ( i = 0 ; i < w->numpoints ; i++ ) for ( int i = 0; i < w->numpoints; i++ )
{ {
total += w->points[i]; total += w->points[i];
} }
total /= w->numpoints; total /= w->numpoints;
bestr = 0; double bestr = 0;
for ( i = 0 ; i < w->numpoints ; i++ ) for ( int i = 0; i < w->numpoints; i++ )
{ {
r = vector3_length( w->points[i] - total ); value_maximize( bestr, vector3_length( w->points[i] - total ) );
if ( r > bestr ) {
bestr = r;
}
} }
p->origin = total; p->origin = total;
p->radius = bestr; p->radius = bestr;

View File

@ -102,9 +102,7 @@ fixedWinding_t *AllocStackWinding( pstack_t *stack ){
} }
void FreeStackWinding( fixedWinding_t *w, pstack_t *stack ){ void FreeStackWinding( fixedWinding_t *w, pstack_t *stack ){
int i; const int i = w - stack->windings;
i = w - stack->windings;
if ( i < 0 || i > 2 ) { if ( i < 0 || i > 2 ) {
return; // not from local return; // not from local
@ -1345,10 +1343,8 @@ void CreatePassages( int portalnum ){
for ( k = 0; k < numseperators; k++ ) for ( k = 0; k < numseperators; k++ )
{ {
/* ydnar: this is a shitty crutch */ /* ydnar: this is a shitty crutch */
if ( in.numpoints > MAX_POINTS_ON_FIXED_WINDING ) { //% if ( in.numpoints > MAX_POINTS_ON_FIXED_WINDING ) Sys_Printf( "[%d]", p->winding->numpoints );
//% Sys_Printf( "[%d]", p->winding->numpoints ); value_minimize( in.numpoints, MAX_POINTS_ON_FIXED_WINDING );
in.numpoints = MAX_POINTS_ON_FIXED_WINDING;
}
res = PassageChopWinding( &in, &out, seperators[ k ] ); res = PassageChopWinding( &in, &out, seperators[ k ] );
if ( res == &out ) { if ( res == &out ) {