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 ------------------------------------------------------------------------------- */
43 ydnar: gs mods: changed to force an explicit type when allocating
46 mapDrawSurface_t *AllocDrawSurface( surfaceType_t type )
51 /* ydnar: gs mods: only allocate valid types */
52 if( type <= SURFACE_BAD || type >= NUM_SURFACE_TYPES )
53 Error( "AllocDrawSurface: Invalid surface type %d specified", type );
56 if( numMapDrawSurfs >= MAX_MAP_DRAW_SURFS )
57 Error( "MAX_MAP_DRAW_SURFS (%d) exceeded", MAX_MAP_DRAW_SURFS );
58 ds = &mapDrawSurfs[ numMapDrawSurfs ];
61 /* ydnar: do initial surface setup */
62 memset( ds, 0, sizeof( mapDrawSurface_t ) );
65 ds->fogNum = defaultFogNum; /* ydnar 2003-02-12 */
66 ds->outputNum = -1; /* ydnar 2002-08-13 */
67 ds->surfaceNum = numMapDrawSurfs - 1; /* ydnar 2003-02-16 */
76 ydnar: general surface finish pass
79 void FinishSurface( mapDrawSurface_t *ds )
81 mapDrawSurface_t *ds2;
85 if( ds->type <= SURFACE_BAD || ds->type >= NUM_SURFACE_TYPES || ds == NULL || ds->shaderInfo == NULL )
88 /* ydnar: rocking tek-fu celshading */
89 if( ds->celShader != NULL )
90 MakeCelSurface( ds, ds->celShader );
92 /* backsides stop here */
96 /* ydnar: rocking surface cloning (fur baby yeah!) */
97 if( ds->shaderInfo->cloneShader != NULL && ds->shaderInfo->cloneShader[ 0 ] != '\0' )
98 CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->cloneShader ) );
100 /* ydnar: q3map_backShader support */
101 if( ds->shaderInfo->backShader != NULL && ds->shaderInfo->backShader[ 0 ] != '\0' )
103 ds2 = CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->backShader ) );
104 ds2->backSide = qtrue;
112 clones a map drawsurface, using the specified shader
115 mapDrawSurface_t *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si )
117 mapDrawSurface_t *ds;
121 if( src == NULL || si == NULL )
124 /* allocate a new surface */
125 ds = AllocDrawSurface( src->type );
130 memcpy( ds, src, sizeof( *ds ) );
132 /* destroy side reference */
139 if( ds->numVerts > 0 )
141 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
142 memcpy( ds->verts, src->verts, ds->numVerts * sizeof( *ds->verts ) );
146 if( ds->numIndexes <= 0 )
148 ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
149 memcpy( ds->indexes, src->indexes, ds->numIndexes * sizeof( *ds->indexes ) );
151 /* return the surface */
158 MakeCelSurface() - ydnar
159 makes a copy of a surface, but specific to cel shading
162 mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si )
164 mapDrawSurface_t *ds;
168 if( src == NULL || si == NULL )
171 /* don't create cel surfaces for certain types of shaders */
172 if( (src->shaderInfo->compileFlags & C_TRANSLUCENT) ||
173 (src->shaderInfo->compileFlags & C_SKY) )
177 ds = CloneSurface( src, si );
181 /* do some fixups for celshading */
184 ds->celShader = NULL; /* don't cel shade cels :P */
186 /* return the surface */
193 MakeSkyboxSurface() - ydnar
194 generates a skybox surface, viewable from everywhere there is sky
197 mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src )
200 mapDrawSurface_t *ds;
208 ds = CloneSurface( src, src->shaderInfo );
215 /* scale the surface vertexes */
216 for( i = 0; i < ds->numVerts; i++ )
218 m4x4_transform_point( skyboxTransform, ds->verts[ i ].xyz );
221 //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 1 ] = 0;
222 //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 2 ] = 0;
225 /* so backface culling creep doesn't bork the surface */
226 VectorClear( ds->lightmapVecs[ 2 ] );
228 /* return the surface */
236 returns qtrue if all three points are colinear, backwards, or the triangle is just plain bogus
239 #define TINY_AREA 1.0f
241 qboolean IsTriangleDegenerate( bspDrawVert_t *points, int a, int b, int c )
247 /* calcuate the area of the triangle */
248 VectorSubtract( points[ b ].xyz, points[ a ].xyz, v1 );
249 VectorSubtract( points[ c ].xyz, points[ a ].xyz, v2 );
250 CrossProduct( v1, v2, v3 );
251 d = VectorLength( v3 );
253 /* assume all very small or backwards triangles will cause problems */
257 /* must be a good triangle */
264 ClearSurface() - ydnar
265 clears a surface and frees any allocated memory
268 void ClearSurface( mapDrawSurface_t *ds )
270 ds->type = SURFACE_BAD;
274 if( ds->verts != NULL )
278 if( ds->indexes != NULL )
281 numClearedSurfaces++;
287 TidyEntitySurfaces() - ydnar
288 deletes all empty or bad surfaces from the surface list
291 void TidyEntitySurfaces( entity_t *e )
294 mapDrawSurface_t *out, *in;
298 Sys_FPrintf( SYS_VRB, "--- TidyEntitySurfaces ---\n" );
300 /* walk the surface list */
302 for( i = e->firstDrawSurf, j = e->firstDrawSurf; j < numMapDrawSurfs; i++, j++ )
304 /* get out surface */
305 out = &mapDrawSurfs[ i ];
307 /* walk the surface list again until a proper surface is found */
308 for( ; j < numMapDrawSurfs; j++ )
311 in = &mapDrawSurfs[ j ];
313 /* this surface ok? */
314 if( in->type == SURFACE_FLARE || in->type == SURFACE_SHADER ||
315 (in->type != SURFACE_BAD && in->numVerts > 0) )
323 /* copy if necessary */
325 memcpy( out, in, sizeof( mapDrawSurface_t ) );
328 /* set the new number of drawsurfs */
331 /* emit some stats */
332 Sys_FPrintf( SYS_VRB, "%9d empty or malformed surfaces deleted\n", deleted );
338 CalcSurfaceTextureRange() - ydnar
339 calculates the clamped texture range for a given surface, returns qtrue if it's within [-texRange,texRange]
342 qboolean CalcSurfaceTextureRange( mapDrawSurface_t *ds )
344 int i, j, v, size[ 2 ];
345 float mins[ 2 ], maxs[ 2 ];
348 /* try to early out */
349 if( ds->numVerts <= 0 )
352 /* walk the verts and determine min/max st values */
357 for( i = 0; i < ds->numVerts; i++ )
359 for( j = 0; j < 2; j++ )
361 if( ds->verts[ i ].st[ j ] < mins[ j ] )
362 mins[ j ] = ds->verts[ i ].st[ j ];
363 if( ds->verts[ i ].st[ j ] > maxs[ j ] )
364 maxs[ j ] = ds->verts[ i ].st[ j ];
368 /* clamp to integer range and calculate surface bias values */
369 for( j = 0; j < 2; j++ )
370 ds->bias[ j ] = -floor( 0.5f * (mins[ j ] + maxs[ j ]) );
372 /* find biased texture coordinate mins/maxs */
373 size[ 0 ] = ds->shaderInfo->shaderWidth;
374 size[ 1 ] = ds->shaderInfo->shaderHeight;
375 ds->texMins[ 0 ] = 999999;
376 ds->texMins[ 1 ] = 999999;
377 ds->texMaxs[ 0 ] = -999999;
378 ds->texMaxs[ 1 ] = -999999;
379 for( i = 0; i < ds->numVerts; i++ )
381 for( j = 0; j < 2; j++ )
383 v = ((float) ds->verts[ i ].st[ j ] + ds->bias[ j ]) * size[ j ];
384 if( v < ds->texMins[ j ] )
385 ds->texMins[ j ] = v;
386 if( v > ds->texMaxs[ j ] )
387 ds->texMaxs[ j ] = v;
392 for( j = 0; j < 2; j++ )
393 ds->texRange[ j ] = (ds->texMaxs[ j ] - ds->texMins[ j ]);
395 /* if range is zero, then assume unlimited precision */
400 for( j = 0; j < 2; j++ )
402 if( ds->texMins[ j ] < -texRange || ds->texMaxs[ j ] > texRange )
413 CalcLightmapAxis() - ydnar
414 gives closed lightmap axis for a plane normal
417 qboolean CalcLightmapAxis( vec3_t normal, vec3_t axis )
423 if( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && normal[ 2 ] == 0.0f )
429 /* get absolute normal */
430 absolute[ 0 ] = fabs( normal[ 0 ] );
431 absolute[ 1 ] = fabs( normal[ 1 ] );
432 absolute[ 2 ] = fabs( normal[ 2 ] );
435 if( absolute[ 2 ] > absolute[ 0 ] - 0.0001f && absolute[ 2 ] > absolute[ 1 ] - 0.0001f )
437 if( normal[ 2 ] > 0.0f )
438 VectorSet( axis, 0.0f, 0.0f, 1.0f );
440 VectorSet( axis, 0.0f, 0.0f, -1.0f );
442 else if( absolute[ 0 ] > absolute[ 1 ] - 0.0001f && absolute[ 0 ] > absolute[ 2 ] - 0.0001f )
444 if( normal[ 0 ] > 0.0f )
445 VectorSet( axis, 1.0f, 0.0f, 0.0f );
447 VectorSet( axis, -1.0f, 0.0f, 0.0f );
451 if( normal[ 1 ] > 0.0f )
452 VectorSet( axis, 0.0f, 1.0f, 0.0f );
454 VectorSet( axis, 0.0f, -1.0f, 0.0f );
464 ClassifySurfaces() - ydnar
465 fills out a bunch of info in the surfaces, including planar status, lightmap projection, and bounding box
468 #define PLANAR_EPSILON 0.5f //% 0.126f 0.25f
470 void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds )
476 static vec3_t axii[ 6 ] =
487 /* walk the list of surfaces */
488 for( ; numSurfs > 0; numSurfs--, ds++ )
490 /* ignore bogus (or flare) surfaces */
491 if( ds->type == SURFACE_BAD || ds->numVerts <= 0 )
497 /* -----------------------------------------------------------------
498 force meta if vertex count is too high or shader requires it
499 ----------------------------------------------------------------- */
501 if( ds->type != SURFACE_PATCH && ds->type != SURFACE_FACE )
503 if( ds->numVerts > SHADER_MAX_VERTEXES )
504 ds->type = SURFACE_FORCED_META;
507 /* -----------------------------------------------------------------
508 plane and bounding box classification
509 ----------------------------------------------------------------- */
511 /* set surface bounding box */
512 ClearBounds( ds->mins, ds->maxs );
513 for( i = 0; i < ds->numVerts; i++ )
514 AddPointToBounds( ds->verts[ i ].xyz, ds->mins, ds->maxs );
516 /* try to get an existing plane */
517 if( ds->planeNum >= 0 )
519 VectorCopy( mapplanes[ ds->planeNum ].normal, plane );
520 plane[ 3 ] = mapplanes[ ds->planeNum ].dist;
523 /* construct one from the first vert with a valid normal */
526 VectorClear( plane );
528 for( i = 0; i < ds->numVerts; i++ )
530 if( ds->verts[ i ].normal[ 0 ] != 0.0f && ds->verts[ i ].normal[ 1 ] != 0.0f && ds->verts[ i ].normal[ 2 ] != 0.0f )
532 VectorCopy( ds->verts[ i ].normal, plane );
533 plane[ 3 ] = DotProduct( ds->verts[ i ].xyz, plane );
539 /* test for bogus plane */
540 if( VectorLength( plane ) <= 0.0f )
547 /* determine if surface is planar */
551 for( i = 0; i < ds->numVerts; i++ )
553 /* point-plane test */
554 dist = DotProduct( ds->verts[ i ].xyz, plane ) - plane[ 3 ];
555 if( fabs( dist ) > PLANAR_EPSILON )
557 //% if( ds->planeNum >= 0 )
559 //% Sys_Printf( "WARNING: Planar surface marked unplanar (%f > %f)\n", fabs( dist ), PLANAR_EPSILON );
560 //% ds->verts[ i ].color[ 0 ][ 0 ] = ds->verts[ i ].color[ 0 ][ 2 ] = 0;
568 /* find map plane if necessary */
571 if( ds->planeNum < 0 )
572 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &ds->verts[ 0 ].xyz );
573 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
578 VectorClear( ds->lightmapVecs[ 2 ] );
579 //% if( ds->type == SURF_META || ds->type == SURF_FACE )
580 //% Sys_Printf( "WARNING: Non-planar face (%d): %s\n", ds->planeNum, ds->shaderInfo->shader );
583 /* -----------------------------------------------------------------
584 lightmap bounds and axis projection
585 ----------------------------------------------------------------- */
587 /* vertex lit surfaces don't need this information */
588 if( si->compileFlags & C_VERTEXLIT || ds->type == SURFACE_TRIANGLES )
590 VectorClear( ds->lightmapAxis );
591 //% VectorClear( ds->lightmapVecs[ 2 ] );
596 /* the shader can specify an explicit lightmap axis */
597 if( si->lightmapAxis[ 0 ] || si->lightmapAxis[ 1 ] || si->lightmapAxis[ 2 ] )
598 VectorCopy( si->lightmapAxis, ds->lightmapAxis );
599 else if( ds->type == SURFACE_FORCED_META )
600 VectorClear( ds->lightmapAxis );
601 else if( ds->planar )
602 CalcLightmapAxis( plane, ds->lightmapAxis );
605 /* find best lightmap axis */
606 for( bestAxis = 0; bestAxis < 6; bestAxis++ )
608 for( i = 0; i < ds->numVerts && bestAxis < 6; i++ )
610 //% Sys_Printf( "Comparing %1.3f %1.3f %1.3f to %1.3f %1.3f %1.3f\n",
611 //% ds->verts[ i ].normal[ 0 ], ds->verts[ i ].normal[ 1 ], ds->verts[ i ].normal[ 2 ],
612 //% axii[ bestAxis ][ 0 ], axii[ bestAxis ][ 1 ], axii[ bestAxis ][ 2 ] );
613 if( DotProduct( ds->verts[ i ].normal, axii[ bestAxis ] ) < 0.25f ) /* fixme: adjust this tolerance to taste */
617 if( i == ds->numVerts )
621 /* set axis if possible */
624 //% if( ds->type == SURFACE_PATCH )
625 //% Sys_Printf( "Mapped axis %d onto patch\n", bestAxis );
626 VectorCopy( axii[ bestAxis ], ds->lightmapAxis );
630 //% if( ds->type == SURFACE_PATCH )
631 //% Sys_Printf( "Failed to map axis %d onto patch\n", bestAxis );
634 /* get lightmap sample size */
635 if( ds->sampleSize <= 0 )
637 ds->sampleSize = sampleSize;
638 if( ds->shaderInfo->lightmapSampleSize )
639 ds->sampleSize = ds->shaderInfo->lightmapSampleSize;
640 if( ds->lightmapScale > 0 )
641 ds->sampleSize *= ds->lightmapScale;
642 if( ds->sampleSize <= 0 )
644 if(ds->sampleSize < minSampleSize)
645 ds->sampleSize = minSampleSize;
646 if( ds->sampleSize > 16384 ) /* powers of 2 are preferred */
647 ds->sampleSize = 16384;
655 ClassifyEntitySurfaces() - ydnar
656 classifies all surfaces in an entity
659 void ClassifyEntitySurfaces( entity_t *e )
665 Sys_FPrintf( SYS_VRB, "--- ClassifyEntitySurfaces ---\n" );
667 /* walk the surface list */
668 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
670 FinishSurface( &mapDrawSurfs[ i ] );
671 ClassifySurfaces( 1, &mapDrawSurfs[ i ] );
675 TidyEntitySurfaces( e );
681 GetShaderIndexForPoint() - ydnar
682 for shader-indexed surfaces (terrain), find a matching index from the indexmap
685 byte GetShaderIndexForPoint( indexMap_t *im, vec3_t eMins, vec3_t eMaxs, vec3_t point )
689 vec3_t mins, maxs, size;
692 /* early out if no indexmap */
696 /* this code is really broken */
698 /* legacy precision fudges for terrain */
699 for( i = 0; i < 3; i++ )
701 mins[ i ] = floor( eMins[ i ] + 0.1 );
702 maxs[ i ] = floor( eMaxs[ i ] + 0.1 );
703 size[ i ] = maxs[ i ] - mins[ i ];
706 /* find st (fixme: support more than just z-axis projection) */
707 s = floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ];
708 t = floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ];
723 for( i = 0; i < 3; i++ )
725 mins[ i ] = eMins[ i ];
726 maxs[ i ] = eMaxs[ i ];
727 size[ i ] = maxs[ i ] - mins[ i ];
731 s = (point[ 0 ] - mins[ 0 ]) / size[ 0 ];
732 t = (maxs[ 1 ] - point[ 1 ]) / size[ 1 ];
739 else if( x > (im->w - 1) )
743 else if( y > (im->h - 1) )
748 return im->pixels[ y * im->w + x ];
754 GetIndexedShader() - ydnar
755 for a given set of indexes and an indexmap, get a shader and set the vertex alpha in-place
756 this combines a couple different functions from terrain.c
759 shaderInfo_t *GetIndexedShader( shaderInfo_t *parent, indexMap_t *im, int numPoints, byte *shaderIndexes )
762 byte minShaderIndex, maxShaderIndex;
763 char shader[ MAX_QPATH ];
767 /* early out if bad data */
768 if( im == NULL || numPoints <= 0 || shaderIndexes == NULL )
769 return ShaderInfoForShader( "default" );
771 /* determine min/max index */
772 minShaderIndex = 255;
774 for( i = 0; i < numPoints; i++ )
776 if( shaderIndexes[ i ] < minShaderIndex )
777 minShaderIndex = shaderIndexes[ i ];
778 if( shaderIndexes[ i ] > maxShaderIndex )
779 maxShaderIndex = shaderIndexes[ i ];
782 /* set alpha inline */
783 for( i = 0; i < numPoints; i++ )
785 /* straight rip from terrain.c */
786 if( shaderIndexes[ i ] < maxShaderIndex )
787 shaderIndexes[ i ] = 0;
789 shaderIndexes[ i ] = 255;
792 /* make a shader name */
793 if( minShaderIndex == maxShaderIndex )
794 sprintf( shader, "textures/%s_%d", im->shader, maxShaderIndex );
796 sprintf( shader, "textures/%s_%dto%d", im->shader, minShaderIndex, maxShaderIndex );
799 si = ShaderInfoForShader( shader );
801 /* inherit a few things from parent shader */
802 if( parent->globalTexture )
803 si->globalTexture = qtrue;
804 if( parent->forceMeta )
805 si->forceMeta = qtrue;
806 if( parent->nonplanar )
807 si->nonplanar = qtrue;
808 if( si->shadeAngleDegrees == 0.0 )
809 si->shadeAngleDegrees = parent->shadeAngleDegrees;
810 if( parent->tcGen && si->tcGen == qfalse )
812 /* set xy texture projection */
814 VectorCopy( parent->vecs[ 0 ], si->vecs[ 0 ] );
815 VectorCopy( parent->vecs[ 1 ], si->vecs[ 1 ] );
817 if( VectorLength( parent->lightmapAxis ) > 0.0f && VectorLength( si->lightmapAxis ) <= 0.0f )
819 /* set lightmap projection axis */
820 VectorCopy( parent->lightmapAxis, si->lightmapAxis );
823 /* return the shader */
832 creates a SURF_FACE drawsurface from a given brush side and winding
835 #define SNAP_FLOAT_TO_INT 8
836 #define SNAP_INT_TO_FLOAT (1.0 / SNAP_FLOAT_TO_INT)
838 mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w )
841 mapDrawSurface_t *ds;
842 shaderInfo_t *si, *parent;
848 byte shaderIndexes[ 256 ];
849 float offsets[ 256 ];
850 char tempShader[ MAX_QPATH ];
853 /* ydnar: don't make a drawsurf for culled sides */
858 if( w->numpoints > MAX_POINTS_ON_WINDING )
859 Error( "DrawSurfaceForSide: w->numpoints = %d (> %d)", w->numpoints, MAX_POINTS_ON_WINDING );
864 /* ydnar: gs mods: check for indexed shader */
865 if( si->indexed && b->im != NULL )
870 /* get shader indexes for each point */
871 for( i = 0; i < w->numpoints; i++ )
873 shaderIndexes[ i ] = GetShaderIndexForPoint( b->im, b->eMins, b->eMaxs, w->p[ i ] );
874 offsets[ i ] = b->im->offsets[ shaderIndexes[ i ] ];
875 //% Sys_Printf( "%f ", offsets[ i ] );
878 /* get matching shader and set alpha */
880 si = GetIndexedShader( parent, b->im, w->numpoints, shaderIndexes );
885 /* ydnar: sky hack/fix for GL_CLAMP borders on ati cards */
886 if( skyFixHack && si->skyParmsImageBase[ 0 ] != '\0' )
888 //% Sys_FPrintf( SYS_VRB, "Enabling sky hack for shader %s using env %s\n", si->shader, si->skyParmsImageBase );
889 sprintf( tempShader, "%s_lf", si->skyParmsImageBase );
890 DrawSurfaceForShader( tempShader );
891 sprintf( tempShader, "%s_rt", si->skyParmsImageBase );
892 DrawSurfaceForShader( tempShader );
893 sprintf( tempShader, "%s_ft", si->skyParmsImageBase );
894 DrawSurfaceForShader( tempShader );
895 sprintf( tempShader, "%s_bk", si->skyParmsImageBase );
896 DrawSurfaceForShader( tempShader );
897 sprintf( tempShader, "%s_up", si->skyParmsImageBase );
898 DrawSurfaceForShader( tempShader );
899 sprintf( tempShader, "%s_dn", si->skyParmsImageBase );
900 DrawSurfaceForShader( tempShader );
904 ds = AllocDrawSurface( SURFACE_FACE );
905 ds->entityNum = b->entityNum;
906 ds->castShadows = b->castShadows;
907 ds->recvShadows = b->recvShadows;
910 ds->planeNum = s->planenum;
911 VectorCopy( mapplanes[ s->planenum ].normal, ds->lightmapVecs[ 2 ] );
915 ds->sideRef = AllocSideRef( s, NULL );
917 ds->lightmapScale = b->lightmapScale;
918 ds->numVerts = w->numpoints;
919 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
920 memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
922 /* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
923 ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY );
925 /* create the vertexes */
926 for( j = 0; j < w->numpoints; j++ )
928 /* get the drawvert */
931 /* copy xyz and do potential z offset */
932 VectorCopy( w->p[ j ], dv->xyz );
934 dv->xyz[ 2 ] += offsets[ j ];
936 /* round the xyz to a given precision and translate by origin */
937 for( i = 0 ; i < 3 ; i++ )
938 dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5f );
939 VectorAdd( dv->xyz, e->origin, vTranslated );
941 /* ydnar: tek-fu celshading support for flat shaded shit */
944 dv->st[ 0 ] = si->stFlat[ 0 ];
945 dv->st[ 1 ] = si->stFlat[ 1 ];
948 /* ydnar: gs mods: added support for explicit shader texcoord generation */
951 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
952 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
955 /* old quake-style texturing */
956 else if( g_bBrushPrimit == BPRIMIT_OLDBRUSHES )
958 /* nearest-axial projection */
959 dv->st[ 0 ] = s->vecs[ 0 ][ 3 ] + DotProduct( s->vecs[ 0 ], vTranslated );
960 dv->st[ 1 ] = s->vecs[ 1 ][ 3 ] + DotProduct( s->vecs[ 1 ], vTranslated );
961 dv->st[ 0 ] /= si->shaderWidth;
962 dv->st[ 1 ] /= si->shaderHeight;
965 /* brush primitive texturing */
968 /* calculate texture s/t from brush primitive texture matrix */
969 x = DotProduct( vTranslated, texX );
970 y = DotProduct( vTranslated, texY );
971 dv->st[ 0 ] = s->texMat[ 0 ][ 0 ] * x + s->texMat[ 0 ][ 1 ] * y + s->texMat[ 0 ][ 2 ];
972 dv->st[ 1 ] = s->texMat[ 1 ][ 0 ] * x + s->texMat[ 1 ][ 1 ] * y + s->texMat[ 1 ][ 2 ];
976 VectorCopy( mapplanes[ s->planenum ].normal, dv->normal );
978 /* ydnar: set color */
979 for( k = 0; k < MAX_LIGHTMAPS; k++ )
981 dv->color[ k ][ 0 ] = 255;
982 dv->color[ k ][ 1 ] = 255;
983 dv->color[ k ][ 2 ] = 255;
985 /* ydnar: gs mods: handle indexed shader blending */
986 dv->color[ k ][ 3 ] = (indexed ? shaderIndexes[ j ] : 255);
991 ds->celShader = b->celShader;
993 /* ydnar: gs mods: moved st biasing elsewhere */
1000 DrawSurfaceForMesh()
1001 moved here from patch.c
1004 #define YDNAR_NORMAL_EPSILON 0.50f
1006 qboolean VectorCompareExt( vec3_t n1, vec3_t n2, float epsilon )
1012 for( i= 0; i < 3; i++ )
1013 if( fabs( n1[ i ] - n2[ i ]) > epsilon )
1018 mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh )
1024 mapDrawSurface_t *ds;
1025 shaderInfo_t *si, *parent;
1030 byte shaderIndexes[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1031 float offsets[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1034 /* get mesh and shader shader */
1038 if( mesh == NULL || si == NULL )
1041 /* get vertex count */
1042 numVerts = mesh->width * mesh->height;
1044 /* to make valid normals for patches with degenerate edges,
1045 we need to make a copy of the mesh and put the aproximating
1046 points onto the curve */
1048 /* create a copy of the mesh */
1049 copy = CopyMesh( mesh );
1051 /* store off the original (potentially bad) normals */
1052 MakeMeshNormals( *copy );
1053 for( i = 0; i < numVerts; i++ )
1054 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1056 /* put the mesh on the curve */
1057 PutMeshOnCurve( *copy );
1059 /* find new normals (to take into account degenerate/flipped edges */
1060 MakeMeshNormals( *copy );
1061 for( i = 0; i < numVerts; i++ )
1063 /* ydnar: only copy normals that are significantly different from the originals */
1064 if( DotProduct( copy->verts[ i ].normal, mesh->verts[ i ].normal ) < 0.75f )
1065 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1068 /* free the old mesh */
1071 /* ydnar: gs mods: check for indexed shader */
1072 if( si->indexed && p->im != NULL )
1077 /* get shader indexes for each point */
1078 for( i = 0; i < numVerts; i++ )
1080 shaderIndexes[ i ] = GetShaderIndexForPoint( p->im, p->eMins, p->eMaxs, mesh->verts[ i ].xyz );
1081 offsets[ i ] = p->im->offsets[ shaderIndexes[ i ] ];
1084 /* get matching shader and set alpha */
1086 si = GetIndexedShader( parent, p->im, numVerts, shaderIndexes );
1092 /* ydnar: gs mods */
1093 ds = AllocDrawSurface( SURFACE_PATCH );
1094 ds->entityNum = p->entityNum;
1095 ds->castShadows = p->castShadows;
1096 ds->recvShadows = p->recvShadows;
1098 ds->shaderInfo = si;
1100 ds->lightmapScale = p->lightmapScale; /* ydnar */
1101 ds->patchWidth = mesh->width;
1102 ds->patchHeight = mesh->height;
1103 ds->numVerts = ds->patchWidth * ds->patchHeight;
1104 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
1105 memcpy( ds->verts, mesh->verts, ds->numVerts * sizeof( *ds->verts ) );
1110 ds->longestCurve = p->longestCurve;
1111 ds->maxIterations = p->maxIterations;
1113 /* construct a plane from the first vert */
1114 VectorCopy( mesh->verts[ 0 ].normal, plane );
1115 plane[ 3 ] = DotProduct( mesh->verts[ 0 ].xyz, plane );
1118 /* spew forth errors */
1119 if( VectorLength( plane ) < 0.001f )
1120 Sys_Printf( "BOGUS " );
1122 /* test each vert */
1123 for( i = 1; i < ds->numVerts && planar; i++ )
1126 if( VectorCompare( plane, mesh->verts[ i ].normal ) == qfalse )
1129 /* point-plane test */
1130 dist = DotProduct( mesh->verts[ i ].xyz, plane ) - plane[ 3 ];
1131 if( fabs( dist ) > EQUAL_EPSILON )
1135 /* add a map plane */
1138 /* make a map plane */
1139 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &mesh->verts[ 0 ].xyz );
1140 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
1142 /* push this normal to all verts (ydnar 2003-02-14: bad idea, small patches get screwed up) */
1143 for( i = 0; i < ds->numVerts; i++ )
1144 VectorCopy( plane, ds->verts[ i ].normal );
1147 /* walk the verts to do special stuff */
1148 for( i = 0; i < ds->numVerts; i++ )
1150 /* get the drawvert */
1151 dv = &ds->verts[ i ];
1153 /* ydnar: tek-fu celshading support for flat shaded shit */
1156 dv->st[ 0 ] = si->stFlat[ 0 ];
1157 dv->st[ 1 ] = si->stFlat[ 1 ];
1160 /* ydnar: gs mods: added support for explicit shader texcoord generation */
1161 else if( si->tcGen )
1163 /* translate by origin and project the texture */
1164 VectorAdd( dv->xyz, e->origin, vTranslated );
1165 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
1166 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
1169 /* ydnar: set color */
1170 for( k = 0; k < MAX_LIGHTMAPS; k++ )
1172 dv->color[ k ][ 0 ] = 255;
1173 dv->color[ k ][ 1 ] = 255;
1174 dv->color[ k ][ 2 ] = 255;
1176 /* ydnar: gs mods: handle indexed shader blending */
1177 dv->color[ k ][ 3 ] = (indexed ? shaderIndexes[ i ] : 255);
1182 dv->xyz[ 2 ] += offsets[ i ];
1185 /* set cel shader */
1186 ds->celShader = p->celShader;
1188 /* return the drawsurface */
1195 DrawSurfaceForFlare() - ydnar
1196 creates a flare draw surface
1199 mapDrawSurface_t *DrawSurfaceForFlare( int entNum, vec3_t origin, vec3_t normal, vec3_t color, char *flareShader, int lightStyle )
1201 mapDrawSurface_t *ds;
1205 if( emitFlares == qfalse )
1208 /* allocate drawsurface */
1209 ds = AllocDrawSurface( SURFACE_FLARE );
1210 ds->entityNum = entNum;
1213 if( flareShader != NULL && flareShader[ 0 ] != '\0' )
1214 ds->shaderInfo = ShaderInfoForShader( flareShader );
1216 ds->shaderInfo = ShaderInfoForShader( game->flareShader );
1217 if( origin != NULL )
1218 VectorCopy( origin, ds->lightmapOrigin );
1219 if( normal != NULL )
1220 VectorCopy( normal, ds->lightmapVecs[ 2 ] );
1222 VectorCopy( color, ds->lightmapVecs[ 0 ] );
1224 /* store light style */
1225 ds->lightStyle = lightStyle;
1226 if( ds->lightStyle < 0 || ds->lightStyle >= LS_NONE )
1227 ds->lightStyle = LS_NORMAL;
1231 /* return to sender */
1238 DrawSurfaceForShader() - ydnar
1239 creates a bogus surface to forcing the game to load a shader
1242 mapDrawSurface_t *DrawSurfaceForShader( char *shader )
1246 mapDrawSurface_t *ds;
1250 si = ShaderInfoForShader( shader );
1252 /* find existing surface */
1253 for( i = 0; i < numMapDrawSurfs; i++ )
1256 ds = &mapDrawSurfs[ i ];
1259 if( ds->shaderInfo == si )
1263 /* create a new surface */
1264 ds = AllocDrawSurface( SURFACE_SHADER );
1266 ds->shaderInfo = ShaderInfoForShader( shader );
1268 /* return to sender */
1275 AddSurfaceFlare() - ydnar
1276 creates flares (coronas) centered on surfaces
1279 static void AddSurfaceFlare( mapDrawSurface_t *ds, vec3_t entityOrigin )
1286 VectorClear( origin );
1287 for ( i = 0; i < ds->numVerts; i++ )
1288 VectorAdd( origin, ds->verts[ i ].xyz, origin );
1289 VectorScale( origin, (1.0f / ds->numVerts), origin );
1290 if( entityOrigin != NULL )
1291 VectorAdd( origin, entityOrigin, origin );
1293 /* push origin off surface a bit */
1294 VectorMA( origin, 2.0f, ds->lightmapVecs[ 2 ], origin );
1296 /* create the drawsurface */
1297 DrawSurfaceForFlare( ds->entityNum, origin, ds->lightmapVecs[ 2 ], ds->shaderInfo->color, ds->shaderInfo->flareShader, ds->shaderInfo->lightStyle );
1304 subdivides a face surface until it is smaller than the specified size (subdivisions)
1307 static void SubdivideFace_r( entity_t *e, brush_t *brush, side_t *side, winding_t *w, int fogNum, float subdivisions )
1312 const float epsilon = 0.1;
1313 int subFloor, subCeil;
1314 winding_t *frontWinding, *backWinding;
1315 mapDrawSurface_t *ds;
1321 if( w->numpoints < 3 )
1322 Error( "SubdivideFace_r: Bad w->numpoints (%d < 3)", w->numpoints );
1324 /* determine surface bounds */
1325 ClearBounds( bounds[ 0 ], bounds[ 1 ] );
1326 for( i = 0; i < w->numpoints; i++ )
1327 AddPointToBounds( w->p[ i ], bounds[ 0 ], bounds[ 1 ] );
1329 /* split the face */
1330 for( axis = 0; axis < 3; axis++ )
1332 vec3_t planePoint = { 0, 0, 0 };
1333 vec3_t planeNormal = { 0, 0, 0 };
1337 /* create an axial clipping plane */
1338 subFloor = floor( bounds[ 0 ][ axis ] / subdivisions) * subdivisions;
1339 subCeil = ceil( bounds[ 1 ][ axis ] / subdivisions) * subdivisions;
1340 planePoint[ axis ] = subFloor + subdivisions;
1341 planeNormal[ axis ] = -1;
1342 d = DotProduct( planePoint, planeNormal );
1344 /* subdivide if necessary */
1345 if( (subCeil - subFloor) > subdivisions )
1347 /* clip the winding */
1348 ClipWindingEpsilon( w, planeNormal, d, epsilon, &frontWinding, &backWinding );
1350 /* the clip may not produce two polygons if it was epsilon close */
1351 if( frontWinding == NULL )
1353 else if( backWinding == NULL )
1357 SubdivideFace_r( e, brush, side, frontWinding, fogNum, subdivisions );
1358 SubdivideFace_r( e, brush, side, backWinding, fogNum, subdivisions );
1364 /* create a face surface */
1365 ds = DrawSurfaceForSide( e, brush, side, w );
1367 /* set correct fog num */
1368 ds->fogNum = fogNum;
1374 SubdivideFaceSurfaces()
1375 chop up brush face surfaces that have subdivision attributes
1376 ydnar: and subdivide surfaces that exceed specified texture coordinate range
1379 void SubdivideFaceSurfaces( entity_t *e, tree_t *tree )
1381 int i, j, numBaseDrawSurfs, fogNum;
1382 mapDrawSurface_t *ds;
1387 float range, size, subdivisions, s2;
1391 Sys_FPrintf( SYS_VRB, "--- SubdivideFaceSurfaces ---\n" );
1393 /* walk the list of surfaces */
1394 numBaseDrawSurfs = numMapDrawSurfs;
1395 for( i = e->firstDrawSurf; i < numBaseDrawSurfs; i++ )
1398 ds = &mapDrawSurfs[ i ];
1400 /* only subdivide brush sides */
1401 if( ds->type != SURFACE_FACE || ds->mapBrush == NULL || ds->sideRef == NULL || ds->sideRef->side == NULL )
1405 brush = ds->mapBrush;
1406 side = ds->sideRef->side;
1408 /* check subdivision for shader */
1409 si = side->shaderInfo;
1413 /* ydnar: don't subdivide sky surfaces */
1414 if( si->compileFlags & C_SKY )
1417 /* do texture coordinate range check */
1418 ClassifySurfaces( 1, ds );
1419 if( CalcSurfaceTextureRange( ds ) == qfalse )
1421 /* calculate subdivisions texture range (this code is shit) */
1422 range = (ds->texRange[ 0 ] > ds->texRange[ 1 ] ? ds->texRange[ 0 ] : ds->texRange[ 1 ]);
1423 size = ds->maxs[ 0 ] - ds->mins[ 0 ];
1424 for( j = 1; j < 3; j++ )
1425 if( (ds->maxs[ j ] - ds->mins[ j ]) > size )
1426 size = ds->maxs[ j ] - ds->mins[ j ];
1427 subdivisions = (size / range) * texRange;
1428 subdivisions = ceil( subdivisions / 2 ) * 2;
1429 for( j = 1; j < 8; j++ )
1431 s2 = ceil( (float) texRange / j );
1432 if( fabs( subdivisions - s2 ) <= 4.0 )
1440 subdivisions = si->subdivisions;
1442 /* get subdivisions from shader */
1443 if( si->subdivisions > 0 && si->subdivisions < subdivisions )
1444 subdivisions = si->subdivisions;
1445 if( subdivisions < 1.0f )
1448 /* preserve fog num */
1449 fogNum = ds->fogNum;
1451 /* make a winding and free the surface */
1452 w = WindingFromDrawSurf( ds );
1456 SubdivideFace_r( e, brush, side, w, fogNum, subdivisions );
1463 ====================
1466 Adds non-opaque leaf fragments to the convex hull
1467 ====================
1470 void ClipSideIntoTree_r( winding_t *w, side_t *side, node_t *node )
1473 winding_t *front, *back;
1479 if ( node->planenum != PLANENUM_LEAF ) {
1480 if ( side->planenum == node->planenum ) {
1481 ClipSideIntoTree_r( w, side, node->children[0] );
1484 if ( side->planenum == ( node->planenum ^ 1) ) {
1485 ClipSideIntoTree_r( w, side, node->children[1] );
1489 plane = &mapplanes[ node->planenum ];
1490 ClipWindingEpsilon ( w, plane->normal, plane->dist,
1491 ON_EPSILON, &front, &back );
1494 ClipSideIntoTree_r( front, side, node->children[0] );
1495 ClipSideIntoTree_r( back, side, node->children[1] );
1500 // if opaque leaf, don't add
1501 if ( !node->opaque ) {
1502 AddWindingToConvexHull( w, &side->visibleHull, mapplanes[ side->planenum ].normal );
1513 static int g_numHiddenFaces, g_numCoinFaces;
1518 CullVectorCompare() - ydnar
1519 compares two vectors with an epsilon
1522 #define CULL_EPSILON 0.1f
1524 qboolean CullVectorCompare( const vec3_t v1, const vec3_t v2 )
1529 for( i = 0; i < 3; i++ )
1530 if( fabs( v1[ i ] - v2[ i ] ) > CULL_EPSILON )
1538 SideInBrush() - ydnar
1539 determines if a brushside lies inside another brush
1542 qboolean SideInBrush( side_t *side, brush_t *b )
1548 /* ignore sides w/o windings or shaders */
1549 if( side->winding == NULL || side->shaderInfo == NULL )
1552 /* ignore culled sides and translucent brushes */
1553 if( side->culled == qtrue || (b->compileFlags & C_TRANSLUCENT) )
1557 for( i = 0; i < b->numsides; i++ )
1559 /* fail if any sides are caulk */
1560 if( b->sides[ i ].compileFlags & C_NODRAW )
1563 /* check if side's winding is on or behind the plane */
1564 plane = &mapplanes[ b->sides[ i ].planenum ];
1565 s = WindingOnPlaneSide( side->winding, plane->normal, plane->dist );
1566 if( s == SIDE_FRONT || s == SIDE_CROSS )
1570 /* don't cull autosprite or polygonoffset surfaces */
1571 if( side->shaderInfo )
1573 if( side->shaderInfo->autosprite || side->shaderInfo->polygonOffset )
1578 side->culled = qtrue;
1586 culls obscured or buried brushsides from the map
1589 void CullSides( entity_t *e )
1592 int i, j, k, l, first, second, dir;
1595 side_t *side1, *side2;
1599 Sys_FPrintf( SYS_VRB, "--- CullSides ---\n" );
1601 g_numHiddenFaces = 0;
1604 /* brush interator 1 */
1605 for( b1 = e->brushes; b1; b1 = b1->next )
1608 if( b1->numsides < 1 )
1611 /* brush iterator 2 */
1612 for( b2 = b1->next; b2; b2 = b2->next )
1615 if( b2->numsides < 1 )
1618 /* original check */
1619 if( b1->original == b2->original && b1->original != NULL )
1624 for( i = 0; i < 3; i++ )
1625 if( b1->mins[ i ] > b2->maxs[ i ] || b1->maxs[ i ] < b2->mins[ i ] )
1630 /* cull inside sides */
1631 for( i = 0; i < b1->numsides; i++ )
1632 SideInBrush( &b1->sides[ i ], b2 );
1633 for( i = 0; i < b2->numsides; i++ )
1634 SideInBrush( &b2->sides[ i ], b1 );
1636 /* side iterator 1 */
1637 for( i = 0; i < b1->numsides; i++ )
1640 side1 = &b1->sides[ i ];
1641 w1 = side1->winding;
1644 numPoints = w1->numpoints;
1645 if( side1->shaderInfo == NULL )
1648 /* side iterator 2 */
1649 for( j = 0; j < b2->numsides; j++ )
1652 side2 = &b2->sides[ j ];
1653 w2 = side2->winding;
1656 if( side2->shaderInfo == NULL )
1658 if( w1->numpoints != w2->numpoints )
1660 if( side1->culled == qtrue && side2->culled == qtrue )
1663 /* compare planes */
1664 if( (side1->planenum & ~0x00000001) != (side2->planenum & ~0x00000001) )
1667 /* get autosprite and polygonoffset status */
1668 if( side1->shaderInfo &&
1669 (side1->shaderInfo->autosprite || side1->shaderInfo->polygonOffset) )
1671 if( side2->shaderInfo &&
1672 (side2->shaderInfo->autosprite || side2->shaderInfo->polygonOffset) )
1675 /* find first common point */
1677 for( k = 0; k < numPoints; k++ )
1679 if( VectorCompare( w1->p[ 0 ], w2->p[ k ] ) )
1688 /* find second common point (regardless of winding order) */
1691 if( (first + 1) < numPoints )
1695 if( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) )
1702 second = numPoints - 1;
1703 if( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) )
1709 /* compare the rest of the points */
1711 for( k = 0; k < numPoints; k++ )
1713 if( !CullVectorCompare( w1->p[ k ], w2->p[ l ] ) )
1719 else if( l >= numPoints )
1726 if( !side2->culled && !(side2->compileFlags & C_TRANSLUCENT) && !(side2->compileFlags & C_NODRAW) )
1728 side1->culled = qtrue;
1732 if( side1->planenum == side2->planenum && side1->culled == qtrue )
1736 if( !side1->culled && !(side1->compileFlags & C_TRANSLUCENT) && !(side1->compileFlags & C_NODRAW) )
1738 side2->culled = qtrue;
1746 /* emit some stats */
1747 Sys_FPrintf( SYS_VRB, "%9d hidden faces culled\n", g_numHiddenFaces );
1748 Sys_FPrintf( SYS_VRB, "%9d coincident faces culled\n", g_numCoinFaces );
1757 creates side->visibleHull for all visible sides
1759 the drawsurf for a side will consist of the convex hull of
1760 all points in non-opaque clusters, which allows overlaps
1761 to be trimmed off automatically.
1764 void ClipSidesIntoTree( entity_t *e, tree_t *tree )
1769 side_t *side, *newSide;
1773 /* ydnar: cull brush sides */
1777 Sys_FPrintf( SYS_VRB, "--- ClipSidesIntoTree ---\n" );
1779 /* walk the brush list */
1780 for( b = e->brushes; b; b = b->next )
1782 /* walk the brush sides */
1783 for( i = 0; i < b->numsides; i++ )
1786 side = &b->sides[ i ];
1787 if( side->winding == NULL )
1790 /* copy the winding */
1791 w = CopyWinding( side->winding );
1792 side->visibleHull = NULL;
1793 ClipSideIntoTree_r( w, side, tree->headnode );
1795 /* anything left? */
1796 w = side->visibleHull;
1801 si = side->shaderInfo;
1805 /* don't create faces for non-visible sides */
1806 /* ydnar: except indexed shaders, like common/terrain and nodraw fog surfaces */
1807 if( (si->compileFlags & C_NODRAW) && si->indexed == qfalse && !(si->compileFlags & C_FOG) )
1810 /* always use the original winding for autosprites and noclip faces */
1811 if( si->autosprite || si->noClip )
1814 /* save this winding as a visible surface */
1815 DrawSurfaceForSide( e, b, side, w );
1817 /* make a back side for fog */
1818 if( !(si->compileFlags & C_FOG) )
1821 /* duplicate the up-facing side */
1822 w = ReverseWinding( w );
1823 newSide = safe_malloc( sizeof( *side ) );
1825 newSide->visibleHull = w;
1826 newSide->planenum ^= 1;
1828 /* save this winding as a visible surface */
1829 DrawSurfaceForSide( e, b, newSide, w );
1838 this section deals with filtering drawsurfaces into the bsp tree,
1839 adding references to each leaf a surface touches
1844 AddReferenceToLeaf() - ydnar
1845 adds a reference to surface ds in the bsp leaf node
1848 int AddReferenceToLeaf( mapDrawSurface_t *ds, node_t *node )
1854 if( node->planenum != PLANENUM_LEAF || node->opaque )
1857 /* try to find an existing reference */
1858 for( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
1860 if( dsr->outputNum == numBSPDrawSurfaces )
1864 /* add a new reference */
1865 dsr = safe_malloc( sizeof( *dsr ) );
1866 dsr->outputNum = numBSPDrawSurfaces;
1867 dsr->nextRef = node->drawSurfReferences;
1868 node->drawSurfReferences = dsr;
1870 /* ydnar: sky/skybox surfaces */
1873 if( ds->shaderInfo->compileFlags & C_SKY )
1883 AddReferenceToTree_r() - ydnar
1884 adds a reference to the specified drawsurface to every leaf in the tree
1887 int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, qboolean skybox )
1896 /* is this a decision node? */
1897 if( node->planenum != PLANENUM_LEAF )
1899 /* add to child nodes and return */
1900 refs += AddReferenceToTree_r( ds, node->children[ 0 ], skybox );
1901 refs += AddReferenceToTree_r( ds, node->children[ 1 ], skybox );
1908 /* skybox surfaces only get added to sky leaves */
1912 /* increase the leaf bounds */
1913 for( i = 0; i < ds->numVerts; i++ )
1914 AddPointToBounds( ds->verts[ i ].xyz, node->mins, node->maxs );
1917 /* add a reference */
1918 return AddReferenceToLeaf( ds, node );
1924 FilterPointIntoTree_r() - ydnar
1925 filters a single point from a surface into the tree
1928 int FilterPointIntoTree_r( vec3_t point, mapDrawSurface_t *ds, node_t *node )
1935 /* is this a decision node? */
1936 if( node->planenum != PLANENUM_LEAF )
1938 /* classify the point in relation to the plane */
1939 plane = &mapplanes[ node->planenum ];
1940 d = DotProduct( point, plane->normal ) - plane->dist;
1942 /* filter by this plane */
1944 if( d >= -ON_EPSILON )
1945 refs += FilterPointIntoTree_r( point, ds, node->children[ 0 ] );
1946 if( d <= ON_EPSILON )
1947 refs += FilterPointIntoTree_r( point, ds, node->children[ 1 ] );
1953 /* add a reference */
1954 return AddReferenceToLeaf( ds, node );
1960 FilterWindingIntoTree_r() - ydnar
1961 filters a winding from a drawsurface into the tree
1964 int FilterWindingIntoTree_r( winding_t *w, mapDrawSurface_t *ds, node_t *node )
1968 vec4_t plane1, plane2, reverse;
1969 winding_t *fat, *front, *back;
1973 /* get shaderinfo */
1974 si = ds->shaderInfo;
1976 /* ydnar: is this the head node? */
1977 if( node->parent == NULL && si != NULL &&
1978 (si->mins[ 0 ] != 0.0f || si->maxs[ 0 ] != 0.0f ||
1979 si->mins[ 1 ] != 0.0f || si->maxs[ 1 ] != 0.0f ||
1980 si->mins[ 2 ] != 0.0f || si->maxs[ 2 ] != 0.0f) )
1982 /* 'fatten' the winding by the shader mins/maxs (parsed from vertexDeform move) */
1983 /* note this winding is completely invalid (concave, nonplanar, etc) */
1984 fat = AllocWinding( w->numpoints * 3 );
1985 fat->numpoints = w->numpoints * 3;
1986 for( i = 0; i < w->numpoints; i++ )
1988 VectorCopy( w->p[ i ], fat->p[ i ] );
1989 VectorAdd( w->p[ i ], si->mins, fat->p[ i * 2 ] );
1990 VectorAdd( w->p[ i ], si->maxs, fat->p[ i * 3 ] );
1997 /* is this a decision node? */
1998 if( node->planenum != PLANENUM_LEAF )
2000 /* get node plane */
2001 p1 = &mapplanes[ node->planenum ];
2002 VectorCopy( p1->normal, plane1 );
2003 plane1[ 3 ] = p1->dist;
2005 /* check if surface is planar */
2006 if( ds->planeNum >= 0 )
2008 /* get surface plane */
2009 p2 = &mapplanes[ ds->planeNum ];
2010 VectorCopy( p2->normal, plane2 );
2011 plane2[ 3 ] = p2->dist;
2014 /* invert surface plane */
2015 VectorSubtract( vec3_origin, plane2, reverse );
2016 reverse[ 3 ] = -plane2[ 3 ];
2018 /* compare planes */
2019 if( DotProduct( plane1, plane2 ) > 0.999f && fabs( plane1[ 3 ] - plane2[ 3 ] ) < 0.001f )
2020 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2021 if( DotProduct( plane1, reverse ) > 0.999f && fabs( plane1[ 3 ] - reverse[ 3 ] ) < 0.001f )
2022 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2024 /* the drawsurf might have an associated plane, if so, force a filter here */
2025 if( ds->planeNum == node->planenum )
2026 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2027 if( ds->planeNum == (node->planenum ^ 1) )
2028 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2032 /* clip the winding by this plane */
2033 ClipWindingEpsilon( w, plane1, plane1[ 3 ], ON_EPSILON, &front, &back );
2035 /* filter by this plane */
2038 refs += FilterWindingIntoTree_r( front, ds, node->children[ 0 ] );
2040 refs += FilterWindingIntoTree_r( back, ds, node->children[ 1 ] );
2047 /* add a reference */
2048 return AddReferenceToLeaf( ds, node );
2054 FilterFaceIntoTree()
2055 filters a planar winding face drawsurface into the bsp tree
2058 int FilterFaceIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2064 /* make a winding and filter it into the tree */
2065 w = WindingFromDrawSurf( ds );
2066 refs = FilterWindingIntoTree_r( w, ds, tree->headnode );
2075 FilterPatchIntoTree()
2076 subdivides a patch into an approximate curve and filters it into the tree
2079 #define FILTER_SUBDIVISION 8
2081 static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2088 /* subdivide the surface */
2089 src.width = ds->patchWidth;
2090 src.height = ds->patchHeight;
2091 src.verts = ds->verts;
2092 mesh = SubdivideMesh( src, FILTER_SUBDIVISION, 32 );
2095 /* filter each quad into the tree (fixme: use new patch x-triangulation code?) */
2097 for( y = 0; y < (mesh->height - 1); y++ )
2099 for( x = 0; x < (mesh->width - 1); x++ )
2102 w = AllocWinding( 3 );
2104 VectorCopy( mesh->verts[ y * mesh->width + x ].xyz, w->p[ 0 ] );
2105 VectorCopy( mesh->verts[ y * mesh->width + x + 1 ].xyz, w->p[ 1 ] );
2106 VectorCopy( mesh->verts[ (y + 1) * mesh->width + x ].xyz, w->p[ 2 ] );
2107 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2110 w = AllocWinding( 3 );
2112 VectorCopy( mesh->verts[ y * mesh->width + x + 1 ].xyz, w->p[ 0 ] );
2113 VectorCopy( mesh->verts[ (y + 1 ) * mesh->width + x + 1 ].xyz, w->p[ 1 ] );
2114 VectorCopy( mesh->verts[ (y + 1 ) * mesh->width + x ].xyz, w->p[ 2 ] );
2115 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2119 /* use point filtering as well */
2120 for( i = 0; i < (mesh->width * mesh->height); i++ )
2121 refs += FilterPointIntoTree_r( mesh->verts[ i ].xyz, ds, tree->headnode );
2123 /* free the subdivided mesh and return */
2131 FilterTrianglesIntoTree()
2132 filters a triangle surface (meta, model) into the bsp
2135 static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2141 /* ydnar: gs mods: this was creating bogus triangles before */
2143 for( i = 0; i < ds->numIndexes; i += 3 )
2146 if( ds->indexes[ i ] >= ds->numVerts ||
2147 ds->indexes[ i + 1 ] >= ds->numVerts ||
2148 ds->indexes[ i + 2 ] >= ds->numVerts )
2149 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2151 /* make a triangle winding and filter it into the tree */
2152 w = AllocWinding( 3 );
2154 VectorCopy( ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2155 VectorCopy( ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2156 VectorCopy( ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2157 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2160 /* use point filtering as well */
2161 for( i = 0; i < ds->numVerts; i++ )
2162 refs += FilterPointIntoTree_r( ds->verts[ i ].xyz, ds, tree->headnode );
2170 FilterFoliageIntoTree()
2171 filters a foliage surface (wolf et/splash damage)
2174 static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2177 bspDrawVert_t *instance;
2182 /* walk origin list */
2184 for( f = 0; f < ds->numFoliageInstances; f++ )
2187 instance = ds->verts + ds->patchHeight + f;
2189 /* walk triangle list */
2190 for( i = 0; i < ds->numIndexes; i += 3 )
2193 if( ds->indexes[ i ] >= ds->numVerts ||
2194 ds->indexes[ i + 1 ] >= ds->numVerts ||
2195 ds->indexes[ i + 2 ] >= ds->numVerts )
2196 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2198 /* make a triangle winding and filter it into the tree */
2199 w = AllocWinding( 3 );
2201 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2202 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2203 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2204 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2207 /* use point filtering as well */
2208 for( i = 0; i < (ds->numVerts - ds->numFoliageInstances); i++ )
2210 VectorAdd( instance->xyz, ds->verts[ i ].xyz, xyz );
2211 refs += FilterPointIntoTree_r( xyz, ds, tree->headnode );
2221 FilterFlareIntoTree()
2222 simple point filtering for flare surfaces
2224 static int FilterFlareSurfIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2226 return FilterPointIntoTree_r( ds->lightmapOrigin, ds, tree->headnode );
2232 EmitDrawVerts() - ydnar
2233 emits bsp drawverts from a map drawsurface
2236 void EmitDrawVerts( mapDrawSurface_t *ds, bspDrawSurface_t *out )
2245 si = ds->shaderInfo;
2246 offset = si->offset;
2248 /* copy the verts */
2249 out->firstVert = numBSPDrawVerts;
2250 out->numVerts = ds->numVerts;
2251 for( i = 0; i < ds->numVerts; i++ )
2253 /* allocate a new vert */
2255 dv = &bspDrawVerts[ numBSPDrawVerts - 1 ];
2258 memcpy( dv, &ds->verts[ i ], sizeof( *dv ) );
2261 if( offset != 0.0f )
2262 VectorMA( dv->xyz, offset, dv->normal, dv->xyz );
2264 /* expand model bounds
2265 necessary because of misc_model surfaces on entities
2266 note: does not happen on worldspawn as its bounds is only used for determining lightgrid bounds */
2267 if( numBSPModels > 0 )
2268 AddPointToBounds( dv->xyz, bspModels[ numBSPModels ].mins, bspModels[ numBSPModels ].maxs );
2273 for( k = 0; k < MAX_LIGHTMAPS; k++ )
2274 VectorCopy( debugColors[ (ds - mapDrawSurfs) % 12 ], dv->color[ k ] );
2282 FindDrawIndexes() - ydnar
2283 this attempts to find a run of indexes in the bsp that match the given indexes
2284 this tends to reduce the size of the bsp index pool by 1/3 or more
2285 returns numIndexes + 1 if the search failed
2288 int FindDrawIndexes( int numIndexes, int *indexes )
2290 int i, j, numTestIndexes;
2294 if( numIndexes < 3 || numBSPDrawIndexes < numIndexes || indexes == NULL )
2295 return numBSPDrawIndexes;
2298 numTestIndexes = 1 + numBSPDrawIndexes - numIndexes;
2300 /* handle 3 indexes as a special case for performance */
2301 if( numIndexes == 3 )
2303 /* run through all indexes */
2304 for( i = 0; i < numTestIndexes; i++ )
2306 /* test 3 indexes */
2307 if( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2308 indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2309 indexes[ 2 ] == bspDrawIndexes[ i + 2 ] )
2311 numRedundantIndexes += numIndexes;
2317 return numBSPDrawIndexes;
2320 /* handle 4 or more indexes */
2321 for( i = 0; i < numTestIndexes; i++ )
2323 /* test first 4 indexes */
2324 if( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2325 indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2326 indexes[ 2 ] == bspDrawIndexes[ i + 2 ] &&
2327 indexes[ 3 ] == bspDrawIndexes[ i + 3 ] )
2329 /* handle 4 indexes */
2330 if( numIndexes == 4 )
2333 /* test the remainder */
2334 for( j = 4; j < numIndexes; j++ )
2336 if( indexes[ j ] != bspDrawIndexes[ i + j ] )
2338 else if( j == (numIndexes - 1) )
2340 numRedundantIndexes += numIndexes;
2348 return numBSPDrawIndexes;
2354 EmitDrawIndexes() - ydnar
2355 attempts to find an existing run of drawindexes before adding new ones
2358 void EmitDrawIndexes( mapDrawSurface_t *ds, bspDrawSurface_t *out )
2363 /* attempt to use redundant indexing */
2364 out->firstIndex = FindDrawIndexes( ds->numIndexes, ds->indexes );
2365 out->numIndexes = ds->numIndexes;
2366 if( out->firstIndex == numBSPDrawIndexes )
2368 /* copy new unique indexes */
2369 for( i = 0; i < ds->numIndexes; i++ )
2371 if( numBSPDrawIndexes == MAX_MAP_DRAW_INDEXES )
2372 Error( "MAX_MAP_DRAW_INDEXES" );
2373 bspDrawIndexes[ numBSPDrawIndexes ] = ds->indexes[ i ];
2375 /* validate the index */
2376 if( ds->type != SURFACE_PATCH )
2378 if( bspDrawIndexes[ numBSPDrawIndexes ] < 0 || bspDrawIndexes[ numBSPDrawIndexes ] >= ds->numVerts )
2380 Sys_Printf( "WARNING: %d %s has invalid index %d (%d)\n",
2382 ds->shaderInfo->shader,
2383 bspDrawIndexes[ numBSPDrawIndexes ],
2385 bspDrawIndexes[ numBSPDrawIndexes ] = 0;
2389 /* increment index count */
2390 numBSPDrawIndexes++;
2400 emits a bsp flare drawsurface
2403 void EmitFlareSurface( mapDrawSurface_t *ds )
2406 bspDrawSurface_t *out;
2409 /* ydnar: nuking useless flare drawsurfaces */
2410 if( emitFlares == qfalse && ds->type != SURFACE_SHADER )
2414 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2415 Error( "MAX_MAP_DRAW_SURFS" );
2417 /* allocate a new surface */
2418 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2419 Error( "MAX_MAP_DRAW_SURFS" );
2420 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2421 ds->outputNum = numBSPDrawSurfaces;
2422 numBSPDrawSurfaces++;
2423 memset( out, 0, sizeof( *out ) );
2426 out->surfaceType = MST_FLARE;
2427 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2428 out->fogNum = ds->fogNum;
2431 for( i = 0; i < MAX_LIGHTMAPS; i++ )
2433 out->lightmapNum[ i ] = -3;
2434 out->lightmapStyles[ i ] = LS_NONE;
2435 out->vertexStyles[ i ] = LS_NONE;
2437 out->lightmapStyles[ 0 ] = ds->lightStyle;
2438 out->vertexStyles[ 0 ] = ds->lightStyle;
2440 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin ); /* origin */
2441 VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] ); /* color */
2442 VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2443 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] ); /* normal */
2446 numSurfacesByType[ ds->type ]++;
2451 emits a bsp patch drawsurface
2454 void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds )
2457 bspDrawSurface_t *out;
2458 int surfaceFlags, contentFlags;
2461 /* vortex: _patchMeta support */
2462 forcePatchMeta = IntForKey(e, "_patchMeta" );
2463 if (!forcePatchMeta)
2464 forcePatchMeta = IntForKey(e, "patchMeta" );
2466 /* invert the surface if necessary */
2467 if( ds->backSide || ds->shaderInfo->invert )
2469 bspDrawVert_t *dv1, *dv2, temp;
2471 /* walk the verts, flip the normal */
2472 for( i = 0; i < ds->numVerts; i++ )
2473 VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2475 /* walk the verts again, but this time reverse their order */
2476 for( j = 0; j < ds->patchHeight; j++ )
2478 for( i = 0; i < (ds->patchWidth / 2); i++ )
2480 dv1 = &ds->verts[ j * ds->patchWidth + i ];
2481 dv2 = &ds->verts[ j * ds->patchWidth + (ds->patchWidth - i - 1) ];
2482 memcpy( &temp, dv1, sizeof( bspDrawVert_t ) );
2483 memcpy( dv1, dv2, sizeof( bspDrawVert_t ) );
2484 memcpy( dv2, &temp, sizeof( bspDrawVert_t ) );
2489 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2492 /* allocate a new surface */
2493 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2494 Error( "MAX_MAP_DRAW_SURFS" );
2495 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2496 ds->outputNum = numBSPDrawSurfaces;
2497 numBSPDrawSurfaces++;
2498 memset( out, 0, sizeof( *out ) );
2501 out->surfaceType = MST_PATCH;
2503 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2504 else if( patchMeta || forcePatchMeta )
2506 /* patch meta requires that we have nodraw patches for collision */
2507 surfaceFlags = ds->shaderInfo->surfaceFlags;
2508 contentFlags = ds->shaderInfo->contentFlags;
2509 ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, NULL );
2510 ApplySurfaceParm( "pointlight", &contentFlags, &surfaceFlags, NULL );
2512 /* we don't want this patch getting lightmapped */
2513 VectorClear( ds->lightmapVecs[ 2 ] );
2514 VectorClear( ds->lightmapAxis );
2517 /* emit the new fake shader */
2518 out->shaderNum = EmitShader( ds->shaderInfo->shader, &contentFlags, &surfaceFlags );
2521 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2522 out->patchWidth = ds->patchWidth;
2523 out->patchHeight = ds->patchHeight;
2524 out->fogNum = ds->fogNum;
2527 for( i = 0; i < MAX_LIGHTMAPS; i++ )
2529 out->lightmapNum[ i ] = -3;
2530 out->lightmapStyles[ i ] = LS_NONE;
2531 out->vertexStyles[ i ] = LS_NONE;
2533 out->lightmapStyles[ 0 ] = LS_NORMAL;
2534 out->vertexStyles[ 0 ] = LS_NORMAL;
2536 /* ydnar: gs mods: previously, the lod bounds were stored in lightmapVecs[ 0 ] and [ 1 ], moved to bounds[ 0 ] and [ 1 ] */
2537 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2538 VectorCopy( ds->bounds[ 0 ], out->lightmapVecs[ 0 ] );
2539 VectorCopy( ds->bounds[ 1 ], out->lightmapVecs[ 1 ] );
2540 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2542 /* ydnar: gs mods: clear out the plane normal */
2543 if( ds->planar == qfalse )
2544 VectorClear( out->lightmapVecs[ 2 ] );
2546 /* emit the verts and indexes */
2547 EmitDrawVerts( ds, out );
2548 EmitDrawIndexes( ds, out );
2551 numSurfacesByType[ ds->type ]++;
2555 OptimizeTriangleSurface() - ydnar
2556 optimizes the vertex/index data in a triangle surface
2559 #define VERTEX_CACHE_SIZE 16
2561 static void OptimizeTriangleSurface( mapDrawSurface_t *ds )
2563 int i, j, k, temp, first, best, bestScore, score;
2564 int vertexCache[ VERTEX_CACHE_SIZE + 1 ]; /* one more for optimizing insert */
2568 /* certain surfaces don't get optimized */
2569 if( ds->numIndexes <= VERTEX_CACHE_SIZE ||
2570 ds->shaderInfo->autosprite )
2573 /* create index scratch pad */
2574 indexes = safe_malloc( ds->numIndexes * sizeof( *indexes ) );
2575 memcpy( indexes, ds->indexes, ds->numIndexes * sizeof( *indexes ) );
2578 for( i = 0; i <= VERTEX_CACHE_SIZE && i < ds->numIndexes; i++ )
2579 vertexCache[ i ] = indexes[ i ];
2581 /* add triangles in a vertex cache-aware order */
2582 for( i = 0; i < ds->numIndexes; i += 3 )
2584 /* find best triangle given the current vertex cache */
2588 for( j = 0; j < ds->numIndexes; j += 3 )
2590 /* valid triangle? */
2591 if( indexes[ j ] != -1 )
2593 /* set first if necessary */
2597 /* score the triangle */
2599 for( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2601 if( indexes[ j ] == vertexCache[ k ] || indexes[ j + 1 ] == vertexCache[ k ] || indexes[ j + 2 ] == vertexCache[ k ] )
2605 /* better triangle? */
2606 if( score > bestScore )
2612 /* a perfect score of 3 means this triangle's verts are already present in the vertex cache */
2618 /* check if no decent triangle was found, and use first available */
2622 /* valid triangle? */
2625 /* add triangle to vertex cache */
2626 for( j = 0; j < 3; j++ )
2628 for( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2630 if( indexes[ best + j ] == vertexCache[ k ] )
2634 if( k >= VERTEX_CACHE_SIZE )
2636 /* pop off top of vertex cache */
2637 for( k = VERTEX_CACHE_SIZE; k > 0; k-- )
2638 vertexCache[ k ] = vertexCache[ k - 1 ];
2641 vertexCache[ 0 ] = indexes[ best + j ];
2645 /* add triangle to surface */
2646 ds->indexes[ i ] = indexes[ best ];
2647 ds->indexes[ i + 1 ] = indexes[ best + 1 ];
2648 ds->indexes[ i + 2 ] = indexes[ best + 2 ];
2650 /* clear from input pool */
2651 indexes[ best ] = -1;
2652 indexes[ best + 1 ] = -1;
2653 indexes[ best + 2 ] = -1;
2655 /* sort triangle windings (312 -> 123) */
2656 while( ds->indexes[ i ] > ds->indexes[ i + 1 ] || ds->indexes[ i ] > ds->indexes[ i + 2 ] )
2658 temp = ds->indexes[ i ];
2659 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2660 ds->indexes[ i + 1 ] = ds->indexes[ i + 2 ];
2661 ds->indexes[ i + 2 ] = temp;
2673 EmitTriangleSurface()
2674 creates a bsp drawsurface from arbitrary triangle surfaces
2677 void EmitTriangleSurface( mapDrawSurface_t *ds )
2680 bspDrawSurface_t *out;
2682 /* invert the surface if necessary */
2683 if( ds->backSide || ds->shaderInfo->invert )
2685 /* walk the indexes, reverse the triangle order */
2686 for( i = 0; i < ds->numIndexes; i += 3 )
2688 temp = ds->indexes[ i ];
2689 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2690 ds->indexes[ i + 1 ] = temp;
2693 /* walk the verts, flip the normal */
2694 for( i = 0; i < ds->numVerts; i++ )
2695 VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2698 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2701 /* allocate a new surface */
2702 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2703 Error( "MAX_MAP_DRAW_SURFS" );
2704 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2705 ds->outputNum = numBSPDrawSurfaces;
2706 numBSPDrawSurfaces++;
2707 memset( out, 0, sizeof( *out ) );
2709 /* ydnar/sd: handle wolf et foliage surfaces */
2710 if( ds->type == SURFACE_FOLIAGE )
2711 out->surfaceType = MST_FOLIAGE;
2713 /* ydnar: gs mods: handle lightmapped terrain (force to planar type) */
2714 //% else if( VectorLength( ds->lightmapAxis ) <= 0.0f || ds->type == SURFACE_TRIANGLES || ds->type == SURFACE_FOGHULL || debugSurfaces )
2715 else if( (VectorLength( ds->lightmapAxis ) <= 0.0f && ds->planar == qfalse) ||
2716 ds->type == SURFACE_TRIANGLES ||
2717 ds->type == SURFACE_FOGHULL ||
2718 ds->numVerts > maxLMSurfaceVerts ||
2720 out->surfaceType = MST_TRIANGLE_SOUP;
2722 /* set to a planar face */
2724 out->surfaceType = MST_PLANAR;
2728 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2730 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2731 out->patchWidth = ds->patchWidth;
2732 out->patchHeight = ds->patchHeight;
2733 out->fogNum = ds->fogNum;
2735 /* debug inset (push each triangle vertex towards the center of each triangle it is on */
2738 bspDrawVert_t *a, *b, *c;
2742 /* walk triangle list */
2743 for( i = 0; i < ds->numIndexes; i += 3 )
2746 a = &ds->verts[ ds->indexes[ i ] ];
2747 b = &ds->verts[ ds->indexes[ i + 1 ] ];
2748 c = &ds->verts[ ds->indexes[ i + 2 ] ];
2750 /* calculate centroid */
2751 VectorCopy( a->xyz, cent );
2752 VectorAdd( cent, b->xyz, cent );
2753 VectorAdd( cent, c->xyz, cent );
2754 VectorScale( cent, 1.0f / 3.0f, cent );
2756 /* offset each vertex */
2757 VectorSubtract( cent, a->xyz, dir );
2758 VectorNormalize( dir, dir );
2759 VectorAdd( a->xyz, dir, a->xyz );
2760 VectorSubtract( cent, b->xyz, dir );
2761 VectorNormalize( dir, dir );
2762 VectorAdd( b->xyz, dir, b->xyz );
2763 VectorSubtract( cent, c->xyz, dir );
2764 VectorNormalize( dir, dir );
2765 VectorAdd( c->xyz, dir, c->xyz );
2770 for( i = 0; i < MAX_LIGHTMAPS; i++ )
2772 out->lightmapNum[ i ] = -3;
2773 out->lightmapStyles[ i ] = LS_NONE;
2774 out->vertexStyles[ i ] = LS_NONE;
2776 out->lightmapStyles[ 0 ] = LS_NORMAL;
2777 out->vertexStyles[ 0 ] = LS_NORMAL;
2779 /* lightmap vectors (lod bounds for patches */
2780 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2781 VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );
2782 VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2783 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2785 /* ydnar: gs mods: clear out the plane normal */
2786 if( ds->planar == qfalse )
2787 VectorClear( out->lightmapVecs[ 2 ] );
2789 /* optimize the surface's triangles */
2790 OptimizeTriangleSurface( ds );
2792 /* emit the verts and indexes */
2793 EmitDrawVerts( ds, out );
2794 EmitDrawIndexes( ds, out );
2797 numSurfacesByType[ ds->type ]++;
2804 emits a bsp planar winding (brush face) drawsurface
2807 static void EmitFaceSurface(mapDrawSurface_t *ds )
2809 /* strip/fan finding was moved elsewhere */
2810 StripFaceSurface( ds );
2811 EmitTriangleSurface(ds);
2816 MakeDebugPortalSurfs_r() - ydnar
2817 generates drawsurfaces for passable portals in the bsp
2820 static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si )
2825 mapDrawSurface_t *ds;
2829 /* recurse if decision node */
2830 if( node->planenum != PLANENUM_LEAF)
2832 MakeDebugPortalSurfs_r( node->children[ 0 ], si );
2833 MakeDebugPortalSurfs_r( node->children[ 1 ], si );
2837 /* don't bother with opaque leaves */
2841 /* walk the list of portals */
2842 for( c = 0, p = node->portals; p != NULL; c++, p = p->next[ s ] )
2844 /* get winding and side even/odd */
2846 s = (p->nodes[ 1 ] == node);
2848 /* is this a valid portal for this leaf? */
2849 if( w && p->nodes[ 0 ] == node )
2851 /* is this portal passable? */
2852 if( PortalPassable( p ) == qfalse )
2855 /* check max points */
2856 if( w->numpoints > 64 )
2857 Error( "MakePortalSurfs_r: w->numpoints = %d", w->numpoints );
2859 /* allocate a drawsurface */
2860 ds = AllocDrawSurface( SURFACE_FACE );
2861 ds->shaderInfo = si;
2863 ds->sideRef = AllocSideRef( p->side, NULL );
2864 ds->planeNum = FindFloatPlane( p->plane.normal, p->plane.dist, 0, NULL );
2865 VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] );
2867 ds->numVerts = w->numpoints;
2868 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
2869 memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
2871 /* walk the winding */
2872 for( i = 0; i < ds->numVerts; i++ )
2878 VectorCopy( w->p[ i ], dv->xyz );
2879 VectorCopy( p->plane.normal, dv->normal );
2882 for( k = 0; k < MAX_LIGHTMAPS; k++ )
2884 VectorCopy( debugColors[ c % 12 ], dv->color[ k ] );
2885 dv->color[ k ][ 3 ] = 32;
2895 MakeDebugPortalSurfs() - ydnar
2896 generates drawsurfaces for passable portals in the bsp
2899 void MakeDebugPortalSurfs( tree_t *tree )
2905 Sys_FPrintf( SYS_VRB, "--- MakeDebugPortalSurfs ---\n" );
2907 /* get portal debug shader */
2908 si = ShaderInfoForShader( "debugportals" );
2911 MakeDebugPortalSurfs_r( tree->headnode, si );
2918 generates drawsurfaces for a foghull (this MUST use a sky shader)
2921 void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader )
2924 mapDrawSurface_t *ds;
2925 vec3_t fogMins, fogMaxs;
2938 if( shader == NULL || shader[ 0 ] == '\0' )
2942 Sys_FPrintf( SYS_VRB, "--- MakeFogHullSurfs ---\n" );
2944 /* get hull bounds */
2945 VectorCopy( mapMins, fogMins );
2946 VectorCopy( mapMaxs, fogMaxs );
2947 for( i = 0; i < 3; i++ )
2949 fogMins[ i ] -= 128;
2950 fogMaxs[ i ] += 128;
2953 /* get foghull shader */
2954 si = ShaderInfoForShader( shader );
2956 /* allocate a drawsurface */
2957 ds = AllocDrawSurface( SURFACE_FOGHULL );
2958 ds->shaderInfo = si;
2961 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
2962 memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
2963 ds->numIndexes = 36;
2964 ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
2965 memset( ds->indexes, 0, ds->numIndexes * sizeof( *ds->indexes ) );
2968 VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
2969 VectorSet( ds->verts[ 1 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
2970 VectorSet( ds->verts[ 2 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
2971 VectorSet( ds->verts[ 3 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
2973 VectorSet( ds->verts[ 4 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
2974 VectorSet( ds->verts[ 5 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
2975 VectorSet( ds->verts[ 6 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
2976 VectorSet( ds->verts[ 7 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
2979 memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( *ds->indexes ) );
2985 BiasSurfaceTextures()
2986 biases a surface's texcoords as close to 0 as possible
2989 void BiasSurfaceTextures( mapDrawSurface_t *ds )
2994 /* calculate the surface texture bias */
2995 CalcSurfaceTextureRange( ds );
2997 /* don't bias globaltextured shaders */
2998 if( ds->shaderInfo->globalTexture )
3001 /* bias the texture coordinates */
3002 for( i = 0; i < ds->numVerts; i++ )
3004 ds->verts[ i ].st[ 0 ] += ds->bias[ 0 ];
3005 ds->verts[ i ].st[ 1 ] += ds->bias[ 1 ];
3012 AddSurfaceModelsToTriangle_r()
3013 adds models to a specified triangle, returns the number of models added
3016 int AddSurfaceModelsToTriangle_r( mapDrawSurface_t *ds, surfaceModel_t *model, bspDrawVert_t **tri )
3018 bspDrawVert_t mid, *tri2[ 3 ];
3019 int max, n, localNumSurfaceModels;
3023 localNumSurfaceModels = 0;
3025 /* subdivide calc */
3028 float *a, *b, dx, dy, dz, dist, maxDist;
3031 /* find the longest edge and split it */
3034 for( i = 0; i < 3; i++ )
3038 b = tri[ (i + 1) % 3 ]->xyz;
3041 dx = a[ 0 ] - b[ 0 ];
3042 dy = a[ 1 ] - b[ 1 ];
3043 dz = a[ 2 ] - b[ 2 ];
3044 dist = (dx * dx) + (dy * dy) + (dz * dz);
3047 if( dist > maxDist )
3054 /* is the triangle small enough? */
3055 if( max < 0 || maxDist <= (model->density * model->density) )
3057 float odds, r, angle;
3058 vec3_t origin, normal, scale, axis[ 3 ], angles;
3059 m4x4_t transform, temp;
3062 /* roll the dice (model's odds scaled by vertex alpha) */
3063 odds = model->odds * (tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ]) / 765.0f;
3065 if( r > model->odds )
3068 /* calculate scale */
3069 r = model->minScale + Random() * (model->maxScale - model->minScale);
3070 VectorSet( scale, r, r, r );
3072 /* calculate angle */
3073 angle = model->minAngle + Random() * (model->maxAngle - model->minAngle);
3075 /* calculate average origin */
3076 VectorCopy( tri[ 0 ]->xyz, origin );
3077 VectorAdd( origin, tri[ 1 ]->xyz, origin );
3078 VectorAdd( origin, tri[ 2 ]->xyz, origin );
3079 VectorScale( origin, (1.0f / 3.0f), origin );
3081 /* clear transform matrix */
3082 m4x4_identity( transform );
3084 /* handle oriented models */
3085 if( model->oriented )
3088 VectorSet( angles, 0.0f, 0.0f, angle );
3090 /* calculate average normal */
3091 VectorCopy( tri[ 0 ]->normal, normal );
3092 VectorAdd( normal, tri[ 1 ]->normal, normal );
3093 VectorAdd( normal, tri[ 2 ]->normal, normal );
3094 if( VectorNormalize( normal, axis[ 2 ] ) == 0.0f )
3095 VectorCopy( tri[ 0 ]->normal, axis[ 2 ] );
3097 /* make perpendicular vectors */
3098 MakeNormalVectors( axis[ 2 ], axis[ 1 ], axis[ 0 ] );
3100 /* copy to matrix */
3101 m4x4_identity( temp );
3102 temp[ 0 ] = axis[ 0 ][ 0 ]; temp[ 1 ] = axis[ 0 ][ 1 ]; temp[ 2 ] = axis[ 0 ][ 2 ];
3103 temp[ 4 ] = axis[ 1 ][ 0 ]; temp[ 5 ] = axis[ 1 ][ 1 ]; temp[ 6 ] = axis[ 1 ][ 2 ];
3104 temp[ 8 ] = axis[ 2 ][ 0 ]; temp[ 9 ] = axis[ 2 ][ 1 ]; temp[ 10 ] = axis[ 2 ][ 2 ];
3107 m4x4_scale_by_vec3( temp, scale );
3109 /* rotate around z axis */
3110 m4x4_rotate_by_vec3( temp, angles, eXYZ );
3113 m4x4_translate_by_vec3( transform, origin );
3115 /* tranform into axis space */
3116 m4x4_multiply_by_m4x4( transform, temp );
3119 /* handle z-up models */
3123 VectorSet( angles, 0.0f, 0.0f, angle );
3126 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
3129 /* insert the model */
3130 InsertModel( (char *) model->model, 0, transform, NULL, ds->celShader, ds->entityNum, ds->castShadows, ds->recvShadows, 0, ds->lightmapScale );
3132 /* return to sender */
3137 /* split the longest edge and map it */
3138 LerpDrawVert( tri[ max ], tri[ (max + 1) % 3 ], &mid );
3140 /* recurse to first triangle */
3141 VectorCopy( tri, tri2 );
3143 n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3146 localNumSurfaceModels += n;
3148 /* recurse to second triangle */
3149 VectorCopy( tri, tri2 );
3150 tri2[ (max + 1) % 3 ] = ∣
3151 n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3154 localNumSurfaceModels += n;
3157 return localNumSurfaceModels;
3164 adds a surface's shader models to the surface
3167 int AddSurfaceModels( mapDrawSurface_t *ds )
3169 surfaceModel_t *model;
3170 int i, x, y, n, pw[ 5 ], r, localNumSurfaceModels, iterations;
3171 mesh_t src, *mesh, *subdivided;
3172 bspDrawVert_t centroid, *tri[ 3 ];
3177 if( ds == NULL || ds->shaderInfo == NULL || ds->shaderInfo->surfaceModel == NULL )
3181 localNumSurfaceModels = 0;
3183 /* walk the model list */
3184 for( model = ds->shaderInfo->surfaceModel; model != NULL; model = model->next )
3186 /* switch on type */
3189 /* handle brush faces and decals */
3192 /* calculate centroid */
3193 memset( ¢roid, 0, sizeof( centroid ) );
3197 for( i = 0; i < ds->numVerts; i++ )
3199 VectorAdd( centroid.xyz, ds->verts[ i ].xyz, centroid.xyz );
3200 VectorAdd( centroid.normal, ds->verts[ i ].normal, centroid.normal );
3201 centroid.st[ 0 ] += ds->verts[ i ].st[ 0 ];
3202 centroid.st[ 1 ] += ds->verts[ i ].st[ 1 ];
3203 alpha += ds->verts[ i ].color[ 0 ][ 3 ];
3207 centroid.xyz[ 0 ] /= ds->numVerts;
3208 centroid.xyz[ 1 ] /= ds->numVerts;
3209 centroid.xyz[ 2 ] /= ds->numVerts;
3210 if( VectorNormalize( centroid.normal, centroid.normal ) == 0.0f )
3211 VectorCopy( ds->verts[ 0 ].normal, centroid.normal );
3212 centroid.st[ 0 ] /= ds->numVerts;
3213 centroid.st[ 1 ] /= ds->numVerts;
3214 alpha /= ds->numVerts;
3215 centroid.color[ 0 ][ 0 ] = 0xFF;
3216 centroid.color[ 0 ][ 1 ] = 0xFF;
3217 centroid.color[ 0 ][ 2 ] = 0xFF;
3218 centroid.color[ 0 ][ 2 ] = (alpha > 255.0f ? 0xFF : alpha);
3220 /* head vert is centroid */
3221 tri[ 0 ] = ¢roid;
3223 /* walk fanned triangles */
3224 for( i = 0; i < ds->numVerts; i++ )
3227 tri[ 1 ] = &ds->verts[ i ];
3228 tri[ 2 ] = &ds->verts[ (i + 1) % ds->numVerts ];
3231 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3234 localNumSurfaceModels += n;
3238 /* handle patches */
3240 /* subdivide the surface */
3241 src.width = ds->patchWidth;
3242 src.height = ds->patchHeight;
3243 src.verts = ds->verts;
3244 //% subdivided = SubdivideMesh( src, 8.0f, 512 );
3245 iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions );
3246 subdivided = SubdivideMesh2( src, iterations );
3248 /* fit it to the curve and remove colinear verts on rows/columns */
3249 PutMeshOnCurve( *subdivided );
3250 mesh = RemoveLinearMeshColumnsRows( subdivided );
3251 FreeMesh( subdivided );
3253 /* subdivide each quad to place the models */
3254 for( y = 0; y < (mesh->height - 1); y++ )
3256 for( x = 0; x < (mesh->width - 1); x++ )
3259 pw[ 0 ] = x + (y * mesh->width);
3260 pw[ 1 ] = x + ((y + 1) * mesh->width);
3261 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
3262 pw[ 3 ] = x + 1 + (y * mesh->width);
3263 pw[ 4 ] = x + (y * mesh->width); /* same as pw[ 0 ] */
3269 tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3270 tri[ 1 ] = &mesh->verts[ pw[ r + 1 ] ];
3271 tri[ 2 ] = &mesh->verts[ pw[ r + 2 ] ];
3272 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3275 localNumSurfaceModels += n;
3278 tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3279 tri[ 1 ] = &mesh->verts[ pw[ r + 2 ] ];
3280 tri[ 2 ] = &mesh->verts[ pw[ r + 3 ] ];
3281 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3284 localNumSurfaceModels += n;
3288 /* free the subdivided mesh */
3292 /* handle triangle surfaces */
3293 case SURFACE_TRIANGLES:
3294 case SURFACE_FORCED_META:
3296 /* walk the triangle list */
3297 for( i = 0; i < ds->numIndexes; i += 3 )
3299 tri[ 0 ] = &ds->verts[ ds->indexes[ i ] ];
3300 tri[ 1 ] = &ds->verts[ ds->indexes[ i + 1 ] ];
3301 tri[ 2 ] = &ds->verts[ ds->indexes[ i + 2 ] ];
3302 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3305 localNumSurfaceModels += n;
3309 /* no support for flares, foghull, etc */
3316 return localNumSurfaceModels;
3322 AddEntitySurfaceModels() - ydnar
3323 adds surfacemodels to an entity's surfaces
3326 void AddEntitySurfaceModels( entity_t *e )
3332 Sys_FPrintf( SYS_VRB, "--- AddEntitySurfaceModels ---\n" );
3334 /* walk the surface list */
3335 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3336 numSurfaceModels += AddSurfaceModels( &mapDrawSurfs[ i ] );
3342 VolumeColorMods() - ydnar
3343 applies brush/volumetric color/alpha modulation to vertexes
3346 static void VolumeColorMods( entity_t *e, mapDrawSurface_t *ds )
3355 if( e->colorModBrushes == NULL )
3358 /* iterate brushes */
3359 for( b = e->colorModBrushes; b != NULL; b = b->nextColorModBrush )
3361 /* worldspawn alpha brushes affect all, grouped ones only affect original entity */
3362 if( b->entityNum != 0 && b->entityNum != ds->entityNum )
3366 if( b->mins[ 0 ] > ds->maxs[ 0 ] || b->maxs[ 0 ] < ds->mins[ 0 ] ||
3367 b->mins[ 1 ] > ds->maxs[ 1 ] || b->maxs[ 1 ] < ds->mins[ 1 ] ||
3368 b->mins[ 2 ] > ds->maxs[ 2 ] || b->maxs[ 2 ] < ds->mins[ 2 ] )
3372 for( i = 0; i < ds->numVerts; i++ )
3374 /* iterate planes */
3375 for( j = 0; j < b->numsides; j++ )
3377 /* point-plane test */
3378 plane = &mapplanes[ b->sides[ j ].planenum ];
3379 d = DotProduct( ds->verts[ i ].xyz, plane->normal ) - plane->dist;
3384 /* apply colormods */
3385 if( j == b->numsides )
3386 ColorMod( b->contentShader->colorMod, 1, &ds->verts[ i ] );
3394 FilterDrawsurfsIntoTree()
3395 upon completion, all drawsurfs that actually generate a reference
3396 will have been emited to the bspfile arrays, and the references
3397 will have valid final indexes
3400 void FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree )
3403 mapDrawSurface_t *ds;
3405 vec3_t origin, mins, maxs;
3407 int numSurfs, numRefs, numSkyboxSurfaces;
3411 Sys_FPrintf( SYS_VRB, "--- FilterDrawsurfsIntoTree ---\n" );
3413 /* filter surfaces into the tree */
3416 numSkyboxSurfaces = 0;
3417 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3419 /* get surface and try to early out */
3420 ds = &mapDrawSurfs[ i ];
3421 if( ds->numVerts == 0 && ds->type != SURFACE_FLARE && ds->type != SURFACE_SHADER )
3425 si = ds->shaderInfo;
3427 /* ydnar: skybox surfaces are special */
3430 refs = AddReferenceToTree_r( ds, tree->headnode, qtrue );
3431 ds->skybox = qfalse;
3435 /* refs initially zero */
3438 /* apply texture coordinate mods */
3439 for( j = 0; j < ds->numVerts; j++ )
3440 TCMod( si->mod, ds->verts[ j ].st );
3442 /* ydnar: apply shader colormod */
3443 ColorMod( ds->shaderInfo->colorMod, ds->numVerts, ds->verts );
3445 /* ydnar: apply brush colormod */
3446 VolumeColorMods( e, ds );
3448 /* ydnar: make fur surfaces */
3449 if( si->furNumLayers > 0 )
3452 /* ydnar/sd: make foliage surfaces */
3453 if( si->foliage != NULL )
3456 /* create a flare surface if necessary */
3457 if( si->flareShader != NULL && si->flareShader[ 0 ] )
3458 AddSurfaceFlare( ds, e->origin );
3460 /* ydnar: don't emit nodraw surfaces (like nodraw fog) */
3461 if( si != NULL && (si->compileFlags & C_NODRAW) && ds->type != SURFACE_PATCH )
3464 /* ydnar: bias the surface textures */
3465 BiasSurfaceTextures( ds );
3467 /* ydnar: globalizing of fog volume handling (eek a hack) */
3468 if( e != entities && si->noFog == qfalse )
3470 /* find surface origin and offset by entity origin */
3471 VectorAdd( ds->mins, ds->maxs, origin );
3472 VectorScale( origin, 0.5f, origin );
3473 VectorAdd( origin, e->origin, origin );
3475 VectorAdd( ds->mins, e->origin, mins );
3476 VectorAdd( ds->maxs, e->origin, maxs );
3478 /* set the fog number for this surface */
3479 ds->fogNum = FogForBounds( mins, maxs, 1.0f ); //% FogForPoint( origin, 0.0f );
3483 /* ydnar: remap shader */
3484 if( ds->shaderInfo->remapShader && ds->shaderInfo->remapShader[ 0 ] )
3485 ds->shaderInfo = ShaderInfoForShader( ds->shaderInfo->remapShader );
3487 /* ydnar: gs mods: handle the various types of surfaces */
3490 /* handle brush faces */
3494 refs = FilterFaceIntoTree( ds, tree );
3496 EmitFaceSurface( ds );
3499 /* handle patches */
3502 refs = FilterPatchIntoTree( ds, tree );
3504 EmitPatchSurface( e, ds );
3507 /* handle triangle surfaces */
3508 case SURFACE_TRIANGLES:
3509 case SURFACE_FORCED_META:
3511 //% Sys_FPrintf( SYS_VRB, "Surface %4d: [%1d] %4d verts %s\n", numSurfs, ds->planar, ds->numVerts, si->shader );
3513 refs = FilterTrianglesIntoTree( ds, tree );
3515 EmitTriangleSurface( ds );
3518 /* handle foliage surfaces (splash damage/wolf et) */
3519 case SURFACE_FOLIAGE:
3520 //% Sys_FPrintf( SYS_VRB, "Surface %4d: [%d] %4d verts %s\n", numSurfs, ds->numFoliageInstances, ds->numVerts, si->shader );
3522 refs = FilterFoliageIntoTree( ds, tree );
3524 EmitTriangleSurface( ds );
3527 /* handle foghull surfaces */
3528 case SURFACE_FOGHULL:
3530 refs = AddReferenceToTree_r( ds, tree->headnode, qfalse );
3532 EmitTriangleSurface( ds );
3538 refs = FilterFlareSurfIntoTree( ds, tree );
3540 EmitFlareSurface( ds );
3543 /* handle shader-only surfaces */
3544 case SURFACE_SHADER:
3546 EmitFlareSurface( ds );
3555 /* tot up the references */
3562 /* emit extra surface data */
3563 SetSurfaceExtra( ds, numBSPDrawSurfaces - 1 );
3564 //% Sys_FPrintf( SYS_VRB, "%d verts %d indexes\n", ds->numVerts, ds->numIndexes );
3566 /* one last sanity check */
3568 bspDrawSurface_t *out;
3569 out = &bspDrawSurfaces[ numBSPDrawSurfaces - 1 ];
3570 if( out->numVerts == 3 && out->numIndexes > 3 )
3572 Sys_Printf( "\nWARNING: Potentially bad %s surface (%d: %d, %d)\n %s\n",
3573 surfaceTypes[ ds->type ],
3574 numBSPDrawSurfaces - 1, out->numVerts, out->numIndexes, si->shader );
3578 /* ydnar: handle skybox surfaces */
3581 MakeSkyboxSurface( ds );
3582 numSkyboxSurfaces++;
3587 /* emit some statistics */
3588 Sys_FPrintf( SYS_VRB, "%9d references\n", numRefs );
3589 Sys_FPrintf( SYS_VRB, "%9d (%d) emitted drawsurfs\n", numSurfs, numBSPDrawSurfaces );
3590 Sys_FPrintf( SYS_VRB, "%9d stripped face surfaces\n", numStripSurfaces );
3591 Sys_FPrintf( SYS_VRB, "%9d fanned face surfaces\n", numFanSurfaces );
3592 Sys_FPrintf( SYS_VRB, "%9d surface models generated\n", numSurfaceModels );
3593 Sys_FPrintf( SYS_VRB, "%9d skybox surfaces generated\n", numSkyboxSurfaces );
3594 for( i = 0; i < NUM_SURFACE_TYPES; i++ )
3595 Sys_FPrintf( SYS_VRB, "%9d %s surfaces\n", numSurfacesByType[ i ], surfaceTypes[ i ] );
3597 Sys_FPrintf( SYS_VRB, "%9d redundant indexes supressed, saving %d Kbytes\n", numRedundantIndexes, (numRedundantIndexes * 4 / 1024) );