diff --git a/tools/quake3/q3map2/bspfile_abstract.cpp b/tools/quake3/q3map2/bspfile_abstract.cpp index f77275e0..ad2146fe 100644 --- a/tools/quake3/q3map2/bspfile_abstract.cpp +++ b/tools/quake3/q3map2/bspfile_abstract.cpp @@ -113,7 +113,7 @@ void BSPFilesCleanup(){ free( bspDrawVerts ); free( bspDrawSurfaces ); bspLightBytes.clear(); - free( bspGridPoints ); + bspGridPoints.clear(); } @@ -504,8 +504,8 @@ void PrintBSPFileSizes( void ){ Sys_Printf( "%9zu lightmaps %9zu\n", bspLightBytes.size() / ( g_game->lightmapSize * g_game->lightmapSize * 3 ), bspLightBytes.size() ); - Sys_Printf( "%9d lightgrid %9d *\n", - numBSPGridPoints, (int) ( numBSPGridPoints * sizeof( *bspGridPoints ) ) ); + Sys_Printf( "%9zu lightgrid %9zu *\n", + bspGridPoints.size(), bspGridPoints.size() * sizeof( bspGridPoints[0] ) ); Sys_Printf( " visibility %9d\n", numBSPVisBytes ); } diff --git a/tools/quake3/q3map2/bspfile_ibsp.cpp b/tools/quake3/q3map2/bspfile_ibsp.cpp index 14bc2c7f..23a9a53c 100644 --- a/tools/quake3/q3map2/bspfile_ibsp.cpp +++ b/tools/quake3/q3map2/bspfile_ibsp.cpp @@ -296,79 +296,22 @@ struct ibspGridPoint_t Vector3b ambient; Vector3b directed; byte latLong[ 2 ]; + ibspGridPoint_t( const bspGridPoint_t& other ) : + ambient( other.ambient[0] ), + directed( other.directed[0] ), + latLong{ other.latLong[0], other.latLong[1] } {} + operator bspGridPoint_t() const { + static_assert( MAX_LIGHTMAPS == 4 ); + return { + { ambient, ambient, ambient, ambient }, + { directed, directed, directed, directed }, + { LS_NORMAL, LS_NONE, LS_NONE, LS_NONE }, + { latLong[0], latLong[1] } + }; + } }; -static void CopyLightGridLumps( ibspHeader_t *header ){ - int i, j; - ibspGridPoint_t *in; - bspGridPoint_t *out; - - - /* get count */ - numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTGRID, sizeof( *in ) ); - - /* allocate buffer */ - bspGridPoints = safe_calloc( numBSPGridPoints * sizeof( *bspGridPoints ) ); - - /* copy */ - in = GetLump( (bspHeader_t*) header, LUMP_LIGHTGRID ); - out = bspGridPoints; - for ( i = 0; i < numBSPGridPoints; i++ ) - { - for ( j = 0; j < MAX_LIGHTMAPS; j++ ) - { - out->ambient[ j ] = in->ambient; - out->directed[ j ] = in->directed; - out->styles[ j ] = LS_NONE; - } - - out->styles[ 0 ] = LS_NORMAL; - - out->latLong[ 0 ] = in->latLong[ 0 ]; - out->latLong[ 1 ] = in->latLong[ 1 ]; - - in++; - out++; - } -} - - -static void AddLightGridLumps( FILE *file, ibspHeader_t *header ){ - int i; - bspGridPoint_t *in; - ibspGridPoint_t *buffer, *out; - - - /* dummy check */ - if ( bspGridPoints == NULL ) { - return; - } - - /* allocate temporary buffer */ - buffer = safe_malloc( numBSPGridPoints * sizeof( *out ) ); - - /* convert */ - in = bspGridPoints; - out = buffer; - for ( i = 0; i < numBSPGridPoints; i++ ) - { - out->ambient = in->ambient[ 0 ]; - out->directed = in->directed[ 0 ]; - - out->latLong[ 0 ] = in->latLong[ 0 ]; - out->latLong[ 1 ] = in->latLong[ 1 ]; - - in++; - out++; - } - - /* write lumps */ - AddLump( file, (bspHeader_t*) header, LUMP_LIGHTGRID, buffer, ( numBSPGridPoints * sizeof( *out ) ) ); - - /* free buffer (ydnar 2002-10-22: [bug 641] thanks Rap70r! */ - free( buffer ); -} /* LoadIBSPFile() @@ -426,7 +369,7 @@ void LoadIBSPFile( const char *filename ){ CopyLump( (bspHeader_t*) header, LUMP_ENTITIES, bspEntData ); - CopyLightGridLumps( header ); + CopyLump( (bspHeader_t*) header, LUMP_LIGHTGRID, bspGridPoints ); /* advertisements */ if ( header->version == 47 && strEqual( g_game->arg, "quakelive" ) ) { // quake live's bsp version minus wolf, et, etut @@ -523,7 +466,7 @@ void WriteIBSPFile( const char *filename ){ AddDrawSurfacesLump( file, header ); AddLump( file, (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes, numBSPVisBytes ); AddLump( file, header->lumps[LUMP_LIGHTMAPS], bspLightBytes ); - AddLightGridLumps( file, header ); + AddLump( file, header->lumps[LUMP_LIGHTGRID], std::vector( bspGridPoints.begin(), bspGridPoints.end() ) ); AddLump( file, header->lumps[LUMP_ENTITIES], bspEntData ); AddLump( file, (bspHeader_t*) header, LUMP_FOGS, bspFogs, numBSPFogs * sizeof( bspFog_t ) ); AddLump( file, (bspHeader_t*) header, LUMP_DRAWINDEXES, bspDrawIndexes, numBSPDrawIndexes * sizeof( bspDrawIndexes[ 0 ] ) ); diff --git a/tools/quake3/q3map2/bspfile_rbsp.cpp b/tools/quake3/q3map2/bspfile_rbsp.cpp index ebe0cf08..3f5121e0 100644 --- a/tools/quake3/q3map2/bspfile_rbsp.cpp +++ b/tools/quake3/q3map2/bspfile_rbsp.cpp @@ -82,84 +82,62 @@ struct rbspHeader_t static void CopyLightGridLumps( rbspHeader_t *header ){ - int i; - unsigned short *inArray; - bspGridPoint_t *in, *out; + std::vector gridPoints; + std::vector gridArray; + CopyLump( (bspHeader_t*) header, LUMP_LIGHTGRID, gridPoints ); + CopyLump( (bspHeader_t*) header, LUMP_LIGHTARRAY, gridArray ); + bspGridPoints.clear(); + bspGridPoints.reserve( gridArray.size() ); - /* get count */ - numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTARRAY, sizeof( *inArray ) ); - - /* allocate buffer */ - bspGridPoints = safe_calloc( numBSPGridPoints * sizeof( *bspGridPoints ) ); - - /* copy */ - inArray = GetLump( (bspHeader_t*) header, LUMP_LIGHTARRAY ); - in = GetLump( (bspHeader_t*) header, LUMP_LIGHTGRID ); - out = bspGridPoints; - for ( i = 0; i < numBSPGridPoints; i++ ) - { - memcpy( out, &in[ *inArray ], sizeof( *in ) ); - inArray++; - out++; - } + for( const auto id : gridArray ) + bspGridPoints.push_back( gridPoints[ id ] ); } static void AddLightGridLumps( FILE *file, rbspHeader_t *header ){ - int i, j, k, c, d; - int numGridPoints, maxGridPoints; - bspGridPoint_t *gridPoints, *in, *out; - int numGridArray; - unsigned short *gridArray; - bool bad; - - /* allocate temporary buffers */ - maxGridPoints = std::min( numBSPGridPoints, MAX_MAP_GRID ); - gridPoints = safe_malloc( maxGridPoints * sizeof( *gridPoints ) ); - gridArray = safe_malloc( numBSPGridPoints * sizeof( *gridArray ) ); - - /* zero out */ - numGridPoints = 0; - numGridArray = numBSPGridPoints; + const size_t maxGridPoints = std::min( bspGridPoints.size(), size_t( MAX_MAP_GRID ) ); + std::vector gridPoints; + std::vector gridArray( bspGridPoints.size() ); /* for each bsp grid point, find an approximate twin */ - Sys_Printf( "Storing lightgrid: %d points\n", numBSPGridPoints ); - for ( i = 0; i < numGridArray; i++ ) + Sys_Printf( "Storing lightgrid: %zu points\n", bspGridPoints.size() ); + for ( size_t i = 0; i < gridArray.size(); ++i ) { /* get points */ - in = &bspGridPoints[ i ]; + const bspGridPoint_t& in = bspGridPoints[ i ]; /* walk existing list */ - for ( j = 0; j < numGridPoints; j++ ) + size_t j; + for ( j = 0; j < gridPoints.size(); ++j ) { /* get point */ - out = &gridPoints[ j ]; + const bspGridPoint_t& out = gridPoints[ j ]; /* compare styles */ - if ( memcmp( in->styles, out->styles, MAX_LIGHTMAPS ) ) { + if ( memcmp( in.styles, out.styles, MAX_LIGHTMAPS ) ) { continue; } /* compare direction */ - d = abs( in->latLong[ 0 ] - out->latLong[ 0 ] ); - if ( d < ( 255 - LG_EPSILON ) && d > LG_EPSILON ) { + if ( const int d = abs( in.latLong[ 0 ] - out.latLong[ 0 ] ); + d < ( 255 - LG_EPSILON ) && d > LG_EPSILON ) { continue; } - d = abs( in->latLong[ 1 ] - out->latLong[ 1 ] ); - if ( d < 255 - LG_EPSILON && d > LG_EPSILON ) { + if ( const int d = abs( in.latLong[ 1 ] - out.latLong[ 1 ] ); + d < 255 - LG_EPSILON && d > LG_EPSILON ) { continue; } /* compare light */ - bad = false; - for ( k = 0; ( k < MAX_LIGHTMAPS && !bad ); k++ ) + bool bad = false; + for ( int k = 0; ( k < MAX_LIGHTMAPS && !bad ); k++ ) { - for ( c = 0; c < 3; c++ ) + for ( int c = 0; c < 3; c++ ) { - if ( abs( (int) in->ambient[ k ][ c ] - (int) out->ambient[ k ][ c ] ) > LG_EPSILON || - abs( (int) in->directed[ k ][ c ] - (int) out->directed[ k ][ c ] ) > LG_EPSILON ) { + if ( abs( (int) in.ambient[ k ][ c ] - (int) out.ambient[ k ][ c ] ) > LG_EPSILON || + abs( (int) in.directed[ k ][ c ] - (int) out.directed[ k ][ c ] ) > LG_EPSILON ) { bad = true; break; } @@ -179,23 +157,18 @@ static void AddLightGridLumps( FILE *file, rbspHeader_t *header ){ gridArray[ i ] = (unsigned short) j; /* if no sample found, add a new one */ - if ( j >= numGridPoints && numGridPoints < maxGridPoints ) { - out = &gridPoints[ numGridPoints++ ]; - memcpy( out, in, sizeof( *in ) ); + if ( j >= gridPoints.size() && gridPoints.size() < maxGridPoints ) { + gridPoints.push_back( in ); } } /* swap array */ - for ( i = 0; i < numGridArray; i++ ) - gridArray[ i ] = LittleShort( gridArray[ i ] ); + for ( auto&& a : gridArray ) + a = LittleShort( a ); /* write lumps */ - AddLump( file, (bspHeader_t*) header, LUMP_LIGHTGRID, gridPoints, ( numGridPoints * sizeof( *gridPoints ) ) ); - AddLump( file, (bspHeader_t*) header, LUMP_LIGHTARRAY, gridArray, ( numGridArray * sizeof( *gridArray ) ) ); - - /* free buffers */ - free( gridPoints ); - free( gridArray ); + AddLump( file, header->lumps[LUMP_LIGHTGRID], gridPoints ); + AddLump( file, header->lumps[LUMP_LIGHTARRAY], gridArray ); } diff --git a/tools/quake3/q3map2/convert_json.cpp b/tools/quake3/q3map2/convert_json.cpp index fe6be506..d94aa32f 100644 --- a/tools/quake3/q3map2/convert_json.cpp +++ b/tools/quake3/q3map2/convert_json.cpp @@ -352,7 +352,7 @@ static void write_json( const char *directory ){ } { doc.RemoveAllMembers(); - for_indexed( auto&& point : Span( bspGridPoints, numBSPGridPoints ) ){ + for_indexed( const auto& point : bspGridPoints ){ rapidjson::Value value( rapidjson::kObjectType ); value.AddMember( "ambient", value_for( point.ambient, all ), all ); value.AddMember( "directed", value_for( point.directed, all ), all ); @@ -558,16 +558,13 @@ static void read_json( const char *directory ){ } { const auto doc = load_json( StringOutputStream( 256 )( directory, "GridPoints.json" ) ); - static std::vector items; for( auto&& obj : doc.GetObj() ){ - auto&& item = items.emplace_back(); + auto&& item = bspGridPoints.emplace_back(); value_to( obj.value["ambient"], item.ambient ); value_to( obj.value["directed"], item.directed ); value_to_array( obj.value["styles"], item.styles ); value_to_array( obj.value["latLong"], item.latLong ); } - bspGridPoints = items.data(); - numBSPGridPoints = items.size(); } } diff --git a/tools/quake3/q3map2/light.cpp b/tools/quake3/q3map2/light.cpp index ba7e82c8..997c2d23 100644 --- a/tools/quake3/q3map2/light.cpp +++ b/tools/quake3/q3map2/light.cpp @@ -1670,14 +1670,11 @@ void SetupGrid( void ){ Sys_FPrintf( SYS_VRB, "Storing adjusted grid size\n" ); } - /* 2nd variable. fixme: is this silly? */ - numBSPGridPoints = numRawGridPoints; - /* allocate lightgrid */ rawGridPoints = safe_calloc( numRawGridPoints * sizeof( *rawGridPoints ) ); - free( bspGridPoints ); - bspGridPoints = safe_calloc( numBSPGridPoints * sizeof( *bspGridPoints ) ); + bspGridPoints.resize( numRawGridPoints ); + memset( bspGridPoints.data(), 0, bspGridPoints.size() * sizeof( bspGridPoints[0] ) ); /* clear lightgrid */ for ( i = 0; i < numRawGridPoints; i++ ) @@ -1779,8 +1776,8 @@ void LightWorld( bool fastAllocate ){ inGrid = true; RunThreadsOnIndividual( numRawGridPoints, true, TraceGrid ); inGrid = false; - Sys_Printf( "%d x %d x %d = %d grid\n", - gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints ); + Sys_Printf( "%d x %d x %d = %zu grid\n", + gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], bspGridPoints.size() ); /* ydnar: emit statistics on light culling */ Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled ); diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 1a42df1b..1311906f 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -360,9 +360,9 @@ enum bspSurfaceType_t struct bspGridPoint_t { - Vector3b ambient[ MAX_LIGHTMAPS ]; - Vector3b directed[ MAX_LIGHTMAPS ]; - byte styles[ MAX_LIGHTMAPS ]; + Vector3b ambient[ MAX_LIGHTMAPS ]; /* RBSP - array */ + Vector3b directed[ MAX_LIGHTMAPS ]; /* RBSP - array */ + byte styles[ MAX_LIGHTMAPS ]; /* RBSP - whole */ byte latLong[ 2 ]; }; @@ -2356,8 +2356,7 @@ Q_EXTERN std::vector bspBrushSides; Q_EXTERN std::vector bspLightBytes; -Q_EXTERN int numBSPGridPoints Q_ASSIGN( 0 ); -Q_EXTERN bspGridPoint_t *bspGridPoints Q_ASSIGN( NULL ); +Q_EXTERN std::vector bspGridPoints; Q_EXTERN int numBSPVisBytes Q_ASSIGN( 0 ); Q_EXTERN byte bspVisBytes[ MAX_MAP_VISIBILITY ];