diff --git a/tools/quake3/q3map2/bspfile_abstract.cpp b/tools/quake3/q3map2/bspfile_abstract.cpp index d4830b5a..1512570c 100644 --- a/tools/quake3/q3map2/bspfile_abstract.cpp +++ b/tools/quake3/q3map2/bspfile_abstract.cpp @@ -189,7 +189,7 @@ void SwapBSPFile( void ){ } /* drawindexes */ - SwapBlock( (int*) bspDrawIndexes, numBSPDrawIndexes * sizeof( bspDrawIndexes[0] ) ); + SwapBlock( bspDrawIndexes ); /* drawsurfs */ /* note: rbsp files (and hence q3map2 abstract bsp) have byte lightstyles index arrays, this follows sof2map convention */ @@ -460,8 +460,8 @@ void PrintBSPFileSizes( void ){ trisoupCount ); Sys_Printf( "%9zu drawverts %9zu *\n", bspDrawVerts.size(), bspDrawVerts.size() * sizeof( bspDrawVerts[0] ) ); - Sys_Printf( "%9d drawindexes %9d\n", - numBSPDrawIndexes, (int) ( numBSPDrawIndexes * sizeof( *bspDrawIndexes ) ) ); + Sys_Printf( "%9zu drawindexes %9zu\n", + bspDrawIndexes.size(), bspDrawIndexes.size() * sizeof( bspDrawIndexes[0] ) ); Sys_Printf( "\n" ); Sys_Printf( "%9zu lightmaps %9zu\n", diff --git a/tools/quake3/q3map2/bspfile_ibsp.cpp b/tools/quake3/q3map2/bspfile_ibsp.cpp index 0b0c7412..c3842013 100644 --- a/tools/quake3/q3map2/bspfile_ibsp.cpp +++ b/tools/quake3/q3map2/bspfile_ibsp.cpp @@ -319,7 +319,7 @@ void LoadIBSPFile( const char *filename ){ numBSPFogs = CopyLump( (bspHeader_t*) header, LUMP_FOGS, bspFogs, sizeof( bspFog_t ) ); // TODO fix overflow - numBSPDrawIndexes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_DRAWINDEXES, (void **) &bspDrawIndexes, sizeof( bspDrawIndexes[ 0 ] ), &allocatedBSPDrawIndexes ); + CopyLump( (bspHeader_t*) header, LUMP_DRAWINDEXES, bspDrawIndexes ); CopyLump( (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes ); @@ -427,7 +427,7 @@ void WriteIBSPFile( const char *filename ){ 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 ] ) ); + AddLump( file, header->lumps[LUMP_DRAWINDEXES], bspDrawIndexes ); /* advertisements */ AddLump( file, (bspHeader_t*) header, LUMP_ADVERTISEMENTS, bspAds, numBSPAds * sizeof( bspAdvertisement_t ) ); diff --git a/tools/quake3/q3map2/bspfile_rbsp.cpp b/tools/quake3/q3map2/bspfile_rbsp.cpp index d17da203..8e6e6da3 100644 --- a/tools/quake3/q3map2/bspfile_rbsp.cpp +++ b/tools/quake3/q3map2/bspfile_rbsp.cpp @@ -223,7 +223,7 @@ void LoadRBSPFile( const char *filename ){ numBSPFogs = CopyLump( (bspHeader_t*) header, LUMP_FOGS, bspFogs, sizeof( bspFogs[ 0 ] ) ); - numBSPDrawIndexes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_DRAWINDEXES, (void **) &bspDrawIndexes, sizeof( bspDrawIndexes[ 0 ] ), &allocatedBSPDrawIndexes ); + CopyLump( (bspHeader_t*) header, LUMP_DRAWINDEXES, bspDrawIndexes ); CopyLump( (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes ); @@ -289,7 +289,7 @@ void WriteRBSPFile( const char *filename ){ AddLightGridLumps( file, header ); 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 ] ) ); + AddLump( file, header->lumps[LUMP_DRAWINDEXES], bspDrawIndexes ); /* emit bsp size */ size = ftell( file ); diff --git a/tools/quake3/q3map2/convert_ase.cpp b/tools/quake3/q3map2/convert_ase.cpp index 6efe1e26..ae2dac69 100644 --- a/tools/quake3/q3map2/convert_ase.cpp +++ b/tools/quake3/q3map2/convert_ase.cpp @@ -42,7 +42,6 @@ int numLightmapsASE = 0; static void ConvertSurface( FILE *f, int modelNum, bspDrawSurface_t *ds, int surfaceNum, const Vector3& origin, const std::vector& lmIndices ){ - int i, face, a, b, c; char name[ 1024 ]; @@ -84,7 +83,7 @@ static void ConvertSurface( FILE *f, int modelNum, bspDrawSurface_t *ds, int sur /* export vertex xyz */ fprintf( f, "\t\t*MESH_VERTEX_LIST\t{\r\n" ); - for ( i = 0; i < ds->numVerts; i++ ) + for ( int i = 0; i < ds->numVerts; i++ ) { const bspDrawVert_t& dv = bspDrawVerts[ ds->firstVert + i ]; fprintf( f, "\t\t\t*MESH_VERTEX\t%d\t%f\t%f\t%f\r\n", i, dv.xyz[ 0 ], dv.xyz[ 1 ], dv.xyz[ 2 ] ); @@ -93,12 +92,12 @@ static void ConvertSurface( FILE *f, int modelNum, bspDrawSurface_t *ds, int sur /* export faces */ fprintf( f, "\t\t*MESH_FACE_LIST\t{\r\n" ); - for ( i = 0; i < ds->numIndexes; i += 3 ) + for ( int i = 0; i < ds->numIndexes; i += 3 ) { - face = ( i / 3 ); - a = bspDrawIndexes[ i + ds->firstIndex ]; - c = bspDrawIndexes[ i + ds->firstIndex + 1 ]; - b = bspDrawIndexes[ i + ds->firstIndex + 2 ]; + const int face = ( i / 3 ); + const int a = bspDrawIndexes[ i + ds->firstIndex ]; + const int c = bspDrawIndexes[ i + ds->firstIndex + 1 ]; + const int b = bspDrawIndexes[ i + ds->firstIndex + 2 ]; fprintf( f, "\t\t\t*MESH_FACE\t%d\tA:\t%d\tB:\t%d\tC:\t%d\tAB:\t1\tBC:\t1\tCA:\t1\t*MESH_SMOOTHING\t0\t*MESH_MTLID\t0\r\n", face, a, b, c ); } @@ -107,7 +106,7 @@ static void ConvertSurface( FILE *f, int modelNum, bspDrawSurface_t *ds, int sur /* export vertex st */ fprintf( f, "\t\t*MESH_NUMTVERTEX\t%d\r\n", ds->numVerts ); fprintf( f, "\t\t*MESH_TVERTLIST\t{\r\n" ); - for ( i = 0; i < ds->numVerts; i++ ) + for ( int i = 0; i < ds->numVerts; i++ ) { const bspDrawVert_t& dv = bspDrawVerts[ ds->firstVert + i ]; if ( lightmapsAsTexcoord ) { @@ -122,24 +121,24 @@ static void ConvertSurface( FILE *f, int modelNum, bspDrawSurface_t *ds, int sur /* export texture faces */ fprintf( f, "\t\t*MESH_NUMTVFACES\t%d\r\n", ds->numIndexes / 3 ); fprintf( f, "\t\t*MESH_TFACELIST\t{\r\n" ); - for ( i = 0; i < ds->numIndexes; i += 3 ) + for ( int i = 0; i < ds->numIndexes; i += 3 ) { - face = ( i / 3 ); - a = bspDrawIndexes[ i + ds->firstIndex ]; - c = bspDrawIndexes[ i + ds->firstIndex + 1 ]; - b = bspDrawIndexes[ i + ds->firstIndex + 2 ]; + const int face = ( i / 3 ); + const int a = bspDrawIndexes[ i + ds->firstIndex ]; + const int c = bspDrawIndexes[ i + ds->firstIndex + 1 ]; + const int b = bspDrawIndexes[ i + ds->firstIndex + 2 ]; fprintf( f, "\t\t\t*MESH_TFACE\t%d\t%d\t%d\t%d\r\n", face, a, b, c ); } fprintf( f, "\t\t}\r\n" ); /* export vertex normals */ fprintf( f, "\t\t*MESH_NORMALS\t{\r\n" ); - for ( i = 0; i < ds->numIndexes; i += 3 ) + for ( int i = 0; i < ds->numIndexes; i += 3 ) { - face = ( i / 3 ); - a = bspDrawIndexes[ i + ds->firstIndex ]; - b = bspDrawIndexes[ i + ds->firstIndex + 1 ]; - c = bspDrawIndexes[ i + ds->firstIndex + 2 ]; + const int face = ( i / 3 ); + const int a = bspDrawIndexes[ i + ds->firstIndex ]; + const int b = bspDrawIndexes[ i + ds->firstIndex + 1 ]; + const int c = bspDrawIndexes[ i + ds->firstIndex + 2 ]; const Vector3 normal = VectorNormalized( bspDrawVerts[ a ].normal + bspDrawVerts[ b ].normal + bspDrawVerts[ c ].normal ); fprintf( f, "\t\t\t*MESH_FACENORMAL\t%d\t%f\t%f\t%f\r\n", face, normal[ 0 ], normal[ 1 ], normal[ 2 ] ); for( const auto idx : { a, b, c } ){ diff --git a/tools/quake3/q3map2/convert_json.cpp b/tools/quake3/q3map2/convert_json.cpp index 20b029a3..dd7c0883 100644 --- a/tools/quake3/q3map2/convert_json.cpp +++ b/tools/quake3/q3map2/convert_json.cpp @@ -311,7 +311,7 @@ static void write_json( const char *directory ){ } { doc.RemoveAllMembers(); - for_indexed( auto&& index : Span( bspDrawIndexes, numBSPDrawIndexes ) ){ + for_indexed( const auto& index : bspDrawIndexes ){ rapidjson::Value value( rapidjson::kObjectType ); value.AddMember( "Num", index, all ); doc.AddMember( rapidjson::Value( StringOutputStream( 16 )( "DrawIndex#", i ).c_str(), all ), value, all ); @@ -516,13 +516,10 @@ static void read_json( const char *directory ){ } { const auto doc = load_json( StringOutputStream( 256 )( directory, "DrawIndexes.json" ) ); - static std::vector items; for( auto&& obj : doc.GetObj() ){ - auto&& item = items.emplace_back(); + auto&& item = bspDrawIndexes.emplace_back(); item = obj.value["Num"].GetInt(); } - bspDrawIndexes = items.data(); - numBSPDrawIndexes = items.size(); } { const auto doc = load_json( StringOutputStream( 256 )( directory, "VisBytes.json" ) ); diff --git a/tools/quake3/q3map2/convert_obj.cpp b/tools/quake3/q3map2/convert_obj.cpp index 09e1b072..6ce64121 100644 --- a/tools/quake3/q3map2/convert_obj.cpp +++ b/tools/quake3/q3map2/convert_obj.cpp @@ -45,8 +45,6 @@ int objVertexCount = 0; int objLastShaderNum = -1; static void ConvertSurfaceToOBJ( FILE *f, int modelNum, bspDrawSurface_t *ds, int surfaceNum, const Vector3& origin, const std::vector& lmIndices ){ - int i, a, b, c; - /* ignore patches for now */ if ( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP ) { return; @@ -88,7 +86,7 @@ static void ConvertSurfaceToOBJ( FILE *f, int modelNum, bspDrawSurface_t *ds, in } /* export vertex */ - for ( i = 0; i < ds->numVerts; i++ ) + for ( int i = 0; i < ds->numVerts; i++ ) { const bspDrawVert_t& dv = bspDrawVerts[ ds->firstVert + i ]; fprintf( f, "# vertex %d\r\n", i + objVertexCount + 1 ); @@ -103,11 +101,11 @@ static void ConvertSurfaceToOBJ( FILE *f, int modelNum, bspDrawSurface_t *ds, in } /* export faces */ - for ( i = 0; i < ds->numIndexes; i += 3 ) + for ( int i = 0; i < ds->numIndexes; i += 3 ) { - a = bspDrawIndexes[ i + ds->firstIndex ]; - c = bspDrawIndexes[ i + ds->firstIndex + 1 ]; - b = bspDrawIndexes[ i + ds->firstIndex + 2 ]; + const int a = bspDrawIndexes[ i + ds->firstIndex ]; + const int c = bspDrawIndexes[ i + ds->firstIndex + 1 ]; + const int b = bspDrawIndexes[ i + ds->firstIndex + 2 ]; fprintf( f, "f %d/%d/%d %d/%d/%d %d/%d/%d\r\n", a + objVertexCount + 1, a + objVertexCount + 1, a + objVertexCount + 1, b + objVertexCount + 1, b + objVertexCount + 1, b + objVertexCount + 1, diff --git a/tools/quake3/q3map2/light_trace.cpp b/tools/quake3/q3map2/light_trace.cpp index 382bff71..49426e8b 100644 --- a/tools/quake3/q3map2/light_trace.cpp +++ b/tools/quake3/q3map2/light_trace.cpp @@ -822,8 +822,8 @@ static void PopulateWithBSPModel( const bspModel_t& model, const Matrix4& transf int i, j, x, y, pw[ 5 ], r, nodeNum; bspDrawSurface_t *ds; surfaceInfo_t *info; - bspDrawVert_t *verts; - int *indexes; + const bspDrawVert_t *verts; + const int *indexes; mesh_t srcMesh, *mesh, *subdivided; traceInfo_t ti; traceWinding_t tw; diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 9ada5b2e..00c15fcd 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -2360,9 +2360,7 @@ Q_EXTERN std::vector bspVisBytes; // MAX_MAP_VISIBILITY Q_EXTERN std::vector bspDrawVerts; -Q_EXTERN int numBSPDrawIndexes Q_ASSIGN( 0 ); -Q_EXTERN int allocatedBSPDrawIndexes Q_ASSIGN( 0 ); -Q_EXTERN int *bspDrawIndexes Q_ASSIGN( NULL ); +Q_EXTERN std::vector bspDrawIndexes; Q_EXTERN int numBSPDrawSurfaces Q_ASSIGN( 0 ); Q_EXTERN bspDrawSurface_t *bspDrawSurfaces Q_ASSIGN( NULL ); diff --git a/tools/quake3/q3map2/surface.cpp b/tools/quake3/q3map2/surface.cpp index ef875962..6c8a02e1 100644 --- a/tools/quake3/q3map2/surface.cpp +++ b/tools/quake3/q3map2/surface.cpp @@ -2076,8 +2076,9 @@ void EmitDrawVerts( const mapDrawSurface_t *ds, bspDrawSurface_t *out ){ returns numIndexes + 1 if the search failed */ -int FindDrawIndexes( int numIndexes, int *indexes ){ +int FindDrawIndexes( int numIndexes, const int *indexes ){ int i, j, numTestIndexes; + const int numBSPDrawIndexes = bspDrawIndexes.size(); /* dummy check */ @@ -2144,34 +2145,27 @@ int FindDrawIndexes( int numIndexes, int *indexes ){ attempts to find an existing run of drawindexes before adding new ones */ -void EmitDrawIndexes( mapDrawSurface_t *ds, bspDrawSurface_t *out ){ - int i; - - +void EmitDrawIndexes( const mapDrawSurface_t *ds, bspDrawSurface_t *out ){ /* attempt to use redundant indexing */ out->firstIndex = FindDrawIndexes( ds->numIndexes, ds->indexes ); out->numIndexes = ds->numIndexes; - if ( out->firstIndex == numBSPDrawIndexes ) { + if ( out->firstIndex == int( bspDrawIndexes.size() ) ) { /* copy new unique indexes */ - for ( i = 0; i < ds->numIndexes; i++ ) + for ( int i = 0; i < ds->numIndexes; i++ ) { - AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 ); - bspDrawIndexes[ numBSPDrawIndexes ] = ds->indexes[ i ]; + auto& index = bspDrawIndexes.emplace_back( ds->indexes[ i ] ); /* validate the index */ if ( ds->type != ESurfaceType::Patch ) { - if ( bspDrawIndexes[ numBSPDrawIndexes ] < 0 || bspDrawIndexes[ numBSPDrawIndexes ] >= ds->numVerts ) { + if ( index < 0 || index >= ds->numVerts ) { Sys_Warning( "%d %s has invalid index %d (%d)\n", numBSPDrawSurfaces, ds->shaderInfo->shader.c_str(), - bspDrawIndexes[ numBSPDrawIndexes ], + index, i ); - bspDrawIndexes[ numBSPDrawIndexes ] = 0; + index = 0; } } - - /* increment index count */ - numBSPDrawIndexes++; } } } diff --git a/tools/quake3/q3map2/writebsp.cpp b/tools/quake3/q3map2/writebsp.cpp index 5e7fc3bb..2c8be748 100644 --- a/tools/quake3/q3map2/writebsp.cpp +++ b/tools/quake3/q3map2/writebsp.cpp @@ -317,16 +317,8 @@ void BeginBSPFile( void ){ /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */ bspLeafs.resize( 1 ); - /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */ - numBSPDrawIndexes = 6; - AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 ); - bspDrawIndexes[ 0 ] = 0; - bspDrawIndexes[ 1 ] = 1; - bspDrawIndexes[ 2 ] = 2; - bspDrawIndexes[ 3 ] = 0; - bspDrawIndexes[ 4 ] = 2; - bspDrawIndexes[ 5 ] = 3; + bspDrawIndexes = { 0, 1, 2, 0, 2, 3 }; }