From 98f9023cdc5e59464226bd81c033cc160d8f64c8 Mon Sep 17 00:00:00 2001 From: Garux Date: Thu, 11 Mar 2021 10:57:17 +0300 Subject: [PATCH] minor tweaks --- tools/quake3/common/qmath.h | 2 +- tools/quake3/q3map2/convert_map.cpp | 16 ++++---- tools/quake3/q3map2/map.cpp | 61 +++++++++++++--------------- tools/quake3/q3map2/q3map2.h | 6 +-- tools/quake3/q3map2/surface.cpp | 35 +++++++--------- tools/quake3/q3map2/surface_meta.cpp | 6 +-- 6 files changed, 56 insertions(+), 70 deletions(-) diff --git a/tools/quake3/common/qmath.h b/tools/quake3/common/qmath.h index 10d853a8..7bc9e96a 100644 --- a/tools/quake3/common/qmath.h +++ b/tools/quake3/common/qmath.h @@ -130,7 +130,7 @@ inline Color4b color_to_byte( const Color4f& color ){ template T VectorNormalize( BasicVector3& vector ) { - DoubleVector3 v( vector ); // intermediate vector to be sure to do in double + const DoubleVector3 v( vector ); // intermediate vector to be sure to do in double const double length = vector3_length( v ); if ( length == 0 ) { diff --git a/tools/quake3/q3map2/convert_map.cpp b/tools/quake3/q3map2/convert_map.cpp index 2c61bd55..2c55c9ab 100644 --- a/tools/quake3/q3map2/convert_map.cpp +++ b/tools/quake3/q3map2/convert_map.cpp @@ -349,7 +349,6 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or side_t *buildSide; bspShader_t *shader; const char *texture; - plane_t *buildPlane; Vector3 pts[ 3 ]; bspDrawVert_t *vert[3]; @@ -437,7 +436,7 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or buildSide = &buildBrush->sides[ i ]; /* get plane */ - buildPlane = &mapplanes[ buildSide->planenum ]; + const plane_t& buildPlane = mapplanes[ buildSide->planenum ]; /* dummy check */ if ( buildSide->shaderInfo == NULL || buildSide->winding == NULL ) { @@ -467,7 +466,7 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or /* recheck and fix winding points, fails occur somehow */ int match = 0; for ( j = 0; j < buildSide->winding->numpoints; j++ ){ - if ( fabs( plane3_distance_to_point( buildPlane->plane, buildSide->winding->p[ j ] ) ) >= distanceEpsilon ) { + if ( fabs( plane3_distance_to_point( buildPlane.plane, buildSide->winding->p[ j ] ) ) >= distanceEpsilon ) { continue; } else{ @@ -503,8 +502,8 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or } else{ Vector3 vecs[ 2 ]; - MakeNormalVectors( buildPlane->normal(), vecs[ 0 ], vecs[ 1 ] ); - pts[ 0 ] = buildPlane->normal() * buildPlane->dist() + origin; + MakeNormalVectors( buildPlane.normal(), vecs[ 0 ], vecs[ 1 ] ); + pts[ 0 ] = buildPlane.normal() * buildPlane.dist() + origin; pts[ 1 ] = pts[ 0 ] + vecs[ 0 ] * 256.0f; pts[ 2 ] = pts[ 0 ] + vecs[ 1 ] * 256.0f; //Sys_Printf( "not\n" ); @@ -518,7 +517,7 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or Vector2 stI, stJ, stK; float D, D0, D1, D2; - ComputeAxisBase( buildPlane->normal(), texX, texY ); + ComputeAxisBase( buildPlane.normal(), texX, texY ); xyI[0] = vector3_dot( vert[0]->xyz, texX ); xyI[1] = vector3_dot( vert[0]->xyz, texY ); @@ -562,7 +561,7 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or } else{ fprintf( stderr, "degenerate triangle found when solving texMat equations for\n(%f %f %f) (%f %f %f) (%f %f %f)\n( %f %f %f )\n( %f %f %f ) -> ( %f %f )\n( %f %f %f ) -> ( %f %f )\n( %f %f %f ) -> ( %f %f )\n", - buildPlane->normal()[0], buildPlane->normal()[1], buildPlane->normal()[2], + buildPlane.normal()[0], buildPlane.normal()[1], buildPlane.normal()[2], vert[0]->normal[0], vert[0]->normal[1], vert[0]->normal[2], texX[0], texX[1], texX[2], texY[0], texY[1], texY[2], vert[0]->xyz[0], vert[0]->xyz[1], vert[0]->xyz[2], xyI[0], xyI[1], @@ -587,7 +586,6 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or { // invert QuakeTextureVecs int i; - Vector3 vecs[2]; int sv, tv; float stI[2], stJ[2], stK[2]; Vector3 sts[2]; @@ -595,7 +593,7 @@ static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, const Vector3& or float rotate; float D, D0, D1, D2; - TextureAxisFromPlane( buildPlane, vecs[0], vecs[1] ); + const auto vecs = TextureAxisFromPlane( buildPlane ); if ( vecs[0][0] ) { sv = 0; } diff --git a/tools/quake3/q3map2/map.cpp b/tools/quake3/q3map2/map.cpp index c5eec734..cada75ce 100644 --- a/tools/quake3/q3map2/map.cpp +++ b/tools/quake3/q3map2/map.cpp @@ -54,7 +54,7 @@ int c_structural; ydnar: replaced with variable epsilon for djbob */ -bool PlaneEqual( const plane_t *p, const Plane3f& plane ){ +bool PlaneEqual( const plane_t& p, const Plane3f& plane ){ /* get local copies */ const float ne = normalEpsilon; const float de = distanceEpsilon; @@ -64,10 +64,10 @@ bool PlaneEqual( const plane_t *p, const Plane3f& plane ){ // (the epsilons may be zero). We want to use '<' instead of '<=' to be // consistent with the true meaning of "epsilon", and also because other // parts of the code uses this inequality. - if ( ( p->dist() == plane.dist() || fabs( p->dist() - plane.dist() ) < de ) && - ( p->normal()[0] == plane.normal()[0] || fabs( p->normal()[0] - plane.normal()[0] ) < ne ) && - ( p->normal()[1] == plane.normal()[1] || fabs( p->normal()[1] - plane.normal()[1] ) < ne ) && - ( p->normal()[2] == plane.normal()[2] || fabs( p->normal()[2] - plane.normal()[2] ) < ne ) ) { + if ( ( p.dist() == plane.dist() || fabs( p.dist() - plane.dist() ) < de ) && + ( p.normal()[0] == plane.normal()[0] || fabs( p.normal()[0] - plane.normal()[0] ) < ne ) && + ( p.normal()[1] == plane.normal()[1] || fabs( p.normal()[1] - plane.normal()[1] ) < ne ) && + ( p.normal()[2] == plane.normal()[2] || fabs( p.normal()[2] - plane.normal()[2] ) < ne ) ) { return true; } @@ -359,7 +359,7 @@ int FindFloatPlane( const Plane3f& inplane, int numPoints, const Vector3 *points plane_t *p = &mapplanes[pidx]; /* do standard plane compare */ - if ( !PlaneEqual( p, plane ) ) { + if ( !PlaneEqual( *p, plane ) ) { continue; } @@ -405,7 +405,7 @@ int FindFloatPlane( const Plane3f& inplane, int numPoints, const Vector3 *points #endif for ( i = 0, p = mapplanes; i < nummapplanes; i++, p++ ) { - if ( !PlaneEqual( p, plane ) ) { + if ( !PlaneEqual( *p, plane ) ) { continue; } @@ -728,7 +728,7 @@ void AddBrushBevels( void ){ for ( k = 0; k < buildBrush->numsides; k++ ) { // if this plane has allready been used, skip it - if ( PlaneEqual( &mapplanes[buildBrush->sides[k].planenum], plane ) ) { + if ( PlaneEqual( mapplanes[buildBrush->sides[k].planenum], plane ) ) { if( buildBrush->sides[k].bevel ){ /* handle bevel surfaceflags */ buildBrush->sides[k].surfaceFlags |= ( s->surfaceFlags & surfaceFlagsMask ); } @@ -891,31 +891,31 @@ brush_t *FinishBrush( bool noCollapseGroups ){ (must be identical in radiant!) */ -Vector3 baseaxis[18] = +const Vector3 baseaxis[18] = { - {0,0,1}, {1,0,0}, {0,-1,0}, // floor - {0,0,-1}, {1,0,0}, {0,-1,0}, // ceiling - {1,0,0}, {0,1,0}, {0,0,-1}, // west wall - {-1,0,0}, {0,1,0}, {0,0,-1}, // east wall - {0,1,0}, {1,0,0}, {0,0,-1}, // south wall - {0,-1,0}, {1,0,0}, {0,0,-1} // north wall + g_vector3_axis_z, g_vector3_axis_x, -g_vector3_axis_y, // floor + -g_vector3_axis_z, g_vector3_axis_x, -g_vector3_axis_y, // ceiling + g_vector3_axis_x, g_vector3_axis_y, -g_vector3_axis_z, // west wall + -g_vector3_axis_x, g_vector3_axis_y, -g_vector3_axis_z, // east wall + g_vector3_axis_y, g_vector3_axis_x, -g_vector3_axis_z, // south wall + -g_vector3_axis_y, g_vector3_axis_x, -g_vector3_axis_z // north wall }; -void TextureAxisFromPlane( const plane_t *pln, Vector3& xv, Vector3& yv ){ +std::array TextureAxisFromPlane( const plane_t& plane ){ float best = 0; int bestaxis = 0; for ( int i = 0 ; i < 6 ; i++ ) { - const float dot = vector3_dot( pln->normal(), baseaxis[i * 3] ); + const float dot = vector3_dot( plane.normal(), baseaxis[i * 3] ); if ( dot > best + 0.0001f ) { /* ydnar: bug 637 fix, suggested by jmonroe */ best = dot; bestaxis = i; } } - xv = baseaxis[bestaxis * 3 + 1]; - yv = baseaxis[bestaxis * 3 + 2]; + return { baseaxis[bestaxis * 3 + 1], + baseaxis[bestaxis * 3 + 2] }; } @@ -925,16 +925,12 @@ void TextureAxisFromPlane( const plane_t *pln, Vector3& xv, Vector3& yv ){ creates world-to-texture mapping vecs for crappy quake plane arrangements */ -void QuakeTextureVecs( const plane_t *plane, float shift[ 2 ], float rotate, float scale[ 2 ], Vector4 mappingVecs[ 2 ] ){ - Vector3 vecs[2]; +void QuakeTextureVecs( const plane_t& plane, float shift[ 2 ], float rotate, float scale[ 2 ], Vector4 mappingVecs[ 2 ] ){ int sv, tv; - float ang, sinv, cosv; - float ns, nt; - int i; + float sinv, cosv; + auto vecs = TextureAxisFromPlane( plane ); - TextureAxisFromPlane( plane, vecs[0], vecs[1] ); - if ( !scale[0] ) { scale[0] = 1; } @@ -957,7 +953,7 @@ void QuakeTextureVecs( const plane_t *plane, float shift[ 2 ], float rotate, flo } else { - ang = degrees_to_radians( rotate ); + const double ang = degrees_to_radians( rotate ); sinv = sin( ang ); cosv = cos( ang ); } @@ -982,15 +978,14 @@ void QuakeTextureVecs( const plane_t *plane, float shift[ 2 ], float rotate, flo tv = 2; } - for ( i = 0 ; i < 2 ; i++ ) { - ns = cosv * vecs[i][sv] - sinv * vecs[i][tv]; - nt = sinv * vecs[i][sv] + cosv * vecs[i][tv]; + for ( int i = 0 ; i < 2 ; i++ ) { + const float ns = cosv * vecs[i][sv] - sinv * vecs[i][tv]; + const float nt = sinv * vecs[i][sv] + cosv * vecs[i][tv]; vecs[i][sv] = ns; vecs[i][tv] = nt; - } - for ( i = 0 ; i < 2 ; i++ ) mappingVecs[i].vec3() = vecs[i] / scale[i]; + } mappingVecs[0][3] = shift[0]; mappingVecs[1][3] = shift[1]; @@ -1181,7 +1176,7 @@ static void ParseRawBrush( bool onlyLights ){ /* bp: get the texture mapping for this texturedef / plane combination */ if ( g_brushType == EBrushType::Quake ) { - QuakeTextureVecs( &mapplanes[ planenum ], shift, rotate, scale, side->vecs ); + QuakeTextureVecs( mapplanes[ planenum ], shift, rotate, scale, side->vecs ); } } diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 51133f9c..3669d66e 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -1606,7 +1606,7 @@ int FindFloatPlane( const Plane3f& plane, int numPoints, inline int FindFloatPlane( const Vector3& normal, float dist, int numPoints, const Vector3 *points ){ return FindFloatPlane( Plane3f( normal, dist ), numPoints, points ); } -bool PlaneEqual( const plane_t *p, const Plane3f& plane ); +bool PlaneEqual( const plane_t& p, const Plane3f& plane ); void AddBrushBevels( void ); brush_t *FinishBrush( bool noCollapseGroups ); @@ -1704,7 +1704,7 @@ void FinishSurface( mapDrawSurface_t *ds ); void StripFaceSurface( mapDrawSurface_t *ds ); void MaxAreaFaceSurface( mapDrawSurface_t *ds ); bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ); -bool CalcLightmapAxis( const Vector3& normal, Vector3& axis ); +Vector3 CalcLightmapAxis( const Vector3& normal ); void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ); void ClassifyEntitySurfaces( entity_t *e ); void TidyEntitySurfaces( entity_t *e ); @@ -1770,7 +1770,7 @@ void ProcessDecals( void ); void MakeEntityDecals( entity_t *e ); /* map.c */ -void TextureAxisFromPlane( const plane_t *pln, Vector3& xv, Vector3& yv ); +std::array TextureAxisFromPlane( const plane_t& plane ); /* vis.c */ fixedWinding_t *NewFixedWinding( int points ); diff --git a/tools/quake3/q3map2/surface.cpp b/tools/quake3/q3map2/surface.cpp index fb8ee02e..d0e5b437 100644 --- a/tools/quake3/q3map2/surface.cpp +++ b/tools/quake3/q3map2/surface.cpp @@ -391,50 +391,43 @@ bool CalcSurfaceTextureRange( mapDrawSurface_t *ds ){ gives closed lightmap axis for a plane normal */ -bool CalcLightmapAxis( const Vector3& normal, Vector3& axis ){ - Vector3 absolute; - - +Vector3 CalcLightmapAxis( const Vector3& normal ){ /* test */ if ( normal == g_vector3_identity ) { - axis.set( 0 ); - return false; + return g_vector3_identity; } /* get absolute normal */ - absolute[ 0 ] = fabs( normal[ 0 ] ); - absolute[ 1 ] = fabs( normal[ 1 ] ); - absolute[ 2 ] = fabs( normal[ 2 ] ); + const Vector3 absolute( fabs( normal[ 0 ] ), + fabs( normal[ 1 ] ), + fabs( normal[ 2 ] ) ); - /* test and set */ + /* test and return */ if ( absolute[ 2 ] > absolute[ 0 ] - 0.0001f && absolute[ 2 ] > absolute[ 1 ] - 0.0001f ) { if ( normal[ 2 ] > 0.0f ) { - axis = g_vector3_axis_z; + return g_vector3_axis_z; } else{ - axis = -g_vector3_axis_z; + return -g_vector3_axis_z; } } else if ( absolute[ 0 ] > absolute[ 1 ] - 0.0001f && absolute[ 0 ] > absolute[ 2 ] - 0.0001f ) { if ( normal[ 0 ] > 0.0f ) { - axis = g_vector3_axis_x; + return g_vector3_axis_x; } else{ - axis = -g_vector3_axis_x; + return -g_vector3_axis_x; } } else { if ( normal[ 1 ] > 0.0f ) { - axis = g_vector3_axis_y; + return g_vector3_axis_y; } else{ - axis = -g_vector3_axis_y; + return -g_vector3_axis_y; } } - - /* return ok */ - return true; } @@ -564,14 +557,14 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ } /* the shader can specify an explicit lightmap axis */ - if ( si->lightmapAxis[ 0 ] || si->lightmapAxis[ 1 ] || si->lightmapAxis[ 2 ] ) { + if ( si->lightmapAxis != g_vector3_identity ) { ds->lightmapAxis = si->lightmapAxis; } else if ( ds->type == ESurfaceType::ForcedMeta ) { ds->lightmapAxis.set( 0 ); } else if ( ds->planar ) { - CalcLightmapAxis( plane.normal(), ds->lightmapAxis ); + ds->lightmapAxis = CalcLightmapAxis( plane.normal() ); } else { diff --git a/tools/quake3/q3map2/surface_meta.cpp b/tools/quake3/q3map2/surface_meta.cpp index b262c82b..68680e84 100644 --- a/tools/quake3/q3map2/surface_meta.cpp +++ b/tools/quake3/q3map2/surface_meta.cpp @@ -159,15 +159,15 @@ int FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, b /* ydnar 2002-10-04: set lightmap axis if not already set */ if ( !( src->si->compileFlags & C_VERTEXLIT ) && - src->lightmapAxis[ 0 ] == 0.0f && src->lightmapAxis[ 1 ] == 0.0f && src->lightmapAxis[ 2 ] == 0.0f ) { + src->lightmapAxis == g_vector3_identity ) { /* the shader can specify an explicit lightmap axis */ - if ( src->si->lightmapAxis[ 0 ] || src->si->lightmapAxis[ 1 ] || src->si->lightmapAxis[ 2 ] ) { + if ( src->si->lightmapAxis != g_vector3_identity ) { src->lightmapAxis = src->si->lightmapAxis; } /* new axis-finding code */ else{ - CalcLightmapAxis( src->plane.normal(), src->lightmapAxis ); + src->lightmapAxis = CalcLightmapAxis( src->plane.normal() ); } }