@@ -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,14 +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
479- uint32_t lastIndex = surface->firstIndex + surface->count ;
484+ memcpy ( idxs + numIndices, indices + surface->firstIndex , surface->count * sizeof ( glIndex_t ) );
485+
486+ surface->firstIndex = numIndices;
487+
488+ numIndices += surface->count ;
489+
480490 for ( uint32_t j = i + 1 ; j < materialSurfaces.size (); j++ ) {
481491 MaterialSurface* surface2 = &materialSurfaces[j];
482492
@@ -558,15 +568,15 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
558568 continue ;
559569 }
560570
561- if ( surface2->firstIndex == lastIndex ) {
562- surface->count += surface2->count ;
563- lastIndex += surface2->count ;
571+ memcpy ( idxs + numIndices, indices + surface2->firstIndex , surface2->count * sizeof ( glIndex_t ) );
572+ numIndices += surface2->count ;
564573
565- VectorCopy ( origin, surface->origin );
566- surface->radius = radius;
574+ surface->count += surface2->count ;
567575
568- surface2->merged = true ;
569- }
576+ VectorCopy ( origin, surface->origin );
577+ surface->radius = radius;
578+
579+ surface2->merged = true ;
570580
571581 if ( surface->count >= MAX_MATERIAL_SURFACE_INDEXES ) {
572582 break ;
@@ -584,5 +594,16 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
584594 materialSystem.SetWorldBounds ( worldBounds );
585595 materialSystem.GenerateWorldCommandBuffer ( processedMaterialSurfaces );
586596
597+ vertexAttributeSpec_t attrs[] {
598+ { ATTR_INDEX_POSITION, GL_FLOAT, GL_FLOAT, &vertices[0 ].xyz , 3 , sizeof ( *vertices ), 0 },
599+ { ATTR_INDEX_COLOR, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, &vertices[0 ].lightColor , 4 , sizeof ( *vertices ), ATTR_OPTION_NORMALIZE },
600+ { ATTR_INDEX_QTANGENT, GL_SHORT, GL_SHORT, &vertices[0 ].qtangent , 4 , sizeof ( *vertices ), ATTR_OPTION_NORMALIZE },
601+ { ATTR_INDEX_TEXCOORD, GL_FLOAT, GL_HALF_FLOAT, &vertices[0 ].st , 4 , sizeof ( *vertices ), 0 },
602+ };
603+
604+ geometryCache.AddMapGeometry ( numVerticesIn, numIndices, std::begin ( attrs ), std::end ( attrs ), idxs );
605+
606+ ri.Hunk_FreeTempMemory ( idxs );
607+
587608 return materialSurfaces;
588609}
0 commit comments