1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
32 #define SURFACE_META_C
41 #define LIGHTMAP_EXCEEDED -1
44 #define ST_EXCEEDED -4
45 #define UNSUITABLE_TRIANGLE -10
46 #define VERTS_EXCEEDED -1000
47 #define INDEXES_EXCEEDED -2000
49 #define GROW_META_VERTS 1024
50 #define GROW_META_TRIANGLES 1024
52 static int numMetaSurfaces, numPatchMetaSurfaces;
54 static int maxMetaVerts = 0;
55 static int numMetaVerts = 0;
56 static int firstSearchMetaVert = 0;
57 static bspDrawVert_t *metaVerts = NULL;
59 static int maxMetaTriangles = 0;
60 static int numMetaTriangles = 0;
61 static metaTriangle_t *metaTriangles = NULL;
67 called before staring a new entity to clear out the triangle list
70 void ClearMetaTriangles( void )
80 finds a matching metavertex in the global list, returning its index
83 static int FindMetaVertex( bspDrawVert_t *src )
86 bspDrawVert_t *v, *temp;
89 /* try to find an existing drawvert */
90 for( i = firstSearchMetaVert, v = &metaVerts[ i ]; i < numMetaVerts; i++, v++ )
92 if( memcmp( src, v, sizeof( bspDrawVert_t ) ) == 0 )
97 if( numMetaVerts >= maxMetaVerts )
99 /* reallocate more room */
100 maxMetaVerts += GROW_META_VERTS;
101 temp = safe_malloc( maxMetaVerts * sizeof( bspDrawVert_t ) );
102 if( metaVerts != NULL )
104 memcpy( temp, metaVerts, numMetaVerts * sizeof( bspDrawVert_t ) );
110 /* add the triangle */
111 memcpy( &metaVerts[ numMetaVerts ], src, sizeof( bspDrawVert_t ) );
114 /* return the count */
115 return (numMetaVerts - 1);
122 adds a new meta triangle, allocating more memory if necessary
125 static int AddMetaTriangle( void )
127 metaTriangle_t *temp;
131 if( numMetaTriangles >= maxMetaTriangles )
133 /* reallocate more room */
134 maxMetaTriangles += GROW_META_TRIANGLES;
135 temp = safe_malloc( maxMetaTriangles * sizeof( metaTriangle_t ) );
136 if( metaTriangles != NULL )
138 memcpy( temp, metaTriangles, numMetaTriangles * sizeof( metaTriangle_t ) );
139 free( metaTriangles );
141 metaTriangles = temp;
144 /* increment and return */
146 return numMetaTriangles - 1;
153 finds a matching metatriangle in the global list,
154 otherwise adds it and returns the index to the metatriangle
157 int FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, bspDrawVert_t *c, int planeNum )
164 /* detect degenerate triangles fixme: do something proper here */
165 VectorSubtract( a->xyz, b->xyz, dir );
166 if( VectorLength( dir ) < 0.125f )
168 VectorSubtract( b->xyz, c->xyz, dir );
169 if( VectorLength( dir ) < 0.125f )
171 VectorSubtract( c->xyz, a->xyz, dir );
172 if( VectorLength( dir ) < 0.125f )
178 /* because of precision issues with small triangles, try to use the specified plane */
179 src->planeNum = planeNum;
180 VectorCopy( mapplanes[ planeNum ].normal, src->plane );
181 src->plane[ 3 ] = mapplanes[ planeNum ].dist;
185 /* calculate a plane from the triangle's points (and bail if a plane can't be constructed) */
187 if( PlaneFromPoints( src->plane, a->xyz, b->xyz, c->xyz ) == qfalse )
191 /* ydnar 2002-10-03: repair any bogus normals (busted ase import kludge) */
192 if( VectorLength( a->normal ) <= 0.0f )
193 VectorCopy( src->plane, a->normal );
194 if( VectorLength( b->normal ) <= 0.0f )
195 VectorCopy( src->plane, b->normal );
196 if( VectorLength( c->normal ) <= 0.0f )
197 VectorCopy( src->plane, c->normal );
199 /* ydnar 2002-10-04: set lightmap axis if not already set */
200 if( !(src->si->compileFlags & C_VERTEXLIT) &&
201 src->lightmapAxis[ 0 ] == 0.0f && src->lightmapAxis[ 1 ] == 0.0f && src->lightmapAxis[ 2 ] == 0.0f )
203 /* the shader can specify an explicit lightmap axis */
204 if( src->si->lightmapAxis[ 0 ] || src->si->lightmapAxis[ 1 ] || src->si->lightmapAxis[ 2 ] )
205 VectorCopy( src->si->lightmapAxis, src->lightmapAxis );
207 /* new axis-finding code */
209 CalcLightmapAxis( src->plane, src->lightmapAxis );
212 /* fill out the src triangle */
213 src->indexes[ 0 ] = FindMetaVertex( a );
214 src->indexes[ 1 ] = FindMetaVertex( b );
215 src->indexes[ 2 ] = FindMetaVertex( c );
217 /* try to find an existing triangle */
218 #ifdef USE_EXHAUSTIVE_SEARCH
224 for( i = 0, tri = metaTriangles; i < numMetaTriangles; i++, tri++ )
226 if( memcmp( src, tri, sizeof( metaTriangle_t ) ) == 0 )
232 /* get a new triangle */
233 triIndex = AddMetaTriangle();
235 /* add the triangle */
236 memcpy( &metaTriangles[ triIndex ], src, sizeof( metaTriangle_t ) );
238 /* return the triangle index */
245 SurfaceToMetaTriangles()
246 converts a classified surface to metatriangles
249 static void SurfaceToMetaTriangles( mapDrawSurface_t *ds )
253 bspDrawVert_t a, b, c;
256 /* only handle certain types of surfaces */
257 if( ds->type != SURFACE_FACE &&
258 ds->type != SURFACE_META &&
259 ds->type != SURFACE_FORCED_META &&
260 ds->type != SURFACE_DECAL )
263 /* speed at the expense of memory */
264 firstSearchMetaVert = numMetaVerts;
266 /* only handle valid surfaces */
267 if( ds->type != SURFACE_BAD && ds->numVerts >= 3 && ds->numIndexes >= 3 )
269 /* walk the indexes and create triangles */
270 for( i = 0; i < ds->numIndexes; i += 3 )
272 /* sanity check the indexes */
273 if( ds->indexes[ i ] == ds->indexes[ i + 1 ] ||
274 ds->indexes[ i ] == ds->indexes[ i + 2 ] ||
275 ds->indexes[ i + 1 ] == ds->indexes[ i + 2 ] )
277 //% Sys_Printf( "%d! ", ds->numVerts );
281 /* build a metatriangle */
282 src.si = ds->shaderInfo;
283 src.side = (ds->sideRef != NULL ? ds->sideRef->side : NULL);
284 src.entityNum = ds->entityNum;
285 src.surfaceNum = ds->surfaceNum;
286 src.planeNum = ds->planeNum;
287 src.castShadows = ds->castShadows;
288 src.recvShadows = ds->recvShadows;
289 src.fogNum = ds->fogNum;
290 src.sampleSize = ds->sampleSize;
291 VectorCopy( ds->lightmapAxis, src.lightmapAxis );
294 memcpy( &a, &ds->verts[ ds->indexes[ i ] ], sizeof( a ) );
295 memcpy( &b, &ds->verts[ ds->indexes[ i + 1 ] ], sizeof( b ) );
296 memcpy( &c, &ds->verts[ ds->indexes[ i + 2 ] ], sizeof( c ) );
297 FindMetaTriangle( &src, &a, &b, &c, ds->planeNum );
304 /* clear the surface (free verts and indexes, sets it to SURFACE_BAD) */
311 TriangulatePatchSurface()
312 creates triangles from a patch
315 void TriangulatePatchSurface( mapDrawSurface_t *ds )
317 int iterations, x, y, pw[ 5 ], r;
318 mapDrawSurface_t *dsNew;
319 mesh_t src, *subdivided, *mesh;
322 /* try to early out */
323 if( ds->numVerts == 0 || ds->type != SURFACE_PATCH || patchMeta == qfalse )
326 /* make a mesh from the drawsurf */
327 src.width = ds->patchWidth;
328 src.height = ds->patchHeight;
329 src.verts = ds->verts;
330 //% subdivided = SubdivideMesh( src, 8, 999 );
331 iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions );
332 subdivided = SubdivideMesh2( src, iterations ); //% ds->maxIterations
334 /* fit it to the curve and remove colinear verts on rows/columns */
335 PutMeshOnCurve( *subdivided );
336 mesh = RemoveLinearMeshColumnsRows( subdivided );
337 FreeMesh( subdivided );
338 //% MakeMeshNormals( mesh );
340 /* make a copy of the drawsurface */
341 dsNew = AllocDrawSurface( SURFACE_META );
342 memcpy( dsNew, ds, sizeof( *ds ) );
344 /* if the patch is nonsolid, then discard it */
345 if( !(ds->shaderInfo->compileFlags & C_SOLID) )
348 /* set new pointer */
351 /* basic transmogrification */
352 ds->type = SURFACE_META;
354 ds->indexes = safe_malloc( mesh->width * mesh->height * 6 * sizeof( int ) );
356 /* copy the verts in */
357 ds->numVerts = (mesh->width * mesh->height);
358 ds->verts = mesh->verts;
360 /* iterate through the mesh quads */
361 for( y = 0; y < (mesh->height - 1); y++ )
363 for( x = 0; x < (mesh->width - 1); x++ )
366 pw[ 0 ] = x + (y * mesh->width);
367 pw[ 1 ] = x + ((y + 1) * mesh->width);
368 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
369 pw[ 3 ] = x + 1 + (y * mesh->width);
370 pw[ 4 ] = x + (y * mesh->width); /* same as pw[ 0 ] */
375 /* make first triangle */
376 ds->indexes[ ds->numIndexes++ ] = pw[ r + 0 ];
377 ds->indexes[ ds->numIndexes++ ] = pw[ r + 1 ];
378 ds->indexes[ ds->numIndexes++ ] = pw[ r + 2 ];
380 /* make second triangle */
381 ds->indexes[ ds->numIndexes++ ] = pw[ r + 0 ];
382 ds->indexes[ ds->numIndexes++ ] = pw[ r + 2 ];
383 ds->indexes[ ds->numIndexes++ ] = pw[ r + 3 ];
387 /* free the mesh, but not the verts */
391 numPatchMetaSurfaces++;
394 ClassifySurfaces( 1, ds );
400 FanFaceSurface() - ydnar
401 creates a tri-fan from a brush face winding
402 loosely based on SurfaceAsTriFan()
405 void FanFaceSurface( mapDrawSurface_t *ds )
407 int i, j, k, a, b, c, color[ MAX_LIGHTMAPS ][ 4 ];
408 bspDrawVert_t *verts, *centroid, *dv;
412 /* try to early out */
413 if( !ds->numVerts || (ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL) )
416 /* add a new vertex at the beginning of the surface */
417 verts = safe_malloc( (ds->numVerts + 1) * sizeof( bspDrawVert_t ) );
418 memset( verts, 0, sizeof( bspDrawVert_t ) );
419 memcpy( &verts[ 1 ], ds->verts, ds->numVerts * sizeof( bspDrawVert_t ) );
423 /* add up the drawverts to create a centroid */
424 centroid = &verts[ 0 ];
425 memset( color, 0, 4 * MAX_LIGHTMAPS * sizeof( int ) );
426 for( i = 1, dv = &verts[ 1 ]; i < (ds->numVerts + 1); i++, dv++ )
428 VectorAdd( centroid->xyz, dv->xyz, centroid->xyz );
429 VectorAdd( centroid->normal, dv->normal, centroid->normal );
430 for( j = 0; j < 4; j++ )
432 for( k = 0; k < MAX_LIGHTMAPS; k++ )
433 color[ k ][ j ] += dv->color[ k ][ j ];
436 centroid->st[ j ] += dv->st[ j ];
437 for( k = 0; k < MAX_LIGHTMAPS; k++ )
438 centroid->lightmap[ k ][ j ] += dv->lightmap[ k ][ j ];
443 /* average the centroid */
444 iv = 1.0f / ds->numVerts;
445 VectorScale( centroid->xyz, iv, centroid->xyz );
446 if( VectorNormalize( centroid->normal, centroid->normal ) <= 0 )
447 VectorCopy( verts[ 1 ].normal, centroid->normal );
448 for( j = 0; j < 4; j++ )
450 for( k = 0; k < MAX_LIGHTMAPS; k++ )
452 color[ k ][ j ] /= ds->numVerts;
453 centroid->color[ k ][ j ] = (color[ k ][ j ] < 255.0f ? color[ k ][ j ] : 255);
457 centroid->st[ j ] *= iv;
458 for( k = 0; k < MAX_LIGHTMAPS; k++ )
459 centroid->lightmap[ k ][ j ] *= iv;
463 /* add to vert count */
466 /* fill indexes in triangle fan order */
468 ds->indexes = safe_malloc( ds->numVerts * 3 * sizeof( int ) );
469 for( i = 1; i < ds->numVerts; i++ )
473 c = (i + 1) % ds->numVerts;
475 ds->indexes[ ds->numIndexes++ ] = a;
476 ds->indexes[ ds->numIndexes++ ] = b;
477 ds->indexes[ ds->numIndexes++ ] = c;
484 ClassifySurfaces( 1, ds );
490 StripFaceSurface() - ydnar
491 attempts to create a valid tri-strip w/o degenerate triangles from a brush face winding
492 based on SurfaceAsTriStrip()
495 #define MAX_INDEXES 4096
497 void StripFaceSurface( mapDrawSurface_t *ds )
499 int i, r, least, rotate, numIndexes, ni, a, b, c, indexes[ MAX_INDEXES ];
503 /* try to early out */
504 if( !ds->numVerts || (ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL) )
507 /* is this a simple triangle? */
508 if( ds->numVerts == 3 )
511 VectorSet( indexes, 0, 1, 2 );
515 /* ydnar: find smallest coordinate */
517 if( ds->shaderInfo != NULL && ds->shaderInfo->autosprite == qfalse )
519 for( i = 0; i < ds->numVerts; i++ )
522 v1 = ds->verts[ i ].xyz;
523 v2 = ds->verts[ least ].xyz;
526 if( v1[ 0 ] < v2[ 0 ] ||
527 (v1[ 0 ] == v2[ 0 ] && v1[ 1 ] < v2[ 1 ]) ||
528 (v1[ 0 ] == v2[ 0 ] && v1[ 1 ] == v2[ 1 ] && v1[ 2 ] < v2[ 2 ]) )
533 /* determine the triangle strip order */
534 numIndexes = (ds->numVerts - 2) * 3;
535 if( numIndexes > MAX_INDEXES )
536 Error( "MAX_INDEXES exceeded for surface (%d > %d) (%d verts)", numIndexes, MAX_INDEXES, ds->numVerts );
538 /* try all possible orderings of the points looking for a non-degenerate strip order */
539 for( r = 0; r < ds->numVerts; r++ )
542 rotate = (r + least) % ds->numVerts;
544 /* walk the winding in both directions */
545 for( ni = 0, i = 0; i < ds->numVerts - 2 - i; i++ )
548 a = (ds->numVerts - 1 - i + rotate) % ds->numVerts;
549 b = (i + rotate ) % ds->numVerts;
550 c = (ds->numVerts - 2 - i + rotate) % ds->numVerts;
552 /* test this triangle */
553 if( ds->numVerts > 4 && IsTriangleDegenerate( ds->verts, a, b, c ) )
559 /* handle end case */
560 if( i + 1 != ds->numVerts - 1 - i )
563 a = (ds->numVerts - 2 - i + rotate ) % ds->numVerts;
564 b = (i + rotate ) % ds->numVerts;
565 c = (i + 1 + rotate ) % ds->numVerts;
568 if( ds->numVerts > 4 && IsTriangleDegenerate( ds->verts, a, b, c ) )
577 if( ni == numIndexes )
581 /* if any triangle in the strip is degenerate, render from a centered fan point instead */
582 if( ni < numIndexes )
584 FanFaceSurface( ds );
589 /* copy strip triangle indexes */
590 ds->numIndexes = numIndexes;
591 ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) );
592 memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( int ) );
598 ClassifySurfaces( 1, ds );
604 MakeEntityMetaTriangles()
605 builds meta triangles from brush faces (tristrips and fans)
608 void MakeEntityMetaTriangles( entity_t *e )
610 int i, f, fOld, start;
611 mapDrawSurface_t *ds;
615 Sys_FPrintf( SYS_VRB, "--- MakeEntityMetaTriangles ---\n" );
619 start = I_FloatTime();
621 /* walk the list of surfaces in the entity */
622 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
625 f = 10 * (i - e->firstDrawSurf) / (numMapDrawSurfs - e->firstDrawSurf);
629 Sys_FPrintf( SYS_VRB, "%d...", f );
633 ds = &mapDrawSurfs[ i ];
634 if( ds->numVerts <= 0 )
637 /* ignore autosprite surfaces */
638 if( ds->shaderInfo->autosprite )
641 /* meta this surface? */
642 if( meta == qfalse && ds->shaderInfo->forceMeta == qfalse )
650 StripFaceSurface( ds );
651 SurfaceToMetaTriangles( ds );
655 TriangulatePatchSurface( ds );
658 case SURFACE_TRIANGLES:
661 case SURFACE_FORCED_META:
663 SurfaceToMetaTriangles( ds );
672 if( (numMapDrawSurfs - e->firstDrawSurf) )
673 Sys_FPrintf( SYS_VRB, " (%d)\n", (int) (I_FloatTime() - start) );
675 /* emit some stats */
676 Sys_FPrintf( SYS_VRB, "%9d total meta surfaces\n", numMetaSurfaces );
677 Sys_FPrintf( SYS_VRB, "%9d stripped surfaces\n", numStripSurfaces );
678 Sys_FPrintf( SYS_VRB, "%9d fanned surfaces\n", numFanSurfaces );
679 Sys_FPrintf( SYS_VRB, "%9d patch meta surfaces\n", numPatchMetaSurfaces );
680 Sys_FPrintf( SYS_VRB, "%9d meta verts\n", numMetaVerts );
681 Sys_FPrintf( SYS_VRB, "%9d meta triangles\n", numMetaTriangles );
684 TidyEntitySurfaces( e );
690 PointTriangleIntersect()
691 assuming that all points lie in plane, determine if pt
692 is inside the triangle abc
693 code originally (c) 2001 softSurfer (www.softsurfer.com)
696 #define MIN_OUTSIDE_EPSILON -0.01f
697 #define MAX_OUTSIDE_EPSILON 1.01f
699 static qboolean PointTriangleIntersect( vec3_t pt, vec4_t plane, vec3_t a, vec3_t b, vec3_t c, vec3_t bary )
702 float uu, uv, vv, wu, wv, d;
706 VectorSubtract( b, a, u );
707 VectorSubtract( c, a, v );
708 VectorSubtract( pt, a, w );
711 uu = DotProduct( u, u );
712 uv = DotProduct( u, v );
713 vv = DotProduct( v, v );
714 wu = DotProduct( w, u );
715 wv = DotProduct( w, v );
716 d = uv * uv - uu * vv;
718 /* calculate barycentric coordinates */
719 bary[ 1 ] = (uv * wv - vv * wu) / d;
720 if( bary[ 1 ] < MIN_OUTSIDE_EPSILON || bary[ 1 ] > MAX_OUTSIDE_EPSILON )
722 bary[ 2 ] = (uv * wv - uu * wv) / d;
723 if( bary[ 2 ] < MIN_OUTSIDE_EPSILON || bary[ 2 ] > MAX_OUTSIDE_EPSILON )
725 bary[ 0 ] = 1.0f - (bary[ 1 ] + bary[ 2 ]);
727 /* point is in triangle */
735 sets up an edge structure from a plane and 2 points that the edge ab falls lies in
738 typedef struct edge_s
741 vec_t length, kingpinLength;
747 void CreateEdge( vec4_t plane, vec3_t a, vec3_t b, edge_t *edge )
749 /* copy edge origin */
750 VectorCopy( a, edge->origin );
752 /* create vector aligned with winding direction of edge */
753 VectorSubtract( b, a, edge->edge );
755 if( fabs( edge->edge[ 0 ] ) > fabs( edge->edge[ 1 ] ) && fabs( edge->edge[ 0 ] ) > fabs( edge->edge[ 2 ] ) )
757 else if( fabs( edge->edge[ 1 ] ) > fabs( edge->edge[ 0 ] ) && fabs( edge->edge[ 1 ] ) > fabs( edge->edge[ 2 ] ) )
761 edge->kingpinLength = edge->edge[ edge->kingpin ];
763 VectorNormalize( edge->edge, edge->edge );
764 edge->edge[ 3 ] = DotProduct( a, edge->edge );
765 edge->length = DotProduct( b, edge->edge ) - edge->edge[ 3 ];
767 /* create perpendicular plane that edge lies in */
768 CrossProduct( plane, edge->edge, edge->plane );
769 edge->plane[ 3 ] = DotProduct( a, edge->plane );
776 fixes t-junctions on meta triangles
779 #define TJ_PLANE_EPSILON (1.0f / 8.0f)
780 #define TJ_EDGE_EPSILON (1.0f / 8.0f)
781 #define TJ_POINT_EPSILON (1.0f / 8.0f)
783 void FixMetaTJunctions( void )
785 int i, j, k, f, fOld, start, vertIndex, triIndex, numTJuncs;
786 metaTriangle_t *tri, *newTri;
788 bspDrawVert_t *a, *b, *c, junc;
795 /* this code is crap; revisit later */
799 Sys_FPrintf( SYS_VRB, "--- FixMetaTJunctions ---\n" );
803 start = I_FloatTime();
805 /* walk triangle list */
807 for( i = 0; i < numMetaTriangles; i++ )
810 tri = &metaTriangles[ i ];
813 f = 10 * i / numMetaTriangles;
817 Sys_FPrintf( SYS_VRB, "%d...", f );
820 /* attempt to early out */
822 if( (si->compileFlags & C_NODRAW) || si->autosprite || si->notjunc )
825 /* calculate planes */
826 VectorCopy( tri->plane, plane );
827 plane[ 3 ] = tri->plane[ 3 ];
828 CreateEdge( plane, metaVerts[ tri->indexes[ 0 ] ].xyz, metaVerts[ tri->indexes[ 1 ] ].xyz, &edges[ 0 ] );
829 CreateEdge( plane, metaVerts[ tri->indexes[ 1 ] ].xyz, metaVerts[ tri->indexes[ 2 ] ].xyz, &edges[ 1 ] );
830 CreateEdge( plane, metaVerts[ tri->indexes[ 2 ] ].xyz, metaVerts[ tri->indexes[ 0 ] ].xyz, &edges[ 2 ] );
832 /* walk meta vert list */
833 for( j = 0; j < numMetaVerts; j++ )
836 VectorCopy( metaVerts[ j ].xyz, pt );
838 /* debug code: darken verts */
840 VectorSet( metaVerts[ j ].color[ 0 ], 8, 8, 8 );
842 /* determine if point lies in the triangle's plane */
843 dist = DotProduct( pt, plane ) - plane[ 3 ];
844 if( fabs( dist ) > TJ_PLANE_EPSILON )
847 /* skip this point if it already exists in the triangle */
848 for( k = 0; k < 3; k++ )
850 if( fabs( pt[ 0 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 0 ] ) <= TJ_POINT_EPSILON &&
851 fabs( pt[ 1 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 1 ] ) <= TJ_POINT_EPSILON &&
852 fabs( pt[ 2 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 2 ] ) <= TJ_POINT_EPSILON )
859 for( k = 0; k < 3; k++ )
861 /* ignore bogus edges */
862 if( fabs( edges[ k ].kingpinLength ) < TJ_EDGE_EPSILON )
865 /* determine if point lies on the edge */
866 dist = DotProduct( pt, edges[ k ].plane ) - edges[ k ].plane[ 3 ];
867 if( fabs( dist ) > TJ_EDGE_EPSILON )
870 /* determine how far along the edge the point lies */
871 amount = (pt[ edges[ k ].kingpin ] - edges[ k ].origin[ edges[ k ].kingpin ]) / edges[ k ].kingpinLength;
872 if( amount <= 0.0f || amount >= 1.0f )
876 dist = DotProduct( pt, edges[ k ].edge ) - edges[ k ].edge[ 3 ];
877 if( dist <= -0.0f || dist >= edges[ k ].length )
879 amount = dist / edges[ k ].length;
882 /* debug code: brighten this point */
883 //% metaVerts[ j ].color[ 0 ][ 0 ] += 5;
884 //% metaVerts[ j ].color[ 0 ][ 1 ] += 4;
885 VectorSet( metaVerts[ tri->indexes[ k ] ].color[ 0 ], 255, 204, 0 );
886 VectorSet( metaVerts[ tri->indexes[ (k + 1) % 3 ] ].color[ 0 ], 255, 204, 0 );
889 /* the edge opposite the zero-weighted vertex was hit, so use that as an amount */
890 a = &metaVerts[ tri->indexes[ k % 3 ] ];
891 b = &metaVerts[ tri->indexes[ (k + 1) % 3 ] ];
892 c = &metaVerts[ tri->indexes[ (k + 2) % 3 ] ];
895 LerpDrawVertAmount( a, b, amount, &junc );
896 VectorCopy( pt, junc.xyz );
898 /* compare against existing verts */
899 if( VectorCompare( junc.xyz, a->xyz ) || VectorCompare( junc.xyz, b->xyz ) || VectorCompare( junc.xyz, c->xyz ) )
902 /* see if we can just re-use the existing vert */
903 if( !memcmp( &metaVerts[ j ], &junc, sizeof( junc ) ) )
907 /* find new vertex (note: a and b are invalid pointers after this) */
908 firstSearchMetaVert = numMetaVerts;
909 vertIndex = FindMetaVertex( &junc );
914 /* make new triangle */
915 triIndex = AddMetaTriangle();
920 tri = &metaTriangles[ i ];
921 newTri = &metaTriangles[ triIndex ];
923 /* copy the triangle */
924 memcpy( newTri, tri, sizeof( *tri ) );
927 tri->indexes[ (k + 1) % 3 ] = vertIndex;
928 newTri->indexes[ k ] = vertIndex;
930 /* recalculate edges */
931 CreateEdge( plane, metaVerts[ tri->indexes[ 0 ] ].xyz, metaVerts[ tri->indexes[ 1 ] ].xyz, &edges[ 0 ] );
932 CreateEdge( plane, metaVerts[ tri->indexes[ 1 ] ].xyz, metaVerts[ tri->indexes[ 2 ] ].xyz, &edges[ 1 ] );
933 CreateEdge( plane, metaVerts[ tri->indexes[ 2 ] ].xyz, metaVerts[ tri->indexes[ 0 ] ].xyz, &edges[ 2 ] );
936 metaVerts[ vertIndex ].color[ 0 ][ 0 ] = 255;
937 metaVerts[ vertIndex ].color[ 0 ][ 1 ] = 204;
938 metaVerts[ vertIndex ].color[ 0 ][ 2 ] = 0;
940 /* add to counter and end processing of this vert */
948 Sys_FPrintf( SYS_VRB, " (%d)\n", (int) (I_FloatTime() - start) );
950 /* emit some stats */
951 Sys_FPrintf( SYS_VRB, "%9d T-junctions added\n", numTJuncs );
957 SmoothMetaTriangles()
958 averages coincident vertex normals in the meta triangles
961 #define MAX_SAMPLES 256
962 #define THETA_EPSILON 0.000001
963 #define EQUAL_NORMAL_EPSILON 0.01
965 void SmoothMetaTriangles( void )
967 int i, j, k, f, fOld, start, cs, numVerts, numVotes, numSmoothed;
968 float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
972 vec3_t average, diff;
973 int indexes[ MAX_SAMPLES ];
974 vec3_t votes[ MAX_SAMPLES ];
978 Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" );
980 /* allocate shade angle table */
981 shadeAngles = safe_malloc( numMetaVerts * sizeof( float ) );
982 memset( shadeAngles, 0, numMetaVerts * sizeof( float ) );
984 /* allocate smoothed table */
985 cs = (numMetaVerts / 8) + 1;
986 smoothed = safe_malloc( cs );
987 memset( smoothed, 0, cs );
989 /* set default shade angle */
990 defaultShadeAngle = DEG2RAD( npDegrees );
991 maxShadeAngle = 0.0f;
993 /* run through every surface and flag verts belonging to non-lightmapped surfaces
994 and set per-vertex smoothing angle */
995 for( i = 0, tri = &metaTriangles[ i ]; i < numMetaTriangles; i++, tri++ )
997 /* get shader for shade angle */
998 if( tri->si->shadeAngleDegrees > 0.0f )
999 shadeAngle = DEG2RAD( tri->si->shadeAngleDegrees );
1001 shadeAngle = defaultShadeAngle;
1002 if( shadeAngle > maxShadeAngle )
1003 maxShadeAngle = shadeAngle;
1005 /* flag its verts */
1006 for( j = 0; j < 3; j++ )
1008 shadeAngles[ tri->indexes[ j ] ] = shadeAngle;
1009 if( shadeAngle <= 0 )
1010 smoothed[ tri->indexes[ j ] >> 3 ] |= (1 << (tri->indexes[ j ] & 7));
1014 /* bail if no surfaces have a shade angle */
1015 if( maxShadeAngle <= 0 )
1017 Sys_FPrintf( SYS_VRB, "No smoothing angles specified, aborting\n" );
1018 free( shadeAngles );
1025 start = I_FloatTime();
1027 /* go through the list of vertexes */
1029 for( i = 0; i < numMetaVerts; i++ )
1031 /* print pacifier */
1032 f = 10 * i / numMetaVerts;
1036 Sys_FPrintf( SYS_VRB, "%d...", f );
1039 /* already smoothed? */
1040 if( smoothed[ i >> 3 ] & (1 << (i & 7)) )
1044 VectorClear( average );
1048 /* build a table of coincident vertexes */
1049 for( j = i; j < numMetaVerts && numVerts < MAX_SAMPLES; j++ )
1051 /* already smoothed? */
1052 if( smoothed[ j >> 3 ] & (1 << (j & 7)) )
1056 if( VectorCompare( metaVerts[ i ].xyz, metaVerts[ j ].xyz ) == qfalse )
1059 /* use smallest shade angle */
1060 shadeAngle = (shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ]);
1062 /* check shade angle */
1063 dot = DotProduct( metaVerts[ i ].normal, metaVerts[ j ].normal );
1066 else if( dot < -1.0 )
1068 testAngle = acos( dot ) + THETA_EPSILON;
1069 if( testAngle >= shadeAngle )
1072 /* add to the list */
1073 indexes[ numVerts++ ] = j;
1076 smoothed[ j >> 3 ] |= (1 << (j & 7));
1078 /* see if this normal has already been voted */
1079 for( k = 0; k < numVotes; k++ )
1081 VectorSubtract( metaVerts[ j ].normal, votes[ k ], diff );
1082 if( fabs( diff[ 0 ] ) < EQUAL_NORMAL_EPSILON &&
1083 fabs( diff[ 1 ] ) < EQUAL_NORMAL_EPSILON &&
1084 fabs( diff[ 2 ] ) < EQUAL_NORMAL_EPSILON )
1088 /* add a new vote? */
1089 if( k == numVotes && numVotes < MAX_SAMPLES )
1091 VectorAdd( average, metaVerts[ j ].normal, average );
1092 VectorCopy( metaVerts[ j ].normal, votes[ numVotes ] );
1097 /* don't average for less than 2 verts */
1101 /* average normal */
1102 if( VectorNormalize( average, average ) > 0 )
1105 for( j = 0; j < numVerts; j++ )
1106 VectorCopy( average, metaVerts[ indexes[ j ] ].normal );
1111 /* free the tables */
1112 free( shadeAngles );
1116 Sys_FPrintf( SYS_VRB, " (%d)\n", (int) (I_FloatTime() - start) );
1118 /* emit some stats */
1119 Sys_FPrintf( SYS_VRB, "%9d smoothed vertexes\n", numSmoothed );
1125 AddMetaVertToSurface()
1126 adds a drawvert to a surface unless an existing vert matching already exists
1127 returns the index of that vert (or < 0 on failure)
1130 int AddMetaVertToSurface( mapDrawSurface_t *ds, bspDrawVert_t *dv1, int *coincident )
1136 /* go through the verts and find a suitable candidate */
1137 for( i = 0; i < ds->numVerts; i++ )
1140 dv2 = &ds->verts[ i ];
1142 /* compare xyz and normal */
1143 if( VectorCompare( dv1->xyz, dv2->xyz ) == qfalse )
1145 if( VectorCompare( dv1->normal, dv2->normal ) == qfalse )
1148 /* good enough at this point */
1151 /* compare texture coordinates and color */
1152 if( dv1->st[ 0 ] != dv2->st[ 0 ] || dv1->st[ 1 ] != dv2->st[ 1 ] )
1154 if( dv1->color[ 0 ][ 3 ] != dv2->color[ 0 ][ 3 ] )
1157 /* found a winner */
1162 /* overflow check */
1163 if( ds->numVerts >= ((ds->shaderInfo->compileFlags & C_VERTEXLIT) ? maxSurfaceVerts : maxLMSurfaceVerts) )
1164 return VERTS_EXCEEDED;
1166 /* made it this far, add the vert and return */
1167 dv2 = &ds->verts[ ds->numVerts++ ];
1169 return (ds->numVerts - 1);
1176 AddMetaTriangleToSurface()
1177 attempts to add a metatriangle to a surface
1178 returns the score of the triangle added
1181 #define AXIS_SCORE 100000
1182 #define AXIS_MIN 100000
1183 #define VERT_SCORE 10000
1184 #define SURFACE_SCORE 1000
1186 #define ST_SCORE2 (2 * (ST_SCORE))
1188 #define ADEQUATE_SCORE ((AXIS_MIN) + 1 * (VERT_SCORE))
1189 #define GOOD_SCORE ((AXIS_MIN) + 2 * (VERT_SCORE) + 4 * (ST_SCORE))
1190 #define PERFECT_SCORE ((AXIS_MIN) + + 3 * (VERT_SCORE) + (SURFACE_SCORE) + 4 * (ST_SCORE))
1192 static int AddMetaTriangleToSurface( mapDrawSurface_t *ds, metaTriangle_t *tri, qboolean testAdd )
1194 int i, score, coincident, ai, bi, ci, oldTexRange[ 2 ];
1197 qboolean inTexRange, es, et;
1198 mapDrawSurface_t old;
1201 /* overflow check */
1202 if( ds->numIndexes >= maxSurfaceIndexes )
1205 /* test the triangle */
1206 if( ds->entityNum != tri->entityNum ) /* ydnar: added 2002-07-06 */
1208 if( ds->castShadows != tri->castShadows || ds->recvShadows != tri->recvShadows )
1210 if( ds->shaderInfo != tri->si || ds->fogNum != tri->fogNum || ds->sampleSize != tri->sampleSize )
1213 if( !(ds->shaderInfo->compileFlags & C_VERTEXLIT) &&
1214 //% VectorCompare( ds->lightmapAxis, tri->lightmapAxis ) == qfalse )
1215 DotProduct( ds->lightmapAxis, tri->plane ) < 0.25f )
1219 /* planar surfaces will only merge with triangles in the same plane */
1220 if( npDegrees == 0.0f && ds->shaderInfo->nonplanar == qfalse && ds->planeNum >= 0 )
1222 if( VectorCompare( mapplanes[ ds->planeNum ].normal, tri->plane ) == qfalse || mapplanes[ ds->planeNum ].dist != tri->plane[ 3 ] )
1224 if( tri->planeNum >= 0 && tri->planeNum != ds->planeNum )
1228 /* set initial score */
1229 score = tri->surfaceNum == ds->surfaceNum ? SURFACE_SCORE : 0;
1231 /* score the the dot product of lightmap axis to plane */
1232 if( (ds->shaderInfo->compileFlags & C_VERTEXLIT) || VectorCompare( ds->lightmapAxis, tri->lightmapAxis ) )
1233 score += AXIS_SCORE;
1235 score += AXIS_SCORE * DotProduct( ds->lightmapAxis, tri->plane );
1237 /* preserve old drawsurface if this fails */
1238 memcpy( &old, ds, sizeof( *ds ) );
1240 /* attempt to add the verts */
1242 ai = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 0 ] ], &coincident );
1243 bi = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 1 ] ], &coincident );
1244 ci = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 2 ] ], &coincident );
1246 /* check vertex underflow */
1247 if( ai < 0 || bi < 0 || ci < 0 )
1249 memcpy( ds, &old, sizeof( *ds ) );
1253 /* score coincident vertex count (2003-02-14: changed so this only matters on planar surfaces) */
1254 score += (coincident * VERT_SCORE);
1256 /* add new vertex bounds to mins/maxs */
1257 VectorCopy( ds->mins, mins );
1258 VectorCopy( ds->maxs, maxs );
1259 AddPointToBounds( metaVerts[ tri->indexes[ 0 ] ].xyz, mins, maxs );
1260 AddPointToBounds( metaVerts[ tri->indexes[ 1 ] ].xyz, mins, maxs );
1261 AddPointToBounds( metaVerts[ tri->indexes[ 2 ] ].xyz, mins, maxs );
1263 /* check lightmap bounds overflow (after at least 1 triangle has been added) */
1264 if( !(ds->shaderInfo->compileFlags & C_VERTEXLIT) &&
1265 ds->numIndexes > 0 && VectorLength( ds->lightmapAxis ) > 0.0f &&
1266 (VectorCompare( ds->mins, mins ) == qfalse || VectorCompare( ds->maxs, maxs ) == qfalse) )
1268 /* set maximum size before lightmap scaling (normally 2032 units) */
1269 /* 2004-02-24: scale lightmap test size by 2 to catch larger brush faces */
1270 /* 2004-04-11: reverting to actual lightmap size */
1271 lmMax = (ds->sampleSize * (ds->shaderInfo->lmCustomWidth - 1));
1272 for( i = 0; i < 3; i++ )
1274 if( (maxs[ i ] - mins[ i ]) > lmMax )
1276 memcpy( ds, &old, sizeof( *ds ) );
1282 /* check texture range overflow */
1283 oldTexRange[ 0 ] = ds->texRange[ 0 ];
1284 oldTexRange[ 1 ] = ds->texRange[ 1 ];
1285 inTexRange = CalcSurfaceTextureRange( ds );
1287 es = (ds->texRange[ 0 ] > oldTexRange[ 0 ]) ? qtrue : qfalse;
1288 et = (ds->texRange[ 1 ] > oldTexRange[ 1 ]) ? qtrue : qfalse;
1290 if( inTexRange == qfalse && ds->numIndexes > 0 )
1292 memcpy( ds, &old, sizeof( *ds ) );
1293 return UNSUITABLE_TRIANGLE;
1296 /* score texture range */
1297 if( ds->texRange[ 0 ] <= oldTexRange[ 0 ] )
1299 else if( ds->texRange[ 0 ] > oldTexRange[ 0 ] && oldTexRange[ 1 ] > oldTexRange[ 0 ] )
1302 if( ds->texRange[ 1 ] <= oldTexRange[ 1 ] )
1304 else if( ds->texRange[ 1 ] > oldTexRange[ 1 ] && oldTexRange[ 0 ] > oldTexRange[ 1 ] )
1308 /* go through the indexes and try to find an existing triangle that matches abc */
1309 for( i = 0; i < ds->numIndexes; i += 3 )
1311 /* 2002-03-11 (birthday!): rotate the triangle 3x to find an existing triangle */
1312 if( (ai == ds->indexes[ i ] && bi == ds->indexes[ i + 1 ] && ci == ds->indexes[ i + 2 ]) ||
1313 (bi == ds->indexes[ i ] && ci == ds->indexes[ i + 1 ] && ai == ds->indexes[ i + 2 ]) ||
1314 (ci == ds->indexes[ i ] && ai == ds->indexes[ i + 1 ] && bi == ds->indexes[ i + 2 ]) )
1316 /* triangle already present */
1317 memcpy( ds, &old, sizeof( *ds ) );
1322 /* rotate the triangle 3x to find an inverse triangle (error case) */
1323 if( (ai == ds->indexes[ i ] && bi == ds->indexes[ i + 2 ] && ci == ds->indexes[ i + 1 ]) ||
1324 (bi == ds->indexes[ i ] && ci == ds->indexes[ i + 2 ] && ai == ds->indexes[ i + 1 ]) ||
1325 (ci == ds->indexes[ i ] && ai == ds->indexes[ i + 2 ] && bi == ds->indexes[ i + 1 ]) )
1328 Sys_Printf( "WARNING: Flipped triangle: (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f)\n",
1329 ds->verts[ ai ].xyz[ 0 ], ds->verts[ ai ].xyz[ 1 ], ds->verts[ ai ].xyz[ 2 ],
1330 ds->verts[ bi ].xyz[ 0 ], ds->verts[ bi ].xyz[ 1 ], ds->verts[ bi ].xyz[ 2 ],
1331 ds->verts[ ci ].xyz[ 0 ], ds->verts[ ci ].xyz[ 1 ], ds->verts[ ci ].xyz[ 2 ] );
1333 /* reverse triangle already present */
1334 memcpy( ds, &old, sizeof( *ds ) );
1340 /* add the triangle indexes */
1341 if( ds->numIndexes < maxSurfaceIndexes )
1342 ds->indexes[ ds->numIndexes++ ] = ai;
1343 if( ds->numIndexes < maxSurfaceIndexes )
1344 ds->indexes[ ds->numIndexes++ ] = bi;
1345 if( ds->numIndexes < maxSurfaceIndexes )
1346 ds->indexes[ ds->numIndexes++ ] = ci;
1348 /* check index overflow */
1349 if( ds->numIndexes >= maxSurfaceIndexes )
1351 memcpy( ds, &old, sizeof( *ds ) );
1355 /* sanity check the indexes */
1356 if( ds->numIndexes >= 3 &&
1357 (ds->indexes[ ds->numIndexes - 3 ] == ds->indexes[ ds->numIndexes - 2 ] ||
1358 ds->indexes[ ds->numIndexes - 3 ] == ds->indexes[ ds->numIndexes - 1 ] ||
1359 ds->indexes[ ds->numIndexes - 2 ] == ds->indexes[ ds->numIndexes - 1 ]) )
1360 Sys_Printf( "DEG:%d! ", ds->numVerts );
1364 memcpy( ds, &old, sizeof( *ds ) );
1367 /* copy bounds back to surface */
1368 VectorCopy( mins, ds->mins );
1369 VectorCopy( maxs, ds->maxs );
1371 /* mark triangle as used */
1375 /* add a side reference */
1376 ds->sideRef = AllocSideRef( tri->side, ds->sideRef );
1378 /* return to sender */
1385 MetaTrianglesToSurface()
1386 creates map drawsurface(s) from the list of possibles
1389 static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles, int *fOld, int *numAdded )
1391 int i, j, f, best, score, bestScore;
1392 metaTriangle_t *seed, *test;
1393 mapDrawSurface_t *ds;
1394 bspDrawVert_t *verts;
1399 /* allocate arrays */
1400 verts = safe_malloc( sizeof( *verts ) * maxSurfaceVerts );
1401 indexes = safe_malloc( sizeof( *indexes ) * maxSurfaceIndexes );
1403 /* walk the list of triangles */
1404 for( i = 0, seed = possibles; i < numPossibles; i++, seed++ )
1406 /* skip this triangle if it has already been merged */
1407 if( seed->si == NULL )
1410 /* -----------------------------------------------------------------
1411 initial drawsurf construction
1412 ----------------------------------------------------------------- */
1414 /* start a new drawsurface */
1415 ds = AllocDrawSurface( SURFACE_META );
1416 ds->entityNum = seed->entityNum;
1417 ds->surfaceNum = seed->surfaceNum;
1418 ds->castShadows = seed->castShadows;
1419 ds->recvShadows = seed->recvShadows;
1421 ds->shaderInfo = seed->si;
1422 ds->planeNum = seed->planeNum;
1423 ds->fogNum = seed->fogNum;
1424 ds->sampleSize = seed->sampleSize;
1426 ds->indexes = indexes;
1427 VectorCopy( seed->lightmapAxis, ds->lightmapAxis );
1428 ds->sideRef = AllocSideRef( seed->side, NULL );
1430 ClearBounds( ds->mins, ds->maxs );
1432 /* clear verts/indexes */
1433 memset( verts, 0, sizeof( verts ) );
1434 memset( indexes, 0, sizeof( indexes ) );
1436 /* add the first triangle */
1437 if( AddMetaTriangleToSurface( ds, seed, qfalse ) )
1440 /* -----------------------------------------------------------------
1442 ----------------------------------------------------------------- */
1444 /* progressively walk the list until no more triangles can be added */
1448 /* print pacifier */
1449 f = 10 * *numAdded / numMetaTriangles;
1453 Sys_FPrintf( SYS_VRB, "%d...", f );
1456 /* reset best score */
1461 /* walk the list of possible candidates for merging */
1462 for( j = i + 1, test = &possibles[ j ]; j < numPossibles; j++, test++ )
1464 /* skip this triangle if it has already been merged */
1465 if( test->si == NULL )
1468 /* score this triangle */
1469 score = AddMetaTriangleToSurface( ds, test, qtrue );
1470 if( score > bestScore )
1475 /* if we have a score over a certain threshold, just use it */
1476 if( bestScore >= GOOD_SCORE )
1478 if( AddMetaTriangleToSurface( ds, &possibles[ best ], qfalse ) )
1489 /* add best candidate */
1490 if( best >= 0 && bestScore > ADEQUATE_SCORE )
1492 if( AddMetaTriangleToSurface( ds, &possibles[ best ], qfalse ) )
1500 /* copy the verts and indexes to the new surface */
1501 ds->verts = safe_malloc( ds->numVerts * sizeof( bspDrawVert_t ) );
1502 memcpy( ds->verts, verts, ds->numVerts * sizeof( bspDrawVert_t ) );
1503 ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) );
1504 memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( int ) );
1506 /* classify the surface */
1507 ClassifySurfaces( 1, ds );
1510 numMergedSurfaces++;
1521 CompareMetaTriangles()
1522 compare function for qsort()
1525 static int CompareMetaTriangles( const void *a, const void *b )
1528 vec3_t aMins, bMins;
1532 if( ((metaTriangle_t*) a)->si < ((metaTriangle_t*) b)->si )
1534 else if( ((metaTriangle_t*) a)->si > ((metaTriangle_t*) b)->si )
1538 else if( ((metaTriangle_t*) a)->fogNum < ((metaTriangle_t*) b)->fogNum )
1540 else if( ((metaTriangle_t*) a)->fogNum > ((metaTriangle_t*) b)->fogNum )
1545 else if( npDegrees == 0.0f && ((metaTriangle_t*) a)->si->nonplanar == qfalse &&
1546 ((metaTriangle_t*) a)->planeNum >= 0 && ((metaTriangle_t*) a)->planeNum >= 0 )
1548 if( ((metaTriangle_t*) a)->plane[ 3 ] < ((metaTriangle_t*) b)->plane[ 3 ] )
1550 else if( ((metaTriangle_t*) a)->plane[ 3 ] > ((metaTriangle_t*) b)->plane[ 3 ] )
1552 else if( ((metaTriangle_t*) a)->plane[ 0 ] < ((metaTriangle_t*) b)->plane[ 0 ] )
1554 else if( ((metaTriangle_t*) a)->plane[ 0 ] > ((metaTriangle_t*) b)->plane[ 0 ] )
1556 else if( ((metaTriangle_t*) a)->plane[ 1 ] < ((metaTriangle_t*) b)->plane[ 1 ] )
1558 else if( ((metaTriangle_t*) a)->plane[ 1 ] > ((metaTriangle_t*) b)->plane[ 1 ] )
1560 else if( ((metaTriangle_t*) a)->plane[ 2 ] < ((metaTriangle_t*) b)->plane[ 2 ] )
1562 else if( ((metaTriangle_t*) a)->plane[ 2 ] > ((metaTriangle_t*) b)->plane[ 2 ] )
1567 /* then position in world */
1570 VectorSet( aMins, 999999, 999999, 999999 );
1571 VectorSet( bMins, 999999, 999999, 999999 );
1572 for( i = 0; i < 3; i++ )
1574 av = ((metaTriangle_t*) a)->indexes[ i ];
1575 bv = ((metaTriangle_t*) b)->indexes[ i ];
1576 for( j = 0; j < 3; j++ )
1578 if( metaVerts[ av ].xyz[ j ] < aMins[ j ] )
1579 aMins[ j ] = metaVerts[ av ].xyz[ j ];
1580 if( metaVerts[ bv ].xyz[ j ] < bMins[ j ] )
1581 bMins[ j ] = metaVerts[ bv ].xyz[ j ];
1586 for( i = 0; i < 3; i++ )
1588 if( aMins[ i ] < bMins[ i ] )
1590 else if( aMins[ i ] > bMins[ i ] )
1594 /* functionally equivalent */
1601 MergeMetaTriangles()
1602 merges meta triangles into drawsurfaces
1605 void MergeMetaTriangles( void )
1607 int i, j, fOld, start, numAdded;
1608 metaTriangle_t *head, *end;
1611 /* only do this if there are meta triangles */
1612 if( numMetaTriangles <= 0 )
1616 Sys_FPrintf( SYS_VRB, "--- MergeMetaTriangles ---\n" );
1618 /* sort the triangles by shader major, fognum minor */
1619 qsort( metaTriangles, numMetaTriangles, sizeof( metaTriangle_t ), CompareMetaTriangles );
1623 start = I_FloatTime();
1627 for( i = 0, j = 0; i < numMetaTriangles; i = j )
1629 /* get head of list */
1630 head = &metaTriangles[ i ];
1632 /* skip this triangle if it has already been merged */
1633 if( head->si == NULL )
1639 for( j = i + 1; j < numMetaTriangles; j++ )
1641 /* get end of list */
1642 end = &metaTriangles[ j ];
1643 if( head->si != end->si || head->fogNum != end->fogNum )
1648 /* try to merge this list of possible merge candidates */
1649 MetaTrianglesToSurface( (j - i), head, &fOld, &numAdded );
1652 /* clear meta triangle list */
1653 ClearMetaTriangles();
1657 Sys_FPrintf( SYS_VRB, " (%d)\n", (int) (I_FloatTime() - start) );
1659 /* emit some stats */
1660 Sys_FPrintf( SYS_VRB, "%9d surfaces merged\n", numMergedSurfaces );
1661 Sys_FPrintf( SYS_VRB, "%9d vertexes merged\n", numMergedVerts );