@@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828#include " GeometryOptimiser.h"
2929
3030#include " ShadeCommon.h"
31+ #include " GeometryCache.h"
3132
3233static int LeafSurfaceCompare ( const void * a, const void * b ) {
3334 bspSurface_t* aa, * bb;
@@ -421,7 +422,8 @@ void MergeDuplicateVertices( bspSurface_t** rendererSurfaces, int numSurfaces, s
421422 Log::Notice ( " Merged %i vertices into %i in %i ms" , numVerticesIn, numVerticesOut, Sys::Milliseconds () - start );
422423}
423424
424- std::vector<MaterialSurface> OptimiseMapGeometryMaterial (bspSurface_t** rendererSurfaces, int numSurfaces ) {
425+ std::vector<MaterialSurface> OptimiseMapGeometryMaterial ( bspSurface_t** rendererSurfaces, int numSurfaces,
426+ const srfVert_t* vertices, const int numVerticesIn, const glIndex_t* indices, const int numIndicesIn ) {
425427 std::vector<MaterialSurface> materialSurfaces;
426428 materialSurfaces.reserve ( numSurfaces );
427429
@@ -469,13 +471,22 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
469471
470472 std::vector<MaterialSurface> processedMaterialSurfaces;
471473 processedMaterialSurfaces.reserve ( numSurfaces );
474+
475+ glIndex_t* idxs = ( glIndex_t* ) ri.Hunk_AllocateTempMemory ( numIndicesIn * sizeof ( glIndex_t ) );
476+ uint32_t numIndices = 0 ;
472477 for ( uint32_t i = 0 ; i < materialSurfaces.size (); i++ ) {
473478 MaterialSurface* surface = &materialSurfaces[i];
474479
475480 if ( surface->merged ) {
476481 continue ;
477482 }
478483
484+ memcpy ( idxs + numIndices, indices + surface->firstIndex , surface->count * sizeof ( glIndex_t ) );
485+
486+ surface->firstIndex = numIndices;
487+
488+ numIndices += surface->count ;
489+
479490 uint32_t lastIndex = surface->firstIndex + surface->count ;
480491 for ( uint32_t j = i + 1 ; j < materialSurfaces.size (); j++ ) {
481492 MaterialSurface* surface2 = &materialSurfaces[j];
@@ -551,15 +562,16 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
551562 continue ;
552563 }
553564
554- if ( surface2->firstIndex == lastIndex ) {
555- surface->count += surface2->count ;
556- lastIndex += surface2->count ;
565+ memcpy ( idxs + numIndices, indices + surface2->firstIndex , surface2->count * sizeof ( glIndex_t ) );
566+ numIndices += surface2->count ;
557567
558- VectorCopy ( origin, surface->origin ) ;
559- surface-> radius = radius ;
568+ surface->count += surface2-> count ;
569+ lastIndex += surface2-> count ;
560570
561- surface2->merged = true ;
562- }
571+ VectorCopy ( origin, surface->origin );
572+ surface->radius = radius;
573+
574+ surface2->merged = true ;
563575
564576 if ( surface->count >= MAX_MATERIAL_SURFACE_INDEXES ) {
565577 break ;
@@ -577,5 +589,16 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
577589 materialSystem.SetWorldBounds ( worldBounds );
578590 materialSystem.GenerateWorldCommandBuffer ( processedMaterialSurfaces );
579591
592+ vertexAttributeSpec_t attrs[] {
593+ { ATTR_INDEX_POSITION , GL_FLOAT , GL_FLOAT , &vertices[0 ].xyz , 3 , sizeof ( *vertices ), 0 },
594+ { ATTR_INDEX_COLOR , GL_UNSIGNED_BYTE , GL_UNSIGNED_BYTE , &vertices[0 ].lightColor , 4 , sizeof ( *vertices ), ATTR_OPTION_NORMALIZE },
595+ { ATTR_INDEX_QTANGENT , GL_SHORT , GL_SHORT , &vertices[0 ].qtangent , 4 , sizeof ( *vertices ), ATTR_OPTION_NORMALIZE },
596+ { ATTR_INDEX_TEXCOORD , GL_FLOAT , GL_HALF_FLOAT , &vertices[0 ].st , 4 , sizeof ( *vertices ), 0 },
597+ };
598+
599+ geometryCache.AddMapGeometry ( numVerticesIn, numIndices, std::begin ( attrs ), std::end ( attrs ), idxs );
600+
601+ ri.Hunk_FreeTempMemory ( idxs );
602+
580603 return materialSurfaces;
581604}
0 commit comments