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 ){
50 /* ydnar: gs mods: only allocate valid types */
51 if ( type <= SURFACE_BAD || type >= NUM_SURFACE_TYPES ) {
52 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 );
59 ds = &mapDrawSurfs[ numMapDrawSurfs ];
62 /* ydnar: do initial surface setup */
63 memset( ds, 0, sizeof( mapDrawSurface_t ) );
66 ds->fogNum = defaultFogNum; /* ydnar 2003-02-12 */
67 ds->outputNum = -1; /* ydnar 2002-08-13 */
68 ds->surfaceNum = numMapDrawSurfs - 1; /* ydnar 2003-02-16 */
77 ydnar: general surface finish pass
80 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 ) {
89 /* ydnar: rocking tek-fu celshading */
90 if ( ds->celShader != NULL ) {
91 MakeCelSurface( ds, ds->celShader );
94 /* backsides stop here */
99 /* ydnar: rocking surface cloning (fur baby yeah!) */
100 if ( ds->shaderInfo->cloneShader != NULL && ds->shaderInfo->cloneShader[ 0 ] != '\0' ) {
101 CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->cloneShader ) );
104 /* ydnar: q3map_backShader support */
105 if ( ds->shaderInfo->backShader != NULL && ds->shaderInfo->backShader[ 0 ] != '\0' ) {
106 ds2 = CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->backShader ) );
107 ds2->backSide = qtrue;
115 clones a map drawsurface, using the specified shader
118 mapDrawSurface_t *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si ){
119 mapDrawSurface_t *ds;
123 if ( src == NULL || si == NULL ) {
127 /* allocate a new surface */
128 ds = AllocDrawSurface( src->type );
134 memcpy( ds, src, sizeof( *ds ) );
136 /* destroy side reference */
143 if ( ds->numVerts > 0 ) {
144 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
145 memcpy( ds->verts, src->verts, ds->numVerts * sizeof( *ds->verts ) );
149 if ( ds->numIndexes <= 0 ) {
152 ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
153 memcpy( ds->indexes, src->indexes, ds->numIndexes * sizeof( *ds->indexes ) );
155 /* return the surface */
162 MakeCelSurface() - ydnar
163 makes a copy of a surface, but specific to cel shading
166 mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si ){
167 mapDrawSurface_t *ds;
171 if ( src == NULL || si == NULL ) {
175 /* don't create cel surfaces for certain types of shaders */
176 if ( ( src->shaderInfo->compileFlags & C_TRANSLUCENT ) ||
177 ( src->shaderInfo->compileFlags & C_SKY ) ) {
182 ds = CloneSurface( src, si );
187 /* do some fixups for celshading */
190 ds->celShader = NULL; /* don't cel shade cels :P */
192 /* return the surface */
199 MakeSkyboxSurface() - ydnar
200 generates a skybox surface, viewable from everywhere there is sky
203 mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src ){
205 mapDrawSurface_t *ds;
214 ds = CloneSurface( src, src->shaderInfo );
222 /* scale the surface vertexes */
223 for ( i = 0; i < ds->numVerts; i++ )
225 m4x4_transform_point( skyboxTransform, ds->verts[ i ].xyz );
228 //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 1 ] = 0;
229 //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 2 ] = 0;
232 /* so backface culling creep doesn't bork the surface */
233 VectorClear( ds->lightmapVecs[ 2 ] );
235 /* return the surface */
243 returns qtrue if all three points are colinear, backwards, or the triangle is just plain bogus
246 #define TINY_AREA 1.0f
248 qboolean IsTriangleDegenerate( bspDrawVert_t *points, int a, int b, int c ){
253 /* calcuate the area of the triangle */
254 VectorSubtract( points[ b ].xyz, points[ a ].xyz, v1 );
255 VectorSubtract( points[ c ].xyz, points[ a ].xyz, v2 );
256 CrossProduct( v1, v2, v3 );
257 d = VectorLength( v3 );
259 /* assume all very small or backwards triangles will cause problems */
260 if ( d < TINY_AREA ) {
264 /* must be a good triangle */
271 ClearSurface() - ydnar
272 clears a surface and frees any allocated memory
275 void ClearSurface( mapDrawSurface_t *ds ){
276 ds->type = SURFACE_BAD;
280 if ( ds->verts != NULL ) {
285 if ( ds->indexes != NULL ) {
289 numClearedSurfaces++;
295 TidyEntitySurfaces() - ydnar
296 deletes all empty or bad surfaces from the surface list
299 void TidyEntitySurfaces( entity_t *e ){
301 mapDrawSurface_t *out, *in = NULL;
305 Sys_FPrintf( SYS_VRB, "--- TidyEntitySurfaces ---\n" );
307 /* walk the surface list */
309 for ( i = e->firstDrawSurf, j = e->firstDrawSurf; j < numMapDrawSurfs; i++, j++ )
311 /* get out surface */
312 out = &mapDrawSurfs[ i ];
314 /* walk the surface list again until a proper surface is found */
315 for ( ; j < numMapDrawSurfs; j++ )
318 in = &mapDrawSurfs[ j ];
320 /* this surface ok? */
321 if ( in->type == SURFACE_FLARE || in->type == SURFACE_SHADER ||
322 ( in->type != SURFACE_BAD && in->numVerts > 0 ) ) {
331 /* copy if necessary */
333 memcpy( out, in, sizeof( mapDrawSurface_t ) );
337 /* set the new number of drawsurfs */
340 /* emit some stats */
341 Sys_FPrintf( SYS_VRB, "%9d empty or malformed surfaces deleted\n", deleted );
347 CalcSurfaceTextureRange() - ydnar
348 calculates the clamped texture range for a given surface, returns qtrue if it's within [-texRange,texRange]
351 qboolean CalcSurfaceTextureRange( mapDrawSurface_t *ds ){
352 int i, j, v, size[ 2 ];
353 float mins[ 2 ], maxs[ 2 ];
356 /* try to early out */
357 if ( ds->numVerts <= 0 ) {
361 /* walk the verts and determine min/max st values */
366 for ( i = 0; i < ds->numVerts; i++ )
368 for ( j = 0; j < 2; j++ )
370 if ( ds->verts[ i ].st[ j ] < mins[ j ] ) {
371 mins[ j ] = ds->verts[ i ].st[ j ];
373 if ( ds->verts[ i ].st[ j ] > maxs[ j ] ) {
374 maxs[ j ] = ds->verts[ i ].st[ j ];
379 /* clamp to integer range and calculate surface bias values */
380 for ( j = 0; j < 2; j++ )
381 ds->bias[ j ] = -floor( 0.5f * ( mins[ j ] + maxs[ j ] ) );
383 /* find biased texture coordinate mins/maxs */
384 size[ 0 ] = ds->shaderInfo->shaderWidth;
385 size[ 1 ] = ds->shaderInfo->shaderHeight;
386 ds->texMins[ 0 ] = 999999;
387 ds->texMins[ 1 ] = 999999;
388 ds->texMaxs[ 0 ] = -999999;
389 ds->texMaxs[ 1 ] = -999999;
390 for ( i = 0; i < ds->numVerts; i++ )
392 for ( j = 0; j < 2; j++ )
394 v = ( (float) ds->verts[ i ].st[ j ] + ds->bias[ j ] ) * size[ j ];
395 if ( v < ds->texMins[ j ] ) {
396 ds->texMins[ j ] = v;
398 if ( v > ds->texMaxs[ j ] ) {
399 ds->texMaxs[ j ] = v;
405 for ( j = 0; j < 2; j++ )
406 ds->texRange[ j ] = ( ds->texMaxs[ j ] - ds->texMins[ j ] );
408 /* if range is zero, then assume unlimited precision */
409 if ( texRange == 0 ) {
414 for ( j = 0; j < 2; j++ )
416 if ( ds->texMins[ j ] < -texRange || ds->texMaxs[ j ] > texRange ) {
428 CalcLightmapAxis() - ydnar
429 gives closed lightmap axis for a plane normal
432 qboolean CalcLightmapAxis( vec3_t normal, vec3_t axis ){
437 if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && normal[ 2 ] == 0.0f ) {
442 /* get absolute normal */
443 absolute[ 0 ] = fabs( normal[ 0 ] );
444 absolute[ 1 ] = fabs( normal[ 1 ] );
445 absolute[ 2 ] = fabs( normal[ 2 ] );
448 if ( absolute[ 2 ] > absolute[ 0 ] - 0.0001f && absolute[ 2 ] > absolute[ 1 ] - 0.0001f ) {
449 if ( normal[ 2 ] > 0.0f ) {
450 VectorSet( axis, 0.0f, 0.0f, 1.0f );
453 VectorSet( axis, 0.0f, 0.0f, -1.0f );
456 else if ( absolute[ 0 ] > absolute[ 1 ] - 0.0001f && absolute[ 0 ] > absolute[ 2 ] - 0.0001f ) {
457 if ( normal[ 0 ] > 0.0f ) {
458 VectorSet( axis, 1.0f, 0.0f, 0.0f );
461 VectorSet( axis, -1.0f, 0.0f, 0.0f );
466 if ( normal[ 1 ] > 0.0f ) {
467 VectorSet( axis, 0.0f, 1.0f, 0.0f );
470 VectorSet( axis, 0.0f, -1.0f, 0.0f );
481 ClassifySurfaces() - ydnar
482 fills out a bunch of info in the surfaces, including planar status, lightmap projection, and bounding box
485 #define PLANAR_EPSILON 0.5f //% 0.126f 0.25f
487 void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){
492 static vec3_t axii[ 6 ] =
503 /* walk the list of surfaces */
504 for ( ; numSurfs > 0; numSurfs--, ds++ )
506 /* ignore bogus (or flare) surfaces */
507 if ( ds->type == SURFACE_BAD || ds->numVerts <= 0 ) {
514 /* -----------------------------------------------------------------
515 force meta if vertex count is too high or shader requires it
516 ----------------------------------------------------------------- */
518 if ( ds->type != SURFACE_PATCH && ds->type != SURFACE_FACE ) {
519 if ( ds->numVerts > SHADER_MAX_VERTEXES ) {
520 ds->type = SURFACE_FORCED_META;
524 /* -----------------------------------------------------------------
525 plane and bounding box classification
526 ----------------------------------------------------------------- */
528 /* set surface bounding box */
529 ClearBounds( ds->mins, ds->maxs );
530 for ( i = 0; i < ds->numVerts; i++ )
531 AddPointToBounds( ds->verts[ i ].xyz, ds->mins, ds->maxs );
533 /* try to get an existing plane */
534 if ( ds->planeNum >= 0 ) {
535 VectorCopy( mapplanes[ ds->planeNum ].normal, plane );
536 plane[ 3 ] = mapplanes[ ds->planeNum ].dist;
539 /* construct one from the first vert with a valid normal */
542 VectorClear( plane );
544 for ( i = 0; i < ds->numVerts; i++ )
546 if ( ds->verts[ i ].normal[ 0 ] != 0.0f && ds->verts[ i ].normal[ 1 ] != 0.0f && ds->verts[ i ].normal[ 2 ] != 0.0f ) {
547 VectorCopy( ds->verts[ i ].normal, plane );
548 plane[ 3 ] = DotProduct( ds->verts[ i ].xyz, plane );
554 /* test for bogus plane */
555 if ( VectorLength( plane ) <= 0.0f ) {
561 /* determine if surface is planar */
565 for ( i = 0; i < ds->numVerts; i++ )
567 /* point-plane test */
568 dist = DotProduct( ds->verts[ i ].xyz, plane ) - plane[ 3 ];
569 if ( fabs( dist ) > PLANAR_EPSILON ) {
570 //% if( ds->planeNum >= 0 )
572 //% Sys_FPrintf( SYS_WRN, "WARNING: Planar surface marked unplanar (%f > %f)\n", fabs( dist ), PLANAR_EPSILON );
573 //% ds->verts[ i ].color[ 0 ][ 0 ] = ds->verts[ i ].color[ 0 ][ 2 ] = 0;
581 /* find map plane if necessary */
583 if ( ds->planeNum < 0 ) {
584 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &ds->verts[ 0 ].xyz );
586 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
591 VectorClear( ds->lightmapVecs[ 2 ] );
592 //% if( ds->type == SURF_META || ds->type == SURF_FACE )
593 //% Sys_FPrintf( SYS_WRN, "WARNING: Non-planar face (%d): %s\n", ds->planeNum, ds->shaderInfo->shader );
596 /* -----------------------------------------------------------------
597 lightmap bounds and axis projection
598 ----------------------------------------------------------------- */
600 /* vertex lit surfaces don't need this information */
601 if ( si->compileFlags & C_VERTEXLIT || ds->type == SURFACE_TRIANGLES ) {
602 VectorClear( ds->lightmapAxis );
603 //% VectorClear( ds->lightmapVecs[ 2 ] );
608 /* the shader can specify an explicit lightmap axis */
609 if ( si->lightmapAxis[ 0 ] || si->lightmapAxis[ 1 ] || si->lightmapAxis[ 2 ] ) {
610 VectorCopy( si->lightmapAxis, ds->lightmapAxis );
612 else if ( ds->type == SURFACE_FORCED_META ) {
613 VectorClear( ds->lightmapAxis );
615 else if ( ds->planar ) {
616 CalcLightmapAxis( plane, ds->lightmapAxis );
620 /* find best lightmap axis */
621 for ( bestAxis = 0; bestAxis < 6; bestAxis++ )
623 for ( i = 0; i < ds->numVerts && bestAxis < 6; i++ )
625 //% Sys_Printf( "Comparing %1.3f %1.3f %1.3f to %1.3f %1.3f %1.3f\n",
626 //% ds->verts[ i ].normal[ 0 ], ds->verts[ i ].normal[ 1 ], ds->verts[ i ].normal[ 2 ],
627 //% axii[ bestAxis ][ 0 ], axii[ bestAxis ][ 1 ], axii[ bestAxis ][ 2 ] );
628 if ( DotProduct( ds->verts[ i ].normal, axii[ bestAxis ] ) < 0.25f ) { /* fixme: adjust this tolerance to taste */
633 if ( i == ds->numVerts ) {
638 /* set axis if possible */
639 if ( bestAxis < 6 ) {
640 //% if( ds->type == SURFACE_PATCH )
641 //% Sys_Printf( "Mapped axis %d onto patch\n", bestAxis );
642 VectorCopy( axii[ bestAxis ], ds->lightmapAxis );
646 //% if( ds->type == SURFACE_PATCH )
647 //% Sys_Printf( "Failed to map axis %d onto patch\n", bestAxis );
650 /* calculate lightmap sample size */
651 if ( ds->shaderInfo->lightmapSampleSize > 0 ) { /* shader value overrides every other */
652 ds->sampleSize = ds->shaderInfo->lightmapSampleSize;
654 else if ( ds->sampleSize <= 0 ) { /* may contain the entity asigned value */
655 ds->sampleSize = sampleSize; /* otherwise use global default */
658 if ( ds->lightmapScale > 0.0f ) { /* apply surface lightmap scaling factor */
659 ds->sampleSize = ds->lightmapScale * (float)ds->sampleSize;
660 ds->lightmapScale = 0; /* applied */
663 if ( ds->sampleSize < minSampleSize ) {
664 ds->sampleSize = minSampleSize;
667 if ( ds->sampleSize < 1 ) {
671 if ( ds->sampleSize > 16384 ) { /* powers of 2 are preferred */
672 ds->sampleSize = 16384;
680 ClassifyEntitySurfaces() - ydnar
681 classifies all surfaces in an entity
684 void ClassifyEntitySurfaces( entity_t *e ){
689 Sys_FPrintf( SYS_VRB, "--- ClassifyEntitySurfaces ---\n" );
691 /* walk the surface list */
692 for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
694 FinishSurface( &mapDrawSurfs[ i ] );
695 ClassifySurfaces( 1, &mapDrawSurfs[ i ] );
699 TidyEntitySurfaces( e );
705 GetShaderIndexForPoint() - ydnar
706 for shader-indexed surfaces (terrain), find a matching index from the indexmap
709 byte GetShaderIndexForPoint( indexMap_t *im, vec3_t eMins, vec3_t eMaxs, vec3_t point ){
712 vec3_t mins, maxs, size;
715 /* early out if no indexmap */
720 /* this code is really broken */
722 /* legacy precision fudges for terrain */
723 for ( i = 0; i < 3; i++ )
725 mins[ i ] = floor( eMins[ i ] + 0.1 );
726 maxs[ i ] = floor( eMaxs[ i ] + 0.1 );
727 size[ i ] = maxs[ i ] - mins[ i ];
730 /* find st (fixme: support more than just z-axis projection) */
731 s = floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ];
732 t = floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ];
736 else if ( s > 1.0f ) {
742 else if ( t > 1.0f ) {
747 x = ( im->w - 1 ) * s;
748 y = ( im->h - 1 ) * t;
751 for ( i = 0; i < 3; i++ )
753 mins[ i ] = eMins[ i ];
754 maxs[ i ] = eMaxs[ i ];
755 size[ i ] = maxs[ i ] - mins[ i ];
759 s = ( point[ 0 ] - mins[ 0 ] ) / size[ 0 ];
760 t = ( maxs[ 1 ] - point[ 1 ] ) / size[ 1 ];
768 else if ( x > ( im->w - 1 ) ) {
774 else if ( y > ( im->h - 1 ) ) {
780 return im->pixels[ y * im->w + x ];
784 #define snprintf_ignore(s, n, format, ...) do { \
785 size_t __n = snprintf(s, n, format, __VA_ARGS__); \
786 if (n >= n) {} /* truncated, ignore */ \
790 GetIndexedShader() - ydnar
791 for a given set of indexes and an indexmap, get a shader and set the vertex alpha in-place
792 this combines a couple different functions from terrain.c
795 shaderInfo_t *GetIndexedShader( shaderInfo_t *parent, indexMap_t *im, int numPoints, byte *shaderIndexes ){
797 byte minShaderIndex, maxShaderIndex;
798 char shader[ MAX_QPATH ];
802 /* early out if bad data */
803 if ( im == NULL || numPoints <= 0 || shaderIndexes == NULL ) {
804 return ShaderInfoForShader( "default" );
807 /* determine min/max index */
808 minShaderIndex = 255;
810 for ( i = 0; i < numPoints; i++ )
812 if ( shaderIndexes[ i ] < minShaderIndex ) {
813 minShaderIndex = shaderIndexes[ i ];
815 if ( shaderIndexes[ i ] > maxShaderIndex ) {
816 maxShaderIndex = shaderIndexes[ i ];
820 /* set alpha inline */
821 for ( i = 0; i < numPoints; i++ )
823 /* straight rip from terrain.c */
824 if ( shaderIndexes[ i ] < maxShaderIndex ) {
825 shaderIndexes[ i ] = 0;
828 shaderIndexes[ i ] = 255;
832 /* make a shader name */
833 if ( minShaderIndex == maxShaderIndex ) {
834 snprintf_ignore( shader, sizeof shader, "textures/%s_%d", im->shader, maxShaderIndex );
837 snprintf_ignore( shader, sizeof shader, "textures/%s_%dto%d", im->shader, minShaderIndex, maxShaderIndex );
841 si = ShaderInfoForShader( shader );
843 /* inherit a few things from parent shader */
844 if ( parent->globalTexture ) {
845 si->globalTexture = qtrue;
847 if ( parent->forceMeta ) {
848 si->forceMeta = qtrue;
850 if ( parent->nonplanar ) {
851 si->nonplanar = qtrue;
853 if ( si->shadeAngleDegrees == 0.0 ) {
854 si->shadeAngleDegrees = parent->shadeAngleDegrees;
856 if ( parent->tcGen && si->tcGen == qfalse ) {
857 /* set xy texture projection */
859 VectorCopy( parent->vecs[ 0 ], si->vecs[ 0 ] );
860 VectorCopy( parent->vecs[ 1 ], si->vecs[ 1 ] );
862 if ( VectorLength( parent->lightmapAxis ) > 0.0f && VectorLength( si->lightmapAxis ) <= 0.0f ) {
863 /* set lightmap projection axis */
864 VectorCopy( parent->lightmapAxis, si->lightmapAxis );
867 /* return the shader */
876 creates a SURF_FACE drawsurface from a given brush side and winding
879 #define SNAP_FLOAT_TO_INT 8
880 #define SNAP_INT_TO_FLOAT ( 1.0 / SNAP_FLOAT_TO_INT )
882 mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w ){
884 mapDrawSurface_t *ds;
885 shaderInfo_t *si, *parent;
891 byte shaderIndexes[ 256 ];
892 float offsets[ 256 ];
893 char tempShader[ MAX_QPATH ];
896 /* ydnar: don't make a drawsurf for culled sides */
902 if ( w->numpoints > MAX_POINTS_ON_WINDING ) {
903 Error( "DrawSurfaceForSide: w->numpoints = %d (> %d)", w->numpoints, MAX_POINTS_ON_WINDING );
909 /* ydnar: gs mods: check for indexed shader */
910 if ( si->indexed && b->im != NULL ) {
914 /* get shader indexes for each point */
915 for ( i = 0; i < w->numpoints; i++ )
917 shaderIndexes[ i ] = GetShaderIndexForPoint( b->im, b->eMins, b->eMaxs, w->p[ i ] );
918 offsets[ i ] = b->im->offsets[ shaderIndexes[ i ] ];
919 //% Sys_Printf( "%f ", offsets[ i ] );
922 /* get matching shader and set alpha */
924 si = GetIndexedShader( parent, b->im, w->numpoints, shaderIndexes );
930 /* ydnar: sky hack/fix for GL_CLAMP borders on ati cards */
931 if ( skyFixHack && si->skyParmsImageBase[ 0 ] != '\0' ) {
932 //% Sys_FPrintf( SYS_VRB, "Enabling sky hack for shader %s using env %s\n", si->shader, si->skyParmsImageBase );
933 snprintf_ignore( tempShader, sizeof tempShader, "%s_lf", si->skyParmsImageBase );
934 DrawSurfaceForShader( tempShader );
935 snprintf_ignore( tempShader, sizeof tempShader, "%s_rt", si->skyParmsImageBase );
936 DrawSurfaceForShader( tempShader );
937 snprintf_ignore( tempShader, sizeof tempShader, "%s_ft", si->skyParmsImageBase );
938 DrawSurfaceForShader( tempShader );
939 snprintf_ignore( tempShader, sizeof tempShader, "%s_bk", si->skyParmsImageBase );
940 DrawSurfaceForShader( tempShader );
941 snprintf_ignore( tempShader, sizeof tempShader, "%s_up", si->skyParmsImageBase );
942 DrawSurfaceForShader( tempShader );
943 snprintf_ignore( tempShader, sizeof tempShader, "%s_dn", si->skyParmsImageBase );
944 DrawSurfaceForShader( tempShader );
948 ds = AllocDrawSurface( SURFACE_FACE );
949 ds->entityNum = b->entityNum;
950 ds->castShadows = b->castShadows;
951 ds->recvShadows = b->recvShadows;
954 ds->planeNum = s->planenum;
955 VectorCopy( mapplanes[ s->planenum ].normal, ds->lightmapVecs[ 2 ] );
959 ds->sideRef = AllocSideRef( s, NULL );
961 ds->sampleSize = b->lightmapSampleSize;
962 ds->lightmapScale = b->lightmapScale;
963 ds->numVerts = w->numpoints;
964 ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) );
966 /* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
967 ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY );
969 /* create the vertexes */
970 for ( j = 0; j < w->numpoints; j++ )
972 /* get the drawvert */
975 /* copy xyz and do potential z offset */
976 VectorCopy( w->p[ j ], dv->xyz );
978 dv->xyz[ 2 ] += offsets[ j ];
981 /* round the xyz to a given precision and translate by origin */
982 for ( i = 0 ; i < 3 ; i++ )
983 dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5f );
984 VectorAdd( dv->xyz, e->origin, vTranslated );
986 /* ydnar: tek-fu celshading support for flat shaded shit */
988 dv->st[ 0 ] = si->stFlat[ 0 ];
989 dv->st[ 1 ] = si->stFlat[ 1 ];
992 /* ydnar: gs mods: added support for explicit shader texcoord generation */
993 else if ( si->tcGen ) {
994 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
995 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
998 /* old quake-style texturing */
999 else if ( g_bBrushPrimit == BPRIMIT_OLDBRUSHES ) {
1000 /* nearest-axial projection */
1001 dv->st[ 0 ] = s->vecs[ 0 ][ 3 ] + DotProduct( s->vecs[ 0 ], vTranslated );
1002 dv->st[ 1 ] = s->vecs[ 1 ][ 3 ] + DotProduct( s->vecs[ 1 ], vTranslated );
1003 dv->st[ 0 ] /= si->shaderWidth;
1004 dv->st[ 1 ] /= si->shaderHeight;
1007 /* brush primitive texturing */
1010 /* calculate texture s/t from brush primitive texture matrix */
1011 x = DotProduct( vTranslated, texX );
1012 y = DotProduct( vTranslated, texY );
1013 dv->st[ 0 ] = s->texMat[ 0 ][ 0 ] * x + s->texMat[ 0 ][ 1 ] * y + s->texMat[ 0 ][ 2 ];
1014 dv->st[ 1 ] = s->texMat[ 1 ][ 0 ] * x + s->texMat[ 1 ][ 1 ] * y + s->texMat[ 1 ][ 2 ];
1018 VectorCopy( mapplanes[ s->planenum ].normal, dv->normal );
1020 /* ydnar: set color */
1021 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
1023 dv->color[ k ][ 0 ] = 255;
1024 dv->color[ k ][ 1 ] = 255;
1025 dv->color[ k ][ 2 ] = 255;
1027 /* ydnar: gs mods: handle indexed shader blending */
1028 dv->color[ k ][ 3 ] = ( indexed ? shaderIndexes[ j ] : 255 );
1032 /* set cel shader */
1033 ds->celShader = b->celShader;
1035 /* set shade angle */
1036 if ( b->shadeAngleDegrees > 0.0f ) {
1037 ds->shadeAngleDegrees = b->shadeAngleDegrees;
1040 /* ydnar: gs mods: moved st biasing elsewhere */
1047 DrawSurfaceForMesh()
1048 moved here from patch.c
1051 #define YDNAR_NORMAL_EPSILON 0.50f
1053 qboolean VectorCompareExt( vec3_t n1, vec3_t n2, float epsilon ){
1058 for ( i = 0; i < 3; i++ )
1059 if ( fabs( n1[ i ] - n2[ i ] ) > epsilon ) {
1065 mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh ){
1070 mapDrawSurface_t *ds;
1071 shaderInfo_t *si, *parent;
1076 byte shaderIndexes[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1077 float offsets[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1080 /* get mesh and shader shader */
1081 if ( mesh == NULL ) {
1085 if ( mesh == NULL || si == NULL ) {
1089 /* get vertex count */
1090 numVerts = mesh->width * mesh->height;
1092 /* to make valid normals for patches with degenerate edges,
1093 we need to make a copy of the mesh and put the aproximating
1094 points onto the curve */
1096 /* create a copy of the mesh */
1097 copy = CopyMesh( mesh );
1099 /* store off the original (potentially bad) normals */
1100 MakeMeshNormals( *copy );
1101 for ( i = 0; i < numVerts; i++ )
1102 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1104 /* put the mesh on the curve */
1105 PutMeshOnCurve( *copy );
1107 /* find new normals (to take into account degenerate/flipped edges */
1108 MakeMeshNormals( *copy );
1109 for ( i = 0; i < numVerts; i++ )
1111 /* ydnar: only copy normals that are significantly different from the originals */
1112 if ( DotProduct( copy->verts[ i ].normal, mesh->verts[ i ].normal ) < 0.75f ) {
1113 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1117 /* free the old mesh */
1120 /* ydnar: gs mods: check for indexed shader */
1121 if ( si->indexed && p->im != NULL ) {
1125 /* get shader indexes for each point */
1126 for ( i = 0; i < numVerts; i++ )
1128 shaderIndexes[ i ] = GetShaderIndexForPoint( p->im, p->eMins, p->eMaxs, mesh->verts[ i ].xyz );
1129 offsets[ i ] = p->im->offsets[ shaderIndexes[ i ] ];
1132 /* get matching shader and set alpha */
1134 si = GetIndexedShader( parent, p->im, numVerts, shaderIndexes );
1141 /* ydnar: gs mods */
1142 ds = AllocDrawSurface( SURFACE_PATCH );
1143 ds->entityNum = p->entityNum;
1144 ds->castShadows = p->castShadows;
1145 ds->recvShadows = p->recvShadows;
1147 ds->shaderInfo = si;
1149 ds->sampleSize = p->lightmapSampleSize;
1150 ds->lightmapScale = p->lightmapScale; /* ydnar */
1151 ds->patchWidth = mesh->width;
1152 ds->patchHeight = mesh->height;
1153 ds->numVerts = ds->patchWidth * ds->patchHeight;
1154 ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
1155 memcpy( ds->verts, mesh->verts, ds->numVerts * sizeof( *ds->verts ) );
1160 ds->longestCurve = p->longestCurve;
1161 ds->maxIterations = p->maxIterations;
1163 /* construct a plane from the first vert */
1164 VectorCopy( mesh->verts[ 0 ].normal, plane );
1165 plane[ 3 ] = DotProduct( mesh->verts[ 0 ].xyz, plane );
1168 /* spew forth errors */
1169 if ( VectorLength( plane ) < 0.001f ) {
1170 Sys_Printf( "DrawSurfaceForMesh: bogus plane\n" );
1173 /* test each vert */
1174 for ( i = 1; i < ds->numVerts && planar; i++ )
1177 if ( VectorCompare( plane, mesh->verts[ i ].normal ) == qfalse ) {
1181 /* point-plane test */
1182 dist = DotProduct( mesh->verts[ i ].xyz, plane ) - plane[ 3 ];
1183 if ( fabs( dist ) > EQUAL_EPSILON ) {
1188 /* add a map plane */
1190 /* make a map plane */
1191 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &mesh->verts[ 0 ].xyz );
1192 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
1194 /* push this normal to all verts (ydnar 2003-02-14: bad idea, small patches get screwed up) */
1195 for ( i = 0; i < ds->numVerts; i++ )
1196 VectorCopy( plane, ds->verts[ i ].normal );
1199 /* walk the verts to do special stuff */
1200 for ( i = 0; i < ds->numVerts; i++ )
1202 /* get the drawvert */
1203 dv = &ds->verts[ i ];
1205 /* ydnar: tek-fu celshading support for flat shaded shit */
1207 dv->st[ 0 ] = si->stFlat[ 0 ];
1208 dv->st[ 1 ] = si->stFlat[ 1 ];
1211 /* ydnar: gs mods: added support for explicit shader texcoord generation */
1212 else if ( si->tcGen ) {
1213 /* translate by origin and project the texture */
1214 VectorAdd( dv->xyz, e->origin, vTranslated );
1215 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
1216 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
1219 /* ydnar: set color */
1220 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
1222 dv->color[ k ][ 0 ] = 255;
1223 dv->color[ k ][ 1 ] = 255;
1224 dv->color[ k ][ 2 ] = 255;
1226 /* ydnar: gs mods: handle indexed shader blending */
1227 dv->color[ k ][ 3 ] = ( indexed ? shaderIndexes[ i ] : 255 );
1232 dv->xyz[ 2 ] += offsets[ i ];
1236 /* set cel shader */
1237 ds->celShader = p->celShader;
1239 /* return the drawsurface */
1246 DrawSurfaceForFlare() - ydnar
1247 creates a flare draw surface
1250 mapDrawSurface_t *DrawSurfaceForFlare( int entNum, vec3_t origin, vec3_t normal, vec3_t color, const char *flareShader, int lightStyle ){
1251 mapDrawSurface_t *ds;
1255 if ( emitFlares == qfalse ) {
1259 /* allocate drawsurface */
1260 ds = AllocDrawSurface( SURFACE_FLARE );
1261 ds->entityNum = entNum;
1264 if ( flareShader != NULL && flareShader[ 0 ] != '\0' ) {
1265 ds->shaderInfo = ShaderInfoForShader( flareShader );
1268 ds->shaderInfo = ShaderInfoForShader( game->flareShader );
1270 if ( origin != NULL ) {
1271 VectorCopy( origin, ds->lightmapOrigin );
1273 if ( normal != NULL ) {
1274 VectorCopy( normal, ds->lightmapVecs[ 2 ] );
1276 if ( color != NULL ) {
1277 VectorCopy( color, ds->lightmapVecs[ 0 ] );
1280 /* store light style */
1281 ds->lightStyle = lightStyle;
1282 if ( ds->lightStyle < 0 || ds->lightStyle >= LS_NONE ) {
1283 ds->lightStyle = LS_NORMAL;
1288 /* return to sender */
1295 DrawSurfaceForShader() - ydnar
1296 creates a bogus surface to forcing the game to load a shader
1299 mapDrawSurface_t *DrawSurfaceForShader( char *shader ){
1302 mapDrawSurface_t *ds;
1306 si = ShaderInfoForShader( shader );
1308 /* find existing surface */
1309 for ( i = 0; i < numMapDrawSurfs; i++ )
1312 ds = &mapDrawSurfs[ i ];
1315 if ( ds->shaderInfo == si ) {
1320 /* create a new surface */
1321 ds = AllocDrawSurface( SURFACE_SHADER );
1323 ds->shaderInfo = ShaderInfoForShader( shader );
1325 /* return to sender */
1332 AddSurfaceFlare() - ydnar
1333 creates flares (coronas) centered on surfaces
1336 static void AddSurfaceFlare( mapDrawSurface_t *ds, vec3_t entityOrigin ){
1342 VectorClear( origin );
1343 for ( i = 0; i < ds->numVerts; i++ )
1344 VectorAdd( origin, ds->verts[ i ].xyz, origin );
1345 VectorScale( origin, ( 1.0f / ds->numVerts ), origin );
1346 if ( entityOrigin != NULL ) {
1347 VectorAdd( origin, entityOrigin, origin );
1350 /* push origin off surface a bit */
1351 VectorMA( origin, 2.0f, ds->lightmapVecs[ 2 ], origin );
1353 /* create the drawsurface */
1354 DrawSurfaceForFlare( ds->entityNum, origin, ds->lightmapVecs[ 2 ], ds->shaderInfo->color, ds->shaderInfo->flareShader, ds->shaderInfo->lightStyle );
1361 subdivides a face surface until it is smaller than the specified size (subdivisions)
1364 static void SubdivideFace_r( entity_t *e, brush_t *brush, side_t *side, winding_t *w, int fogNum, float subdivisions ){
1368 const float epsilon = 0.1;
1369 int subFloor, subCeil;
1370 winding_t *frontWinding, *backWinding;
1371 mapDrawSurface_t *ds;
1378 if ( w->numpoints < 3 ) {
1379 Error( "SubdivideFace_r: Bad w->numpoints (%d < 3)", w->numpoints );
1382 /* determine surface bounds */
1383 ClearBounds( bounds[ 0 ], bounds[ 1 ] );
1384 for ( i = 0; i < w->numpoints; i++ )
1385 AddPointToBounds( w->p[ i ], bounds[ 0 ], bounds[ 1 ] );
1387 /* split the face */
1388 for ( axis = 0; axis < 3; axis++ )
1390 vec3_t planePoint = { 0, 0, 0 };
1391 vec3_t planeNormal = { 0, 0, 0 };
1395 /* create an axial clipping plane */
1396 subFloor = floor( bounds[ 0 ][ axis ] / subdivisions ) * subdivisions;
1397 subCeil = ceil( bounds[ 1 ][ axis ] / subdivisions ) * subdivisions;
1398 planePoint[ axis ] = subFloor + subdivisions;
1399 planeNormal[ axis ] = -1;
1400 d = DotProduct( planePoint, planeNormal );
1402 /* subdivide if necessary */
1403 if ( ( subCeil - subFloor ) > subdivisions ) {
1404 /* clip the winding */
1405 ClipWindingEpsilon( w, planeNormal, d, epsilon, &frontWinding, &backWinding ); /* not strict; we assume we always keep a winding */
1407 /* the clip may not produce two polygons if it was epsilon close */
1408 if ( frontWinding == NULL ) {
1411 else if ( backWinding == NULL ) {
1416 SubdivideFace_r( e, brush, side, frontWinding, fogNum, subdivisions );
1417 SubdivideFace_r( e, brush, side, backWinding, fogNum, subdivisions );
1423 /* create a face surface */
1424 ds = DrawSurfaceForSide( e, brush, side, w );
1426 /* set correct fog num */
1427 ds->fogNum = fogNum;
1433 SubdivideFaceSurfaces()
1434 chop up brush face surfaces that have subdivision attributes
1435 ydnar: and subdivide surfaces that exceed specified texture coordinate range
1438 void SubdivideFaceSurfaces( entity_t *e, tree_t *tree ){
1439 int i, j, numBaseDrawSurfs, fogNum;
1440 mapDrawSurface_t *ds;
1445 float range, size, subdivisions, s2;
1449 Sys_FPrintf( SYS_VRB, "--- SubdivideFaceSurfaces ---\n" );
1451 /* walk the list of surfaces */
1452 numBaseDrawSurfs = numMapDrawSurfs;
1453 for ( i = e->firstDrawSurf; i < numBaseDrawSurfs; i++ )
1456 ds = &mapDrawSurfs[ i ];
1458 /* only subdivide brush sides */
1459 if ( ds->type != SURFACE_FACE || ds->mapBrush == NULL || ds->sideRef == NULL || ds->sideRef->side == NULL ) {
1464 brush = ds->mapBrush;
1465 side = ds->sideRef->side;
1467 /* check subdivision for shader */
1468 si = side->shaderInfo;
1473 /* ydnar: don't subdivide sky surfaces */
1474 if ( si->compileFlags & C_SKY ) {
1478 /* do texture coordinate range check */
1479 ClassifySurfaces( 1, ds );
1480 if ( CalcSurfaceTextureRange( ds ) == qfalse ) {
1481 /* calculate subdivisions texture range (this code is shit) */
1482 range = ( ds->texRange[ 0 ] > ds->texRange[ 1 ] ? ds->texRange[ 0 ] : ds->texRange[ 1 ] );
1483 size = ds->maxs[ 0 ] - ds->mins[ 0 ];
1484 for ( j = 1; j < 3; j++ )
1485 if ( ( ds->maxs[ j ] - ds->mins[ j ] ) > size ) {
1486 size = ds->maxs[ j ] - ds->mins[ j ];
1488 subdivisions = ( size / range ) * texRange;
1489 subdivisions = ceil( subdivisions / 2 ) * 2;
1490 for ( j = 1; j < 8; j++ )
1492 s2 = ceil( (float) texRange / j );
1493 if ( fabs( subdivisions - s2 ) <= 4.0 ) {
1500 subdivisions = si->subdivisions;
1503 /* get subdivisions from shader */
1504 if ( si->subdivisions > 0 && si->subdivisions < subdivisions ) {
1505 subdivisions = si->subdivisions;
1507 if ( subdivisions < 1.0f ) {
1511 /* preserve fog num */
1512 fogNum = ds->fogNum;
1514 /* make a winding and free the surface */
1515 w = WindingFromDrawSurf( ds );
1519 SubdivideFace_r( e, brush, side, w, fogNum, subdivisions );
1526 ====================
1529 Adds non-opaque leaf fragments to the convex hull
1530 ====================
1533 void ClipSideIntoTree_r( winding_t *w, side_t *side, node_t *node ){
1535 winding_t *front, *back;
1541 if ( node->planenum != PLANENUM_LEAF ) {
1542 if ( side->planenum == node->planenum ) {
1543 ClipSideIntoTree_r( w, side, node->children[0] );
1546 if ( side->planenum == ( node->planenum ^ 1 ) ) {
1547 ClipSideIntoTree_r( w, side, node->children[1] );
1551 plane = &mapplanes[ node->planenum ];
1552 ClipWindingEpsilonStrict( w, plane->normal, plane->dist,
1553 ON_EPSILON, &front, &back ); /* strict, we handle the "winding disappeared" case */
1554 if ( !front && !back ) {
1555 /* in doubt, register it in both nodes */
1556 front = CopyWinding( w );
1557 back = CopyWinding( w );
1561 ClipSideIntoTree_r( front, side, node->children[0] );
1562 ClipSideIntoTree_r( back, side, node->children[1] );
1567 // if opaque leaf, don't add
1568 if ( !node->opaque ) {
1569 AddWindingToConvexHull( w, &side->visibleHull, mapplanes[ side->planenum ].normal );
1580 static int g_numHiddenFaces, g_numCoinFaces;
1585 CullVectorCompare() - ydnar
1586 compares two vectors with an epsilon
1589 #define CULL_EPSILON 0.1f
1591 qboolean CullVectorCompare( const vec3_t v1, const vec3_t v2 ){
1595 for ( i = 0; i < 3; i++ )
1596 if ( fabs( v1[ i ] - v2[ i ] ) > CULL_EPSILON ) {
1605 SideInBrush() - ydnar
1606 determines if a brushside lies inside another brush
1609 qboolean SideInBrush( side_t *side, brush_t *b ){
1614 /* ignore sides w/o windings or shaders */
1615 if ( side->winding == NULL || side->shaderInfo == NULL ) {
1619 /* ignore culled sides and translucent brushes */
1620 if ( side->culled == qtrue || ( b->compileFlags & C_TRANSLUCENT ) ) {
1625 for ( i = 0; i < b->numsides; i++ )
1627 /* fail if any sides are caulk */
1628 if ( b->sides[ i ].compileFlags & C_NODRAW ) {
1632 /* check if side's winding is on or behind the plane */
1633 plane = &mapplanes[ b->sides[ i ].planenum ];
1634 s = WindingOnPlaneSide( side->winding, plane->normal, plane->dist );
1635 if ( s == SIDE_FRONT || s == SIDE_CROSS ) {
1640 /* don't cull autosprite or polygonoffset surfaces */
1641 if ( side->shaderInfo ) {
1642 if ( side->shaderInfo->autosprite || side->shaderInfo->polygonOffset ) {
1648 side->culled = qtrue;
1656 culls obscured or buried brushsides from the map
1659 void CullSides( entity_t *e ){
1661 int i, j, k, l, first, second, dir;
1664 side_t *side1, *side2;
1668 Sys_FPrintf( SYS_VRB, "--- CullSides ---\n" );
1670 g_numHiddenFaces = 0;
1673 /* brush interator 1 */
1674 for ( b1 = e->brushes; b1; b1 = b1->next )
1677 if ( b1->numsides < 1 ) {
1681 /* brush iterator 2 */
1682 for ( b2 = b1->next; b2; b2 = b2->next )
1685 if ( b2->numsides < 1 ) {
1689 /* original check */
1690 if ( b1->original == b2->original && b1->original != NULL ) {
1696 for ( i = 0; i < 3; i++ )
1697 if ( b1->mins[ i ] > b2->maxs[ i ] || b1->maxs[ i ] < b2->mins[ i ] ) {
1704 /* cull inside sides */
1705 for ( i = 0; i < b1->numsides; i++ )
1706 SideInBrush( &b1->sides[ i ], b2 );
1707 for ( i = 0; i < b2->numsides; i++ )
1708 SideInBrush( &b2->sides[ i ], b1 );
1710 /* side iterator 1 */
1711 for ( i = 0; i < b1->numsides; i++ )
1714 side1 = &b1->sides[ i ];
1715 w1 = side1->winding;
1719 numPoints = w1->numpoints;
1720 if ( side1->shaderInfo == NULL ) {
1724 /* side iterator 2 */
1725 for ( j = 0; j < b2->numsides; j++ )
1728 side2 = &b2->sides[ j ];
1729 w2 = side2->winding;
1733 if ( side2->shaderInfo == NULL ) {
1736 if ( w1->numpoints != w2->numpoints ) {
1739 if ( side1->culled == qtrue && side2->culled == qtrue ) {
1743 /* compare planes */
1744 if ( ( side1->planenum & ~0x00000001 ) != ( side2->planenum & ~0x00000001 ) ) {
1748 /* get autosprite and polygonoffset status */
1749 if ( side1->shaderInfo &&
1750 ( side1->shaderInfo->autosprite || side1->shaderInfo->polygonOffset ) ) {
1753 if ( side2->shaderInfo &&
1754 ( side2->shaderInfo->autosprite || side2->shaderInfo->polygonOffset ) ) {
1758 /* find first common point */
1760 for ( k = 0; k < numPoints; k++ )
1762 if ( VectorCompare( w1->p[ 0 ], w2->p[ k ] ) ) {
1767 if ( first == -1 ) {
1771 /* find second common point (regardless of winding order) */
1774 if ( ( first + 1 ) < numPoints ) {
1780 if ( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) ) {
1789 second = numPoints - 1;
1791 if ( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) ) {
1799 /* compare the rest of the points */
1801 for ( k = 0; k < numPoints; k++ )
1803 if ( !CullVectorCompare( w1->p[ k ], w2->p[ l ] ) ) {
1811 else if ( l >= numPoints ) {
1815 if ( k >= 100000 ) {
1820 if ( !side2->culled && !( side2->compileFlags & C_TRANSLUCENT ) && !( side2->compileFlags & C_NODRAW ) ) {
1821 side1->culled = qtrue;
1825 if ( side1->planenum == side2->planenum && side1->culled == qtrue ) {
1830 if ( !side1->culled && !( side1->compileFlags & C_TRANSLUCENT ) && !( side1->compileFlags & C_NODRAW ) ) {
1831 side2->culled = qtrue;
1839 /* emit some stats */
1840 Sys_FPrintf( SYS_VRB, "%9d hidden faces culled\n", g_numHiddenFaces );
1841 Sys_FPrintf( SYS_VRB, "%9d coincident faces culled\n", g_numCoinFaces );
1850 creates side->visibleHull for all visible sides
1852 the drawsurf for a side will consist of the convex hull of
1853 all points in non-opaque clusters, which allows overlaps
1854 to be trimmed off automatically.
1857 void ClipSidesIntoTree( entity_t *e, tree_t *tree ){
1861 side_t *side, *newSide;
1865 /* ydnar: cull brush sides */
1869 Sys_FPrintf( SYS_VRB, "--- ClipSidesIntoTree ---\n" );
1871 /* walk the brush list */
1872 for ( b = e->brushes; b; b = b->next )
1874 /* walk the brush sides */
1875 for ( i = 0; i < b->numsides; i++ )
1878 side = &b->sides[ i ];
1879 if ( side->winding == NULL ) {
1883 /* copy the winding */
1884 w = CopyWinding( side->winding );
1885 side->visibleHull = NULL;
1886 ClipSideIntoTree_r( w, side, tree->headnode );
1888 /* anything left? */
1889 w = side->visibleHull;
1895 si = side->shaderInfo;
1900 /* don't create faces for non-visible sides */
1901 /* ydnar: except indexed shaders, like common/terrain and nodraw fog surfaces */
1902 if ( ( si->compileFlags & C_NODRAW ) && si->indexed == qfalse && !( si->compileFlags & C_FOG ) ) {
1906 /* always use the original winding for autosprites and noclip faces */
1907 if ( si->autosprite || si->noClip ) {
1911 /* save this winding as a visible surface */
1912 DrawSurfaceForSide( e, b, side, w );
1914 /* make a back side for fog */
1915 if ( !( si->compileFlags & C_FOG ) ) {
1919 /* duplicate the up-facing side */
1920 w = ReverseWinding( w );
1921 newSide = safe_malloc( sizeof( *side ) );
1923 newSide->visibleHull = w;
1924 newSide->planenum ^= 1;
1926 /* save this winding as a visible surface */
1927 DrawSurfaceForSide( e, b, newSide, w );
1936 this section deals with filtering drawsurfaces into the bsp tree,
1937 adding references to each leaf a surface touches
1942 AddReferenceToLeaf() - ydnar
1943 adds a reference to surface ds in the bsp leaf node
1946 int AddReferenceToLeaf( mapDrawSurface_t *ds, node_t *node ){
1951 if ( node->planenum != PLANENUM_LEAF || node->opaque ) {
1955 /* try to find an existing reference */
1956 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
1958 if ( dsr->outputNum == numBSPDrawSurfaces ) {
1963 /* add a new reference */
1964 dsr = safe_malloc( sizeof( *dsr ) );
1965 dsr->outputNum = numBSPDrawSurfaces;
1966 dsr->nextRef = node->drawSurfReferences;
1967 node->drawSurfReferences = dsr;
1969 /* ydnar: sky/skybox surfaces */
1970 if ( node->skybox ) {
1973 if ( ds->shaderInfo->compileFlags & C_SKY ) {
1984 AddReferenceToTree_r() - ydnar
1985 adds a reference to the specified drawsurface to every leaf in the tree
1988 int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, qboolean skybox ){
1993 if ( node == NULL ) {
1997 /* is this a decision node? */
1998 if ( node->planenum != PLANENUM_LEAF ) {
1999 /* add to child nodes and return */
2000 refs += AddReferenceToTree_r( ds, node->children[ 0 ], skybox );
2001 refs += AddReferenceToTree_r( ds, node->children[ 1 ], skybox );
2007 /* skybox surfaces only get added to sky leaves */
2012 /* increase the leaf bounds */
2013 for ( i = 0; i < ds->numVerts; i++ )
2014 AddPointToBounds( ds->verts[ i ].xyz, node->mins, node->maxs );
2017 /* add a reference */
2018 return AddReferenceToLeaf( ds, node );
2024 FilterPointIntoTree_r() - ydnar
2025 filters a single point from a surface into the tree
2028 int FilterPointIntoTree_r( vec3_t point, mapDrawSurface_t *ds, node_t *node ){
2034 /* is this a decision node? */
2035 if ( node->planenum != PLANENUM_LEAF ) {
2036 /* classify the point in relation to the plane */
2037 plane = &mapplanes[ node->planenum ];
2038 d = DotProduct( point, plane->normal ) - plane->dist;
2040 /* filter by this plane */
2042 if ( d >= -ON_EPSILON ) {
2043 refs += FilterPointIntoTree_r( point, ds, node->children[ 0 ] );
2045 if ( d <= ON_EPSILON ) {
2046 refs += FilterPointIntoTree_r( point, ds, node->children[ 1 ] );
2053 /* add a reference */
2054 return AddReferenceToLeaf( ds, node );
2060 FilterPointConvexHullIntoTree_r() - ydnar
2061 filters the convex hull of multiple points from a surface into the tree
2064 int FilterPointConvexHullIntoTree_r( vec3_t **points, int npoints, mapDrawSurface_t *ds, node_t *node ){
2065 float d, dmin, dmax;
2074 /* is this a decision node? */
2075 if ( node->planenum != PLANENUM_LEAF ) {
2076 /* classify the point in relation to the plane */
2077 plane = &mapplanes[ node->planenum ];
2079 dmin = dmax = DotProduct( *( points[0] ), plane->normal ) - plane->dist;
2080 for ( i = 1; i < npoints; ++i )
2082 d = DotProduct( *( points[i] ), plane->normal ) - plane->dist;
2091 /* filter by this plane */
2093 if ( dmax >= -ON_EPSILON ) {
2094 refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 0 ] );
2096 if ( dmin <= ON_EPSILON ) {
2097 refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 1 ] );
2104 /* add a reference */
2105 return AddReferenceToLeaf( ds, node );
2111 FilterWindingIntoTree_r() - ydnar
2112 filters a winding from a drawsurface into the tree
2115 int FilterWindingIntoTree_r( winding_t *w, mapDrawSurface_t *ds, node_t *node ){
2118 vec4_t plane1, plane2;
2119 winding_t *fat, *front, *back;
2123 /* get shaderinfo */
2124 si = ds->shaderInfo;
2126 /* ydnar: is this the head node? */
2127 if ( node->parent == NULL && si != NULL &&
2128 ( si->mins[ 0 ] != 0.0f || si->maxs[ 0 ] != 0.0f ||
2129 si->mins[ 1 ] != 0.0f || si->maxs[ 1 ] != 0.0f ||
2130 si->mins[ 2 ] != 0.0f || si->maxs[ 2 ] != 0.0f ) ) {
2131 static qboolean warned = qfalse;
2133 Sys_FPrintf( SYS_WRN, "WARNING: this map uses the deformVertexes move hack\n" );
2137 /* 'fatten' the winding by the shader mins/maxs (parsed from vertexDeform move) */
2138 /* note this winding is completely invalid (concave, nonplanar, etc) */
2139 fat = AllocWinding( w->numpoints * 3 + 3 );
2140 fat->numpoints = w->numpoints * 3 + 3;
2141 for ( i = 0; i < w->numpoints; i++ )
2143 VectorCopy( w->p[ i ], fat->p[ i ] );
2144 VectorAdd( w->p[ i ], si->mins, fat->p[ i + ( w->numpoints + 1 ) ] );
2145 VectorAdd( w->p[ i ], si->maxs, fat->p[ i + ( w->numpoints + 1 ) * 2 ] );
2147 VectorCopy( w->p[ 0 ], fat->p[ i ] );
2148 VectorAdd( w->p[ 0 ], si->mins, fat->p[ i + w->numpoints ] );
2149 VectorAdd( w->p[ 0 ], si->maxs, fat->p[ i + w->numpoints * 2 ] );
2152 * note: this winding is STILL not suitable for ClipWindingEpsilon, and
2153 * also does not really fulfill the intention as it only contains
2154 * origin, +mins, +maxs, but thanks to the "closing" points I just
2155 * added to the three sub-windings, the fattening at least doesn't make
2163 /* is this a decision node? */
2164 if ( node->planenum != PLANENUM_LEAF ) {
2165 /* get node plane */
2166 p1 = &mapplanes[ node->planenum ];
2167 VectorCopy( p1->normal, plane1 );
2168 plane1[ 3 ] = p1->dist;
2170 /* check if surface is planar */
2171 if ( ds->planeNum >= 0 ) {
2172 /* get surface plane */
2173 p2 = &mapplanes[ ds->planeNum ];
2174 VectorCopy( p2->normal, plane2 );
2175 plane2[ 3 ] = p2->dist;
2178 /* div0: this is the plague (inaccurate) */
2181 /* invert surface plane */
2182 VectorSubtract( vec3_origin, plane2, reverse );
2183 reverse[ 3 ] = -plane2[ 3 ];
2185 /* compare planes */
2186 if ( DotProduct( plane1, plane2 ) > 0.999f && fabs( plane1[ 3 ] - plane2[ 3 ] ) < 0.001f ) {
2187 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2189 if ( DotProduct( plane1, reverse ) > 0.999f && fabs( plane1[ 3 ] - reverse[ 3 ] ) < 0.001f ) {
2190 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2194 /* div0: this is the cholera (doesn't hit enough) */
2196 /* the drawsurf might have an associated plane, if so, force a filter here */
2197 if ( ds->planeNum == node->planenum ) {
2198 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2200 if ( ds->planeNum == ( node->planenum ^ 1 ) ) {
2201 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2206 /* clip the winding by this plane */
2207 ClipWindingEpsilonStrict( w, plane1, plane1[ 3 ], ON_EPSILON, &front, &back ); /* strict; we handle the "winding disappeared" case */
2209 /* filter by this plane */
2211 if ( front == NULL && back == NULL ) {
2212 /* same plane, this is an ugly hack */
2213 /* but better too many than too few refs */
2214 refs += FilterWindingIntoTree_r( CopyWinding( w ), ds, node->children[ 0 ] );
2215 refs += FilterWindingIntoTree_r( CopyWinding( w ), ds, node->children[ 1 ] );
2217 if ( front != NULL ) {
2218 refs += FilterWindingIntoTree_r( front, ds, node->children[ 0 ] );
2220 if ( back != NULL ) {
2221 refs += FilterWindingIntoTree_r( back, ds, node->children[ 1 ] );
2229 /* add a reference */
2230 return AddReferenceToLeaf( ds, node );
2236 FilterFaceIntoTree()
2237 filters a planar winding face drawsurface into the bsp tree
2240 int FilterFaceIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2245 /* make a winding and filter it into the tree */
2246 w = WindingFromDrawSurf( ds );
2247 refs = FilterWindingIntoTree_r( w, ds, tree->headnode );
2256 FilterPatchIntoTree()
2257 subdivides a patch into an approximate curve and filters it into the tree
2260 #define FILTER_SUBDIVISION 8
2262 static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2265 for ( y = 0; y + 2 < ds->patchHeight; y += 2 )
2266 for ( x = 0; x + 2 < ds->patchWidth; x += 2 )
2269 points[0] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 0 )].xyz;
2270 points[1] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 1 )].xyz;
2271 points[2] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 2 )].xyz;
2272 points[3] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 0 )].xyz;
2273 points[4] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 1 )].xyz;
2274 points[5] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 2 )].xyz;
2275 points[6] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 0 )].xyz;
2276 points[7] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 1 )].xyz;
2277 points[8] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 2 )].xyz;
2278 refs += FilterPointConvexHullIntoTree_r( points, 9, ds, tree->headnode );
2287 FilterTrianglesIntoTree()
2288 filters a triangle surface (meta, model) into the bsp
2291 static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2296 /* ydnar: gs mods: this was creating bogus triangles before */
2298 for ( i = 0; i < ds->numIndexes; i += 3 )
2301 if ( ds->indexes[ i ] >= ds->numVerts ||
2302 ds->indexes[ i + 1 ] >= ds->numVerts ||
2303 ds->indexes[ i + 2 ] >= ds->numVerts ) {
2304 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2307 /* make a triangle winding and filter it into the tree */
2308 w = AllocWinding( 3 );
2310 VectorCopy( ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2311 VectorCopy( ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2312 VectorCopy( ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2313 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2316 /* use point filtering as well */
2317 for ( i = 0; i < ds->numVerts; i++ )
2318 refs += FilterPointIntoTree_r( ds->verts[ i ].xyz, ds, tree->headnode );
2326 FilterFoliageIntoTree()
2327 filters a foliage surface (wolf et/splash damage)
2330 static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2332 bspDrawVert_t *instance;
2337 /* walk origin list */
2339 for ( f = 0; f < ds->numFoliageInstances; f++ )
2342 instance = ds->verts + ds->patchHeight + f;
2344 /* walk triangle list */
2345 for ( i = 0; i < ds->numIndexes; i += 3 )
2348 if ( ds->indexes[ i ] >= ds->numVerts ||
2349 ds->indexes[ i + 1 ] >= ds->numVerts ||
2350 ds->indexes[ i + 2 ] >= ds->numVerts ) {
2351 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2354 /* make a triangle winding and filter it into the tree */
2355 w = AllocWinding( 3 );
2357 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2358 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2359 VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2360 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2363 /* use point filtering as well */
2364 for ( i = 0; i < ( ds->numVerts - ds->numFoliageInstances ); i++ )
2366 VectorAdd( instance->xyz, ds->verts[ i ].xyz, xyz );
2367 refs += FilterPointIntoTree_r( xyz, ds, tree->headnode );
2377 FilterFlareIntoTree()
2378 simple point filtering for flare surfaces
2380 static int FilterFlareSurfIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2381 return FilterPointIntoTree_r( ds->lightmapOrigin, ds, tree->headnode );
2387 EmitDrawVerts() - ydnar
2388 emits bsp drawverts from a map drawsurface
2391 void EmitDrawVerts( mapDrawSurface_t *ds, bspDrawSurface_t *out ){
2399 si = ds->shaderInfo;
2400 offset = si->offset;
2402 /* copy the verts */
2403 out->firstVert = numBSPDrawVerts;
2404 out->numVerts = ds->numVerts;
2405 for ( i = 0; i < ds->numVerts; i++ )
2407 /* allocate a new vert */
2409 dv = &bspDrawVerts[ numBSPDrawVerts - 1 ];
2412 memcpy( dv, &ds->verts[ i ], sizeof( *dv ) );
2415 if ( offset != 0.0f ) {
2416 VectorMA( dv->xyz, offset, dv->normal, dv->xyz );
2419 /* expand model bounds
2420 necessary because of misc_model surfaces on entities
2421 note: does not happen on worldspawn as its bounds is only used for determining lightgrid bounds */
2422 if ( numBSPModels > 0 ) {
2423 AddPointToBounds( dv->xyz, bspModels[ numBSPModels ].mins, bspModels[ numBSPModels ].maxs );
2427 if ( debugSurfaces ) {
2428 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
2429 VectorCopy( debugColors[ ( ds - mapDrawSurfs ) % 12 ], dv->color[ k ] );
2437 FindDrawIndexes() - ydnar
2438 this attempts to find a run of indexes in the bsp that match the given indexes
2439 this tends to reduce the size of the bsp index pool by 1/3 or more
2440 returns numIndexes + 1 if the search failed
2443 int FindDrawIndexes( int numIndexes, int *indexes ){
2444 int i, j, numTestIndexes;
2448 if ( numIndexes < 3 || numBSPDrawIndexes < numIndexes || indexes == NULL ) {
2449 return numBSPDrawIndexes;
2453 numTestIndexes = 1 + numBSPDrawIndexes - numIndexes;
2455 /* handle 3 indexes as a special case for performance */
2456 if ( numIndexes == 3 ) {
2457 /* run through all indexes */
2458 for ( i = 0; i < numTestIndexes; i++ )
2460 /* test 3 indexes */
2461 if ( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2462 indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2463 indexes[ 2 ] == bspDrawIndexes[ i + 2 ] ) {
2464 numRedundantIndexes += numIndexes;
2470 return numBSPDrawIndexes;
2473 /* handle 4 or more indexes */
2474 for ( i = 0; i < numTestIndexes; i++ )
2476 /* test first 4 indexes */
2477 if ( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2478 indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2479 indexes[ 2 ] == bspDrawIndexes[ i + 2 ] &&
2480 indexes[ 3 ] == bspDrawIndexes[ i + 3 ] ) {
2481 /* handle 4 indexes */
2482 if ( numIndexes == 4 ) {
2486 /* test the remainder */
2487 for ( j = 4; j < numIndexes; j++ )
2489 if ( indexes[ j ] != bspDrawIndexes[ i + j ] ) {
2492 else if ( j == ( numIndexes - 1 ) ) {
2493 numRedundantIndexes += numIndexes;
2501 return numBSPDrawIndexes;
2507 EmitDrawIndexes() - ydnar
2508 attempts to find an existing run of drawindexes before adding new ones
2511 void EmitDrawIndexes( mapDrawSurface_t *ds, bspDrawSurface_t *out ){
2515 /* attempt to use redundant indexing */
2516 out->firstIndex = FindDrawIndexes( ds->numIndexes, ds->indexes );
2517 out->numIndexes = ds->numIndexes;
2518 if ( out->firstIndex == numBSPDrawIndexes ) {
2519 /* copy new unique indexes */
2520 for ( i = 0; i < ds->numIndexes; i++ )
2522 AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
2523 bspDrawIndexes[ numBSPDrawIndexes ] = ds->indexes[ i ];
2525 /* validate the index */
2526 if ( ds->type != SURFACE_PATCH ) {
2527 if ( bspDrawIndexes[ numBSPDrawIndexes ] < 0 || bspDrawIndexes[ numBSPDrawIndexes ] >= ds->numVerts ) {
2528 Sys_FPrintf( SYS_WRN, "WARNING: %d %s has invalid index %d (%d)\n",
2530 ds->shaderInfo->shader,
2531 bspDrawIndexes[ numBSPDrawIndexes ],
2533 bspDrawIndexes[ numBSPDrawIndexes ] = 0;
2537 /* increment index count */
2538 numBSPDrawIndexes++;
2548 emits a bsp flare drawsurface
2551 void EmitFlareSurface( mapDrawSurface_t *ds ){
2553 bspDrawSurface_t *out;
2556 /* ydnar: nuking useless flare drawsurfaces */
2557 if ( emitFlares == qfalse && ds->type != SURFACE_SHADER ) {
2562 if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2563 Error( "MAX_MAP_DRAW_SURFS" );
2566 /* allocate a new surface */
2567 if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2568 Error( "MAX_MAP_DRAW_SURFS" );
2570 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2571 ds->outputNum = numBSPDrawSurfaces;
2572 numBSPDrawSurfaces++;
2573 memset( out, 0, sizeof( *out ) );
2576 out->surfaceType = MST_FLARE;
2577 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2578 out->fogNum = ds->fogNum;
2581 for ( i = 0; i < MAX_LIGHTMAPS; i++ )
2583 out->lightmapNum[ i ] = -3;
2584 out->lightmapStyles[ i ] = LS_NONE;
2585 out->vertexStyles[ i ] = LS_NONE;
2587 out->lightmapStyles[ 0 ] = ds->lightStyle;
2588 out->vertexStyles[ 0 ] = ds->lightStyle;
2590 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin ); /* origin */
2591 VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] ); /* color */
2592 VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2593 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] ); /* normal */
2596 numSurfacesByType[ ds->type ]++;
2603 emits a bsp patch drawsurface
2606 void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds ){
2608 bspDrawSurface_t *out;
2609 int surfaceFlags, contentFlags;
2612 /* vortex: _patchMeta support */
2613 forcePatchMeta = IntForKey( e, "_patchMeta" );
2614 if ( !forcePatchMeta ) {
2615 forcePatchMeta = IntForKey( e, "patchMeta" );
2618 /* invert the surface if necessary */
2619 if ( ds->backSide || ds->shaderInfo->invert ) {
2620 bspDrawVert_t *dv1, *dv2, temp;
2622 /* walk the verts, flip the normal */
2623 for ( i = 0; i < ds->numVerts; i++ )
2624 VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2626 /* walk the verts again, but this time reverse their order */
2627 for ( j = 0; j < ds->patchHeight; j++ )
2629 for ( i = 0; i < ( ds->patchWidth / 2 ); i++ )
2631 dv1 = &ds->verts[ j * ds->patchWidth + i ];
2632 dv2 = &ds->verts[ j * ds->patchWidth + ( ds->patchWidth - i - 1 ) ];
2633 memcpy( &temp, dv1, sizeof( bspDrawVert_t ) );
2634 memcpy( dv1, dv2, sizeof( bspDrawVert_t ) );
2635 memcpy( dv2, &temp, sizeof( bspDrawVert_t ) );
2640 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2643 /* allocate a new surface */
2644 if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2645 Error( "MAX_MAP_DRAW_SURFS" );
2647 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2648 ds->outputNum = numBSPDrawSurfaces;
2649 numBSPDrawSurfaces++;
2650 memset( out, 0, sizeof( *out ) );
2653 out->surfaceType = MST_PATCH;
2654 if ( debugSurfaces ) {
2655 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2657 else if ( patchMeta || forcePatchMeta ) {
2658 /* patch meta requires that we have nodraw patches for collision */
2659 surfaceFlags = ds->shaderInfo->surfaceFlags;
2660 contentFlags = ds->shaderInfo->contentFlags;
2661 ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, NULL );
2662 ApplySurfaceParm( "pointlight", &contentFlags, &surfaceFlags, NULL );
2664 /* we don't want this patch getting lightmapped */
2665 VectorClear( ds->lightmapVecs[ 2 ] );
2666 VectorClear( ds->lightmapAxis );
2669 /* emit the new fake shader */
2670 out->shaderNum = EmitShader( ds->shaderInfo->shader, &contentFlags, &surfaceFlags );
2673 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2675 out->patchWidth = ds->patchWidth;
2676 out->patchHeight = ds->patchHeight;
2677 out->fogNum = ds->fogNum;
2680 for ( i = 0; i < MAX_LIGHTMAPS; i++ )
2682 out->lightmapNum[ i ] = -3;
2683 out->lightmapStyles[ i ] = LS_NONE;
2684 out->vertexStyles[ i ] = LS_NONE;
2686 out->lightmapStyles[ 0 ] = LS_NORMAL;
2687 out->vertexStyles[ 0 ] = LS_NORMAL;
2689 /* ydnar: gs mods: previously, the lod bounds were stored in lightmapVecs[ 0 ] and [ 1 ], moved to bounds[ 0 ] and [ 1 ] */
2690 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2691 VectorCopy( ds->bounds[ 0 ], out->lightmapVecs[ 0 ] );
2692 VectorCopy( ds->bounds[ 1 ], out->lightmapVecs[ 1 ] );
2693 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2695 /* ydnar: gs mods: clear out the plane normal */
2696 if ( ds->planar == qfalse ) {
2697 VectorClear( out->lightmapVecs[ 2 ] );
2700 /* emit the verts and indexes */
2701 EmitDrawVerts( ds, out );
2702 EmitDrawIndexes( ds, out );
2705 numSurfacesByType[ ds->type ]++;
2711 OptimizeTriangleSurface() - ydnar
2712 optimizes the vertex/index data in a triangle surface
2715 #define VERTEX_CACHE_SIZE 16
2717 static void OptimizeTriangleSurface( mapDrawSurface_t *ds ){
2718 int i, j, k, temp, first, best, bestScore, score;
2719 int vertexCache[ VERTEX_CACHE_SIZE + 1 ]; /* one more for optimizing insert */
2723 /* certain surfaces don't get optimized */
2724 if ( ds->numIndexes <= VERTEX_CACHE_SIZE ||
2725 ds->shaderInfo->autosprite ) {
2729 /* create index scratch pad */
2730 indexes = safe_malloc( ds->numIndexes * sizeof( *indexes ) );
2731 memcpy( indexes, ds->indexes, ds->numIndexes * sizeof( *indexes ) );
2734 for ( i = 0; i <= VERTEX_CACHE_SIZE && i < ds->numIndexes; i++ )
2735 vertexCache[ i ] = indexes[ i ];
2737 /* add triangles in a vertex cache-aware order */
2738 for ( i = 0; i < ds->numIndexes; i += 3 )
2740 /* find best triangle given the current vertex cache */
2744 for ( j = 0; j < ds->numIndexes; j += 3 )
2746 /* valid triangle? */
2747 if ( indexes[ j ] != -1 ) {
2748 /* set first if necessary */
2753 /* score the triangle */
2755 for ( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2757 if ( indexes[ j ] == vertexCache[ k ] || indexes[ j + 1 ] == vertexCache[ k ] || indexes[ j + 2 ] == vertexCache[ k ] ) {
2762 /* better triangle? */
2763 if ( score > bestScore ) {
2768 /* a perfect score of 3 means this triangle's verts are already present in the vertex cache */
2775 /* check if no decent triangle was found, and use first available */
2780 /* valid triangle? */
2782 /* add triangle to vertex cache */
2783 for ( j = 0; j < 3; j++ )
2785 for ( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2787 if ( indexes[ best + j ] == vertexCache[ k ] ) {
2792 if ( k >= VERTEX_CACHE_SIZE ) {
2793 /* pop off top of vertex cache */
2794 for ( k = VERTEX_CACHE_SIZE; k > 0; k-- )
2795 vertexCache[ k ] = vertexCache[ k - 1 ];
2798 vertexCache[ 0 ] = indexes[ best + j ];
2802 /* add triangle to surface */
2803 ds->indexes[ i ] = indexes[ best ];
2804 ds->indexes[ i + 1 ] = indexes[ best + 1 ];
2805 ds->indexes[ i + 2 ] = indexes[ best + 2 ];
2807 /* clear from input pool */
2808 indexes[ best ] = -1;
2809 indexes[ best + 1 ] = -1;
2810 indexes[ best + 2 ] = -1;
2812 /* sort triangle windings (312 -> 123) */
2813 while ( ds->indexes[ i ] > ds->indexes[ i + 1 ] || ds->indexes[ i ] > ds->indexes[ i + 2 ] )
2815 temp = ds->indexes[ i ];
2816 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2817 ds->indexes[ i + 1 ] = ds->indexes[ i + 2 ];
2818 ds->indexes[ i + 2 ] = temp;
2830 EmitTriangleSurface()
2831 creates a bsp drawsurface from arbitrary triangle surfaces
2834 void EmitTriangleSurface( mapDrawSurface_t *ds ){
2836 bspDrawSurface_t *out;
2838 /* invert the surface if necessary */
2839 if ( ds->backSide || ds->shaderInfo->invert ) {
2840 /* walk the indexes, reverse the triangle order */
2841 for ( i = 0; i < ds->numIndexes; i += 3 )
2843 temp = ds->indexes[ i ];
2844 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2845 ds->indexes[ i + 1 ] = temp;
2848 /* walk the verts, flip the normal */
2849 for ( i = 0; i < ds->numVerts; i++ )
2850 VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2853 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2856 /* allocate a new surface */
2857 if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2858 Error( "MAX_MAP_DRAW_SURFS" );
2860 out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2861 ds->outputNum = numBSPDrawSurfaces;
2862 numBSPDrawSurfaces++;
2863 memset( out, 0, sizeof( *out ) );
2865 /* ydnar/sd: handle wolf et foliage surfaces */
2866 if ( ds->type == SURFACE_FOLIAGE ) {
2867 out->surfaceType = MST_FOLIAGE;
2870 /* ydnar: gs mods: handle lightmapped terrain (force to planar type) */
2871 //% else if( VectorLength( ds->lightmapAxis ) <= 0.0f || ds->type == SURFACE_TRIANGLES || ds->type == SURFACE_FOGHULL || debugSurfaces )
2872 else if ( ( VectorLength( ds->lightmapAxis ) <= 0.0f && ds->planar == qfalse ) ||
2873 ds->type == SURFACE_TRIANGLES ||
2874 ds->type == SURFACE_FOGHULL ||
2875 ds->numVerts > maxLMSurfaceVerts ||
2877 out->surfaceType = MST_TRIANGLE_SOUP;
2880 /* set to a planar face */
2882 out->surfaceType = MST_PLANAR;
2886 if ( debugSurfaces ) {
2887 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2890 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2892 out->patchWidth = ds->patchWidth;
2893 out->patchHeight = ds->patchHeight;
2894 out->fogNum = ds->fogNum;
2896 /* debug inset (push each triangle vertex towards the center of each triangle it is on */
2898 bspDrawVert_t *a, *b, *c;
2902 /* walk triangle list */
2903 for ( i = 0; i < ds->numIndexes; i += 3 )
2906 a = &ds->verts[ ds->indexes[ i ] ];
2907 b = &ds->verts[ ds->indexes[ i + 1 ] ];
2908 c = &ds->verts[ ds->indexes[ i + 2 ] ];
2910 /* calculate centroid */
2911 VectorCopy( a->xyz, cent );
2912 VectorAdd( cent, b->xyz, cent );
2913 VectorAdd( cent, c->xyz, cent );
2914 VectorScale( cent, 1.0f / 3.0f, cent );
2916 /* offset each vertex */
2917 VectorSubtract( cent, a->xyz, dir );
2918 VectorNormalize( dir, dir );
2919 VectorAdd( a->xyz, dir, a->xyz );
2920 VectorSubtract( cent, b->xyz, dir );
2921 VectorNormalize( dir, dir );
2922 VectorAdd( b->xyz, dir, b->xyz );
2923 VectorSubtract( cent, c->xyz, dir );
2924 VectorNormalize( dir, dir );
2925 VectorAdd( c->xyz, dir, c->xyz );
2930 for ( i = 0; i < MAX_LIGHTMAPS; i++ )
2932 out->lightmapNum[ i ] = -3;
2933 out->lightmapStyles[ i ] = LS_NONE;
2934 out->vertexStyles[ i ] = LS_NONE;
2936 out->lightmapStyles[ 0 ] = LS_NORMAL;
2937 out->vertexStyles[ 0 ] = LS_NORMAL;
2939 /* lightmap vectors (lod bounds for patches */
2940 VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2941 VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );
2942 VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2943 VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2945 /* ydnar: gs mods: clear out the plane normal */
2946 if ( ds->planar == qfalse ) {
2947 VectorClear( out->lightmapVecs[ 2 ] );
2950 /* optimize the surface's triangles */
2951 OptimizeTriangleSurface( ds );
2953 /* emit the verts and indexes */
2954 EmitDrawVerts( ds, out );
2955 EmitDrawIndexes( ds, out );
2958 numSurfacesByType[ ds->type ]++;
2965 emits a bsp planar winding (brush face) drawsurface
2968 static void EmitFaceSurface( mapDrawSurface_t *ds ){
2969 /* strip/fan finding was moved elsewhere */
2970 if ( maxAreaFaceSurface ) {
2971 MaxAreaFaceSurface( ds );
2974 StripFaceSurface( ds );
2976 EmitTriangleSurface( ds );
2982 MakeDebugPortalSurfs_r() - ydnar
2983 generates drawsurfaces for passable portals in the bsp
2986 static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si ){
2990 mapDrawSurface_t *ds;
2994 /* recurse if decision node */
2995 if ( node->planenum != PLANENUM_LEAF ) {
2996 MakeDebugPortalSurfs_r( node->children[ 0 ], si );
2997 MakeDebugPortalSurfs_r( node->children[ 1 ], si );
3001 /* don't bother with opaque leaves */
3002 if ( node->opaque ) {
3006 /* walk the list of portals */
3007 for ( c = 0, p = node->portals; p != NULL; c++, p = p->next[ s ] )
3009 /* get winding and side even/odd */
3011 s = ( p->nodes[ 1 ] == node );
3013 /* is this a valid portal for this leaf? */
3014 if ( w && p->nodes[ 0 ] == node ) {
3015 /* is this portal passable? */
3016 if ( PortalPassable( p ) == qfalse ) {
3020 /* check max points */
3021 if ( w->numpoints > 64 ) {
3022 Error( "MakePortalSurfs_r: w->numpoints = %d", w->numpoints );
3025 /* allocate a drawsurface */
3026 ds = AllocDrawSurface( SURFACE_FACE );
3027 ds->shaderInfo = si;
3029 ds->sideRef = AllocSideRef( p->side, NULL );
3030 ds->planeNum = FindFloatPlane( p->plane.normal, p->plane.dist, 0, NULL );
3031 VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] );
3033 ds->numVerts = w->numpoints;
3034 ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) );
3036 /* walk the winding */
3037 for ( i = 0; i < ds->numVerts; i++ )
3043 VectorCopy( w->p[ i ], dv->xyz );
3044 VectorCopy( p->plane.normal, dv->normal );
3047 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
3049 VectorCopy( debugColors[ c % 12 ], dv->color[ k ] );
3050 dv->color[ k ][ 3 ] = 32;
3060 MakeDebugPortalSurfs() - ydnar
3061 generates drawsurfaces for passable portals in the bsp
3064 void MakeDebugPortalSurfs( tree_t *tree ){
3069 Sys_FPrintf( SYS_VRB, "--- MakeDebugPortalSurfs ---\n" );
3071 /* get portal debug shader */
3072 si = ShaderInfoForShader( "debugportals" );
3075 MakeDebugPortalSurfs_r( tree->headnode, si );
3082 generates drawsurfaces for a foghull (this MUST use a sky shader)
3085 void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader ){
3087 mapDrawSurface_t *ds;
3088 vec3_t fogMins, fogMaxs;
3101 if ( shader == NULL || shader[ 0 ] == '\0' ) {
3106 Sys_FPrintf( SYS_VRB, "--- MakeFogHullSurfs ---\n" );
3108 /* get hull bounds */
3109 VectorCopy( mapMins, fogMins );
3110 VectorCopy( mapMaxs, fogMaxs );
3111 for ( i = 0; i < 3; i++ )
3113 fogMins[ i ] -= 128;
3114 fogMaxs[ i ] += 128;
3117 /* get foghull shader */
3118 si = ShaderInfoForShader( shader );
3120 /* allocate a drawsurface */
3121 ds = AllocDrawSurface( SURFACE_FOGHULL );
3122 ds->shaderInfo = si;
3125 ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) );
3126 ds->numIndexes = 36;
3127 ds->indexes = safe_malloc0( ds->numIndexes * sizeof( *ds->indexes ) );
3130 VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
3131 VectorSet( ds->verts[ 1 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
3132 VectorSet( ds->verts[ 2 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
3133 VectorSet( ds->verts[ 3 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
3135 VectorSet( ds->verts[ 4 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
3136 VectorSet( ds->verts[ 5 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
3137 VectorSet( ds->verts[ 6 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
3138 VectorSet( ds->verts[ 7 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
3141 memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( *ds->indexes ) );
3147 BiasSurfaceTextures()
3148 biases a surface's texcoords as close to 0 as possible
3151 void BiasSurfaceTextures( mapDrawSurface_t *ds ){
3155 /* calculate the surface texture bias */
3156 CalcSurfaceTextureRange( ds );
3158 /* don't bias globaltextured shaders */
3159 if ( ds->shaderInfo->globalTexture ) {
3163 /* bias the texture coordinates */
3164 for ( i = 0; i < ds->numVerts; i++ )
3166 ds->verts[ i ].st[ 0 ] += ds->bias[ 0 ];
3167 ds->verts[ i ].st[ 1 ] += ds->bias[ 1 ];
3174 AddSurfaceModelsToTriangle_r()
3175 adds models to a specified triangle, returns the number of models added
3178 int AddSurfaceModelsToTriangle_r( mapDrawSurface_t *ds, surfaceModel_t *model, bspDrawVert_t **tri ){
3179 bspDrawVert_t mid, *tri2[ 3 ];
3180 int max, n, localNumSurfaceModels;
3184 localNumSurfaceModels = 0;
3186 /* subdivide calc */
3189 float *a, *b, dx, dy, dz, dist, maxDist;
3192 /* find the longest edge and split it */
3195 for ( i = 0; i < 3; i++ )
3199 b = tri[ ( i + 1 ) % 3 ]->xyz;
3202 dx = a[ 0 ] - b[ 0 ];
3203 dy = a[ 1 ] - b[ 1 ];
3204 dz = a[ 2 ] - b[ 2 ];
3205 dist = ( dx * dx ) + ( dy * dy ) + ( dz * dz );
3208 if ( dist > maxDist ) {
3214 /* is the triangle small enough? */
3215 if ( max < 0 || maxDist <= ( model->density * model->density ) ) {
3216 float odds, r, angle;
3217 vec3_t origin, normal, scale, axis[ 3 ], angles;
3218 m4x4_t transform, temp;
3221 /* roll the dice (model's odds scaled by vertex alpha) */
3222 odds = model->odds * ( tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] ) / 765.0f;
3228 /* calculate scale */
3229 r = model->minScale + Random() * ( model->maxScale - model->minScale );
3230 VectorSet( scale, r, r, r );
3232 /* calculate angle */
3233 angle = model->minAngle + Random() * ( model->maxAngle - model->minAngle );
3235 /* calculate average origin */
3236 VectorCopy( tri[ 0 ]->xyz, origin );
3237 VectorAdd( origin, tri[ 1 ]->xyz, origin );
3238 VectorAdd( origin, tri[ 2 ]->xyz, origin );
3239 VectorScale( origin, ( 1.0f / 3.0f ), origin );
3241 /* clear transform matrix */
3242 m4x4_identity( transform );
3244 /* handle oriented models */
3245 if ( model->oriented ) {
3247 VectorSet( angles, 0.0f, 0.0f, angle );
3249 /* calculate average normal */
3250 VectorCopy( tri[ 0 ]->normal, normal );
3251 VectorAdd( normal, tri[ 1 ]->normal, normal );
3252 VectorAdd( normal, tri[ 2 ]->normal, normal );
3253 if ( VectorNormalize( normal, axis[ 2 ] ) == 0.0f ) {
3254 VectorCopy( tri[ 0 ]->normal, axis[ 2 ] );
3257 /* make perpendicular vectors */
3258 MakeNormalVectors( axis[ 2 ], axis[ 1 ], axis[ 0 ] );
3260 /* copy to matrix */
3261 m4x4_identity( temp );
3262 temp[ 0 ] = axis[ 0 ][ 0 ]; temp[ 1 ] = axis[ 0 ][ 1 ]; temp[ 2 ] = axis[ 0 ][ 2 ];
3263 temp[ 4 ] = axis[ 1 ][ 0 ]; temp[ 5 ] = axis[ 1 ][ 1 ]; temp[ 6 ] = axis[ 1 ][ 2 ];
3264 temp[ 8 ] = axis[ 2 ][ 0 ]; temp[ 9 ] = axis[ 2 ][ 1 ]; temp[ 10 ] = axis[ 2 ][ 2 ];
3267 m4x4_scale_by_vec3( temp, scale );
3269 /* rotate around z axis */
3270 m4x4_rotate_by_vec3( temp, angles, eXYZ );
3273 m4x4_translate_by_vec3( transform, origin );
3275 /* tranform into axis space */
3276 m4x4_multiply_by_m4x4( transform, temp );
3279 /* handle z-up models */
3283 VectorSet( angles, 0.0f, 0.0f, angle );
3286 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
3289 /* insert the model */
3290 InsertModel( (char *) model->model, 0, 0, transform, NULL, ds->celShader, ds->entityNum, ds->castShadows, ds->recvShadows, 0, ds->lightmapScale, 0, 0 );
3292 /* return to sender */
3297 /* split the longest edge and map it */
3298 LerpDrawVert( tri[ max ], tri[ ( max + 1 ) % 3 ], &mid );
3300 /* recurse to first triangle */
3301 VectorCopy( tri, tri2 );
3303 n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3307 localNumSurfaceModels += n;
3309 /* recurse to second triangle */
3310 VectorCopy( tri, tri2 );
3311 tri2[ ( max + 1 ) % 3 ] = ∣
3312 n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3316 localNumSurfaceModels += n;
3319 return localNumSurfaceModels;
3326 adds a surface's shader models to the surface
3329 int AddSurfaceModels( mapDrawSurface_t *ds ){
3330 surfaceModel_t *model;
3331 int i, x, y, n, pw[ 5 ], r, localNumSurfaceModels, iterations;
3332 mesh_t src, *mesh, *subdivided;
3333 bspDrawVert_t centroid, *tri[ 3 ];
3338 if ( ds == NULL || ds->shaderInfo == NULL || ds->shaderInfo->surfaceModel == NULL ) {
3343 localNumSurfaceModels = 0;
3345 /* walk the model list */
3346 for ( model = ds->shaderInfo->surfaceModel; model != NULL; model = model->next )
3348 /* switch on type */
3351 /* handle brush faces and decals */
3354 /* calculate centroid */
3355 memset( ¢roid, 0, sizeof( centroid ) );
3359 for ( i = 0; i < ds->numVerts; i++ )
3361 VectorAdd( centroid.xyz, ds->verts[ i ].xyz, centroid.xyz );
3362 VectorAdd( centroid.normal, ds->verts[ i ].normal, centroid.normal );
3363 centroid.st[ 0 ] += ds->verts[ i ].st[ 0 ];
3364 centroid.st[ 1 ] += ds->verts[ i ].st[ 1 ];
3365 alpha += ds->verts[ i ].color[ 0 ][ 3 ];
3369 centroid.xyz[ 0 ] /= ds->numVerts;
3370 centroid.xyz[ 1 ] /= ds->numVerts;
3371 centroid.xyz[ 2 ] /= ds->numVerts;
3372 if ( VectorNormalize( centroid.normal, centroid.normal ) == 0.0f ) {
3373 VectorCopy( ds->verts[ 0 ].normal, centroid.normal );
3375 centroid.st[ 0 ] /= ds->numVerts;
3376 centroid.st[ 1 ] /= ds->numVerts;
3377 alpha /= ds->numVerts;
3378 centroid.color[ 0 ][ 0 ] = 0xFF;
3379 centroid.color[ 0 ][ 1 ] = 0xFF;
3380 centroid.color[ 0 ][ 2 ] = 0xFF;
3381 centroid.color[ 0 ][ 2 ] = ( alpha > 255.0f ? 0xFF : alpha );
3383 /* head vert is centroid */
3384 tri[ 0 ] = ¢roid;
3386 /* walk fanned triangles */
3387 for ( i = 0; i < ds->numVerts; i++ )
3390 tri[ 1 ] = &ds->verts[ i ];
3391 tri[ 2 ] = &ds->verts[ ( i + 1 ) % ds->numVerts ];
3394 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3398 localNumSurfaceModels += n;
3402 /* handle patches */
3404 /* subdivide the surface */
3405 src.width = ds->patchWidth;
3406 src.height = ds->patchHeight;
3407 src.verts = ds->verts;
3408 //% subdivided = SubdivideMesh( src, 8.0f, 512 );
3409 iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions );
3410 subdivided = SubdivideMesh2( src, iterations );
3412 /* fit it to the curve and remove colinear verts on rows/columns */
3413 PutMeshOnCurve( *subdivided );
3414 mesh = RemoveLinearMeshColumnsRows( subdivided );
3415 FreeMesh( subdivided );
3417 /* subdivide each quad to place the models */
3418 for ( y = 0; y < ( mesh->height - 1 ); y++ )
3420 for ( x = 0; x < ( mesh->width - 1 ); x++ )
3423 pw[ 0 ] = x + ( y * mesh->width );
3424 pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
3425 pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
3426 pw[ 3 ] = x + 1 + ( y * mesh->width );
3427 pw[ 4 ] = x + ( y * mesh->width ); /* same as pw[ 0 ] */
3433 tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3434 tri[ 1 ] = &mesh->verts[ pw[ r + 1 ] ];
3435 tri[ 2 ] = &mesh->verts[ pw[ r + 2 ] ];
3436 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3440 localNumSurfaceModels += n;
3443 tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3444 tri[ 1 ] = &mesh->verts[ pw[ r + 2 ] ];
3445 tri[ 2 ] = &mesh->verts[ pw[ r + 3 ] ];
3446 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3450 localNumSurfaceModels += n;
3454 /* free the subdivided mesh */
3458 /* handle triangle surfaces */
3459 case SURFACE_TRIANGLES:
3460 case SURFACE_FORCED_META:
3462 /* walk the triangle list */
3463 for ( i = 0; i < ds->numIndexes; i += 3 )
3465 tri[ 0 ] = &ds->verts[ ds->indexes[ i ] ];
3466 tri[ 1 ] = &ds->verts[ ds->indexes[ i + 1 ] ];
3467 tri[ 2 ] = &ds->verts[ ds->indexes[ i + 2 ] ];
3468 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3472 localNumSurfaceModels += n;
3476 /* no support for flares, foghull, etc */
3483 return localNumSurfaceModels;
3489 AddEntitySurfaceModels() - ydnar
3490 adds surfacemodels to an entity's surfaces
3493 void AddEntitySurfaceModels( entity_t *e ){
3498 Sys_FPrintf( SYS_VRB, "--- AddEntitySurfaceModels ---\n" );
3500 /* walk the surface list */
3501 for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3502 numSurfaceModels += AddSurfaceModels( &mapDrawSurfs[ i ] );
3508 VolumeColorMods() - ydnar
3509 applies brush/volumetric color/alpha modulation to vertexes
3512 static void VolumeColorMods( entity_t *e, mapDrawSurface_t *ds ){
3520 if ( e->colorModBrushes == NULL ) {
3524 /* iterate brushes */
3525 for ( b = e->colorModBrushes; b != NULL; b = b->nextColorModBrush )
3527 /* worldspawn alpha brushes affect all, grouped ones only affect original entity */
3528 if ( b->entityNum != 0 && b->entityNum != ds->entityNum ) {
3533 if ( b->mins[ 0 ] > ds->maxs[ 0 ] || b->maxs[ 0 ] < ds->mins[ 0 ] ||
3534 b->mins[ 1 ] > ds->maxs[ 1 ] || b->maxs[ 1 ] < ds->mins[ 1 ] ||
3535 b->mins[ 2 ] > ds->maxs[ 2 ] || b->maxs[ 2 ] < ds->mins[ 2 ] ) {
3540 for ( i = 0; i < ds->numVerts; i++ )
3542 /* iterate planes */
3543 for ( j = 0; j < b->numsides; j++ )
3545 /* point-plane test */
3546 plane = &mapplanes[ b->sides[ j ].planenum ];
3547 d = DotProduct( ds->verts[ i ].xyz, plane->normal ) - plane->dist;
3553 /* apply colormods */
3554 if ( j == b->numsides ) {
3555 ColorMod( b->contentShader->colorMod, 1, &ds->verts[ i ] );
3564 FilterDrawsurfsIntoTree()
3565 upon completion, all drawsurfs that actually generate a reference
3566 will have been emited to the bspfile arrays, and the references
3567 will have valid final indexes
3570 void FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree ){
3572 mapDrawSurface_t *ds;
3574 vec3_t origin, mins, maxs;
3576 int numSurfs, numRefs, numSkyboxSurfaces;
3581 Sys_FPrintf( SYS_VRB, "--- FilterDrawsurfsIntoTree ---\n" );
3583 /* filter surfaces into the tree */
3586 numSkyboxSurfaces = 0;
3587 for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3589 /* get surface and try to early out */
3590 ds = &mapDrawSurfs[ i ];
3591 if ( ds->numVerts == 0 && ds->type != SURFACE_FLARE && ds->type != SURFACE_SHADER ) {
3596 si = ds->shaderInfo;
3598 /* ydnar: skybox surfaces are special */
3600 refs = AddReferenceToTree_r( ds, tree->headnode, qtrue );
3601 ds->skybox = qfalse;
3608 /* refs initially zero */
3611 /* apply texture coordinate mods */
3612 for ( j = 0; j < ds->numVerts; j++ )
3613 TCMod( si->mod, ds->verts[ j ].st );
3615 /* ydnar: apply shader colormod */
3616 ColorMod( ds->shaderInfo->colorMod, ds->numVerts, ds->verts );
3618 /* ydnar: apply brush colormod */
3619 VolumeColorMods( e, ds );
3621 /* ydnar: make fur surfaces */
3622 if ( si->furNumLayers > 0 ) {
3626 /* ydnar/sd: make foliage surfaces */
3627 if ( si->foliage != NULL ) {
3631 /* create a flare surface if necessary */
3632 if ( si->flareShader != NULL && si->flareShader[ 0 ] ) {
3633 AddSurfaceFlare( ds, e->origin );
3636 /* ydnar: don't emit nodraw surfaces (like nodraw fog) */
3637 if ( ( si->compileFlags & C_NODRAW ) && ds->type != SURFACE_PATCH ) {
3641 /* ydnar: bias the surface textures */
3642 BiasSurfaceTextures( ds );
3644 /* ydnar: globalizing of fog volume handling (eek a hack) */
3645 if ( e != entities && si->noFog == qfalse ) {
3646 /* find surface origin and offset by entity origin */
3647 VectorAdd( ds->mins, ds->maxs, origin );
3648 VectorScale( origin, 0.5f, origin );
3649 VectorAdd( origin, e->origin, origin );
3651 VectorAdd( ds->mins, e->origin, mins );
3652 VectorAdd( ds->maxs, e->origin, maxs );
3654 /* set the fog number for this surface */
3655 ds->fogNum = FogForBounds( mins, maxs, 1.0f ); //% FogForPoint( origin, 0.0f );
3659 /* ydnar: remap shader */
3660 if ( ds->shaderInfo->remapShader && ds->shaderInfo->remapShader[ 0 ] ) {
3661 ds->shaderInfo = ShaderInfoForShader( ds->shaderInfo->remapShader );
3664 /* ydnar: gs mods: handle the various types of surfaces */
3667 /* handle brush faces */
3671 refs = FilterFaceIntoTree( ds, tree );
3674 EmitFaceSurface( ds );
3678 /* handle patches */
3681 refs = FilterPatchIntoTree( ds, tree );
3684 EmitPatchSurface( e, ds );
3688 /* handle triangle surfaces */
3689 case SURFACE_TRIANGLES:
3690 case SURFACE_FORCED_META:
3692 //% Sys_FPrintf( SYS_VRB, "Surface %4d: [%1d] %4d verts %s\n", numSurfs, ds->planar, ds->numVerts, si->shader );
3694 refs = FilterTrianglesIntoTree( ds, tree );
3697 EmitTriangleSurface( ds );
3701 /* handle foliage surfaces (splash damage/wolf et) */
3702 case SURFACE_FOLIAGE:
3703 //% Sys_FPrintf( SYS_VRB, "Surface %4d: [%d] %4d verts %s\n", numSurfs, ds->numFoliageInstances, ds->numVerts, si->shader );
3705 refs = FilterFoliageIntoTree( ds, tree );
3708 EmitTriangleSurface( ds );
3712 /* handle foghull surfaces */
3713 case SURFACE_FOGHULL:
3715 refs = AddReferenceToTree_r( ds, tree->headnode, qfalse );
3718 EmitTriangleSurface( ds );
3725 refs = FilterFlareSurfIntoTree( ds, tree );
3728 EmitFlareSurface( ds );
3732 /* handle shader-only surfaces */
3733 case SURFACE_SHADER:
3735 EmitFlareSurface( ds );
3744 /* maybe surface got marked as skybox again */
3745 /* if we keep that flag, it will get scaled up AGAIN */
3747 ds->skybox = qfalse;
3750 /* tot up the references */
3756 /* emit extra surface data */
3757 SetSurfaceExtra( ds, numBSPDrawSurfaces - 1 );
3758 //% Sys_FPrintf( SYS_VRB, "%d verts %d indexes\n", ds->numVerts, ds->numIndexes );
3760 /* one last sanity check */
3762 bspDrawSurface_t *out;
3763 out = &bspDrawSurfaces[ numBSPDrawSurfaces - 1 ];
3764 if ( out->numVerts == 3 && out->numIndexes > 3 ) {
3765 Sys_FPrintf( SYS_WRN, "WARNING: Potentially bad %s surface (%d: %d, %d)\n %s\n",
3766 surfaceTypes[ ds->type ],
3767 numBSPDrawSurfaces - 1, out->numVerts, out->numIndexes, si->shader );
3771 /* ydnar: handle skybox surfaces */
3773 MakeSkyboxSurface( ds );
3774 numSkyboxSurfaces++;
3779 /* emit some statistics */
3780 Sys_FPrintf( SYS_VRB, "%9d references\n", numRefs );
3781 Sys_FPrintf( SYS_VRB, "%9d (%d) emitted drawsurfs\n", numSurfs, numBSPDrawSurfaces );
3782 Sys_FPrintf( SYS_VRB, "%9d stripped face surfaces\n", numStripSurfaces );
3783 Sys_FPrintf( SYS_VRB, "%9d fanned face surfaces\n", numFanSurfaces );
3784 Sys_FPrintf( SYS_VRB, "%9d maxarea'd face surfaces\n", numMaxAreaSurfaces );
3785 Sys_FPrintf( SYS_VRB, "%9d surface models generated\n", numSurfaceModels );
3786 Sys_FPrintf( SYS_VRB, "%9d skybox surfaces generated\n", numSkyboxSurfaces );
3787 for ( i = 0; i < NUM_SURFACE_TYPES; i++ )
3788 Sys_FPrintf( SYS_VRB, "%9d %s surfaces\n", numSurfacesByType[ i ], surfaceTypes[ i ] );
3790 Sys_FPrintf( SYS_VRB, "%9d redundant indexes supressed, saving %d Kbytes\n", numRedundantIndexes, ( numRedundantIndexes * 4 / 1024 ) );