@@ -308,20 +308,25 @@ void MergeDuplicateVertices( bspSurface_t** rendererSurfaces, int numSurfaces, s
308308 for ( int i = 0 ; i < numSurfaces; i++ ) {
309309 bspSurface_t* surface = rendererSurfaces[i];
310310
311- srfGeneric_t* face = ( srfGeneric_t* ) surface->data ;
312- face ->firstIndex = idx;
313- for ( srfTriangle_t* triangle = face ->triangles ; triangle < face ->triangles + face ->numTriangles ; triangle++ ) {
311+ srfGeneric_t* srf = ( srfGeneric_t* ) surface->data ;
312+ srf ->firstIndex = idx;
313+ for ( srfTriangle_t* triangle = srf ->triangles ; triangle < srf ->triangles + srf ->numTriangles ; triangle++ ) {
314314 for ( int j = 0 ; j < 3 ; j++ ) {
315- srfVert_t& vert = face ->verts [triangle->indexes [j]];
315+ srfVert_t& vert = srf ->verts [triangle->indexes [j]];
316316 uint32_t index = verts[vert];
317317
318- ASSERT_LT ( idx, numIndicesIn );
318+ /* There were some crashes due to bad lightmap values in .bsp vertices,
319+ do the check again here just in case some calculation earlier, like patch mesh triangulation,
320+ fucks things up again */
321+ ValidateVertex ( &vert, -1 , surface->shader );
322+
323+ ASSERT_LT ( idx, ( uint32_t ) numIndicesIn );
319324 if ( !index ) {
320325 verts[vert] = vertIdx + 1 ;
321326 vertices[vertIdx] = vert;
322327 indices[idx] = vertIdx;
323328
324- ASSERT_LT ( vertIdx, numVerticesIn );
329+ ASSERT_LT ( vertIdx, ( uint32_t ) numVerticesIn );
325330
326331 vertIdx++;
327332 } else {
@@ -356,7 +361,7 @@ void MergeDuplicateVertices( bspSurface_t** rendererSurfaces, int numSurfaces, s
356361 }
357362} */
358363
359- std::vector<MaterialSurface> OptimiseMapGeometryMaterial ( world_t * world , int numSurfaces ) {
364+ std::vector<MaterialSurface> OptimiseMapGeometryMaterial (bspSurface_t** rendererSurfaces , int numSurfaces ) {
360365 std::vector<MaterialSurface> materialSurfaces;
361366 materialSurfaces.reserve ( numSurfaces );
362367
@@ -365,24 +370,46 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial( world_t* world, int nu
365370
366371 // std::unordered_map<TriEdge, TriIndex> triEdges;
367372
368- int surfaceIndex = 0 ;
369- for ( int k = 0 ; k < world->numSurfaces ; k++ ) {
370- bspSurface_t* surface = &world->surfaces [k];
373+ vec3_t worldBounds[2 ] = {};
374+ for ( int i = 0 ; i < numSurfaces; i++ ) {
375+ bspSurface_t* surface = rendererSurfaces[i];
376+
377+ if ( surface->BSPModel ) {
378+ // Not implemented yet
379+ continue ;
380+ }
371381
372382 MaterialSurface srf {};
373383
374384 srf.shader = surface->shader ;
385+
375386 srf.bspSurface = true ;
387+ srf.skyBrush = surface->skyBrush ;
388+
389+ srf.lightMapNum = surface->lightmapNum ;
376390 srf.fog = surface->fogIndex ;
391+ srf.portalNum = surface->portalNum ;
377392
378393 srf.firstIndex = ( ( srfGeneric_t* ) surface->data )->firstIndex ;
379- srf.count = ( ( srfGeneric_t* ) surface->data )->numTriangles ;
394+ srf.count = ( ( srfGeneric_t* ) surface->data )->numTriangles * 3 ;
380395 srf.verts = ( ( srfGeneric_t* ) surface->data )->verts ;
381396 srf.tris = ( ( srfGeneric_t* ) surface->data )->triangles ;
382397
398+ VectorCopy ( ( ( srfGeneric_t* ) surface->data )->origin , srf.origin );
399+ srf.radius = ( ( srfGeneric_t* ) surface->data )->radius ;
400+
401+ BoundsAdd ( worldBounds[0 ], worldBounds[1 ],
402+ ( ( srfGeneric_t* ) surface->data )->bounds [0 ], ( ( srfGeneric_t* ) surface->data )->bounds [1 ] );
403+
404+ materialSystem.GenerateMaterial ( &srf );
405+
383406 materialSurfaces.emplace_back ( srf );
384- surfaceIndex++;
385407 }
386408
409+ materialSystem.GenerateWorldMaterialsBuffer ();
410+ materialSystem.GeneratePortalBoundingSpheres ();
411+ materialSystem.SetWorldBounds ( worldBounds );
412+ materialSystem.GenerateWorldCommandBuffer ( materialSurfaces );
413+
387414 return materialSurfaces;
388415}
0 commit comments