2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
42 this section handles drawsurface allocation and creation
48 ydnar: gs mods: changed to force an explicit type when allocating
51 mapDrawSurface_t *AllocDrawSurface( surfaceType_t type )
56 /* ydnar: gs mods: only allocate valid types */
57 if( type <= SURFACE_BAD || type >= NUM_SURFACE_TYPES )
58 Error( "AllocDrawSurface: Invalid surface type %d specified", type );
61 if( numMapDrawSurfs >= MAX_MAP_DRAW_SURFS )
62 Error( "MAX_MAP_DRAW_SURFS (%d) exceeded", MAX_MAP_DRAW_SURFS );
63 ds = &mapDrawSurfs[ numMapDrawSurfs ];
66 /* ydnar: do initial surface setup */
67 memset( ds, 0, sizeof( mapDrawSurface_t ) );
70 ds->fogNum = defaultFogNum; /* ydnar 2003-02-12 */
71 ds->outputNum = -1; /* ydnar 2002-08-13 */
72 ds->surfaceNum = numMapDrawSurfs - 1; /* ydnar 2003-02-16 */
81 ydnar: general surface finish pass
84 void FinishSurface( mapDrawSurface_t *ds )
87 if( ds == NULL || ds->shaderInfo == NULL )
90 /* ydnar: rocking tek-fu celshading */
91 if( ds->celShader != NULL )
92 MakeCelSurface( ds, ds->celShader );
94 /* ydnar: rocking surface cloning (fur baby yeah!) */
95 if( ds->shaderInfo->cloneShader[ 0 ] != '\0' )
96 CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->cloneShader ) );
103 clones a map drawsurface, using the specified shader
106 mapDrawSurface_t *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si )
108 mapDrawSurface_t *ds;
112 if( src == NULL || si == NULL )
115 /* allocate a new surface */
116 ds = AllocDrawSurface( src->type );
121 memcpy( ds, src, sizeof( *ds ) );
123 /* destroy side reference */
130 if( ds->numVerts > 0 )
132 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
133 memcpy( ds->verts, src->verts, ds->numVerts * sizeof( *ds->verts ) );
137 if( ds->numIndexes <= 0 )
139 ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
140 memcpy( ds->indexes, src->indexes, ds->numIndexes * sizeof( *ds->indexes ) );
142 /* return the surface */
149 MakeCelSurface() - ydnar
150 makes a copy of a surface, but specific to cel shading
153 mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si )
155 mapDrawSurface_t *ds;
159 if( src == NULL || si == NULL )
162 /* don't create cel surfaces for certain types of shaders */
163 if( (src->shaderInfo->compileFlags & C_TRANSLUCENT) ||
164 (src->shaderInfo->compileFlags & C_SKY) )
168 ds = CloneSurface( src, si );
172 /* do some fixups for celshading */
176 /* return the surface */
183 MakeSkyboxSurface() - ydnar
184 generates a skybox surface, viewable from everywhere there is sky
187 mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src )
190 mapDrawSurface_t *ds;
198 ds = CloneSurface( src, src->shaderInfo );
205 /* scale the surface vertexes */
206 for( i = 0; i < ds->numVerts; i++ )
208 m4x4_transform_point( skyboxTransform, ds->verts[ i ].xyz );
211 //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 1 ] = 0;
212 //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 2 ] = 0;
215 /* so backface culling creep doesn't bork the surface */
216 VectorClear( ds->lightmapVecs[ 2 ] );
218 /* return the surface */
226 returns qtrue if all three points are colinear, backwards, or the triangle is just plain bogus
229 #define TINY_AREA 1.0f
231 qboolean IsTriangleDegenerate( bspDrawVert_t *points, int a, int b, int c )
237 /* calcuate the area of the triangle */
238 VectorSubtract( points[ b ].xyz, points[ a ].xyz, v1 );
239 VectorSubtract( points[ c ].xyz, points[ a ].xyz, v2 );
240 CrossProduct( v1, v2, v3 );
241 d = VectorLength( v3 );
243 /* assume all very small or backwards triangles will cause problems */
247 /* must be a good triangle */
254 ClearSurface() - ydnar
255 clears a surface and frees any allocated memory
258 void ClearSurface( mapDrawSurface_t *ds )
260 ds->type = SURFACE_BAD;
264 if( ds->verts != NULL )
268 if( ds->indexes != NULL )
271 numClearedSurfaces++;
277 TidyEntitySurfaces() - ydnar
278 deletes all empty or bad surfaces from the surface list
281 void TidyEntitySurfaces( entity_t *e )
284 mapDrawSurface_t *out, *in;
288 Sys_FPrintf( SYS_VRB, "--- TidyEntitySurfaces ---\n" );
290 /* walk the surface list */
292 for( i = e->firstDrawSurf, j = e->firstDrawSurf; j < numMapDrawSurfs; i++, j++ )
294 /* get out surface */
295 out = &mapDrawSurfs[ i ];
297 /* walk the surface list again until a proper surface is found */
298 for( j; j < numMapDrawSurfs; j++ )
301 in = &mapDrawSurfs[ j ];
303 /* this surface ok? */
304 if( in->type == SURFACE_FLARE || in->type == SURFACE_SHADER ||
305 (in->type != SURFACE_BAD && in->numVerts > 0) )
313 /* copy if necessary */
315 memcpy( out, in, sizeof( mapDrawSurface_t ) );
318 /* set the new number of drawsurfs */
321 /* emit some stats */
322 Sys_FPrintf( SYS_VRB, "%9d empty or malformed surfaces deleted\n", deleted );
328 CalcSurfaceTextureRange() - ydnar
329 calculates the clamped texture range for a given surface, returns qtrue if it's within [-texRange,texRange]
332 qboolean CalcSurfaceTextureRange( mapDrawSurface_t *ds )
334 int i, j, v, size[ 2 ];
335 float mins[ 2 ], maxs[ 2 ];
338 /* try to early out */
339 if( ds->numVerts <= 0 )
342 /* walk the verts and determine min/max st values */
347 for( i = 0; i < ds->numVerts; i++ )
349 for( j = 0; j < 2; j++ )
351 if( ds->verts[ i ].st[ j ] < mins[ j ] )
352 mins[ j ] = ds->verts[ i ].st[ j ];
353 if( ds->verts[ i ].st[ j ] > maxs[ j ] )
354 maxs[ j ] = ds->verts[ i ].st[ j ];
358 /* clamp to integer range and calculate surface bias values */
359 for( j = 0; j < 2; j++ )
360 ds->bias[ j ] = -floor( 0.5f * (mins[ j ] + maxs[ j ]) );
362 /* find biased texture coordinate mins/maxs */
363 size[ 0 ] = ds->shaderInfo->shaderWidth;
364 size[ 1 ] = ds->shaderInfo->shaderHeight;
365 ds->texMins[ 0 ] = 999999;
366 ds->texMins[ 1 ] = 999999;
367 ds->texMaxs[ 0 ] = -999999;
368 ds->texMaxs[ 1 ] = -999999;
369 for( i = 0; i < ds->numVerts; i++ )
371 for( j = 0; j < 2; j++ )
373 v = ((float) ds->verts[ i ].st[ j ] + ds->bias[ j ]) * size[ j ];
374 if( v < ds->texMins[ j ] )
375 ds->texMins[ j ] = v;
376 if( v > ds->texMaxs[ j ] )
377 ds->texMaxs[ j ] = v;
382 for( j = 0; j < 2; j++ )
383 ds->texRange[ j ] = (ds->texMaxs[ j ] - ds->texMins[ j ]);
385 /* if range is zero, then assume unlimited precision */
390 for( j = 0; j < 2; j++ )
392 if( ds->texMins[ j ] < -texRange || ds->texMaxs[ j ] > texRange )
403 CalcLightmapAxis() - ydnar
404 gives closed lightmap axis for a plane normal
407 qboolean CalcLightmapAxis( vec3_t normal, vec3_t axis )
413 if( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && normal[ 2 ] == 0.0f )
419 /* get absolute normal */
420 absolute[ 0 ] = fabs( normal[ 0 ] );
421 absolute[ 1 ] = fabs( normal[ 1 ] );
422 absolute[ 2 ] = fabs( normal[ 2 ] );
425 if( absolute[ 2 ] > absolute[ 0 ] - 0.0001f && absolute[ 2 ] > absolute[ 1 ] - 0.0001f )
427 if( normal[ 2 ] > 0.0f )
428 VectorSet( axis, 0.0f, 0.0f, 1.0f );
430 VectorSet( axis, 0.0f, 0.0f, -1.0f );
432 else if( absolute[ 0 ] > absolute[ 1 ] - 0.0001f && absolute[ 0 ] > absolute[ 2 ] - 0.0001f )
434 if( normal[ 0 ] > 0.0f )
435 VectorSet( axis, 1.0f, 0.0f, 0.0f );
437 VectorSet( axis, -1.0f, 0.0f, 0.0f );
441 if( normal[ 1 ] > 0.0f )
442 VectorSet( axis, 0.0f, 1.0f, 0.0f );
444 VectorSet( axis, 0.0f, -1.0f, 0.0f );
454 ClassifySurfaces() - ydnar
455 fills out a bunch of info in the surfaces, including planar status, lightmap projection, and bounding box
458 #define PLANAR_EPSILON 0.5f //% 0.126f 0.25f
460 void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds )
466 static vec3_t axii[ 6 ] =
477 /* walk the list of surfaces */
478 for( numSurfs; numSurfs > 0; numSurfs--, ds++ )
480 /* ignore bogus (or flare) surfaces */
481 if( ds->type == SURFACE_BAD || ds->numVerts <= 0 )
487 /* -----------------------------------------------------------------
488 force meta if vertex count is too high or shader requires it
489 ----------------------------------------------------------------- */
491 if( ds->type != SURFACE_PATCH && ds->type != SURFACE_FACE )
493 if( ds->numVerts > SHADER_MAX_VERTEXES )
494 ds->type = SURFACE_FORCED_META;
497 /* -----------------------------------------------------------------
498 plane and bounding box classification
499 ----------------------------------------------------------------- */
501 /* set surface bounding box */
502 ClearBounds( ds->mins, ds->maxs );
503 for( i = 0; i < ds->numVerts; i++ )
504 AddPointToBounds( ds->verts[ i ].xyz, ds->mins, ds->maxs );
506 /* try to get an existing plane */
507 if( ds->planeNum >= 0 )
509 VectorCopy( mapplanes[ ds->planeNum ].normal, plane );
510 plane[ 3 ] = mapplanes[ ds->planeNum ].dist;
513 /* construct one from the first vert with a valid normal */
516 VectorClear( plane );
518 for( i = 0; i < ds->numVerts; i++ )
520 if( ds->verts[ i ].normal[ 0 ] != 0.0f && ds->verts[ i ].normal[ 1 ] != 0.0f && ds->verts[ i ].normal[ 2 ] != 0.0f )
522 VectorCopy( ds->verts[ i ].normal, plane );
523 plane[ 3 ] = DotProduct( ds->verts[ i ].xyz, plane );
529 /* test for bogus plane */
530 if( VectorLength( plane ) <= 0.0f )
537 /* determine if surface is planar */
541 for( i = 0; i < ds->numVerts; i++ )
543 /* point-plane test */
544 dist = DotProduct( ds->verts[ i ].xyz, plane ) - plane[ 3 ];
545 if( fabs( dist ) > PLANAR_EPSILON )
547 //% if( ds->planeNum >= 0 )
549 //% Sys_Printf( "WARNING: Planar surface marked unplanar (%f > %f)\n", fabs( dist ), PLANAR_EPSILON );
550 //% ds->verts[ i ].color[ 0 ][ 0 ] = ds->verts[ i ].color[ 0 ][ 2 ] = 0;
558 /* find map plane if necessary */
561 if( ds->planeNum < 0 )
562 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &ds->verts[ 0 ].xyz );
563 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
568 VectorClear( ds->lightmapVecs[ 2 ] );
569 //% if( ds->type == SURF_META || ds->type == SURF_FACE )
570 //% Sys_Printf( "WARNING: Non-planar face (%d): %s\n", ds->planeNum, ds->shaderInfo->shader );
573 /* -----------------------------------------------------------------
574 lightmap bounds and axis projection
575 ----------------------------------------------------------------- */
577 /* vertex lit surfaces don't need this information */
578 if( si->compileFlags & C_VERTEXLIT || ds->type == SURFACE_TRIANGLES )
580 VectorClear( ds->lightmapAxis );
581 //% VectorClear( ds->lightmapVecs[ 2 ] );
586 /* the shader can specify an explicit lightmap axis */
587 if( si->lightmapAxis[ 0 ] || si->lightmapAxis[ 1 ] || si->lightmapAxis[ 2 ] )
588 VectorCopy( si->lightmapAxis, ds->lightmapAxis );
589 else if( ds->type == SURFACE_FORCED_META )
590 VectorClear( ds->lightmapAxis );
591 else if( ds->planar )
592 CalcLightmapAxis( plane, ds->lightmapAxis );
595 /* find best lightmap axis */
596 for( bestAxis = 0; bestAxis < 6; bestAxis++ )
598 for( i = 0; i < ds->numVerts && bestAxis < 6; i++ )
600 //% Sys_Printf( "Comparing %1.3f %1.3f %1.3f to %1.3f %1.3f %1.3f\n",
601 //% ds->verts[ i ].normal[ 0 ], ds->verts[ i ].normal[ 1 ], ds->verts[ i ].normal[ 2 ],
602 //% axii[ bestAxis ][ 0 ], axii[ bestAxis ][ 1 ], axii[ bestAxis ][ 2 ] );
603 if( DotProduct( ds->verts[ i ].normal, axii[ bestAxis ] ) < 0.25f ) /* fixme: adjust this tolerance to taste */
607 if( i == ds->numVerts )
611 /* set axis if possible */
614 //% if( ds->type == SURFACE_PATCH )
615 //% Sys_Printf( "Mapped axis %d onto patch\n", bestAxis );
616 VectorCopy( axii[ bestAxis ], ds->lightmapAxis );
620 //% if( ds->type == SURFACE_PATCH )
621 //% Sys_Printf( "Failed to map axis %d onto patch\n", bestAxis );
624 /* get lightmap sample size */
625 if( ds->sampleSize <= 0 )
627 ds->sampleSize = sampleSize;
628 if( ds->shaderInfo->lightmapSampleSize )
629 ds->sampleSize = ds->shaderInfo->lightmapSampleSize;
630 if( ds->lightmapScale > 0 )
631 ds->sampleSize *= ds->lightmapScale;
632 if( ds->sampleSize <= 0 )
634 else if( ds->sampleSize > 16384 ) /* powers of 2 are preferred */
635 ds->sampleSize = 16384;
643 ClassifyEntitySurfaces() - ydnar
644 classifies all surfaces in an entity
647 void ClassifyEntitySurfaces( entity_t *e )
653 Sys_FPrintf( SYS_VRB, "--- ClassifyEntitySurfaces ---\n" );
655 /* walk the surface list */
656 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
657 ClassifySurfaces( 1, &mapDrawSurfs[ i ] );
660 TidyEntitySurfaces( e );
666 GetShaderIndexForPoint() - ydnar
667 for shader-indexed surfaces (terrain), find a matching index from the indexmap
670 byte GetShaderIndexForPoint( indexMap_t *im, vec3_t eMins, vec3_t eMaxs, vec3_t point )
674 vec3_t mins, maxs, size;
677 /* early out if no indexmap */
681 /* this code is really broken */
683 /* legacy precision fudges for terrain */
684 for( i = 0; i < 3; i++ )
686 mins[ i ] = floor( eMins[ i ] + 0.1 );
687 maxs[ i ] = floor( eMaxs[ i ] + 0.1 );
688 size[ i ] = maxs[ i ] - mins[ i ];
691 /* find st (fixme: support more than just z-axis projection) */
692 s = floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ];
693 t = floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ];
708 for( i = 0; i < 3; i++ )
710 mins[ i ] = eMins[ i ];
711 maxs[ i ] = eMaxs[ i ];
712 size[ i ] = maxs[ i ] - mins[ i ];
716 s = (point[ 0 ] - mins[ 0 ]) / size[ 0 ];
717 t = (maxs[ 1 ] - point[ 1 ]) / size[ 1 ];
724 else if( x > (im->w - 1) )
728 else if( y > (im->h - 1) )
733 return im->pixels[ y * im->w + x ];
739 GetIndexedShader() - ydnar
740 for a given set of indexes and an indexmap, get a shader and set the vertex alpha in-place
741 this combines a couple different functions from terrain.c
744 shaderInfo_t *GetIndexedShader( shaderInfo_t *parent, indexMap_t *im, int numPoints, byte *shaderIndexes )
747 byte minShaderIndex, maxShaderIndex;
748 char shader[ MAX_QPATH ];
752 /* early out if bad data */
753 if( im == NULL || numPoints <= 0 || shaderIndexes == NULL )
754 return ShaderInfoForShader( "default" );
756 /* determine min/max index */
757 minShaderIndex = 255;
759 for( i = 0; i < numPoints; i++ )
761 if( shaderIndexes[ i ] < minShaderIndex )
762 minShaderIndex = shaderIndexes[ i ];
763 if( shaderIndexes[ i ] > maxShaderIndex )
764 maxShaderIndex = shaderIndexes[ i ];
767 /* set alpha inline */
768 for( i = 0; i < numPoints; i++ )
770 /* straight rip from terrain.c */
771 if( shaderIndexes[ i ] < maxShaderIndex )
772 shaderIndexes[ i ] = 0;
774 shaderIndexes[ i ] = 255;
777 /* make a shader name */
778 if( minShaderIndex == maxShaderIndex )
779 sprintf( shader, "textures/%s_%d", im->shader, maxShaderIndex );
781 sprintf( shader, "textures/%s_%dto%d", im->shader, minShaderIndex, maxShaderIndex );
784 si = ShaderInfoForShader( shader );
786 /* inherit a few things from parent shader */
787 if( parent->globalTexture )
788 si->globalTexture = qtrue;
789 if( parent->forceMeta )
790 si->forceMeta = qtrue;
791 if( parent->nonplanar )
792 si->nonplanar = qtrue;
793 if( si->shadeAngleDegrees == 0.0 )
794 si->shadeAngleDegrees = parent->shadeAngleDegrees;
795 if( parent->tcGen && si->tcGen == qfalse )
797 /* set xy texture projection */
799 VectorCopy( parent->vecs[ 0 ], si->vecs[ 0 ] );
800 VectorCopy( parent->vecs[ 1 ], si->vecs[ 1 ] );
802 if( VectorLength( parent->lightmapAxis ) > 0.0f && VectorLength( si->lightmapAxis ) <= 0.0f )
804 /* set lightmap projection axis */
805 VectorCopy( parent->lightmapAxis, si->lightmapAxis );
808 /* return the shader */
817 creates a SURF_FACE drawsurface from a given brush side and winding
820 #define SNAP_FLOAT_TO_INT 8
821 #define SNAP_INT_TO_FLOAT (1.0 / SNAP_FLOAT_TO_INT)
823 mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w )
826 mapDrawSurface_t *ds;
827 shaderInfo_t *si, *parent;
833 byte shaderIndexes[ 256 ];
834 float offsets[ 256 ];
835 char tempShader[ MAX_QPATH ];
838 /* ydnar: don't make a drawsurf for culled sides */
843 if( w->numpoints > MAX_POINTS_ON_WINDING )
844 Error( "DrawSurfaceForSide: w->numpoints = %d (> %d)", w->numpoints, MAX_POINTS_ON_WINDING );
849 /* ydnar: gs mods: check for indexed shader */
850 if( si->indexed && b->im != NULL )
855 /* get shader indexes for each point */
856 for( i = 0; i < w->numpoints; i++ )
858 shaderIndexes[ i ] = GetShaderIndexForPoint( b->im, b->eMins, b->eMaxs, w->p[ i ] );
859 offsets[ i ] = b->im->offsets[ shaderIndexes[ i ] ];
860 //% Sys_Printf( "%f ", offsets[ i ] );
863 /* get matching shader and set alpha */
865 si = GetIndexedShader( parent, b->im, w->numpoints, shaderIndexes );
870 /* ydnar: sky hack/fix for GL_CLAMP borders on ati cards */
871 if( skyFixHack && si->skyParmsImageBase[ 0 ] != '\0' )
873 //% Sys_FPrintf( SYS_VRB, "Enabling sky hack for shader %s using env %s\n", si->shader, si->skyParmsImageBase );
874 sprintf( tempShader, "%s_lf", si->skyParmsImageBase );
875 DrawSurfaceForShader( tempShader );
876 sprintf( tempShader, "%s_rt", si->skyParmsImageBase );
877 DrawSurfaceForShader( tempShader );
878 sprintf( tempShader, "%s_ft", si->skyParmsImageBase );
879 DrawSurfaceForShader( tempShader );
880 sprintf( tempShader, "%s_bk", si->skyParmsImageBase );
881 DrawSurfaceForShader( tempShader );
882 sprintf( tempShader, "%s_up", si->skyParmsImageBase );
883 DrawSurfaceForShader( tempShader );
884 sprintf( tempShader, "%s_dn", si->skyParmsImageBase );
885 DrawSurfaceForShader( tempShader );
889 ds = AllocDrawSurface( SURFACE_FACE );
890 ds->entityNum = b->entityNum;
891 ds->castShadows = b->castShadows;
892 ds->recvShadows = b->recvShadows;
895 ds->planeNum = s->planenum;
896 VectorCopy( mapplanes[ s->planenum ].normal, ds->lightmapVecs[ 2 ] );
900 ds->sideRef = AllocSideRef( s, NULL );
902 ds->lightmapScale = b->lightmapScale;
903 ds->numVerts = w->numpoints;
904 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
905 memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
907 /* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
908 ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY );
910 /* create the vertexes */
911 for( j = 0; j < w->numpoints; j++ )
913 /* get the drawvert */
916 /* copy xyz and do potential z offset */
917 VectorCopy( w->p[ j ], dv->xyz );
919 dv->xyz[ 2 ] += offsets[ j ];
921 /* round the xyz to a given precision and translate by origin */
922 for( i = 0 ; i < 3 ; i++ )
923 dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5f );
924 VectorAdd( dv->xyz, e->origin, vTranslated );
926 /* ydnar: tek-fu celshading support for flat shaded shit */
929 dv->st[ 0 ] = si->stFlat[ 0 ];
930 dv->st[ 1 ] = si->stFlat[ 1 ];
933 /* ydnar: gs mods: added support for explicit shader texcoord generation */
936 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
937 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
940 /* old quake-style texturing */
941 else if( g_bBrushPrimit == BPRIMIT_OLDBRUSHES )
943 /* nearest-axial projection */
944 dv->st[ 0 ] = s->vecs[ 0 ][ 3 ] + DotProduct( s->vecs[ 0 ], vTranslated );
945 dv->st[ 1 ] = s->vecs[ 1 ][ 3 ] + DotProduct( s->vecs[ 1 ], vTranslated );
946 dv->st[ 0 ] /= si->shaderWidth;
947 dv->st[ 1 ] /= si->shaderHeight;
950 /* brush primitive texturing */
953 /* calculate texture s/t from brush primitive texture matrix */
954 x = DotProduct( vTranslated, texX );
955 y = DotProduct( vTranslated, texY );
956 dv->st[ 0 ] = s->texMat[ 0 ][ 0 ] * x + s->texMat[ 0 ][ 1 ] * y + s->texMat[ 0 ][ 2 ];
957 dv->st[ 1 ] = s->texMat[ 1 ][ 0 ] * x + s->texMat[ 1 ][ 1 ] * y + s->texMat[ 1 ][ 2 ];
961 VectorCopy( mapplanes[ s->planenum ].normal, dv->normal );
963 /* ydnar: set color */
964 for( k = 0; k < MAX_LIGHTMAPS; k++ )
966 dv->color[ k ][ 0 ] = 255;
967 dv->color[ k ][ 1 ] = 255;
968 dv->color[ k ][ 2 ] = 255;
970 /* ydnar: gs mods: handle indexed shader blending */
971 dv->color[ k ][ 3 ] = (indexed ? shaderIndexes[ j ] : 255);
976 ds->celShader = b->celShader;
981 /* ydnar: gs mods: moved st biasing elsewhere */
989 moved here from patch.c
992 #define YDNAR_NORMAL_EPSILON 0.50f
994 qboolean VectorCompareExt( vec3_t n1, vec3_t n2, float epsilon )
1000 for( i= 0; i < 3; i++ )
1001 if( fabs( n1[ i ] - n2[ i ]) > epsilon )
1006 mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh )
1012 mapDrawSurface_t *ds;
1013 shaderInfo_t *si, *parent;
1018 byte shaderIndexes[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1019 float offsets[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1022 /* get mesh and shader shader */
1026 if( mesh == NULL || si == NULL )
1029 /* get vertex count */
1030 numVerts = mesh->width * mesh->height;
1032 /* to make valid normals for patches with degenerate edges,
1033 we need to make a copy of the mesh and put the aproximating
1034 points onto the curve */
1036 /* create a copy of the mesh */
1037 copy = CopyMesh( mesh );
1039 /* store off the original (potentially bad) normals */
1040 MakeMeshNormals( *copy );
1041 for( i = 0; i < numVerts; i++ )
1042 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1044 /* put the mesh on the curve */
1045 PutMeshOnCurve( *copy );
1047 /* find new normals (to take into account degenerate/flipped edges */
1048 MakeMeshNormals( *copy );
1049 for( i = 0; i < numVerts; i++ )
1051 /* ydnar: only copy normals that are significantly different from the originals */
1052 if( DotProduct( copy->verts[ i ].normal, mesh->verts[ i ].normal ) < 0.75f )
1053 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1056 /* free the old mesh */
1059 /* ydnar: gs mods: check for indexed shader */
1060 if( si->indexed && p->im != NULL )
1065 /* get shader indexes for each point */
1066 for( i = 0; i < numVerts; i++ )
1068 shaderIndexes[ i ] = GetShaderIndexForPoint( p->im, p->eMins, p->eMaxs, mesh->verts[ i ].xyz );
1069 offsets[ i ] = p->im->offsets[ shaderIndexes[ i ] ];
1072 /* get matching shader and set alpha */
1074 si = GetIndexedShader( parent, p->im, numVerts, shaderIndexes );
1080 /* ydnar: gs mods */
1081 ds = AllocDrawSurface( SURFACE_PATCH );
1082 ds->entityNum = p->entityNum;
1083 ds->castShadows = p->castShadows;
1084 ds->recvShadows = p->recvShadows;
1086 ds->shaderInfo = si;
1088 ds->lightmapScale = p->lightmapScale; /* ydnar */
1089 ds->patchWidth = mesh->width;
1090 ds->patchHeight = mesh->height;
1091 ds->numVerts = ds->patchWidth * ds->patchHeight;
1092 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
1093 memcpy( ds->verts, mesh->verts, ds->numVerts * sizeof( *ds->verts ) );
1098 ds->longestCurve = p->longestCurve;
1099 ds->maxIterations = p->maxIterations;
1101 /* construct a plane from the first vert */
1102 VectorCopy( mesh->verts[ 0 ].normal, plane );
1103 plane[ 3 ] = DotProduct( mesh->verts[ 0 ].xyz, plane );
1106 /* spew forth errors */
1107 if( VectorLength( plane ) < 0.001f )
1108 Sys_Printf( "BOGUS " );
1110 /* test each vert */
1111 for( i = 1; i < ds->numVerts && planar; i++ )
1114 if( VectorCompare( plane, mesh->verts[ i ].normal ) == qfalse )
1117 /* point-plane test */
1118 dist = DotProduct( mesh->verts[ i ].xyz, plane ) - plane[ 3 ];
1119 if( fabs( dist ) > EQUAL_EPSILON )
1123 /* add a map plane */
1126 /* make a map plane */
1127 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &mesh->verts[ 0 ].xyz );
1128 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
1130 /* push this normal to all verts (ydnar 2003-02-14: bad idea, small patches get screwed up) */
1131 for( i = 0; i < ds->numVerts; i++ )
1132 VectorCopy( plane, ds->verts[ i ].normal );
1135 /* walk the verts to do special stuff */
1136 for( i = 0; i < ds->numVerts; i++ )
1138 /* get the drawvert */
1139 dv = &ds->verts[ i ];
1141 /* ydnar: tek-fu celshading support for flat shaded shit */
1144 dv->st[ 0 ] = si->stFlat[ 0 ];
1145 dv->st[ 1 ] = si->stFlat[ 1 ];
1148 /* ydnar: gs mods: added support for explicit shader texcoord generation */
1149 else if( si->tcGen )
1151 /* translate by origin and project the texture */
1152 VectorAdd( dv->xyz, e->origin, vTranslated );
1153 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
1154 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
1157 /* ydnar: set color */
1158 for( k = 0; k < MAX_LIGHTMAPS; k++ )
1160 dv->color[ k ][ 0 ] = 255;
1161 dv->color[ k ][ 1 ] = 255;
1162 dv->color[ k ][ 2 ] = 255;
1164 /* ydnar: gs mods: handle indexed shader blending */
1165 dv->color[ k ][ 3 ] = (indexed ? shaderIndexes[ i ] : 255);
1170 dv->xyz[ 2 ] += offsets[ i ];
1173 /* set cel shader */
1174 ds->celShader = p->celShader;
1176 /* finish surface */
1177 FinishSurface( ds );
1179 /* return the drawsurface */
1186 DrawSurfaceForFlare() - ydnar
1187 creates a flare draw surface
1190 mapDrawSurface_t *DrawSurfaceForFlare( int entNum, vec3_t origin, vec3_t normal, vec3_t color, char *flareShader, int lightStyle )
1192 mapDrawSurface_t *ds;
1196 if( emitFlares == qfalse )
1199 /* allocate drawsurface */
1200 ds = AllocDrawSurface( SURFACE_FLARE );
1201 ds->entityNum = entNum;
1204 if( flareShader != NULL && flareShader[ 0 ] != '\0' )
1205 ds->shaderInfo = ShaderInfoForShader( flareShader );
1207 ds->shaderInfo = ShaderInfoForShader( game->flareShader );
1208 if( origin != NULL )
1209 VectorCopy( origin, ds->lightmapOrigin );
1210 if( normal != NULL )
1211 VectorCopy( normal, ds->lightmapVecs[ 2 ] );
1213 VectorCopy( color, ds->lightmapVecs[ 0 ] );
1215 /* store light style */
1216 ds->lightStyle = lightStyle;
1217 if( ds->lightStyle < 0 || ds->lightStyle >= LS_NONE )
1218 ds->lightStyle = LS_NORMAL;
1222 /* return to sender */
1229 DrawSurfaceForShader() - ydnar
1230 creates a bogus surface to forcing the game to load a shader
1233 mapDrawSurface_t *DrawSurfaceForShader( char *shader )
1237 mapDrawSurface_t *ds;
1241 si = ShaderInfoForShader( shader );
1243 /* find existing surface */
1244 for( i = 0; i < numMapDrawSurfs; i++ )
1247 ds = &mapDrawSurfs[ i ];
1250 if( ds->shaderInfo == si )
1254 /* create a new surface */
1255 ds = AllocDrawSurface( SURFACE_SHADER );
1257 ds->shaderInfo = ShaderInfoForShader( shader );
1259 /* return to sender */
1266 AddSurfaceFlare() - ydnar
1267 creates flares (coronas) centered on surfaces
1270 static void AddSurfaceFlare( mapDrawSurface_t *ds, vec3_t entityOrigin )
1277 VectorClear( origin );
1278 for ( i = 0; i < ds->numVerts; i++ )
1279 VectorAdd( origin, ds->verts[ i ].xyz, origin );
1280 VectorScale( origin, (1.0f / ds->numVerts), origin );
1281 if( entityOrigin != NULL )
1282 VectorAdd( origin, entityOrigin, origin );
1284 /* push origin off surface a bit */
1285 VectorMA( origin, 2.0f, ds->lightmapVecs[ 2 ], origin );
1287 /* create the drawsurface */
1288 DrawSurfaceForFlare( ds->entityNum, origin, ds->lightmapVecs[ 2 ], ds->shaderInfo->color, ds->shaderInfo->flareShader, ds->shaderInfo->lightStyle );
1295 subdivides a face surface until it is smaller than the specified size (subdivisions)
1298 static void SubdivideFace( entity_t *e, brush_t *brush, side_t *side, winding_t *w, int fogNum, float subdivisions )
1303 const float epsilon = 0.1;
1304 int subFloor, subCeil;
1305 winding_t *frontWinding, *backWinding;
1306 mapDrawSurface_t *ds;
1312 if( w->numpoints < 3 )
1313 Error( "SubdivideFaceSurface: Bad w->numpoints" );
1315 /* determine surface bounds */
1316 ClearBounds( bounds[ 0 ], bounds[ 1 ] );
1317 for( i = 0; i < w->numpoints; i++ )
1318 AddPointToBounds( w->p[ i ], bounds[ 0 ], bounds[ 1 ] );
1320 /* split the face */
1321 for( axis = 0; axis < 3; axis++ )
1323 vec3_t planePoint = { 0, 0, 0 };
1324 vec3_t planeNormal = { 0, 0, 0 };
1328 /* create an axial clipping plane */
1329 subFloor = floor( bounds[ 0 ][ axis ] / subdivisions) * subdivisions;
1330 subCeil = ceil( bounds[ 1 ][ axis ] / subdivisions) * subdivisions;
1331 planePoint[ axis ] = subFloor + subdivisions;
1332 planeNormal[ axis ] = -1;
1333 d = DotProduct( planePoint, planeNormal );
1335 /* subdivide if necessary */
1336 if( (subCeil - subFloor) > subdivisions )
1338 /* clip the winding */
1339 ClipWindingEpsilon( w, planeNormal, d, epsilon, &frontWinding, &backWinding );
1341 /* the clip may not produce two polygons if it was epsilon close */
1342 if( frontWinding == NULL )
1344 else if( backWinding == NULL )
1348 SubdivideFace( e, brush, side, frontWinding, fogNum, subdivisions );
1349 SubdivideFace( e, brush, side, backWinding, fogNum, subdivisions );
1355 /* create a face surface */
1356 ds = DrawSurfaceForSide( e, brush, side, w );
1358 /* set correct fog num */
1359 ds->fogNum = fogNum;
1365 SubdivideFaceSurfaces()
1366 chop up brush face surfaces that have subdivision attributes
1367 ydnar: and subdivide surfaces that exceed specified texture coordinate range
1370 void SubdivideFaceSurfaces( entity_t *e, tree_t *tree )
1372 int i, j, numBaseDrawSurfs, fogNum;
1373 mapDrawSurface_t *ds;
1378 float range, size, subdivisions, s2;
1382 Sys_FPrintf( SYS_VRB, "--- SubdivideFaceSurfaces ---\n" );
1384 /* walk the list of surfaces */
1385 numBaseDrawSurfs = numMapDrawSurfs;
1386 for( i = e->firstDrawSurf; i < numBaseDrawSurfs; i++ )
1389 ds = &mapDrawSurfs[ i ];
1391 /* only subdivide brush sides */
1392 if( ds->type != SURFACE_FACE || ds->mapBrush == NULL || ds->sideRef == NULL || ds->sideRef->side == NULL )
1396 brush = ds->mapBrush;
1397 side = ds->sideRef->side;
1399 /* check subdivision for shader */
1400 si = side->shaderInfo;
1404 /* ydnar: don't subdivide sky surfaces */
1405 if( si->compileFlags & C_SKY )
1408 /* do texture coordinate range check */
1409 ClassifySurfaces( 1, ds );
1410 if( CalcSurfaceTextureRange( ds ) == qfalse )
1412 /* calculate subdivisions texture range (this code is shit) */
1413 range = (ds->texRange[ 0 ] > ds->texRange[ 1 ] ? ds->texRange[ 0 ] : ds->texRange[ 1 ]);
1414 size = ds->maxs[ 0 ] - ds->mins[ 0 ];
1415 for( j = 1; j < 3; j++ )
1416 if( (ds->maxs[ j ] - ds->mins[ j ]) > size )
1417 size = ds->maxs[ j ] - ds->mins[ j ];
1418 subdivisions = (size / range) * texRange;
1419 subdivisions = ceil( subdivisions / 2 ) * 2;
1420 for( j = 1; j < 8; j++ )
1422 s2 = ceil( (float) texRange / j );
1423 if( fabs( subdivisions - s2 ) <= 4.0 )
1431 subdivisions = si->subdivisions;
1433 /* get subdivisions from shader */
1434 if( si->subdivisions > 0 && si->subdivisions < subdivisions )
1435 subdivisions = si->subdivisions;
1436 if( subdivisions < 1.0f )
1439 /* preserve fog num */
1440 fogNum = ds->fogNum;
1442 /* make a winding and free the surface */
1443 w = WindingFromDrawSurf( ds );
1447 SubdivideFace( e, brush, side, w, fogNum, subdivisions );
1454 ====================
1457 Adds non-opaque leaf fragments to the convex hull
1458 ====================
1461 void ClipSideIntoTree_r( winding_t *w, side_t *side, node_t *node )
1464 winding_t *front, *back;
1470 if ( node->planenum != PLANENUM_LEAF ) {
1471 if ( side->planenum == node->planenum ) {
1472 ClipSideIntoTree_r( w, side, node->children[0] );
1475 if ( side->planenum == ( node->planenum ^ 1) ) {
1476 ClipSideIntoTree_r( w, side, node->children[1] );
1480 plane = &mapplanes[ node->planenum ];
1481 ClipWindingEpsilon ( w, plane->normal, plane->dist,
1482 ON_EPSILON, &front, &back );
1485 ClipSideIntoTree_r( front, side, node->children[0] );
1486 ClipSideIntoTree_r( back, side, node->children[1] );
1491 // if opaque leaf, don't add
1492 if ( !node->opaque ) {
1493 AddWindingToConvexHull( w, &side->visibleHull, mapplanes[ side->planenum ].normal );
1504 static int g_numHiddenFaces, g_numCoinFaces;
1509 CullVectorCompare() - ydnar
1510 compares two vectors with an epsilon
1513 #define CULL_EPSILON 0.1f
1515 qboolean CullVectorCompare( const vec3_t v1, const vec3_t v2 )
1520 for( i = 0; i < 3; i++ )
1521 if( fabs( v1[ i ] - v2[ i ] ) > CULL_EPSILON )
1529 SideInBrush() - ydnar
1530 determines if a brushside lies inside another brush
1533 qboolean SideInBrush( side_t *side, brush_t *b )
1539 /* ignore sides w/o windings or shaders */
1540 if( side->winding == NULL || side->shaderInfo == NULL )
1543 /* ignore culled sides and translucent brushes */
1544 if( side->culled == qtrue || (b->compileFlags & C_TRANSLUCENT) )
1548 for( i = 0; i < b->numsides; i++ )
1550 /* fail if any sides are caulk */
1551 if( b->sides[ i ].compileFlags & C_NODRAW )
1554 /* check if side's winding is on or behind the plane */
1555 plane = &mapplanes[ b->sides[ i ].planenum ];
1556 s = WindingOnPlaneSide( side->winding, plane->normal, plane->dist );
1557 if( s == SIDE_FRONT || s == SIDE_CROSS )
1561 /* don't cull autosprite or polygonoffset surfaces */
1562 if( side->shaderInfo )
1564 if( side->shaderInfo->autosprite || side->shaderInfo->polygonOffset )
1569 side->culled = qtrue;
1577 culls obscured or buried brushsides from the map
1580 void CullSides( entity_t *e )
1583 int i, j, k, l, first, second, dir;
1586 side_t *side1, *side2;
1590 Sys_FPrintf( SYS_VRB, "--- CullSides ---\n" );
1592 g_numHiddenFaces = 0;
1595 /* brush interator 1 */
1596 for( b1 = e->brushes; b1; b1 = b1->next )
1599 if( b1->numsides < 1 )
1602 /* brush iterator 2 */
1603 for( b2 = b1->next; b2; b2 = b2->next )
1606 if( b2->numsides < 1 )
1609 /* original check */
1610 if( b1->original == b2->original && b1->original != NULL )
1615 for( i = 0; i < 3; i++ )
1616 if( b1->mins[ i ] > b2->maxs[ i ] || b1->maxs[ i ] < b2->mins[ i ] )
1621 /* cull inside sides */
1622 for( i = 0; i < b1->numsides; i++ )
1623 SideInBrush( &b1->sides[ i ], b2 );
1624 for( i = 0; i < b2->numsides; i++ )
1625 SideInBrush( &b2->sides[ i ], b1 );
1627 /* side iterator 1 */
1628 for( i = 0; i < b1->numsides; i++ )
1631 side1 = &b1->sides[ i ];
1632 w1 = side1->winding;
1635 numPoints = w1->numpoints;
1636 if( side1->shaderInfo == NULL )
1639 /* side iterator 2 */
1640 for( j = 0; j < b2->numsides; j++ )
1643 side2 = &b2->sides[ j ];
1644 w2 = side2->winding;
1647 if( side2->shaderInfo == NULL )
1649 if( w1->numpoints != w2->numpoints )
1651 if( side1->culled == qtrue && side2->culled == qtrue )
1654 /* compare planes */
1655 if( (side1->planenum & ~0x00000001) != (side2->planenum & ~0x00000001) )
1658 /* get autosprite and polygonoffset status */
1659 if( side1->shaderInfo &&
1660 (side1->shaderInfo->autosprite || side1->shaderInfo->polygonOffset) )
1662 if( side2->shaderInfo &&
1663 (side2->shaderInfo->autosprite || side2->shaderInfo->polygonOffset) )
1666 /* find first common point */
1668 for( k = 0; k < numPoints; k++ )
1670 if( VectorCompare( w1->p[ 0 ], w2->p[ k ] ) )
1679 /* find second common point (regardless of winding order) */
1682 if( (first + 1) < numPoints )
1686 if( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) )
1693 second = numPoints - 1;
1694 if( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) )
1700 /* compare the rest of the points */
1702 for( k = 0; k < numPoints; k++ )
1704 if( !CullVectorCompare( w1->p[ k ], w2->p[ l ] ) )
1710 else if( l >= numPoints )
1717 if( !side2->culled && !(side2->compileFlags & C_TRANSLUCENT) && !(side2->compileFlags & C_NODRAW) )
1719 side1->culled = qtrue;
1723 if( side1->planenum == side2->planenum && side1->culled == qtrue )
1727 if( !side1->culled && !(side1->compileFlags & C_TRANSLUCENT) && !(side1->compileFlags & C_NODRAW) )
1729 side2->culled = qtrue;
1737 /* emit some stats */
1738 Sys_FPrintf( SYS_VRB, "%9d hidden faces culled\n", g_numHiddenFaces );
1739 Sys_FPrintf( SYS_VRB, "%9d coincident faces culled\n", g_numCoinFaces );
1748 creates side->visibleHull for all visible sides
1750 the drawsurf for a side will consist of the convex hull of
1751 all points in non-opaque clusters, which allows overlaps
1752 to be trimmed off automatically.
1755 void ClipSidesIntoTree( entity_t *e, tree_t *tree )
1760 side_t *side, *newSide;
1764 /* ydnar: cull brush sides */
1768 Sys_FPrintf( SYS_VRB, "--- ClipSidesIntoTree ---\n" );
1770 /* walk the brush list */
1771 for( b = e->brushes; b; b = b->next )
1773 /* walk the brush sides */
1774 for( i = 0; i < b->numsides; i++ )
1777 side = &b->sides[ i ];
1778 if( side->winding == NULL )
1781 /* copy the winding */
1782 w = CopyWinding( side->winding );
1783 side->visibleHull = NULL;
1784 ClipSideIntoTree_r( w, side, tree->headnode );
1786 /* anything left? */
1787 w = side->visibleHull;
1792 si = side->shaderInfo;
1796 /* don't create faces for non-visible sides */
1797 /* ydnar: except indexed shaders, like common/terrain and nodraw fog surfaces */
1798 if( (si->compileFlags & C_NODRAW) && si->indexed == qfalse && !(si->compileFlags & C_FOG) )
1801 /* always use the original winding for autosprites and noclip faces */
1802 if( si->autosprite || si->noClip )
1805 /* save this winding as a visible surface */
1806 DrawSurfaceForSide( e, b, side, w );
1808 /* make a back side for fog */
1809 if( !(si->compileFlags & C_FOG) )
1812 /* duplicate the up-facing side */
1813 w = ReverseWinding( w );
1814 newSide = safe_malloc( sizeof( *side ) );
1816 newSide->visibleHull = w;
1817 newSide->planenum ^= 1;
1819 /* save this winding as a visible surface */
1820 DrawSurfaceForSide( e, b, newSide, w );
1829 this section deals with filtering drawsurfaces into the bsp tree,
1830 adding references to each leaf a surface touches
1835 AddReferenceToLeaf() - ydnar
1836 adds a reference to surface ds in the bsp leaf node
1839 int AddReferenceToLeaf( mapDrawSurface_t *ds, node_t *node )
1845 if( node->planenum != PLANENUM_LEAF || node->opaque )
1848 /* try to find an existing reference */
1849 for( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
1851 if( dsr->outputNum == numBSPDrawSurfaces )
1855 /* add a new reference */
1856 dsr = safe_malloc( sizeof( *dsr ) );
1857 dsr->outputNum = numBSPDrawSurfaces;
1858 dsr->nextRef = node->drawSurfReferences;
1859 node->drawSurfReferences = dsr;
1861 /* ydnar: sky/skybox surfaces */
1864 if( ds->shaderInfo->compileFlags & C_SKY )
1874 AddReferenceToTree_r() - ydnar
1875 adds a reference to the specified drawsurface to every leaf in the tree
1878 int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, qboolean skybox )
1887 /* is this a decision node? */
1888 if( node->planenum != PLANENUM_LEAF )
1890 /* add to child nodes and return */
1891 refs += AddReferenceToTree_r( ds, node->children[ 0 ], skybox );
1892 refs += AddReferenceToTree_r( ds, node->children[ 1 ], skybox );
1899 /* skybox surfaces only get added to sky leaves */
1903 /* increase the leaf bounds */
1904 for( i = 0; i < ds->numVerts; i++ )
1905 AddPointToBounds( ds->verts[ i ].xyz, node->mins, node->maxs );
1908 /* add a reference */
1909 return AddReferenceToLeaf( ds, node );
1915 FilterPointIntoTree_r() - ydnar
1916 filters a single point from a surface into the tree
1919 int FilterPointIntoTree_r( vec3_t point, mapDrawSurface_t *ds, node_t *node )
1926 /* is this a decision node? */
1927 if( node->planenum != PLANENUM_LEAF )
1929 /* classify the point in relation to the plane */
1930 plane = &mapplanes[ node->planenum ];
1931 d = DotProduct( point, plane->normal ) - plane->dist;
1933 /* filter by this plane */
1935 if( d >= -ON_EPSILON )
1936 refs += FilterPointIntoTree_r( point, ds, node->children[ 0 ] );
1937 if( d <= ON_EPSILON )
1938 refs += FilterPointIntoTree_r( point, ds, node->children[ 1 ] );
1944 /* add a reference */
1945 return AddReferenceToLeaf( ds, node );
1951 FilterWindingIntoTree_r() - ydnar
1952 filters a winding from a drawsurface into the tree
1955 int FilterWindingIntoTree_r( winding_t *w, mapDrawSurface_t *ds, node_t *node )
1959 vec4_t plane1, plane2, reverse;
1960 winding_t *fat, *front, *back;
1964 /* get shaderinfo */
1965 si = ds->shaderInfo;
1967 /* ydnar: is this the head node? */
1968 if( node->parent == NULL && si != NULL &&
1969 (si->mins[ 0 ] != 0.0f || si->maxs[ 0 ] != 0.0f ||
1970 si->mins[ 1 ] != 0.0f || si->maxs[ 1 ] != 0.0f ||
1971 si->mins[ 2 ] != 0.0f || si->maxs[ 2 ] != 0.0f) )
1973 /* 'fatten' the winding by the shader mins/maxs (parsed from vertexDeform move) */
1974 /* note this winding is completely invalid (concave, nonplanar, etc) */
1975 fat = AllocWinding( w->numpoints * 3 );
1976 fat->numpoints = w->numpoints * 3;
1977 for( i = 0; i < w->numpoints; i++ )
1979 VectorCopy( w->p[ i ], fat->p[ i ] );
1980 VectorAdd( w->p[ i ], si->mins, fat->p[ i * 2 ] );
1981 VectorAdd( w->p[ i ], si->maxs, fat->p[ i * 3 ] );
1988 /* is this a decision node? */
1989 if( node->planenum != PLANENUM_LEAF )
1991 /* get node plane */
1992 p1 = &mapplanes[ node->planenum ];
1993 VectorCopy( p1->normal, plane1 );
1994 plane1[ 3 ] = p1->dist;
1996 /* check if surface is planar */
1997 if( ds->planeNum >= 0 )
1999 /* get surface plane */
2000 p2 = &mapplanes[ ds->planeNum ];
2001 VectorCopy( p2->normal, plane2 );
2002 plane2[ 3 ] = p2->dist;
2005 /* invert surface plane */
2006 VectorSubtract( vec3_origin, plane2, reverse );
2007 reverse[ 3 ] = -plane2[ 3 ];
2009 /* compare planes */
2010 if( DotProduct( plane1, plane2 ) > 0.999f && fabs( plane1[ 3 ] - plane2[ 3 ] ) < 0.001f )
2011 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2012 if( DotProduct( plane1, reverse ) > 0.999f && fabs( plane1[ 3 ] - reverse[ 3 ] ) < 0.001f )
2013 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2015 /* the drawsurf might have an associated plane, if so, force a filter here */
2016 if( ds->planeNum == node->planenum )
2017 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2018 if( ds->planeNum == (node->planenum ^ 1) )
2019 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2023 /* clip the winding by this plane */
2024 ClipWindingEpsilon( w, plane1, plane1[ 3 ], ON_EPSILON, &front, &back );
2026 /* filter by this plane */
2029 refs += FilterWindingIntoTree_r( front, ds, node->children[ 0 ] );
2031 refs += FilterWindingIntoTree_r( back, ds, node->children[ 1 ] );
2038 /* add a reference */
2039 return AddReferenceToLeaf( ds, node );
2045 FilterFaceIntoTree()
2046 filters a planar winding face drawsurface into the bsp tree
2049 int FilterFaceIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2055 /* make a winding and filter it into the tree */
2056 w = WindingFromDrawSurf( ds );
2057 refs = FilterWindingIntoTree_r( w, ds, tree->headnode );
2066 FilterPatchIntoTree()
2067 subdivides a patch into an approximate curve and filters it into the tree
2070 #define FILTER_SUBDIVISION 8
2072 static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2079 /* subdivide the surface */
2080 src.width = ds->patchWidth;
2081 src.height = ds->patchHeight;
2082 src.verts = ds->verts;
2083 mesh = SubdivideMesh( src, FILTER_SUBDIVISION, 32 );
2086 /* filter each quad into the tree (fixme: use new patch x-triangulation code?) */
2088 for( y = 0; y < (mesh->height - 1); y++ )
2090 for( x = 0; x < (mesh->width - 1); x++ )
2093 w = AllocWinding( 3 );
2095 VectorCopy( mesh->verts[ y * mesh->width + x ].xyz, w->p[ 0 ] );
2096 VectorCopy( mesh->verts[ y * mesh->width + x + 1 ].xyz, w->p[ 1 ] );
2097 VectorCopy( mesh->verts[ (y + 1) * mesh->width + x ].xyz, w->p[ 2 ] );
2098 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2101 w = AllocWinding( 3 );
2103 VectorCopy( mesh->verts[ y * mesh->width + x + 1 ].xyz, w->p[ 0 ] );
2104 VectorCopy( mesh->verts[ (y + 1 ) * mesh->width + x + 1 ].xyz, w->p[ 1 ] );
2105 VectorCopy( mesh->verts[ (y + 1 ) * mesh->width + x ].xyz, w->p[ 2 ] );
2106 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2110 /* use point filtering as well */
2111 for( i = 0; i < (mesh->width * mesh->height); i++ )
2112 refs += FilterPointIntoTree_r( mesh->verts[ i ].xyz, ds, tree->headnode );
2114 /* free the subdivided mesh and return */
2122 FilterTrianglesIntoTree()
2123 filters a triangle surface (meta, model) into the bsp
2126 static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2132 /* ydnar: gs mods: this was creating bogus triangles before */
2134 for( i = 0; i < ds->numIndexes; i += 3 )
2137 if( ds->indexes[ i ] >= ds->numVerts ||
2138 ds->indexes[ i + 1 ] >= ds->numVerts ||
2139 ds->indexes[ i + 2 ] >= ds->numVerts )
2140 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2142 /* make a triangle winding and filter it into the tree */
2143 w = AllocWinding( 3 );
2145 VectorCopy( ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2146 VectorCopy( ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2147 VectorCopy( ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2148 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2151 /* use point filtering as well */
2152 for( i = 0; i < ds->numVerts; i++ )
2153 refs += FilterPointIntoTree_r( ds->verts[ i ].xyz, ds, tree->headnode );
2161 FilterFoliageIntoTree()
2162 filters a foliage surface (wolf et/splash damage)
2165 static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2168 bspDrawVert_t *instance;
2173 /* walk origin list */
2175 for( f = 0; f < ds->numFoliageInstances; f++ )
2178 instance = ds->verts + ds->patchHeight + f;
2180 /* walk triangle list */
2181 for( i = 0; i < ds->numIndexes; i += 3 )
2184 if( ds->indexes[ i ] >= ds->numVerts ||
2185 ds->indexes[ i + 1 ] >= ds->numVerts ||
2186 ds->indexes[ i + 2 ] >= ds->numVerts )
2187 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2189 /* make a triangle winding and filter it into the tree */
2190 w = AllocWinding( 3 );
2192 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2193 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2194 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2195 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2198 /* use point filtering as well */
2199 for( i = 0; i < (ds->numVerts - ds->numFoliageInstances); i++ )
2201 VectorAdd( instance->xyz, ds->verts[ i ].xyz, xyz );
2202 refs += FilterPointIntoTree_r( xyz, ds, tree->headnode );
2212 FilterFlareIntoTree()
2213 simple point filtering for flare surfaces
2215 static int FilterFlareSurfIntoTree( mapDrawSurface_t *ds, tree_t *tree )
2217 return FilterPointIntoTree_r( ds->lightmapOrigin, ds, tree->headnode );
2223 EmitDrawVerts() - ydnar
2224 emits bsp drawverts from a map drawsurface
2227 void EmitDrawVerts( mapDrawSurface_t *ds, bspDrawSurface_t *out )
2236 si = ds->shaderInfo;
2237 offset = si->offset;
2239 /* copy the verts */
2240 out->firstVert = numBSPDrawVerts;
2241 out->numVerts = ds->numVerts;
2242 for( i = 0; i < ds->numVerts; i++ )
2244 /* allocate a new vert */
2245 if( numBSPDrawVerts == MAX_MAP_DRAW_VERTS )
2246 Error( "MAX_MAP_DRAW_VERTS" );
2248 dv = &bspDrawVerts[ numBSPDrawVerts - 1 ];
2251 memcpy( dv, &ds->verts[ i ], sizeof( *dv ) );
2254 if( offset != 0.0f )
2255 VectorMA( dv->xyz, offset, dv->normal, dv->xyz );
2257 /* expand model bounds
2258 necessary because of misc_model surfaces on entities
2259 note: does not happen on worldspawn as its bounds is only used for determining lightgrid bounds */
2260 if( numBSPModels > 0 )
2261 AddPointToBounds( dv->xyz, bspModels[ numBSPModels ].mins, bspModels[ numBSPModels ].maxs );
2266 for( k = 0; k < MAX_LIGHTMAPS; k++ )
2267 VectorCopy( debugColors[ (ds - mapDrawSurfs) % 12 ], dv->color[ k ] );
2275 FindDrawIndexes() - ydnar
2276 this attempts to find a run of indexes in the bsp that match the given indexes
2277 this tends to reduce the size of the bsp index pool by 1/3 or more
2278 returns numIndexes + 1 if the search failed
2281 int FindDrawIndexes( int numIndexes, int *indexes )
2283 int i, j, numTestIndexes;
2287 if( numIndexes < 3 || numBSPDrawIndexes < numIndexes || indexes == NULL )
2288 return numBSPDrawIndexes;
2291 numTestIndexes = 1 + numBSPDrawIndexes - numIndexes;
2293 /* handle 3 indexes as a special case for performance */
2294 if( numIndexes == 3 )
2296 /* run through all indexes */
2297 for( i = 0; i < numTestIndexes; i++ )
2299 /* test 3 indexes */
2300 if( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2301 indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2302 indexes[ 2 ] == bspDrawIndexes[ i + 2 ] )
2304 numRedundantIndexes += numIndexes;
2310 return numBSPDrawIndexes;
2313 /* handle 4 or more indexes */
2314 for( i = 0; i < numTestIndexes; i++ )
2316 /* test first 4 indexes */
2317 if( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2318 indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2319 indexes[ 2 ] == bspDrawIndexes[ i + 2 ] &&
2320 indexes[ 3 ] == bspDrawIndexes[ i + 3 ] )
2322 /* handle 4 indexes */
2323 if( numIndexes == 4 )
2326 /* test the remainder */
2327 for( j = 4; j < numIndexes; j++ )
2329 if( indexes[ j ] != bspDrawIndexes[ i + j ] )
2331 else if( j == (numIndexes - 1) )
2333 numRedundantIndexes += numIndexes;
2341 return numBSPDrawIndexes;
2347 EmitDrawIndexes() - ydnar
2348 attempts to find an existing run of drawindexes before adding new ones
2351 void EmitDrawIndexes( mapDrawSurface_t *ds, bspDrawSurface_t *out )
2356 /* attempt to use redundant indexing */
2357 out->firstIndex = FindDrawIndexes( ds->numIndexes, ds->indexes );
2358 out->numIndexes = ds->numIndexes;
2359 if( out->firstIndex == numBSPDrawIndexes )
2361 /* copy new unique indexes */
2362 for( i = 0; i < ds->numIndexes; i++ )
2364 if( numBSPDrawIndexes == MAX_MAP_DRAW_INDEXES )
2365 Error( "MAX_MAP_DRAW_INDEXES" );
2366 bspDrawIndexes[ numBSPDrawIndexes ] = ds->indexes[ i ];
2368 /* validate the index */
2369 if( ds->type != SURFACE_PATCH )
2371 if( bspDrawIndexes[ numBSPDrawIndexes ] < 0 || bspDrawIndexes[ numBSPDrawIndexes ] >= ds->numVerts )
2373 Sys_Printf( "WARNING: %d %s has invalid index %d (%d)\n",
2375 ds->shaderInfo->shader,
2376 bspDrawIndexes[ numBSPDrawIndexes ],
2378 bspDrawIndexes[ numBSPDrawIndexes ] = 0;
2382 /* increment index count */
2383 numBSPDrawIndexes++;
2393 emits a bsp flare drawsurface
2396 void EmitFlareSurface( mapDrawSurface_t *ds )
2399 bspDrawSurface_t *out;
2402 /* ydnar: nuking useless flare drawsurfaces */
2403 if( emitFlares == qfalse && ds->type != SURFACE_SHADER )
2407 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2408 Error( "MAX_MAP_DRAW_SURFS" );
2410 /* allocate a new surface */
2411 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2412 Error( "MAX_MAP_DRAW_SURFS" );
2413 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2414 ds->outputNum = numBSPDrawSurfaces;
2415 numBSPDrawSurfaces++;
2416 memset( out, 0, sizeof( *out ) );
2419 out->surfaceType = MST_FLARE;
2420 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2421 out->fogNum = ds->fogNum;
2424 for( i = 0; i < MAX_LIGHTMAPS; i++ )
2426 out->lightmapNum[ i ] = -3;
2427 out->lightmapStyles[ i ] = LS_NONE;
2428 out->vertexStyles[ i ] = LS_NONE;
2430 out->lightmapStyles[ 0 ] = ds->lightStyle;
2431 out->vertexStyles[ 0 ] = ds->lightStyle;
2433 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin ); /* origin */
2434 VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] ); /* color */
2435 VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2436 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] ); /* normal */
2439 numSurfacesByType[ ds->type ]++;
2446 emits a bsp patch drawsurface
2449 void EmitPatchSurface( mapDrawSurface_t *ds )
2452 bspDrawSurface_t *out;
2453 int surfaceFlags, contentFlags;
2456 /* invert the surface if necessary */
2457 if( ds->shaderInfo->invert )
2459 bspDrawVert_t *dv1, *dv2, temp;
2462 /* walk the verts, flip the normal */
2463 for( i = 0; i < ds->numVerts; i++ )
2464 VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2466 /* walk the verts again, but this time reverse their order */
2467 for( j = 0; j < ds->patchHeight; j++ )
2469 for( i = 0; i < (ds->patchWidth / 2); i++ )
2471 dv1 = &ds->verts[ j * ds->patchWidth + i ];
2472 dv2 = &ds->verts[ j * ds->patchWidth + (ds->patchWidth - i - 1) ];
2473 memcpy( &temp, dv1, sizeof( bspDrawVert_t ) );
2474 memcpy( dv1, dv2, sizeof( bspDrawVert_t ) );
2475 memcpy( dv2, &temp, sizeof( bspDrawVert_t ) );
2480 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2483 /* allocate a new surface */
2484 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2485 Error( "MAX_MAP_DRAW_SURFS" );
2486 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2487 ds->outputNum = numBSPDrawSurfaces;
2488 numBSPDrawSurfaces++;
2489 memset( out, 0, sizeof( *out ) );
2492 out->surfaceType = MST_PATCH;
2494 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2495 else if( patchMeta )
2497 /* patch meta requires that we have nodraw patches for collision */
2498 surfaceFlags = ds->shaderInfo->surfaceFlags;
2499 contentFlags = ds->shaderInfo->contentFlags;
2500 ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, NULL );
2501 ApplySurfaceParm( "pointlight", &contentFlags, &surfaceFlags, NULL );
2503 /* we don't want this patch getting lightmapped */
2504 VectorClear( ds->lightmapVecs[ 2 ] );
2505 VectorClear( ds->lightmapAxis );
2508 /* emit the new fake shader */
2509 out->shaderNum = EmitShader( ds->shaderInfo->shader, &contentFlags, &surfaceFlags );
2512 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2513 out->patchWidth = ds->patchWidth;
2514 out->patchHeight = ds->patchHeight;
2515 out->fogNum = ds->fogNum;
2518 for( i = 0; i < MAX_LIGHTMAPS; i++ )
2520 out->lightmapNum[ i ] = -3;
2521 out->lightmapStyles[ i ] = LS_NONE;
2522 out->vertexStyles[ i ] = LS_NONE;
2524 out->lightmapStyles[ 0 ] = LS_NORMAL;
2525 out->vertexStyles[ 0 ] = LS_NORMAL;
2527 /* ydnar: gs mods: previously, the lod bounds were stored in lightmapVecs[ 0 ] and [ 1 ], moved to bounds[ 0 ] and [ 1 ] */
2528 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2529 VectorCopy( ds->bounds[ 0 ], out->lightmapVecs[ 0 ] );
2530 VectorCopy( ds->bounds[ 1 ], out->lightmapVecs[ 1 ] );
2531 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2533 /* ydnar: gs mods: clear out the plane normal */
2534 if( ds->planar == qfalse )
2535 VectorClear( out->lightmapVecs[ 2 ] );
2537 /* emit the verts and indexes */
2538 EmitDrawVerts( ds, out );
2539 EmitDrawIndexes( ds, out );
2542 numSurfacesByType[ ds->type ]++;
2548 OptimizeTriangleSurface() - ydnar
2549 optimizes the vertex/index data in a triangle surface
2552 #define VERTEX_CACHE_SIZE 16
2554 static void OptimizeTriangleSurface( mapDrawSurface_t *ds )
2556 int i, j, k, temp, first, best, bestScore, score;
2557 int vertexCache[ VERTEX_CACHE_SIZE + 1 ]; /* one more for optimizing insert */
2561 /* certain surfaces don't get optimized */
2562 if( ds->numIndexes <= VERTEX_CACHE_SIZE ||
2563 ds->shaderInfo->autosprite )
2566 /* create index scratch pad */
2567 indexes = safe_malloc( ds->numIndexes * sizeof( *indexes ) );
2568 memcpy( indexes, ds->indexes, ds->numIndexes * sizeof( *indexes ) );
2571 for( i = 0; i <= VERTEX_CACHE_SIZE && i < ds->numIndexes; i++ )
2572 vertexCache[ i ] = indexes[ i ];
2574 /* add triangles in a vertex cache-aware order */
2575 for( i = 0; i < ds->numIndexes; i += 3 )
2577 /* find best triangle given the current vertex cache */
2581 for( j = 0; j < ds->numIndexes; j += 3 )
2583 /* valid triangle? */
2584 if( indexes[ j ] != -1 )
2586 /* set first if necessary */
2590 /* score the triangle */
2592 for( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2594 if( indexes[ j ] == vertexCache[ k ] || indexes[ j + 1 ] == vertexCache[ k ] || indexes[ j + 2 ] == vertexCache[ k ] )
2598 /* better triangle? */
2599 if( score > bestScore )
2605 /* a perfect score of 3 means this triangle's verts are already present in the vertex cache */
2611 /* check if no decent triangle was found, and use first available */
2615 /* valid triangle? */
2618 /* add triangle to vertex cache */
2619 for( j = 0; j < 3; j++ )
2621 for( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2623 if( indexes[ best + j ] == vertexCache[ k ] )
2627 if( k >= VERTEX_CACHE_SIZE )
2629 /* pop off top of vertex cache */
2630 for( k = VERTEX_CACHE_SIZE; k > 0; k-- )
2631 vertexCache[ k ] = vertexCache[ k - 1 ];
2634 vertexCache[ 0 ] = indexes[ best + j ];
2638 /* add triangle to surface */
2639 ds->indexes[ i ] = indexes[ best ];
2640 ds->indexes[ i + 1 ] = indexes[ best + 1 ];
2641 ds->indexes[ i + 2 ] = indexes[ best + 2 ];
2643 /* clear from input pool */
2644 indexes[ best ] = -1;
2645 indexes[ best + 1 ] = -1;
2646 indexes[ best + 2 ] = -1;
2648 /* sort triangle windings (312 -> 123) */
2649 while( ds->indexes[ i ] > ds->indexes[ i + 1 ] || ds->indexes[ i ] > ds->indexes[ i + 2 ] )
2651 temp = ds->indexes[ i ];
2652 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2653 ds->indexes[ i + 1 ] = ds->indexes[ i + 2 ];
2654 ds->indexes[ i + 2 ] = temp;
2666 EmitTriangleSurface()
2667 creates a bsp drawsurface from arbitrary triangle surfaces
2670 static void EmitTriangleSurface( mapDrawSurface_t *ds )
2673 bspDrawSurface_t *out;
2676 /* invert the surface if necessary */
2677 if( ds->shaderInfo->invert )
2679 /* walk the indexes, reverse the triangle order */
2680 for( i = 0; i < ds->numIndexes; i += 3 )
2682 temp = ds->indexes[ i ];
2683 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2684 ds->indexes[ i + 1 ] = temp;
2687 /* walk the verts, flip the normal */
2688 for( i = 0; i < ds->numVerts; i++ )
2689 VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2692 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2695 /* allocate a new surface */
2696 if( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS )
2697 Error( "MAX_MAP_DRAW_SURFS" );
2698 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2699 ds->outputNum = numBSPDrawSurfaces;
2700 numBSPDrawSurfaces++;
2701 memset( out, 0, sizeof( *out ) );
2703 /* ydnar/sd: handle wolf et foliage surfaces */
2704 if( ds->type == SURFACE_FOLIAGE )
2705 out->surfaceType = MST_FOLIAGE;
2707 /* ydnar: gs mods: handle lightmapped terrain (force to planar type) */
2708 //% else if( VectorLength( ds->lightmapAxis ) <= 0.0f || ds->type == SURFACE_TRIANGLES || ds->type == SURFACE_FOGHULL || debugSurfaces )
2709 else if( (VectorLength( ds->lightmapAxis ) <= 0.0f && ds->planar == qfalse) || ds->type == SURFACE_TRIANGLES || ds->type == SURFACE_FOGHULL || debugSurfaces )
2710 out->surfaceType = MST_TRIANGLE_SOUP;
2712 /* set to a planar face */
2714 out->surfaceType = MST_PLANAR;
2718 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2720 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2721 out->patchWidth = ds->patchWidth;
2722 out->patchHeight = ds->patchHeight;
2723 out->fogNum = ds->fogNum;
2725 /* debug inset (push each triangle vertex towards the center of each triangle it is on */
2728 bspDrawVert_t *a, *b, *c;
2732 /* walk triangle list */
2733 for( i = 0; i < ds->numIndexes; i += 3 )
2736 a = &ds->verts[ ds->indexes[ i ] ];
2737 b = &ds->verts[ ds->indexes[ i + 1 ] ];
2738 c = &ds->verts[ ds->indexes[ i + 2 ] ];
2740 /* calculate centroid */
2741 VectorCopy( a->xyz, cent );
2742 VectorAdd( cent, b->xyz, cent );
2743 VectorAdd( cent, c->xyz, cent );
2744 VectorScale( cent, 1.0f / 3.0f, cent );
2746 /* offset each vertex */
2747 VectorSubtract( cent, a->xyz, dir );
2748 VectorNormalize( dir, dir );
2749 VectorAdd( a->xyz, dir, a->xyz );
2750 VectorSubtract( cent, b->xyz, dir );
2751 VectorNormalize( dir, dir );
2752 VectorAdd( b->xyz, dir, b->xyz );
2753 VectorSubtract( cent, c->xyz, dir );
2754 VectorNormalize( dir, dir );
2755 VectorAdd( c->xyz, dir, c->xyz );
2760 for( i = 0; i < MAX_LIGHTMAPS; i++ )
2762 out->lightmapNum[ i ] = -3;
2763 out->lightmapStyles[ i ] = LS_NONE;
2764 out->vertexStyles[ i ] = LS_NONE;
2766 out->lightmapStyles[ 0 ] = LS_NORMAL;
2767 out->vertexStyles[ 0 ] = LS_NORMAL;
2769 /* lightmap vectors (lod bounds for patches */
2770 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2771 VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );
2772 VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2773 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2775 /* ydnar: gs mods: clear out the plane normal */
2776 if( ds->planar == qfalse )
2777 VectorClear( out->lightmapVecs[ 2 ] );
2779 /* optimize the surface's triangles */
2780 OptimizeTriangleSurface( ds );
2782 /* emit the verts and indexes */
2783 EmitDrawVerts( ds, out );
2784 EmitDrawIndexes( ds, out );
2787 numSurfacesByType[ ds->type ]++;
2794 emits a bsp planar winding (brush face) drawsurface
2797 static void EmitFaceSurface( mapDrawSurface_t *ds )
2799 /* strip/fan finding was moved elsewhere */
2800 StripFaceSurface( ds );
2801 EmitTriangleSurface( ds );
2807 MakeDebugPortalSurfs_r() - ydnar
2808 generates drawsurfaces for passable portals in the bsp
2811 static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si )
2816 mapDrawSurface_t *ds;
2820 /* recurse if decision node */
2821 if( node->planenum != PLANENUM_LEAF)
2823 MakeDebugPortalSurfs_r( node->children[ 0 ], si );
2824 MakeDebugPortalSurfs_r( node->children[ 1 ], si );
2828 /* don't bother with opaque leaves */
2832 /* walk the list of portals */
2833 for( c = 0, p = node->portals; p != NULL; c++, p = p->next[ s ] )
2835 /* get winding and side even/odd */
2837 s = (p->nodes[ 1 ] == node);
2839 /* is this a valid portal for this leaf? */
2840 if( w && p->nodes[ 0 ] == node )
2842 /* is this portal passable? */
2843 if( PortalPassable( p ) == qfalse )
2846 /* check max points */
2847 if( w->numpoints > 64 )
2848 Error( "MakePortalSurfs_r: w->numpoints = %d", w->numpoints );
2850 /* allocate a drawsurface */
2851 ds = AllocDrawSurface( SURFACE_FACE );
2852 ds->shaderInfo = si;
2854 ds->sideRef = AllocSideRef( p->side, NULL );
2855 ds->planeNum = FindFloatPlane( p->plane.normal, p->plane.dist, 0, NULL );
2856 VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] );
2858 ds->numVerts = w->numpoints;
2859 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
2860 memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
2862 /* walk the winding */
2863 for( i = 0; i < ds->numVerts; i++ )
2869 VectorCopy( w->p[ i ], dv->xyz );
2870 VectorCopy( p->plane.normal, dv->normal );
2873 for( k = 0; k < MAX_LIGHTMAPS; k++ )
2875 VectorCopy( debugColors[ c % 12 ], dv->color[ k ] );
2876 dv->color[ k ][ 3 ] = 32;
2886 MakeDebugPortalSurfs() - ydnar
2887 generates drawsurfaces for passable portals in the bsp
2890 void MakeDebugPortalSurfs( tree_t *tree )
2896 Sys_FPrintf( SYS_VRB, "--- MakeDebugPortalSurfs ---\n" );
2898 /* get portal debug shader */
2899 si = ShaderInfoForShader( "debugportals" );
2902 MakeDebugPortalSurfs_r( tree->headnode, si );
2909 generates drawsurfaces for a foghull (this MUST use a sky shader)
2912 void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader )
2915 mapDrawSurface_t *ds;
2916 vec3_t fogMins, fogMaxs;
2929 if( shader == NULL || shader[ 0 ] == '\0' )
2933 Sys_FPrintf( SYS_VRB, "--- MakeFogHullSurfs ---\n" );
2935 /* get hull bounds */
2936 VectorCopy( mapMins, fogMins );
2937 VectorCopy( mapMaxs, fogMaxs );
2938 for( i = 0; i < 3; i++ )
2940 fogMins[ i ] -= 128;
2941 fogMaxs[ i ] += 128;
2944 /* get foghull shader */
2945 si = ShaderInfoForShader( shader );
2947 /* allocate a drawsurface */
2948 ds = AllocDrawSurface( SURFACE_FOGHULL );
2949 ds->shaderInfo = si;
2952 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
2953 memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
2954 ds->numIndexes = 36;
2955 ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
2956 memset( ds->indexes, 0, ds->numIndexes * sizeof( *ds->indexes ) );
2959 VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
2960 VectorSet( ds->verts[ 1 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
2961 VectorSet( ds->verts[ 2 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
2962 VectorSet( ds->verts[ 3 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
2964 VectorSet( ds->verts[ 4 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
2965 VectorSet( ds->verts[ 5 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
2966 VectorSet( ds->verts[ 6 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
2967 VectorSet( ds->verts[ 7 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
2970 memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( *ds->indexes ) );
2976 BiasSurfaceTextures()
2977 biases a surface's texcoords as close to 0 as possible
2980 void BiasSurfaceTextures( mapDrawSurface_t *ds )
2985 /* calculate the surface texture bias */
2986 CalcSurfaceTextureRange( ds );
2988 /* don't bias globaltextured shaders */
2989 if( ds->shaderInfo->globalTexture )
2992 /* bias the texture coordinates */
2993 for( i = 0; i < ds->numVerts; i++ )
2995 ds->verts[ i ].st[ 0 ] += ds->bias[ 0 ];
2996 ds->verts[ i ].st[ 1 ] += ds->bias[ 1 ];
3003 AddSurfaceModelsToTriangle_r()
3004 adds models to a specified triangle, returns the number of models added
3007 int AddSurfaceModelsToTriangle_r( mapDrawSurface_t *ds, surfaceModel_t *model, bspDrawVert_t **tri )
3009 bspDrawVert_t mid, *tri2[ 3 ];
3010 int max, n, localNumSurfaceModels;
3014 localNumSurfaceModels = 0;
3016 /* subdivide calc */
3019 float *a, *b, dx, dy, dz, dist, maxDist;
3022 /* find the longest edge and split it */
3025 for( i = 0; i < 3; i++ )
3029 b = tri[ (i + 1) % 3 ]->xyz;
3032 dx = a[ 0 ] - b[ 0 ];
3033 dy = a[ 1 ] - b[ 1 ];
3034 dz = a[ 2 ] - b[ 2 ];
3035 dist = (dx * dx) + (dy * dy) + (dz * dz);
3038 if( dist > maxDist )
3045 /* is the triangle small enough? */
3046 if( max < 0 || maxDist <= (model->density * model->density) )
3048 float odds, r, angle;
3049 vec3_t origin, normal, scale, axis[ 3 ], angles;
3050 m4x4_t transform, temp;
3053 /* roll the dice (model's odds scaled by vertex alpha) */
3054 odds = model->odds * (tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ]) / 765.0f;
3056 if( r > model->odds )
3059 /* calculate scale */
3060 r = model->minScale + Random() * (model->maxScale - model->minScale);
3061 VectorSet( scale, r, r, r );
3063 /* calculate angle */
3064 angle = model->minAngle + Random() * (model->maxAngle - model->minAngle);
3066 /* calculate average origin */
3067 VectorCopy( tri[ 0 ]->xyz, origin );
3068 VectorAdd( origin, tri[ 1 ]->xyz, origin );
3069 VectorAdd( origin, tri[ 2 ]->xyz, origin );
3070 VectorScale( origin, (1.0f / 3.0f), origin );
3072 /* clear transform matrix */
3073 m4x4_identity( transform );
3075 /* handle oriented models */
3076 if( model->oriented )
3079 VectorSet( angles, 0.0f, 0.0f, angle );
3081 /* calculate average normal */
3082 VectorCopy( tri[ 0 ]->normal, normal );
3083 VectorAdd( normal, tri[ 1 ]->normal, normal );
3084 VectorAdd( normal, tri[ 2 ]->normal, normal );
3085 if( VectorNormalize( normal, axis[ 2 ] ) == 0.0f )
3086 VectorCopy( tri[ 0 ]->normal, axis[ 2 ] );
3088 /* make perpendicular vectors */
3089 MakeNormalVectors( axis[ 2 ], axis[ 1 ], axis[ 0 ] );
3091 /* copy to matrix */
3092 m4x4_identity( temp );
3093 temp[ 0 ] = axis[ 0 ][ 0 ]; temp[ 1 ] = axis[ 0 ][ 1 ]; temp[ 2 ] = axis[ 0 ][ 2 ];
3094 temp[ 4 ] = axis[ 1 ][ 0 ]; temp[ 5 ] = axis[ 1 ][ 1 ]; temp[ 6 ] = axis[ 1 ][ 2 ];
3095 temp[ 8 ] = axis[ 2 ][ 0 ]; temp[ 9 ] = axis[ 2 ][ 1 ]; temp[ 10 ] = axis[ 2 ][ 2 ];
3098 m4x4_scale_by_vec3( temp, scale );
3100 /* rotate around z axis */
3101 m4x4_rotate_by_vec3( temp, angles, eXYZ );
3104 m4x4_translate_by_vec3( transform, origin );
3106 /* tranform into axis space */
3107 m4x4_multiply_by_m4x4( transform, temp );
3110 /* handle z-up models */
3114 VectorSet( angles, 0.0f, 0.0f, angle );
3117 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
3120 /* insert the model */
3121 InsertModel( (char *) model->model, 0, transform, NULL, ds->celShader, ds->entityNum, ds->castShadows, ds->recvShadows, 0, ds->lightmapScale );
3123 /* return to sender */
3128 /* split the longest edge and map it */
3129 LerpDrawVert( tri[ max ], tri[ (max + 1) % 3 ], &mid );
3131 /* recurse to first triangle */
3132 VectorCopy( tri, tri2 );
3134 n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3137 localNumSurfaceModels += n;
3139 /* recurse to second triangle */
3140 VectorCopy( tri, tri2 );
3141 tri2[ (max + 1) % 3 ] = ∣
3142 n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3145 localNumSurfaceModels += n;
3148 return localNumSurfaceModels;
3155 adds a surface's shader models to the surface
3158 int AddSurfaceModels( mapDrawSurface_t *ds )
3160 surfaceModel_t *model;
3161 int i, x, y, n, pw[ 5 ], r, localNumSurfaceModels, iterations;
3162 mesh_t src, *mesh, *subdivided;
3163 bspDrawVert_t centroid, *tri[ 3 ];
3168 if( ds == NULL || ds->shaderInfo == NULL || ds->shaderInfo->surfaceModel == NULL )
3172 localNumSurfaceModels = 0;
3174 /* walk the model list */
3175 for( model = ds->shaderInfo->surfaceModel; model != NULL; model = model->next )
3177 /* switch on type */
3180 /* handle brush faces and decals */
3183 /* calculate centroid */
3184 memset( ¢roid, 0, sizeof( centroid ) );
3188 for( i = 0; i < ds->numVerts; i++ )
3190 VectorAdd( centroid.xyz, ds->verts[ i ].xyz, centroid.xyz );
3191 VectorAdd( centroid.normal, ds->verts[ i ].normal, centroid.normal );
3192 centroid.st[ 0 ] += ds->verts[ i ].st[ 0 ];
3193 centroid.st[ 1 ] += ds->verts[ i ].st[ 1 ];
3194 alpha += ds->verts[ i ].color[ 0 ][ 3 ];
3198 centroid.xyz[ 0 ] /= ds->numVerts;
3199 centroid.xyz[ 1 ] /= ds->numVerts;
3200 centroid.xyz[ 2 ] /= ds->numVerts;
3201 if( VectorNormalize( centroid.normal, centroid.normal ) == 0.0f )
3202 VectorCopy( ds->verts[ 0 ].normal, centroid.normal );
3203 centroid.st[ 0 ] /= ds->numVerts;
3204 centroid.st[ 1 ] /= ds->numVerts;
3205 alpha /= ds->numVerts;
3206 centroid.color[ 0 ][ 0 ] = 0xFF;
3207 centroid.color[ 0 ][ 1 ] = 0xFF;
3208 centroid.color[ 0 ][ 2 ] = 0xFF;
3209 centroid.color[ 0 ][ 2 ] = (alpha > 255.0f ? 0xFF : alpha);
3211 /* head vert is centroid */
3212 tri[ 0 ] = ¢roid;
3214 /* walk fanned triangles */
3215 for( i = 0; i < ds->numVerts; i++ )
3218 tri[ 1 ] = &ds->verts[ i ];
3219 tri[ 2 ] = &ds->verts[ (i + 1) % ds->numVerts ];
3222 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3225 localNumSurfaceModels += n;
3229 /* handle patches */
3231 /* subdivide the surface */
3232 src.width = ds->patchWidth;
3233 src.height = ds->patchHeight;
3234 src.verts = ds->verts;
3235 //% subdivided = SubdivideMesh( src, 8.0f, 512 );
3236 iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions );
3237 subdivided = SubdivideMesh2( src, iterations );
3239 /* fit it to the curve and remove colinear verts on rows/columns */
3240 PutMeshOnCurve( *subdivided );
3241 mesh = RemoveLinearMeshColumnsRows( subdivided );
3242 FreeMesh( subdivided );
3244 /* subdivide each quad to place the models */
3245 for( y = 0; y < (mesh->height - 1); y++ )
3247 for( x = 0; x < (mesh->width - 1); x++ )
3250 pw[ 0 ] = x + (y * mesh->width);
3251 pw[ 1 ] = x + ((y + 1) * mesh->width);
3252 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
3253 pw[ 3 ] = x + 1 + (y * mesh->width);
3254 pw[ 4 ] = x + (y * mesh->width); /* same as pw[ 0 ] */
3260 tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3261 tri[ 1 ] = &mesh->verts[ pw[ r + 1 ] ];
3262 tri[ 2 ] = &mesh->verts[ pw[ r + 2 ] ];
3263 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3266 localNumSurfaceModels += n;
3269 tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3270 tri[ 1 ] = &mesh->verts[ pw[ r + 2 ] ];
3271 tri[ 2 ] = &mesh->verts[ pw[ r + 3 ] ];
3272 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3275 localNumSurfaceModels += n;
3279 /* free the subdivided mesh */
3283 /* handle triangle surfaces */
3284 case SURFACE_TRIANGLES:
3285 case SURFACE_FORCED_META:
3287 /* walk the triangle list */
3288 for( i = 0; i < ds->numIndexes; i += 3 )
3290 tri[ 0 ] = &ds->verts[ ds->indexes[ i ] ];
3291 tri[ 1 ] = &ds->verts[ ds->indexes[ i + 1 ] ];
3292 tri[ 2 ] = &ds->verts[ ds->indexes[ i + 2 ] ];
3293 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3296 localNumSurfaceModels += n;
3300 /* no support for flares, foghull, etc */
3307 return localNumSurfaceModels;
3313 AddEntitySurfaceModels() - ydnar
3314 adds surfacemodels to an entity's surfaces
3317 void AddEntitySurfaceModels( entity_t *e )
3323 Sys_FPrintf( SYS_VRB, "--- AddEntitySurfaceModels ---\n" );
3325 /* walk the surface list */
3326 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3327 numSurfaceModels += AddSurfaceModels( &mapDrawSurfs[ i ] );
3333 FilterDrawsurfsIntoTree()
3334 upon completion, all drawsurfs that actually generate a reference
3335 will have been emited to the bspfile arrays, and the references
3336 will have valid final indexes
3339 void FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree )
3342 mapDrawSurface_t *ds;
3344 vec3_t origin, mins, maxs;
3346 int numSurfs, numRefs, numSkyboxSurfaces;
3350 Sys_FPrintf( SYS_VRB, "--- FilterDrawsurfsIntoTree ---\n" );
3352 /* filter surfaces into the tree */
3355 numSkyboxSurfaces = 0;
3356 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3358 /* get surface and try to early out */
3359 ds = &mapDrawSurfs[ i ];
3360 if( ds->numVerts == 0 && ds->type != SURFACE_FLARE && ds->type != SURFACE_SHADER )
3364 si = ds->shaderInfo;
3366 /* ydnar: skybox surfaces are special */
3369 refs = AddReferenceToTree_r( ds, tree->headnode, qtrue );
3370 ds->skybox = qfalse;
3374 /* refs initially zero */
3377 /* ydnar: apply alphamod */
3378 AlphaMod( ds->shaderInfo->alphaMod, ds->numVerts, ds->verts );
3380 /* apply texture coordinate mods */
3381 for( j = 0; j < ds->numVerts; j++ )
3382 TcMod( si->mod, ds->verts[ j ].st );
3384 /* ydnar: make fur surfaces */
3385 if( si->furNumLayers > 0 )
3388 /* ydnar/sd: make foliage surfaces */
3389 if( si->foliage != NULL )
3392 /* create a flare surface if necessary */
3393 if( si->flareShader[ 0 ] )
3394 AddSurfaceFlare( ds, e->origin );
3396 /* ydnar: don't emit nodraw surfaces (like nodraw fog) */
3397 if( si != NULL && (si->compileFlags & C_NODRAW) && ds->type != SURFACE_PATCH )
3400 /* ydnar: bias the surface textures */
3401 BiasSurfaceTextures( ds );
3403 /* ydnar: globalizing of fog volume handling (eek a hack) */
3404 if( e != entities && si->noFog == qfalse )
3406 /* find surface origin and offset by entity origin */
3407 VectorAdd( ds->mins, ds->maxs, origin );
3408 VectorScale( origin, 0.5f, origin );
3409 VectorAdd( origin, e->origin, origin );
3411 VectorAdd( ds->mins, e->origin, mins );
3412 VectorAdd( ds->maxs, e->origin, maxs );
3414 /* set the fog number for this surface */
3415 ds->fogNum = FogForBounds( mins, maxs, 1.0f ); //% FogForPoint( origin, 0.0f );
3419 /* ydnar: gs mods: handle the various types of surfaces */
3422 /* handle brush faces */
3426 refs = FilterFaceIntoTree( ds, tree );
3428 EmitFaceSurface( ds );
3431 /* handle patches */
3434 refs = FilterPatchIntoTree( ds, tree );
3436 EmitPatchSurface( ds );
3439 /* handle triangle surfaces */
3440 case SURFACE_TRIANGLES:
3441 case SURFACE_FORCED_META:
3443 //% Sys_FPrintf( SYS_VRB, "Surface %4d: [%1d] %4d verts %s\n", numSurfs, ds->planar, ds->numVerts, si->shader );
3445 refs = FilterTrianglesIntoTree( ds, tree );
3447 EmitTriangleSurface( ds );
3450 /* handle foliage surfaces (splash damage/wolf et) */
3451 case SURFACE_FOLIAGE:
3452 //% Sys_FPrintf( SYS_VRB, "Surface %4d: [%d] %4d verts %s\n", numSurfs, ds->numFoliageInstances, ds->numVerts, si->shader );
3454 refs = FilterFoliageIntoTree( ds, tree );
3456 EmitTriangleSurface( ds );
3459 /* handle foghull surfaces */
3460 case SURFACE_FOGHULL:
3462 refs = AddReferenceToTree_r( ds, tree->headnode, qfalse );
3464 EmitTriangleSurface( ds );
3470 refs = FilterFlareSurfIntoTree( ds, tree );
3472 EmitFlareSurface( ds );
3475 /* handle shader-only surfaces */
3476 case SURFACE_SHADER:
3478 EmitFlareSurface( ds );
3487 /* tot up the references */
3494 /* emit extra surface data */
3495 SetSurfaceExtra( ds, numBSPDrawSurfaces - 1 );
3496 //% Sys_FPrintf( SYS_VRB, "%d verts %d indexes\n", ds->numVerts, ds->numIndexes );
3498 /* one last sanity check */
3500 bspDrawSurface_t *out;
3501 out = &bspDrawSurfaces[ numBSPDrawSurfaces - 1 ];
3502 if( out->numVerts == 3 && out->numIndexes > 3 )
3504 Sys_Printf( "\nWARNING: Potentially bad %s surface (%d: %d, %d)\n %s\n",
3505 surfaceTypes[ ds->type ],
3506 numBSPDrawSurfaces - 1, out->numVerts, out->numIndexes, si->shader );
3510 /* ydnar: handle skybox surfaces */
3513 MakeSkyboxSurface( ds );
3514 numSkyboxSurfaces++;
3519 /* emit some statistics */
3520 Sys_FPrintf( SYS_VRB, "%9d references\n", numRefs );
3521 Sys_FPrintf( SYS_VRB, "%9d (%d) emitted drawsurfs\n", numSurfs, numBSPDrawSurfaces );
3522 Sys_FPrintf( SYS_VRB, "%9d stripped face surfaces\n", numStripSurfaces );
3523 Sys_FPrintf( SYS_VRB, "%9d fanned face surfaces\n", numFanSurfaces );
3524 Sys_FPrintf( SYS_VRB, "%9d surface models generated\n", numSurfaceModels );
3525 Sys_FPrintf( SYS_VRB, "%9d skybox surfaces generated\n", numSkyboxSurfaces );
3526 for( i = 0; i < NUM_SURFACE_TYPES; i++ )
3527 Sys_FPrintf( SYS_VRB, "%9d %s surfaces\n", numSurfacesByType[ i ], surfaceTypes[ i ] );
3529 Sys_FPrintf( SYS_VRB, "%9d redundant indexes supressed, saving %d Kbytes\n", numRedundantIndexes, (numRedundantIndexes * 4 / 1024) );