]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/surface.c
Remove -Wno-unused-but-set-variable
[xonotic/netradiant.git] / tools / quake3 / q3map2 / surface.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
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.
12
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.
17
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
21
22    ----------------------------------------------------------------------------------
23
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."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define SURFACE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42    AllocDrawSurface()
43    ydnar: gs mods: changed to force an explicit type when allocating
44  */
45
46 mapDrawSurface_t *AllocDrawSurface( surfaceType_t type ){
47         mapDrawSurface_t    *ds;
48
49
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 );
53         }
54
55         /* bounds check */
56         if ( numMapDrawSurfs >= MAX_MAP_DRAW_SURFS ) {
57                 Error( "MAX_MAP_DRAW_SURFS (%d) exceeded", MAX_MAP_DRAW_SURFS );
58         }
59         ds = &mapDrawSurfs[ numMapDrawSurfs ];
60         numMapDrawSurfs++;
61
62         /* ydnar: do initial surface setup */
63         memset( ds, 0, sizeof( mapDrawSurface_t ) );
64         ds->type = type;
65         ds->planeNum = -1;
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 */
69
70         return ds;
71 }
72
73
74
75 /*
76    FinishSurface()
77    ydnar: general surface finish pass
78  */
79
80 void FinishSurface( mapDrawSurface_t *ds ){
81         mapDrawSurface_t    *ds2;
82
83
84         /* dummy check */
85         if ( ds->type <= SURFACE_BAD || ds->type >= NUM_SURFACE_TYPES || ds == NULL || ds->shaderInfo == NULL ) {
86                 return;
87         }
88
89         /* ydnar: rocking tek-fu celshading */
90         if ( ds->celShader != NULL ) {
91                 MakeCelSurface( ds, ds->celShader );
92         }
93
94         /* backsides stop here */
95         if ( ds->backSide ) {
96                 return;
97         }
98
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 ) );
102         }
103
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;
108         }
109 }
110
111
112
113 /*
114    CloneSurface()
115    clones a map drawsurface, using the specified shader
116  */
117
118 mapDrawSurface_t *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si ){
119         mapDrawSurface_t    *ds;
120
121
122         /* dummy check */
123         if ( src == NULL || si == NULL ) {
124                 return NULL;
125         }
126
127         /* allocate a new surface */
128         ds = AllocDrawSurface( src->type );
129         if ( ds == NULL ) {
130                 return NULL;
131         }
132
133         /* copy it */
134         memcpy( ds, src, sizeof( *ds ) );
135
136         /* destroy side reference */
137         ds->sideRef = NULL;
138
139         /* set shader */
140         ds->shaderInfo = si;
141
142         /* copy verts */
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 ) );
146         }
147
148         /* copy indexes */
149         if ( ds->numIndexes <= 0 ) {
150                 return ds;
151         }
152         ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
153         memcpy( ds->indexes, src->indexes, ds->numIndexes * sizeof( *ds->indexes ) );
154
155         /* return the surface */
156         return ds;
157 }
158
159
160
161 /*
162    MakeCelSurface() - ydnar
163    makes a copy of a surface, but specific to cel shading
164  */
165
166 mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si ){
167         mapDrawSurface_t    *ds;
168
169
170         /* dummy check */
171         if ( src == NULL || si == NULL ) {
172                 return NULL;
173         }
174
175         /* don't create cel surfaces for certain types of shaders */
176         if ( ( src->shaderInfo->compileFlags & C_TRANSLUCENT ) ||
177                  ( src->shaderInfo->compileFlags & C_SKY ) ) {
178                 return NULL;
179         }
180
181         /* make a copy */
182         ds = CloneSurface( src, si );
183         if ( ds == NULL ) {
184                 return NULL;
185         }
186
187         /* do some fixups for celshading */
188         ds->planar = qfalse;
189         ds->planeNum = -1;
190         ds->celShader = NULL; /* don't cel shade cels :P */
191
192         /* return the surface */
193         return ds;
194 }
195
196
197
198 /*
199    MakeSkyboxSurface() - ydnar
200    generates a skybox surface, viewable from everywhere there is sky
201  */
202
203 mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src ){
204         int i;
205         mapDrawSurface_t    *ds;
206
207
208         /* dummy check */
209         if ( src == NULL ) {
210                 return NULL;
211         }
212
213         /* make a copy */
214         ds = CloneSurface( src, src->shaderInfo );
215         if ( ds == NULL ) {
216                 return NULL;
217         }
218
219         /* set parent */
220         ds->parent = src;
221
222         /* scale the surface vertexes */
223         for ( i = 0; i < ds->numVerts; i++ )
224         {
225                 m4x4_transform_point( skyboxTransform, ds->verts[ i ].xyz );
226
227                 /* debug code */
228                 //%     bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 1 ] = 0;
229                 //%     bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 2 ] = 0;
230         }
231
232         /* so backface culling creep doesn't bork the surface */
233         VectorClear( ds->lightmapVecs[ 2 ] );
234
235         /* return the surface */
236         return ds;
237 }
238
239
240
241 /*
242    IsTriangleDegenerate
243    returns qtrue if all three points are colinear, backwards, or the triangle is just plain bogus
244  */
245
246 #define TINY_AREA   1.0f
247
248 qboolean IsTriangleDegenerate( bspDrawVert_t *points, int a, int b, int c ){
249         vec3_t v1, v2, v3;
250         float d;
251
252
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 );
258
259         /* assume all very small or backwards triangles will cause problems */
260         if ( d < TINY_AREA ) {
261                 return qtrue;
262         }
263
264         /* must be a good triangle */
265         return qfalse;
266 }
267
268
269
270 /*
271    ClearSurface() - ydnar
272    clears a surface and frees any allocated memory
273  */
274
275 void ClearSurface( mapDrawSurface_t *ds ){
276         ds->type = SURFACE_BAD;
277         ds->planar = qfalse;
278         ds->planeNum = -1;
279         ds->numVerts = 0;
280         if ( ds->verts != NULL ) {
281                 free( ds->verts );
282         }
283         ds->verts = NULL;
284         ds->numIndexes = 0;
285         if ( ds->indexes != NULL ) {
286                 free( ds->indexes );
287         }
288         ds->indexes = NULL;
289         numClearedSurfaces++;
290 }
291
292
293
294 /*
295    TidyEntitySurfaces() - ydnar
296    deletes all empty or bad surfaces from the surface list
297  */
298
299 void TidyEntitySurfaces( entity_t *e ){
300         int i, j, deleted;
301         mapDrawSurface_t    *out, *in = NULL;
302
303
304         /* note it */
305         Sys_FPrintf( SYS_VRB, "--- TidyEntitySurfaces ---\n" );
306
307         /* walk the surface list */
308         deleted = 0;
309         for ( i = e->firstDrawSurf, j = e->firstDrawSurf; j < numMapDrawSurfs; i++, j++ )
310         {
311                 /* get out surface */
312                 out = &mapDrawSurfs[ i ];
313
314                 /* walk the surface list again until a proper surface is found */
315                 for ( ; j < numMapDrawSurfs; j++ )
316                 {
317                         /* get in surface */
318                         in = &mapDrawSurfs[ j ];
319
320                         /* this surface ok? */
321                         if ( in->type == SURFACE_FLARE || in->type == SURFACE_SHADER ||
322                                  ( in->type != SURFACE_BAD && in->numVerts > 0 ) ) {
323                                 break;
324                         }
325
326                         /* nuke it */
327                         ClearSurface( in );
328                         deleted++;
329                 }
330
331                 /* copy if necessary */
332                 if ( i != j ) {
333                         memcpy( out, in, sizeof( mapDrawSurface_t ) );
334                 }
335         }
336
337         /* set the new number of drawsurfs */
338         numMapDrawSurfs = i;
339
340         /* emit some stats */
341         Sys_FPrintf( SYS_VRB, "%9d empty or malformed surfaces deleted\n", deleted );
342 }
343
344
345
346 /*
347    CalcSurfaceTextureRange() - ydnar
348    calculates the clamped texture range for a given surface, returns qtrue if it's within [-texRange,texRange]
349  */
350
351 qboolean CalcSurfaceTextureRange( mapDrawSurface_t *ds ){
352         int i, j, v, size[ 2 ];
353         float mins[ 2 ], maxs[ 2 ];
354
355
356         /* try to early out */
357         if ( ds->numVerts <= 0 ) {
358                 return qtrue;
359         }
360
361         /* walk the verts and determine min/max st values */
362         mins[ 0 ] = 999999;
363         mins[ 1 ] = 999999;
364         maxs[ 0 ] = -999999;
365         maxs[ 1 ] = -999999;
366         for ( i = 0; i < ds->numVerts; i++ )
367         {
368                 for ( j = 0; j < 2; j++ )
369                 {
370                         if ( ds->verts[ i ].st[ j ] < mins[ j ] ) {
371                                 mins[ j ] = ds->verts[ i ].st[ j ];
372                         }
373                         if ( ds->verts[ i ].st[ j ] > maxs[ j ] ) {
374                                 maxs[ j ] = ds->verts[ i ].st[ j ];
375                         }
376                 }
377         }
378
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 ] ) );
382
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++ )
391         {
392                 for ( j = 0; j < 2; j++ )
393                 {
394                         v = ( (float) ds->verts[ i ].st[ j ] + ds->bias[ j ] ) * size[ j ];
395                         if ( v < ds->texMins[ j ] ) {
396                                 ds->texMins[ j ] = v;
397                         }
398                         if ( v > ds->texMaxs[ j ] ) {
399                                 ds->texMaxs[ j ] = v;
400                         }
401                 }
402         }
403
404         /* calc ranges */
405         for ( j = 0; j < 2; j++ )
406                 ds->texRange[ j ] = ( ds->texMaxs[ j ] - ds->texMins[ j ] );
407
408         /* if range is zero, then assume unlimited precision */
409         if ( texRange == 0 ) {
410                 return qtrue;
411         }
412
413         /* within range? */
414         for ( j = 0; j < 2; j++ )
415         {
416                 if ( ds->texMins[ j ] < -texRange || ds->texMaxs[ j ] > texRange ) {
417                         return qfalse;
418                 }
419         }
420
421         /* within range */
422         return qtrue;
423 }
424
425
426
427 /*
428    CalcLightmapAxis() - ydnar
429    gives closed lightmap axis for a plane normal
430  */
431
432 qboolean CalcLightmapAxis( vec3_t normal, vec3_t axis ){
433         vec3_t absolute;
434
435
436         /* test */
437         if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && normal[ 2 ] == 0.0f ) {
438                 VectorClear( axis );
439                 return qfalse;
440         }
441
442         /* get absolute normal */
443         absolute[ 0 ] = fabs( normal[ 0 ] );
444         absolute[ 1 ] = fabs( normal[ 1 ] );
445         absolute[ 2 ] = fabs( normal[ 2 ] );
446
447         /* test and set */
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 );
451                 }
452                 else{
453                         VectorSet( axis, 0.0f, 0.0f, -1.0f );
454                 }
455         }
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 );
459                 }
460                 else{
461                         VectorSet( axis, -1.0f, 0.0f, 0.0f );
462                 }
463         }
464         else
465         {
466                 if ( normal[ 1 ] > 0.0f ) {
467                         VectorSet( axis, 0.0f, 1.0f, 0.0f );
468                 }
469                 else{
470                         VectorSet( axis, 0.0f, -1.0f, 0.0f );
471                 }
472         }
473
474         /* return ok */
475         return qtrue;
476 }
477
478
479
480 /*
481    ClassifySurfaces() - ydnar
482    fills out a bunch of info in the surfaces, including planar status, lightmap projection, and bounding box
483  */
484
485 #define PLANAR_EPSILON  0.5f    //% 0.126f 0.25f
486
487 void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){
488         int i, bestAxis;
489         float dist;
490         vec4_t plane;
491         shaderInfo_t        *si;
492         static vec3_t axii[ 6 ] =
493         {
494                 { 0, 0, -1 },
495                 { 0, 0, 1 },
496                 { -1, 0, 0 },
497                 { 1, 0, 0 },
498                 { 0, -1, 0 },
499                 { 0, 1, 0 }
500         };
501
502
503         /* walk the list of surfaces */
504         for ( ; numSurfs > 0; numSurfs--, ds++ )
505         {
506                 /* ignore bogus (or flare) surfaces */
507                 if ( ds->type == SURFACE_BAD || ds->numVerts <= 0 ) {
508                         continue;
509                 }
510
511                 /* get shader */
512                 si = ds->shaderInfo;
513
514                 /* -----------------------------------------------------------------
515                    force meta if vertex count is too high or shader requires it
516                    ----------------------------------------------------------------- */
517
518                 if ( ds->type != SURFACE_PATCH && ds->type != SURFACE_FACE ) {
519                         if ( ds->numVerts > SHADER_MAX_VERTEXES ) {
520                                 ds->type = SURFACE_FORCED_META;
521                         }
522                 }
523
524                 /* -----------------------------------------------------------------
525                    plane and bounding box classification
526                    ----------------------------------------------------------------- */
527
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 );
532
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;
537                 }
538
539                 /* construct one from the first vert with a valid normal */
540                 else
541                 {
542                         VectorClear( plane );
543                         plane[ 3 ] = 0.0f;
544                         for ( i = 0; i < ds->numVerts; i++ )
545                         {
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 );
549                                         break;
550                                 }
551                         }
552                 }
553
554                 /* test for bogus plane */
555                 if ( VectorLength( plane ) <= 0.0f ) {
556                         ds->planar = qfalse;
557                         ds->planeNum = -1;
558                 }
559                 else
560                 {
561                         /* determine if surface is planar */
562                         ds->planar = qtrue;
563
564                         /* test each vert */
565                         for ( i = 0; i < ds->numVerts; i++ )
566                         {
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 )
571                                         //%     {
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;
574                                         //%     }
575                                         ds->planar = qfalse;
576                                         break;
577                                 }
578                         }
579                 }
580
581                 /* find map plane if necessary */
582                 if ( ds->planar ) {
583                         if ( ds->planeNum < 0 ) {
584                                 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &ds->verts[ 0 ].xyz );
585                         }
586                         VectorCopy( plane, ds->lightmapVecs[ 2 ] );
587                 }
588                 else
589                 {
590                         ds->planeNum = -1;
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 );
594                 }
595
596                 /* -----------------------------------------------------------------
597                    lightmap bounds and axis projection
598                    ----------------------------------------------------------------- */
599
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 ] );
604                         ds->sampleSize = 0;
605                         continue;
606                 }
607
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 );
611                 }
612                 else if ( ds->type == SURFACE_FORCED_META ) {
613                         VectorClear( ds->lightmapAxis );
614                 }
615                 else if ( ds->planar ) {
616                         CalcLightmapAxis( plane, ds->lightmapAxis );
617                 }
618                 else
619                 {
620                         /* find best lightmap axis */
621                         for ( bestAxis = 0; bestAxis < 6; bestAxis++ )
622                         {
623                                 for ( i = 0; i < ds->numVerts && bestAxis < 6; i++ )
624                                 {
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 */
629                                                 break;
630                                         }
631                                 }
632
633                                 if ( i == ds->numVerts ) {
634                                         break;
635                                 }
636                         }
637
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 );
643                         }
644
645                         /* debug code */
646                         //% if( ds->type == SURFACE_PATCH )
647                         //%     Sys_Printf( "Failed to map axis %d onto patch\n", bestAxis );
648                 }
649
650                 /* calculate lightmap sample size */
651                 if ( ds->shaderInfo->lightmapSampleSize > 0 ) { /* shader value overrides every other */
652                         ds->sampleSize = ds->shaderInfo->lightmapSampleSize;
653                 }
654                 else if ( ds->sampleSize <= 0 ) { /* may contain the entity asigned value */
655                         ds->sampleSize = sampleSize; /* otherwise use global default */
656
657                 }
658                 if ( ds->lightmapScale > 0.0f ) { /* apply surface lightmap scaling factor */
659                         ds->sampleSize = ds->lightmapScale * (float)ds->sampleSize;
660                         ds->lightmapScale = 0; /* applied */
661                 }
662
663                 if ( ds->sampleSize < minSampleSize ) {
664                         ds->sampleSize = minSampleSize;
665                 }
666
667                 if ( ds->sampleSize < 1 ) {
668                         ds->sampleSize = 1;
669                 }
670
671                 if ( ds->sampleSize > 16384 ) { /* powers of 2 are preferred */
672                         ds->sampleSize = 16384;
673                 }
674         }
675 }
676
677
678
679 /*
680    ClassifyEntitySurfaces() - ydnar
681    classifies all surfaces in an entity
682  */
683
684 void ClassifyEntitySurfaces( entity_t *e ){
685         int i;
686
687
688         /* note it */
689         Sys_FPrintf( SYS_VRB, "--- ClassifyEntitySurfaces ---\n" );
690
691         /* walk the surface list */
692         for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
693         {
694                 FinishSurface( &mapDrawSurfs[ i ] );
695                 ClassifySurfaces( 1, &mapDrawSurfs[ i ] );
696         }
697
698         /* tidy things up */
699         TidyEntitySurfaces( e );
700 }
701
702
703
704 /*
705    GetShaderIndexForPoint() - ydnar
706    for shader-indexed surfaces (terrain), find a matching index from the indexmap
707  */
708
709 byte GetShaderIndexForPoint( indexMap_t *im, vec3_t eMins, vec3_t eMaxs, vec3_t point ){
710         int i, x, y;
711         float s, t;
712         vec3_t mins, maxs, size;
713
714
715         /* early out if no indexmap */
716         if ( im == NULL ) {
717                 return 0;
718         }
719
720         /* this code is really broken */
721         #if 0
722         /* legacy precision fudges for terrain */
723         for ( i = 0; i < 3; i++ )
724         {
725                 mins[ i ] = floor( eMins[ i ] + 0.1 );
726                 maxs[ i ] = floor( eMaxs[ i ] + 0.1 );
727                 size[ i ] = maxs[ i ] - mins[ i ];
728         }
729
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 ];
733         if ( s < 0.0f ) {
734                 s = 0.0f;
735         }
736         else if ( s > 1.0f ) {
737                 s = 1.0f;
738         }
739         if ( t < 0.0f ) {
740                 t = 0.0f;
741         }
742         else if ( t > 1.0f ) {
743                 t = 1.0f;
744         }
745
746         /* make xy */
747         x = ( im->w - 1 ) * s;
748         y = ( im->h - 1 ) * t;
749         #else
750         /* get size */
751         for ( i = 0; i < 3; i++ )
752         {
753                 mins[ i ] = eMins[ i ];
754                 maxs[ i ] = eMaxs[ i ];
755                 size[ i ] = maxs[ i ] - mins[ i ];
756         }
757
758         /* calc st */
759         s = ( point[ 0 ] - mins[ 0 ] ) / size[ 0 ];
760         t = ( maxs[ 1 ] - point[ 1 ] ) / size[ 1 ];
761
762         /* calc xy */
763         x = s * im->w;
764         y = t * im->h;
765         if ( x < 0 ) {
766                 x = 0;
767         }
768         else if ( x > ( im->w - 1 ) ) {
769                 x = ( im->w - 1 );
770         }
771         if ( y < 0 ) {
772                 y = 0;
773         }
774         else if ( y > ( im->h - 1 ) ) {
775                 y = ( im->h - 1 );
776         }
777         #endif
778
779         /* return index */
780         return im->pixels[ y * im->w + x ];
781 }
782
783
784
785 /*
786    GetIndexedShader() - ydnar
787    for a given set of indexes and an indexmap, get a shader and set the vertex alpha in-place
788    this combines a couple different functions from terrain.c
789  */
790
791 shaderInfo_t *GetIndexedShader( shaderInfo_t *parent, indexMap_t *im, int numPoints, byte *shaderIndexes ){
792         int i;
793         byte minShaderIndex, maxShaderIndex;
794         char shader[ MAX_QPATH ];
795         shaderInfo_t    *si;
796
797
798         /* early out if bad data */
799         if ( im == NULL || numPoints <= 0 || shaderIndexes == NULL ) {
800                 return ShaderInfoForShader( "default" );
801         }
802
803         /* determine min/max index */
804         minShaderIndex = 255;
805         maxShaderIndex = 0;
806         for ( i = 0; i < numPoints; i++ )
807         {
808                 if ( shaderIndexes[ i ] < minShaderIndex ) {
809                         minShaderIndex = shaderIndexes[ i ];
810                 }
811                 if ( shaderIndexes[ i ] > maxShaderIndex ) {
812                         maxShaderIndex = shaderIndexes[ i ];
813                 }
814         }
815
816         /* set alpha inline */
817         for ( i = 0; i < numPoints; i++ )
818         {
819                 /* straight rip from terrain.c */
820                 if ( shaderIndexes[ i ] < maxShaderIndex ) {
821                         shaderIndexes[ i ] = 0;
822                 }
823                 else{
824                         shaderIndexes[ i ] = 255;
825                 }
826         }
827
828         /* make a shader name */
829         if ( minShaderIndex == maxShaderIndex ) {
830                 sprintf( shader, "textures/%s_%d", im->shader, maxShaderIndex );
831         }
832         else{
833                 sprintf( shader, "textures/%s_%dto%d", im->shader, minShaderIndex, maxShaderIndex );
834         }
835
836         /* get the shader */
837         si = ShaderInfoForShader( shader );
838
839         /* inherit a few things from parent shader */
840         if ( parent->globalTexture ) {
841                 si->globalTexture = qtrue;
842         }
843         if ( parent->forceMeta ) {
844                 si->forceMeta = qtrue;
845         }
846         if ( parent->nonplanar ) {
847                 si->nonplanar = qtrue;
848         }
849         if ( si->shadeAngleDegrees == 0.0 ) {
850                 si->shadeAngleDegrees = parent->shadeAngleDegrees;
851         }
852         if ( parent->tcGen && si->tcGen == qfalse ) {
853                 /* set xy texture projection */
854                 si->tcGen = qtrue;
855                 VectorCopy( parent->vecs[ 0 ], si->vecs[ 0 ] );
856                 VectorCopy( parent->vecs[ 1 ], si->vecs[ 1 ] );
857         }
858         if ( VectorLength( parent->lightmapAxis ) > 0.0f && VectorLength( si->lightmapAxis ) <= 0.0f ) {
859                 /* set lightmap projection axis */
860                 VectorCopy( parent->lightmapAxis, si->lightmapAxis );
861         }
862
863         /* return the shader */
864         return si;
865 }
866
867
868
869
870 /*
871    DrawSurfaceForSide()
872    creates a SURF_FACE drawsurface from a given brush side and winding
873  */
874
875 #define SNAP_FLOAT_TO_INT   8
876 #define SNAP_INT_TO_FLOAT   ( 1.0 / SNAP_FLOAT_TO_INT )
877
878 mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w ){
879         int i, j, k;
880         mapDrawSurface_t    *ds;
881         shaderInfo_t        *si, *parent;
882         bspDrawVert_t       *dv;
883         vec3_t texX, texY;
884         vec_t x, y;
885         vec3_t vTranslated;
886         qboolean indexed;
887         byte shaderIndexes[ 256 ];
888         float offsets[ 256 ];
889         char tempShader[ MAX_QPATH ];
890
891
892         /* ydnar: don't make a drawsurf for culled sides */
893         if ( s->culled ) {
894                 return NULL;
895         }
896
897         /* range check */
898         if ( w->numpoints > MAX_POINTS_ON_WINDING ) {
899                 Error( "DrawSurfaceForSide: w->numpoints = %d (> %d)", w->numpoints, MAX_POINTS_ON_WINDING );
900         }
901
902         /* get shader */
903         si = s->shaderInfo;
904
905         /* ydnar: gs mods: check for indexed shader */
906         if ( si->indexed && b->im != NULL ) {
907                 /* indexed */
908                 indexed = qtrue;
909
910                 /* get shader indexes for each point */
911                 for ( i = 0; i < w->numpoints; i++ )
912                 {
913                         shaderIndexes[ i ] = GetShaderIndexForPoint( b->im, b->eMins, b->eMaxs, w->p[ i ] );
914                         offsets[ i ] = b->im->offsets[ shaderIndexes[ i ] ];
915                         //%     Sys_Printf( "%f ", offsets[ i ] );
916                 }
917
918                 /* get matching shader and set alpha */
919                 parent = si;
920                 si = GetIndexedShader( parent, b->im, w->numpoints, shaderIndexes );
921         }
922         else{
923                 indexed = qfalse;
924         }
925
926         /* ydnar: sky hack/fix for GL_CLAMP borders on ati cards */
927         if ( skyFixHack && si->skyParmsImageBase[ 0 ] != '\0' ) {
928                 //%     Sys_FPrintf( SYS_VRB, "Enabling sky hack for shader %s using env %s\n", si->shader, si->skyParmsImageBase );
929                 sprintf( tempShader, "%s_lf", si->skyParmsImageBase );
930                 DrawSurfaceForShader( tempShader );
931                 sprintf( tempShader, "%s_rt", si->skyParmsImageBase );
932                 DrawSurfaceForShader( tempShader );
933                 sprintf( tempShader, "%s_ft", si->skyParmsImageBase );
934                 DrawSurfaceForShader( tempShader );
935                 sprintf( tempShader, "%s_bk", si->skyParmsImageBase );
936                 DrawSurfaceForShader( tempShader );
937                 sprintf( tempShader, "%s_up", si->skyParmsImageBase );
938                 DrawSurfaceForShader( tempShader );
939                 sprintf( tempShader, "%s_dn", si->skyParmsImageBase );
940                 DrawSurfaceForShader( tempShader );
941         }
942
943         /* ydnar: gs mods */
944         ds = AllocDrawSurface( SURFACE_FACE );
945         ds->entityNum = b->entityNum;
946         ds->castShadows = b->castShadows;
947         ds->recvShadows = b->recvShadows;
948
949         ds->planar = qtrue;
950         ds->planeNum = s->planenum;
951         VectorCopy( mapplanes[ s->planenum ].normal, ds->lightmapVecs[ 2 ] );
952
953         ds->shaderInfo = si;
954         ds->mapBrush = b;
955         ds->sideRef = AllocSideRef( s, NULL );
956         ds->fogNum = -1;
957         ds->sampleSize = b->lightmapSampleSize;
958         ds->lightmapScale = b->lightmapScale;
959         ds->numVerts = w->numpoints;
960         ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
961         memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
962
963         /* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
964         ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY );
965
966         /* create the vertexes */
967         for ( j = 0; j < w->numpoints; j++ )
968         {
969                 /* get the drawvert */
970                 dv = ds->verts + j;
971
972                 /* copy xyz and do potential z offset */
973                 VectorCopy( w->p[ j ], dv->xyz );
974                 if ( indexed ) {
975                         dv->xyz[ 2 ] += offsets[ j ];
976                 }
977
978                 /* round the xyz to a given precision and translate by origin */
979                 for ( i = 0 ; i < 3 ; i++ )
980                         dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5f );
981                 VectorAdd( dv->xyz, e->origin, vTranslated );
982
983                 /* ydnar: tek-fu celshading support for flat shaded shit */
984                 if ( flat ) {
985                         dv->st[ 0 ] = si->stFlat[ 0 ];
986                         dv->st[ 1 ] = si->stFlat[ 1 ];
987                 }
988
989                 /* ydnar: gs mods: added support for explicit shader texcoord generation */
990                 else if ( si->tcGen ) {
991                         dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
992                         dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
993                 }
994
995                 /* old quake-style texturing */
996                 else if ( g_bBrushPrimit == BPRIMIT_OLDBRUSHES ) {
997                         /* nearest-axial projection */
998                         dv->st[ 0 ] = s->vecs[ 0 ][ 3 ] + DotProduct( s->vecs[ 0 ], vTranslated );
999                         dv->st[ 1 ] = s->vecs[ 1 ][ 3 ] + DotProduct( s->vecs[ 1 ], vTranslated );
1000                         dv->st[ 0 ] /= si->shaderWidth;
1001                         dv->st[ 1 ] /= si->shaderHeight;
1002                 }
1003
1004                 /* brush primitive texturing */
1005                 else
1006                 {
1007                         /* calculate texture s/t from brush primitive texture matrix */
1008                         x = DotProduct( vTranslated, texX );
1009                         y = DotProduct( vTranslated, texY );
1010                         dv->st[ 0 ] = s->texMat[ 0 ][ 0 ] * x + s->texMat[ 0 ][ 1 ] * y + s->texMat[ 0 ][ 2 ];
1011                         dv->st[ 1 ] = s->texMat[ 1 ][ 0 ] * x + s->texMat[ 1 ][ 1 ] * y + s->texMat[ 1 ][ 2 ];
1012                 }
1013
1014                 /* copy normal */
1015                 VectorCopy( mapplanes[ s->planenum ].normal, dv->normal );
1016
1017                 /* ydnar: set color */
1018                 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
1019                 {
1020                         dv->color[ k ][ 0 ] = 255;
1021                         dv->color[ k ][ 1 ] = 255;
1022                         dv->color[ k ][ 2 ] = 255;
1023
1024                         /* ydnar: gs mods: handle indexed shader blending */
1025                         dv->color[ k ][ 3 ] = ( indexed ? shaderIndexes[ j ] : 255 );
1026                 }
1027         }
1028
1029         /* set cel shader */
1030         ds->celShader = b->celShader;
1031
1032         /* set shade angle */
1033         if ( b->shadeAngleDegrees > 0.0f ) {
1034                 ds->shadeAngleDegrees = b->shadeAngleDegrees;
1035         }
1036
1037         /* ydnar: gs mods: moved st biasing elsewhere */
1038         return ds;
1039 }
1040
1041
1042
1043 /*
1044    DrawSurfaceForMesh()
1045    moved here from patch.c
1046  */
1047
1048 #define YDNAR_NORMAL_EPSILON 0.50f
1049
1050 qboolean VectorCompareExt( vec3_t n1, vec3_t n2, float epsilon ){
1051         int i;
1052
1053
1054         /* test */
1055         for ( i = 0; i < 3; i++ )
1056                 if ( fabs( n1[ i ] - n2[ i ] ) > epsilon ) {
1057                         return qfalse;
1058                 }
1059         return qtrue;
1060 }
1061
1062 mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh ){
1063         int i, k, numVerts;
1064         vec4_t plane;
1065         qboolean planar;
1066         float dist;
1067         mapDrawSurface_t    *ds;
1068         shaderInfo_t        *si, *parent;
1069         bspDrawVert_t       *dv;
1070         vec3_t vTranslated;
1071         mesh_t              *copy;
1072         qboolean indexed;
1073         byte shaderIndexes[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1074         float offsets[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ];
1075
1076
1077         /* get mesh and shader shader */
1078         if ( mesh == NULL ) {
1079                 mesh = &p->mesh;
1080         }
1081         si = p->shaderInfo;
1082         if ( mesh == NULL || si == NULL ) {
1083                 return NULL;
1084         }
1085
1086         /* get vertex count */
1087         numVerts = mesh->width * mesh->height;
1088
1089         /* to make valid normals for patches with degenerate edges,
1090            we need to make a copy of the mesh and put the aproximating
1091            points onto the curve */
1092
1093         /* create a copy of the mesh */
1094         copy = CopyMesh( mesh );
1095
1096         /* store off the original (potentially bad) normals */
1097         MakeMeshNormals( *copy );
1098         for ( i = 0; i < numVerts; i++ )
1099                 VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1100
1101         /* put the mesh on the curve */
1102         PutMeshOnCurve( *copy );
1103
1104         /* find new normals (to take into account degenerate/flipped edges */
1105         MakeMeshNormals( *copy );
1106         for ( i = 0; i < numVerts; i++ )
1107         {
1108                 /* ydnar: only copy normals that are significantly different from the originals */
1109                 if ( DotProduct( copy->verts[ i ].normal, mesh->verts[ i ].normal ) < 0.75f ) {
1110                         VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal );
1111                 }
1112         }
1113
1114         /* free the old mesh */
1115         FreeMesh( copy );
1116
1117         /* ydnar: gs mods: check for indexed shader */
1118         if ( si->indexed && p->im != NULL ) {
1119                 /* indexed */
1120                 indexed = qtrue;
1121
1122                 /* get shader indexes for each point */
1123                 for ( i = 0; i < numVerts; i++ )
1124                 {
1125                         shaderIndexes[ i ] = GetShaderIndexForPoint( p->im, p->eMins, p->eMaxs, mesh->verts[ i ].xyz );
1126                         offsets[ i ] = p->im->offsets[ shaderIndexes[ i ] ];
1127                 }
1128
1129                 /* get matching shader and set alpha */
1130                 parent = si;
1131                 si = GetIndexedShader( parent, p->im, numVerts, shaderIndexes );
1132         }
1133         else{
1134                 indexed = qfalse;
1135         }
1136
1137
1138         /* ydnar: gs mods */
1139         ds = AllocDrawSurface( SURFACE_PATCH );
1140         ds->entityNum = p->entityNum;
1141         ds->castShadows = p->castShadows;
1142         ds->recvShadows = p->recvShadows;
1143
1144         ds->shaderInfo = si;
1145         ds->mapMesh = p;
1146         ds->sampleSize = p->lightmapSampleSize;
1147         ds->lightmapScale = p->lightmapScale;   /* ydnar */
1148         ds->patchWidth = mesh->width;
1149         ds->patchHeight = mesh->height;
1150         ds->numVerts = ds->patchWidth * ds->patchHeight;
1151         ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
1152         memcpy( ds->verts, mesh->verts, ds->numVerts * sizeof( *ds->verts ) );
1153
1154         ds->fogNum = -1;
1155         ds->planeNum = -1;
1156
1157         ds->longestCurve = p->longestCurve;
1158         ds->maxIterations = p->maxIterations;
1159
1160         /* construct a plane from the first vert */
1161         VectorCopy( mesh->verts[ 0 ].normal, plane );
1162         plane[ 3 ] = DotProduct( mesh->verts[ 0 ].xyz, plane );
1163         planar = qtrue;
1164
1165         /* spew forth errors */
1166         if ( VectorLength( plane ) < 0.001f ) {
1167                 Sys_Printf( "DrawSurfaceForMesh: bogus plane\n" );
1168         }
1169
1170         /* test each vert */
1171         for ( i = 1; i < ds->numVerts && planar; i++ )
1172         {
1173                 /* normal test */
1174                 if ( VectorCompare( plane, mesh->verts[ i ].normal ) == qfalse ) {
1175                         planar = qfalse;
1176                 }
1177
1178                 /* point-plane test */
1179                 dist = DotProduct( mesh->verts[ i ].xyz, plane ) - plane[ 3 ];
1180                 if ( fabs( dist ) > EQUAL_EPSILON ) {
1181                         planar = qfalse;
1182                 }
1183         }
1184
1185         /* add a map plane */
1186         if ( planar ) {
1187                 /* make a map plane */
1188                 ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &mesh->verts[ 0 ].xyz );
1189                 VectorCopy( plane, ds->lightmapVecs[ 2 ] );
1190
1191                 /* push this normal to all verts (ydnar 2003-02-14: bad idea, small patches get screwed up) */
1192                 for ( i = 0; i < ds->numVerts; i++ )
1193                         VectorCopy( plane, ds->verts[ i ].normal );
1194         }
1195
1196         /* walk the verts to do special stuff */
1197         for ( i = 0; i < ds->numVerts; i++ )
1198         {
1199                 /* get the drawvert */
1200                 dv = &ds->verts[ i ];
1201
1202                 /* ydnar: tek-fu celshading support for flat shaded shit */
1203                 if ( flat ) {
1204                         dv->st[ 0 ] = si->stFlat[ 0 ];
1205                         dv->st[ 1 ] = si->stFlat[ 1 ];
1206                 }
1207
1208                 /* ydnar: gs mods: added support for explicit shader texcoord generation */
1209                 else if ( si->tcGen ) {
1210                         /* translate by origin and project the texture */
1211                         VectorAdd( dv->xyz, e->origin, vTranslated );
1212                         dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated );
1213                         dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated );
1214                 }
1215
1216                 /* ydnar: set color */
1217                 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
1218                 {
1219                         dv->color[ k ][ 0 ] = 255;
1220                         dv->color[ k ][ 1 ] = 255;
1221                         dv->color[ k ][ 2 ] = 255;
1222
1223                         /* ydnar: gs mods: handle indexed shader blending */
1224                         dv->color[ k ][ 3 ] = ( indexed ? shaderIndexes[ i ] : 255 );
1225                 }
1226
1227                 /* ydnar: offset */
1228                 if ( indexed ) {
1229                         dv->xyz[ 2 ] += offsets[ i ];
1230                 }
1231         }
1232
1233         /* set cel shader */
1234         ds->celShader = p->celShader;
1235
1236         /* return the drawsurface */
1237         return ds;
1238 }
1239
1240
1241
1242 /*
1243    DrawSurfaceForFlare() - ydnar
1244    creates a flare draw surface
1245  */
1246
1247 mapDrawSurface_t *DrawSurfaceForFlare( int entNum, vec3_t origin, vec3_t normal, vec3_t color, const char *flareShader, int lightStyle ){
1248         mapDrawSurface_t    *ds;
1249
1250
1251         /* emit flares? */
1252         if ( emitFlares == qfalse ) {
1253                 return NULL;
1254         }
1255
1256         /* allocate drawsurface */
1257         ds = AllocDrawSurface( SURFACE_FLARE );
1258         ds->entityNum = entNum;
1259
1260         /* set it up */
1261         if ( flareShader != NULL && flareShader[ 0 ] != '\0' ) {
1262                 ds->shaderInfo = ShaderInfoForShader( flareShader );
1263         }
1264         else{
1265                 ds->shaderInfo = ShaderInfoForShader( game->flareShader );
1266         }
1267         if ( origin != NULL ) {
1268                 VectorCopy( origin, ds->lightmapOrigin );
1269         }
1270         if ( normal != NULL ) {
1271                 VectorCopy( normal, ds->lightmapVecs[ 2 ] );
1272         }
1273         if ( color != NULL ) {
1274                 VectorCopy( color, ds->lightmapVecs[ 0 ] );
1275         }
1276
1277         /* store light style */
1278         ds->lightStyle = lightStyle;
1279         if ( ds->lightStyle < 0 || ds->lightStyle >= LS_NONE ) {
1280                 ds->lightStyle = LS_NORMAL;
1281         }
1282
1283         /* fixme: fog */
1284
1285         /* return to sender */
1286         return ds;
1287 }
1288
1289
1290
1291 /*
1292    DrawSurfaceForShader() - ydnar
1293    creates a bogus surface to forcing the game to load a shader
1294  */
1295
1296 mapDrawSurface_t *DrawSurfaceForShader( char *shader ){
1297         int i;
1298         shaderInfo_t        *si;
1299         mapDrawSurface_t    *ds;
1300
1301
1302         /* get shader */
1303         si = ShaderInfoForShader( shader );
1304
1305         /* find existing surface */
1306         for ( i = 0; i < numMapDrawSurfs; i++ )
1307         {
1308                 /* get surface */
1309                 ds = &mapDrawSurfs[ i ];
1310
1311                 /* check it */
1312                 if ( ds->shaderInfo == si ) {
1313                         return ds;
1314                 }
1315         }
1316
1317         /* create a new surface */
1318         ds = AllocDrawSurface( SURFACE_SHADER );
1319         ds->entityNum = 0;
1320         ds->shaderInfo = ShaderInfoForShader( shader );
1321
1322         /* return to sender */
1323         return ds;
1324 }
1325
1326
1327
1328 /*
1329    AddSurfaceFlare() - ydnar
1330    creates flares (coronas) centered on surfaces
1331  */
1332
1333 static void AddSurfaceFlare( mapDrawSurface_t *ds, vec3_t entityOrigin ){
1334         vec3_t origin;
1335         int i;
1336
1337
1338         /* find centroid */
1339         VectorClear( origin );
1340         for ( i = 0; i < ds->numVerts; i++ )
1341                 VectorAdd( origin, ds->verts[ i ].xyz, origin );
1342         VectorScale( origin, ( 1.0f / ds->numVerts ), origin );
1343         if ( entityOrigin != NULL ) {
1344                 VectorAdd( origin, entityOrigin, origin );
1345         }
1346
1347         /* push origin off surface a bit */
1348         VectorMA( origin, 2.0f,  ds->lightmapVecs[ 2 ], origin );
1349
1350         /* create the drawsurface */
1351         DrawSurfaceForFlare( ds->entityNum, origin, ds->lightmapVecs[ 2 ], ds->shaderInfo->color, ds->shaderInfo->flareShader, ds->shaderInfo->lightStyle );
1352 }
1353
1354
1355
1356 /*
1357    SubdivideFace()
1358    subdivides a face surface until it is smaller than the specified size (subdivisions)
1359  */
1360
1361 static void SubdivideFace_r( entity_t *e, brush_t *brush, side_t *side, winding_t *w, int fogNum, float subdivisions ){
1362         int i;
1363         int axis;
1364         vec3_t bounds[ 2 ];
1365         const float epsilon = 0.1;
1366         int subFloor, subCeil;
1367         winding_t           *frontWinding, *backWinding;
1368         mapDrawSurface_t    *ds;
1369
1370
1371         /* dummy check */
1372         if ( w == NULL ) {
1373                 return;
1374         }
1375         if ( w->numpoints < 3 ) {
1376                 Error( "SubdivideFace_r: Bad w->numpoints (%d < 3)", w->numpoints );
1377         }
1378
1379         /* determine surface bounds */
1380         ClearBounds( bounds[ 0 ], bounds[ 1 ] );
1381         for ( i = 0; i < w->numpoints; i++ )
1382                 AddPointToBounds( w->p[ i ], bounds[ 0 ], bounds[ 1 ] );
1383
1384         /* split the face */
1385         for ( axis = 0; axis < 3; axis++ )
1386         {
1387                 vec3_t planePoint = { 0, 0, 0 };
1388                 vec3_t planeNormal = { 0, 0, 0 };
1389                 float d;
1390
1391
1392                 /* create an axial clipping plane */
1393                 subFloor = floor( bounds[ 0 ][ axis ] / subdivisions ) * subdivisions;
1394                 subCeil = ceil( bounds[ 1 ][ axis ] / subdivisions ) * subdivisions;
1395                 planePoint[ axis ] = subFloor + subdivisions;
1396                 planeNormal[ axis ] = -1;
1397                 d = DotProduct( planePoint, planeNormal );
1398
1399                 /* subdivide if necessary */
1400                 if ( ( subCeil - subFloor ) > subdivisions ) {
1401                         /* clip the winding */
1402                         ClipWindingEpsilon( w, planeNormal, d, epsilon, &frontWinding, &backWinding ); /* not strict; we assume we always keep a winding */
1403
1404                         /* the clip may not produce two polygons if it was epsilon close */
1405                         if ( frontWinding == NULL ) {
1406                                 w = backWinding;
1407                         }
1408                         else if ( backWinding == NULL ) {
1409                                 w = frontWinding;
1410                         }
1411                         else
1412                         {
1413                                 SubdivideFace_r( e, brush, side, frontWinding, fogNum, subdivisions );
1414                                 SubdivideFace_r( e, brush, side, backWinding, fogNum, subdivisions );
1415                                 return;
1416                         }
1417                 }
1418         }
1419
1420         /* create a face surface */
1421         ds = DrawSurfaceForSide( e, brush, side, w );
1422
1423         /* set correct fog num */
1424         ds->fogNum = fogNum;
1425 }
1426
1427
1428
1429 /*
1430    SubdivideFaceSurfaces()
1431    chop up brush face surfaces that have subdivision attributes
1432    ydnar: and subdivide surfaces that exceed specified texture coordinate range
1433  */
1434
1435 void SubdivideFaceSurfaces( entity_t *e, tree_t *tree ){
1436         int i, j, numBaseDrawSurfs, fogNum;
1437         mapDrawSurface_t    *ds;
1438         brush_t             *brush;
1439         side_t              *side;
1440         shaderInfo_t        *si;
1441         winding_t           *w;
1442         float range, size, subdivisions, s2;
1443
1444
1445         /* note it */
1446         Sys_FPrintf( SYS_VRB, "--- SubdivideFaceSurfaces ---\n" );
1447
1448         /* walk the list of surfaces */
1449         numBaseDrawSurfs = numMapDrawSurfs;
1450         for ( i = e->firstDrawSurf; i < numBaseDrawSurfs; i++ )
1451         {
1452                 /* get surface */
1453                 ds = &mapDrawSurfs[ i ];
1454
1455                 /* only subdivide brush sides */
1456                 if ( ds->type != SURFACE_FACE || ds->mapBrush == NULL || ds->sideRef == NULL || ds->sideRef->side == NULL ) {
1457                         continue;
1458                 }
1459
1460                 /* get bits */
1461                 brush = ds->mapBrush;
1462                 side = ds->sideRef->side;
1463
1464                 /* check subdivision for shader */
1465                 si = side->shaderInfo;
1466                 if ( si == NULL ) {
1467                         continue;
1468                 }
1469
1470                 /* ydnar: don't subdivide sky surfaces */
1471                 if ( si->compileFlags & C_SKY ) {
1472                         continue;
1473                 }
1474
1475                 /* do texture coordinate range check */
1476                 ClassifySurfaces( 1, ds );
1477                 if ( CalcSurfaceTextureRange( ds ) == qfalse ) {
1478                         /* calculate subdivisions texture range (this code is shit) */
1479                         range = ( ds->texRange[ 0 ] > ds->texRange[ 1 ] ? ds->texRange[ 0 ] : ds->texRange[ 1 ] );
1480                         size = ds->maxs[ 0 ] - ds->mins[ 0 ];
1481                         for ( j = 1; j < 3; j++ )
1482                                 if ( ( ds->maxs[ j ] - ds->mins[ j ] ) > size ) {
1483                                         size = ds->maxs[ j ] - ds->mins[ j ];
1484                                 }
1485                         subdivisions = ( size / range ) * texRange;
1486                         subdivisions = ceil( subdivisions / 2 ) * 2;
1487                         for ( j = 1; j < 8; j++ )
1488                         {
1489                                 s2 = ceil( (float) texRange / j );
1490                                 if ( fabs( subdivisions - s2 ) <= 4.0 ) {
1491                                         subdivisions = s2;
1492                                         break;
1493                                 }
1494                         }
1495                 }
1496                 else{
1497                         subdivisions = si->subdivisions;
1498                 }
1499
1500                 /* get subdivisions from shader */
1501                 if ( si->subdivisions > 0 && si->subdivisions < subdivisions ) {
1502                         subdivisions = si->subdivisions;
1503                 }
1504                 if ( subdivisions < 1.0f ) {
1505                         continue;
1506                 }
1507
1508                 /* preserve fog num */
1509                 fogNum = ds->fogNum;
1510
1511                 /* make a winding and free the surface */
1512                 w = WindingFromDrawSurf( ds );
1513                 ClearSurface( ds );
1514
1515                 /* subdivide it */
1516                 SubdivideFace_r( e, brush, side, w, fogNum, subdivisions );
1517         }
1518 }
1519
1520
1521
1522 /*
1523    ====================
1524    ClipSideIntoTree_r
1525
1526    Adds non-opaque leaf fragments to the convex hull
1527    ====================
1528  */
1529
1530 void ClipSideIntoTree_r( winding_t *w, side_t *side, node_t *node ){
1531         plane_t         *plane;
1532         winding_t       *front, *back;
1533
1534         if ( !w ) {
1535                 return;
1536         }
1537
1538         if ( node->planenum != PLANENUM_LEAF ) {
1539                 if ( side->planenum == node->planenum ) {
1540                         ClipSideIntoTree_r( w, side, node->children[0] );
1541                         return;
1542                 }
1543                 if ( side->planenum == ( node->planenum ^ 1 ) ) {
1544                         ClipSideIntoTree_r( w, side, node->children[1] );
1545                         return;
1546                 }
1547
1548                 plane = &mapplanes[ node->planenum ];
1549                 ClipWindingEpsilonStrict( w, plane->normal, plane->dist,
1550                                                                   ON_EPSILON, &front, &back ); /* strict, we handle the "winding disappeared" case */
1551                 if ( !front && !back ) {
1552                         /* in doubt, register it in both nodes */
1553                         front = CopyWinding( w );
1554                         back = CopyWinding( w );
1555                 }
1556                 FreeWinding( w );
1557
1558                 ClipSideIntoTree_r( front, side, node->children[0] );
1559                 ClipSideIntoTree_r( back, side, node->children[1] );
1560
1561                 return;
1562         }
1563
1564         // if opaque leaf, don't add
1565         if ( !node->opaque ) {
1566                 AddWindingToConvexHull( w, &side->visibleHull, mapplanes[ side->planenum ].normal );
1567         }
1568
1569         FreeWinding( w );
1570         return;
1571 }
1572
1573
1574
1575
1576
1577 static int g_numHiddenFaces, g_numCoinFaces;
1578
1579
1580
1581 /*
1582    CullVectorCompare() - ydnar
1583    compares two vectors with an epsilon
1584  */
1585
1586 #define CULL_EPSILON 0.1f
1587
1588 qboolean CullVectorCompare( const vec3_t v1, const vec3_t v2 ){
1589         int i;
1590
1591
1592         for ( i = 0; i < 3; i++ )
1593                 if ( fabs( v1[ i ] - v2[ i ] ) > CULL_EPSILON ) {
1594                         return qfalse;
1595                 }
1596         return qtrue;
1597 }
1598
1599
1600
1601 /*
1602    SideInBrush() - ydnar
1603    determines if a brushside lies inside another brush
1604  */
1605
1606 qboolean SideInBrush( side_t *side, brush_t *b ){
1607         int i, s;
1608         plane_t     *plane;
1609
1610
1611         /* ignore sides w/o windings or shaders */
1612         if ( side->winding == NULL || side->shaderInfo == NULL ) {
1613                 return qtrue;
1614         }
1615
1616         /* ignore culled sides and translucent brushes */
1617         if ( side->culled == qtrue || ( b->compileFlags & C_TRANSLUCENT ) ) {
1618                 return qfalse;
1619         }
1620
1621         /* side iterator */
1622         for ( i = 0; i < b->numsides; i++ )
1623         {
1624                 /* fail if any sides are caulk */
1625                 if ( b->sides[ i ].compileFlags & C_NODRAW ) {
1626                         return qfalse;
1627                 }
1628
1629                 /* check if side's winding is on or behind the plane */
1630                 plane = &mapplanes[ b->sides[ i ].planenum ];
1631                 s = WindingOnPlaneSide( side->winding, plane->normal, plane->dist );
1632                 if ( s == SIDE_FRONT || s == SIDE_CROSS ) {
1633                         return qfalse;
1634                 }
1635         }
1636
1637         /* don't cull autosprite or polygonoffset surfaces */
1638         if ( side->shaderInfo ) {
1639                 if ( side->shaderInfo->autosprite || side->shaderInfo->polygonOffset ) {
1640                         return qfalse;
1641                 }
1642         }
1643
1644         /* inside */
1645         side->culled = qtrue;
1646         g_numHiddenFaces++;
1647         return qtrue;
1648 }
1649
1650
1651 /*
1652    CullSides() - ydnar
1653    culls obscured or buried brushsides from the map
1654  */
1655
1656 void CullSides( entity_t *e ){
1657         int numPoints;
1658         int i, j, k, l, first, second, dir;
1659         winding_t   *w1, *w2;
1660         brush_t *b1, *b2;
1661         side_t      *side1, *side2;
1662
1663
1664         /* note it */
1665         Sys_FPrintf( SYS_VRB, "--- CullSides ---\n" );
1666
1667         g_numHiddenFaces = 0;
1668         g_numCoinFaces = 0;
1669
1670         /* brush interator 1 */
1671         for ( b1 = e->brushes; b1; b1 = b1->next )
1672         {
1673                 /* sides check */
1674                 if ( b1->numsides < 1 ) {
1675                         continue;
1676                 }
1677
1678                 /* brush iterator 2 */
1679                 for ( b2 = b1->next; b2; b2 = b2->next )
1680                 {
1681                         /* sides check */
1682                         if ( b2->numsides < 1 ) {
1683                                 continue;
1684                         }
1685
1686                         /* original check */
1687                         if ( b1->original == b2->original && b1->original != NULL ) {
1688                                 continue;
1689                         }
1690
1691                         /* bbox check */
1692                         j = 0;
1693                         for ( i = 0; i < 3; i++ )
1694                                 if ( b1->mins[ i ] > b2->maxs[ i ] || b1->maxs[ i ] < b2->mins[ i ] ) {
1695                                         j++;
1696                                 }
1697                         if ( j ) {
1698                                 continue;
1699                         }
1700
1701                         /* cull inside sides */
1702                         for ( i = 0; i < b1->numsides; i++ )
1703                                 SideInBrush( &b1->sides[ i ], b2 );
1704                         for ( i = 0; i < b2->numsides; i++ )
1705                                 SideInBrush( &b2->sides[ i ], b1 );
1706
1707                         /* side iterator 1 */
1708                         for ( i = 0; i < b1->numsides; i++ )
1709                         {
1710                                 /* winding check */
1711                                 side1 = &b1->sides[ i ];
1712                                 w1 = side1->winding;
1713                                 if ( w1 == NULL ) {
1714                                         continue;
1715                                 }
1716                                 numPoints = w1->numpoints;
1717                                 if ( side1->shaderInfo == NULL ) {
1718                                         continue;
1719                                 }
1720
1721                                 /* side iterator 2 */
1722                                 for ( j = 0; j < b2->numsides; j++ )
1723                                 {
1724                                         /* winding check */
1725                                         side2 = &b2->sides[ j ];
1726                                         w2 = side2->winding;
1727                                         if ( w2 == NULL ) {
1728                                                 continue;
1729                                         }
1730                                         if ( side2->shaderInfo == NULL ) {
1731                                                 continue;
1732                                         }
1733                                         if ( w1->numpoints != w2->numpoints ) {
1734                                                 continue;
1735                                         }
1736                                         if ( side1->culled == qtrue && side2->culled == qtrue ) {
1737                                                 continue;
1738                                         }
1739
1740                                         /* compare planes */
1741                                         if ( ( side1->planenum & ~0x00000001 ) != ( side2->planenum & ~0x00000001 ) ) {
1742                                                 continue;
1743                                         }
1744
1745                                         /* get autosprite and polygonoffset status */
1746                                         if ( side1->shaderInfo &&
1747                                                  ( side1->shaderInfo->autosprite || side1->shaderInfo->polygonOffset ) ) {
1748                                                 continue;
1749                                         }
1750                                         if ( side2->shaderInfo &&
1751                                                  ( side2->shaderInfo->autosprite || side2->shaderInfo->polygonOffset ) ) {
1752                                                 continue;
1753                                         }
1754
1755                                         /* find first common point */
1756                                         first = -1;
1757                                         for ( k = 0; k < numPoints; k++ )
1758                                         {
1759                                                 if ( VectorCompare( w1->p[ 0 ], w2->p[ k ] ) ) {
1760                                                         first = k;
1761                                                         k = numPoints;
1762                                                 }
1763                                         }
1764                                         if ( first == -1 ) {
1765                                                 continue;
1766                                         }
1767
1768                                         /* find second common point (regardless of winding order) */
1769                                         second = -1;
1770                                         dir = 0;
1771                                         if ( ( first + 1 ) < numPoints ) {
1772                                                 second = first + 1;
1773                                         }
1774                                         else{
1775                                                 second = 0;
1776                                         }
1777                                         if ( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) ) {
1778                                                 dir = 1;
1779                                         }
1780                                         else
1781                                         {
1782                                                 if ( first > 0 ) {
1783                                                         second = first - 1;
1784                                                 }
1785                                                 else{
1786                                                         second = numPoints - 1;
1787                                                 }
1788                                                 if ( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) ) {
1789                                                         dir = -1;
1790                                                 }
1791                                         }
1792                                         if ( dir == 0 ) {
1793                                                 continue;
1794                                         }
1795
1796                                         /* compare the rest of the points */
1797                                         l = first;
1798                                         for ( k = 0; k < numPoints; k++ )
1799                                         {
1800                                                 if ( !CullVectorCompare( w1->p[ k ], w2->p[ l ] ) ) {
1801                                                         k = 100000;
1802                                                 }
1803
1804                                                 l += dir;
1805                                                 if ( l < 0 ) {
1806                                                         l = numPoints - 1;
1807                                                 }
1808                                                 else if ( l >= numPoints ) {
1809                                                         l = 0;
1810                                                 }
1811                                         }
1812                                         if ( k >= 100000 ) {
1813                                                 continue;
1814                                         }
1815
1816                                         /* cull face 1 */
1817                                         if ( !side2->culled && !( side2->compileFlags & C_TRANSLUCENT ) && !( side2->compileFlags & C_NODRAW ) ) {
1818                                                 side1->culled = qtrue;
1819                                                 g_numCoinFaces++;
1820                                         }
1821
1822                                         if ( side1->planenum == side2->planenum && side1->culled == qtrue ) {
1823                                                 continue;
1824                                         }
1825
1826                                         /* cull face 2 */
1827                                         if ( !side1->culled && !( side1->compileFlags & C_TRANSLUCENT ) && !( side1->compileFlags & C_NODRAW ) ) {
1828                                                 side2->culled = qtrue;
1829                                                 g_numCoinFaces++;
1830                                         }
1831                                 }
1832                         }
1833                 }
1834         }
1835
1836         /* emit some stats */
1837         Sys_FPrintf( SYS_VRB, "%9d hidden faces culled\n", g_numHiddenFaces );
1838         Sys_FPrintf( SYS_VRB, "%9d coincident faces culled\n", g_numCoinFaces );
1839 }
1840
1841
1842
1843
1844 /*
1845    ClipSidesIntoTree()
1846
1847    creates side->visibleHull for all visible sides
1848
1849    the drawsurf for a side will consist of the convex hull of
1850    all points in non-opaque clusters, which allows overlaps
1851    to be trimmed off automatically.
1852  */
1853
1854 void ClipSidesIntoTree( entity_t *e, tree_t *tree ){
1855         brush_t     *b;
1856         int i;
1857         winding_t       *w;
1858         side_t          *side, *newSide;
1859         shaderInfo_t    *si;
1860
1861
1862         /* ydnar: cull brush sides */
1863         CullSides( e );
1864
1865         /* note it */
1866         Sys_FPrintf( SYS_VRB, "--- ClipSidesIntoTree ---\n" );
1867
1868         /* walk the brush list */
1869         for ( b = e->brushes; b; b = b->next )
1870         {
1871                 /* walk the brush sides */
1872                 for ( i = 0; i < b->numsides; i++ )
1873                 {
1874                         /* get side */
1875                         side = &b->sides[ i ];
1876                         if ( side->winding == NULL ) {
1877                                 continue;
1878                         }
1879
1880                         /* copy the winding */
1881                         w = CopyWinding( side->winding );
1882                         side->visibleHull = NULL;
1883                         ClipSideIntoTree_r( w, side, tree->headnode );
1884
1885                         /* anything left? */
1886                         w = side->visibleHull;
1887                         if ( w == NULL ) {
1888                                 continue;
1889                         }
1890
1891                         /* shader? */
1892                         si = side->shaderInfo;
1893                         if ( si == NULL ) {
1894                                 continue;
1895                         }
1896
1897                         /* don't create faces for non-visible sides */
1898                         /* ydnar: except indexed shaders, like common/terrain and nodraw fog surfaces */
1899                         if ( ( si->compileFlags & C_NODRAW ) && si->indexed == qfalse && !( si->compileFlags & C_FOG ) ) {
1900                                 continue;
1901                         }
1902
1903                         /* always use the original winding for autosprites and noclip faces */
1904                         if ( si->autosprite || si->noClip ) {
1905                                 w = side->winding;
1906                         }
1907
1908                         /* save this winding as a visible surface */
1909                         DrawSurfaceForSide( e, b, side, w );
1910
1911                         /* make a back side for fog */
1912                         if ( !( si->compileFlags & C_FOG ) ) {
1913                                 continue;
1914                         }
1915
1916                         /* duplicate the up-facing side */
1917                         w = ReverseWinding( w );
1918                         newSide = safe_malloc( sizeof( *side ) );
1919                         *newSide = *side;
1920                         newSide->visibleHull = w;
1921                         newSide->planenum ^= 1;
1922
1923                         /* save this winding as a visible surface */
1924                         DrawSurfaceForSide( e, b, newSide, w );
1925                 }
1926         }
1927 }
1928
1929
1930
1931 /*
1932
1933    this section deals with filtering drawsurfaces into the bsp tree,
1934    adding references to each leaf a surface touches
1935
1936  */
1937
1938 /*
1939    AddReferenceToLeaf() - ydnar
1940    adds a reference to surface ds in the bsp leaf node
1941  */
1942
1943 int AddReferenceToLeaf( mapDrawSurface_t *ds, node_t *node ){
1944         drawSurfRef_t   *dsr;
1945
1946
1947         /* dummy check */
1948         if ( node->planenum != PLANENUM_LEAF || node->opaque ) {
1949                 return 0;
1950         }
1951
1952         /* try to find an existing reference */
1953         for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
1954         {
1955                 if ( dsr->outputNum == numBSPDrawSurfaces ) {
1956                         return 0;
1957                 }
1958         }
1959
1960         /* add a new reference */
1961         dsr = safe_malloc( sizeof( *dsr ) );
1962         dsr->outputNum = numBSPDrawSurfaces;
1963         dsr->nextRef = node->drawSurfReferences;
1964         node->drawSurfReferences = dsr;
1965
1966         /* ydnar: sky/skybox surfaces */
1967         if ( node->skybox ) {
1968                 ds->skybox = qtrue;
1969         }
1970         if ( ds->shaderInfo->compileFlags & C_SKY ) {
1971                 node->sky = qtrue;
1972         }
1973
1974         /* return */
1975         return 1;
1976 }
1977
1978
1979
1980 /*
1981    AddReferenceToTree_r() - ydnar
1982    adds a reference to the specified drawsurface to every leaf in the tree
1983  */
1984
1985 int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, qboolean skybox ){
1986         int i, refs = 0;
1987
1988
1989         /* dummy check */
1990         if ( node == NULL ) {
1991                 return 0;
1992         }
1993
1994         /* is this a decision node? */
1995         if ( node->planenum != PLANENUM_LEAF ) {
1996                 /* add to child nodes and return */
1997                 refs += AddReferenceToTree_r( ds, node->children[ 0 ], skybox );
1998                 refs += AddReferenceToTree_r( ds, node->children[ 1 ], skybox );
1999                 return refs;
2000         }
2001
2002         /* ydnar */
2003         if ( skybox ) {
2004                 /* skybox surfaces only get added to sky leaves */
2005                 if ( !node->sky ) {
2006                         return 0;
2007                 }
2008
2009                 /* increase the leaf bounds */
2010                 for ( i = 0; i < ds->numVerts; i++ )
2011                         AddPointToBounds( ds->verts[ i ].xyz, node->mins, node->maxs );
2012         }
2013
2014         /* add a reference */
2015         return AddReferenceToLeaf( ds, node );
2016 }
2017
2018
2019
2020 /*
2021    FilterPointIntoTree_r() - ydnar
2022    filters a single point from a surface into the tree
2023  */
2024
2025 int FilterPointIntoTree_r( vec3_t point, mapDrawSurface_t *ds, node_t *node ){
2026         float d;
2027         plane_t         *plane;
2028         int refs = 0;
2029
2030
2031         /* is this a decision node? */
2032         if ( node->planenum != PLANENUM_LEAF ) {
2033                 /* classify the point in relation to the plane */
2034                 plane = &mapplanes[ node->planenum ];
2035                 d = DotProduct( point, plane->normal ) - plane->dist;
2036
2037                 /* filter by this plane */
2038                 refs = 0;
2039                 if ( d >= -ON_EPSILON ) {
2040                         refs += FilterPointIntoTree_r( point, ds, node->children[ 0 ] );
2041                 }
2042                 if ( d <= ON_EPSILON ) {
2043                         refs += FilterPointIntoTree_r( point, ds, node->children[ 1 ] );
2044                 }
2045
2046                 /* return */
2047                 return refs;
2048         }
2049
2050         /* add a reference */
2051         return AddReferenceToLeaf( ds, node );
2052 }
2053
2054 /*
2055    FilterPointConvexHullIntoTree_r() - ydnar
2056    filters the convex hull of multiple points from a surface into the tree
2057  */
2058
2059 int FilterPointConvexHullIntoTree_r( vec3_t **points, int npoints, mapDrawSurface_t *ds, node_t *node ){
2060         float d, dmin, dmax;
2061         plane_t         *plane;
2062         int refs = 0;
2063         int i;
2064
2065         if ( !points ) {
2066                 return 0;
2067         }
2068
2069         /* is this a decision node? */
2070         if ( node->planenum != PLANENUM_LEAF ) {
2071                 /* classify the point in relation to the plane */
2072                 plane = &mapplanes[ node->planenum ];
2073
2074                 dmin = dmax = DotProduct( *( points[0] ), plane->normal ) - plane->dist;
2075                 for ( i = 1; i < npoints; ++i )
2076                 {
2077                         d = DotProduct( *( points[i] ), plane->normal ) - plane->dist;
2078                         if ( d > dmax ) {
2079                                 dmax = d;
2080                         }
2081                         if ( d < dmin ) {
2082                                 dmin = d;
2083                         }
2084                 }
2085
2086                 /* filter by this plane */
2087                 refs = 0;
2088                 if ( dmax >= -ON_EPSILON ) {
2089                         refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 0 ] );
2090                 }
2091                 if ( dmin <= ON_EPSILON ) {
2092                         refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 1 ] );
2093                 }
2094
2095                 /* return */
2096                 return refs;
2097         }
2098
2099         /* add a reference */
2100         return AddReferenceToLeaf( ds, node );
2101 }
2102
2103
2104 /*
2105    FilterWindingIntoTree_r() - ydnar
2106    filters a winding from a drawsurface into the tree
2107  */
2108
2109 int FilterWindingIntoTree_r( winding_t *w, mapDrawSurface_t *ds, node_t *node ){
2110         int i, refs = 0;
2111         plane_t         *p1, *p2;
2112         vec4_t plane1, plane2;
2113         winding_t       *fat, *front, *back;
2114         shaderInfo_t    *si;
2115
2116
2117         /* get shaderinfo */
2118         si = ds->shaderInfo;
2119
2120         /* ydnar: is this the head node? */
2121         if ( node->parent == NULL && si != NULL &&
2122                  ( si->mins[ 0 ] != 0.0f || si->maxs[ 0 ] != 0.0f ||
2123                    si->mins[ 1 ] != 0.0f || si->maxs[ 1 ] != 0.0f ||
2124                    si->mins[ 2 ] != 0.0f || si->maxs[ 2 ] != 0.0f ) ) {
2125                 static qboolean warned = qfalse;
2126                 if ( !warned ) {
2127                         Sys_FPrintf( SYS_WRN, "WARNING: this map uses the deformVertexes move hack\n" );
2128                         warned = qtrue;
2129                 }
2130
2131                 /* 'fatten' the winding by the shader mins/maxs (parsed from vertexDeform move) */
2132                 /* note this winding is completely invalid (concave, nonplanar, etc) */
2133                 fat = AllocWinding( w->numpoints * 3 + 3 );
2134                 fat->numpoints = w->numpoints * 3 + 3;
2135                 for ( i = 0; i < w->numpoints; i++ )
2136                 {
2137                         VectorCopy( w->p[ i ], fat->p[ i ] );
2138                         VectorAdd( w->p[ i ], si->mins, fat->p[ i + ( w->numpoints + 1 ) ] );
2139                         VectorAdd( w->p[ i ], si->maxs, fat->p[ i + ( w->numpoints + 1 ) * 2 ] );
2140                 }
2141                 VectorCopy( w->p[ 0 ], fat->p[ i ] );
2142                 VectorAdd( w->p[ 0 ], si->mins, fat->p[ i + w->numpoints ] );
2143                 VectorAdd( w->p[ 0 ], si->maxs, fat->p[ i + w->numpoints * 2 ] );
2144
2145                 /*
2146                  * note: this winding is STILL not suitable for ClipWindingEpsilon, and
2147                  * also does not really fulfill the intention as it only contains
2148                  * origin, +mins, +maxs, but thanks to the "closing" points I just
2149                  * added to the three sub-windings, the fattening at least doesn't make
2150                  * it worse
2151                  */
2152
2153                 FreeWinding( w );
2154                 w = fat;
2155         }
2156
2157         /* is this a decision node? */
2158         if ( node->planenum != PLANENUM_LEAF ) {
2159                 /* get node plane */
2160                 p1 = &mapplanes[ node->planenum ];
2161                 VectorCopy( p1->normal, plane1 );
2162                 plane1[ 3 ] = p1->dist;
2163
2164                 /* check if surface is planar */
2165                 if ( ds->planeNum >= 0 ) {
2166                         /* get surface plane */
2167                         p2 = &mapplanes[ ds->planeNum ];
2168                         VectorCopy( p2->normal, plane2 );
2169                         plane2[ 3 ] = p2->dist;
2170
2171                         #if 0
2172                         /* div0: this is the plague (inaccurate) */
2173                         vec4_t reverse;
2174
2175                         /* invert surface plane */
2176                         VectorSubtract( vec3_origin, plane2, reverse );
2177                         reverse[ 3 ] = -plane2[ 3 ];
2178
2179                         /* compare planes */
2180                         if ( DotProduct( plane1, plane2 ) > 0.999f && fabs( plane1[ 3 ] - plane2[ 3 ] ) < 0.001f ) {
2181                                 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2182                         }
2183                         if ( DotProduct( plane1, reverse ) > 0.999f && fabs( plane1[ 3 ] - reverse[ 3 ] ) < 0.001f ) {
2184                                 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2185                         }
2186             #else
2187                         (void) plane2;
2188                         /* div0: this is the cholera (doesn't hit enough) */
2189
2190                         /* the drawsurf might have an associated plane, if so, force a filter here */
2191                         if ( ds->planeNum == node->planenum ) {
2192                                 return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] );
2193                         }
2194                         if ( ds->planeNum == ( node->planenum ^ 1 ) ) {
2195                                 return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] );
2196                         }
2197                         #endif
2198                 }
2199
2200                 /* clip the winding by this plane */
2201                 ClipWindingEpsilonStrict( w, plane1, plane1[ 3 ], ON_EPSILON, &front, &back ); /* strict; we handle the "winding disappeared" case */
2202
2203                 /* filter by this plane */
2204                 refs = 0;
2205                 if ( front == NULL && back == NULL ) {
2206                         /* same plane, this is an ugly hack */
2207                         /* but better too many than too few refs */
2208                         refs += FilterWindingIntoTree_r( CopyWinding( w ), ds, node->children[ 0 ] );
2209                         refs += FilterWindingIntoTree_r( CopyWinding( w ), ds, node->children[ 1 ] );
2210                 }
2211                 if ( front != NULL ) {
2212                         refs += FilterWindingIntoTree_r( front, ds, node->children[ 0 ] );
2213                 }
2214                 if ( back != NULL ) {
2215                         refs += FilterWindingIntoTree_r( back, ds, node->children[ 1 ] );
2216                 }
2217                 FreeWinding( w );
2218
2219                 /* return */
2220                 return refs;
2221         }
2222
2223         /* add a reference */
2224         return AddReferenceToLeaf( ds, node );
2225 }
2226
2227
2228
2229 /*
2230    FilterFaceIntoTree()
2231    filters a planar winding face drawsurface into the bsp tree
2232  */
2233
2234 int FilterFaceIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2235         winding_t   *w;
2236         int refs = 0;
2237
2238
2239         /* make a winding and filter it into the tree */
2240         w = WindingFromDrawSurf( ds );
2241         refs = FilterWindingIntoTree_r( w, ds, tree->headnode );
2242
2243         /* return */
2244         return refs;
2245 }
2246
2247
2248
2249 /*
2250    FilterPatchIntoTree()
2251    subdivides a patch into an approximate curve and filters it into the tree
2252  */
2253
2254 #define FILTER_SUBDIVISION      8
2255
2256 static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2257         int x, y, refs = 0;
2258
2259         for ( y = 0; y + 2 < ds->patchHeight; y += 2 )
2260                 for ( x = 0; x + 2 < ds->patchWidth; x += 2 )
2261                 {
2262                         vec3_t *points[9];
2263                         points[0] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 0 )].xyz;
2264                         points[1] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 1 )].xyz;
2265                         points[2] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 2 )].xyz;
2266                         points[3] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 0 )].xyz;
2267                         points[4] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 1 )].xyz;
2268                         points[5] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 2 )].xyz;
2269                         points[6] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 0 )].xyz;
2270                         points[7] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 1 )].xyz;
2271                         points[8] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 2 )].xyz;
2272                         refs += FilterPointConvexHullIntoTree_r( points, 9, ds, tree->headnode );
2273                 }
2274
2275         return refs;
2276 }
2277
2278
2279
2280 /*
2281    FilterTrianglesIntoTree()
2282    filters a triangle surface (meta, model) into the bsp
2283  */
2284
2285 static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2286         int i, refs;
2287         winding_t   *w;
2288
2289
2290         /* ydnar: gs mods: this was creating bogus triangles before */
2291         refs = 0;
2292         for ( i = 0; i < ds->numIndexes; i += 3 )
2293         {
2294                 /* error check */
2295                 if ( ds->indexes[ i ] >= ds->numVerts ||
2296                          ds->indexes[ i + 1 ] >= ds->numVerts ||
2297                          ds->indexes[ i + 2 ] >= ds->numVerts ) {
2298                         Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2299                 }
2300
2301                 /* make a triangle winding and filter it into the tree */
2302                 w = AllocWinding( 3 );
2303                 w->numpoints = 3;
2304                 VectorCopy( ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2305                 VectorCopy( ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2306                 VectorCopy( ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2307                 refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2308         }
2309
2310         /* use point filtering as well */
2311         for ( i = 0; i < ds->numVerts; i++ )
2312                 refs += FilterPointIntoTree_r( ds->verts[ i ].xyz, ds, tree->headnode );
2313
2314         return refs;
2315 }
2316
2317
2318
2319 /*
2320    FilterFoliageIntoTree()
2321    filters a foliage surface (wolf et/splash damage)
2322  */
2323
2324 static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2325         int f, i, refs;
2326         bspDrawVert_t   *instance;
2327         vec3_t xyz;
2328         winding_t       *w;
2329
2330
2331         /* walk origin list */
2332         refs = 0;
2333         for ( f = 0; f < ds->numFoliageInstances; f++ )
2334         {
2335                 /* get instance */
2336                 instance = ds->verts + ds->patchHeight + f;
2337
2338                 /* walk triangle list */
2339                 for ( i = 0; i < ds->numIndexes; i += 3 )
2340                 {
2341                         /* error check */
2342                         if ( ds->indexes[ i ] >= ds->numVerts ||
2343                                  ds->indexes[ i + 1 ] >= ds->numVerts ||
2344                                  ds->indexes[ i + 2 ] >= ds->numVerts ) {
2345                                 Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts );
2346                         }
2347
2348                         /* make a triangle winding and filter it into the tree */
2349                         w = AllocWinding( 3 );
2350                         w->numpoints = 3;
2351                         VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] );
2352                         VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] );
2353                         VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] );
2354                         refs += FilterWindingIntoTree_r( w, ds, tree->headnode );
2355                 }
2356
2357                 /* use point filtering as well */
2358                 for ( i = 0; i < ( ds->numVerts - ds->numFoliageInstances ); i++ )
2359                 {
2360                         VectorAdd( instance->xyz, ds->verts[ i ].xyz, xyz );
2361                         refs += FilterPointIntoTree_r( xyz, ds, tree->headnode );
2362                 }
2363         }
2364
2365         return refs;
2366 }
2367
2368
2369
2370 /*
2371    FilterFlareIntoTree()
2372    simple point filtering for flare surfaces
2373  */
2374 static int FilterFlareSurfIntoTree( mapDrawSurface_t *ds, tree_t *tree ){
2375         return FilterPointIntoTree_r( ds->lightmapOrigin, ds, tree->headnode );
2376 }
2377
2378
2379
2380 /*
2381    EmitDrawVerts() - ydnar
2382    emits bsp drawverts from a map drawsurface
2383  */
2384
2385 void EmitDrawVerts( mapDrawSurface_t *ds, bspDrawSurface_t *out ){
2386         int i, k;
2387         bspDrawVert_t   *dv;
2388         shaderInfo_t    *si;
2389         float offset;
2390
2391
2392         /* get stuff */
2393         si = ds->shaderInfo;
2394         offset = si->offset;
2395
2396         /* copy the verts */
2397         out->firstVert = numBSPDrawVerts;
2398         out->numVerts = ds->numVerts;
2399         for ( i = 0; i < ds->numVerts; i++ )
2400         {
2401                 /* allocate a new vert */
2402                 IncDrawVerts();
2403                 dv = &bspDrawVerts[ numBSPDrawVerts - 1 ];
2404
2405                 /* copy it */
2406                 memcpy( dv, &ds->verts[ i ], sizeof( *dv ) );
2407
2408                 /* offset? */
2409                 if ( offset != 0.0f ) {
2410                         VectorMA( dv->xyz, offset, dv->normal, dv->xyz );
2411                 }
2412
2413                 /* expand model bounds
2414                    necessary because of misc_model surfaces on entities
2415                    note: does not happen on worldspawn as its bounds is only used for determining lightgrid bounds */
2416                 if ( numBSPModels > 0 ) {
2417                         AddPointToBounds( dv->xyz, bspModels[ numBSPModels ].mins, bspModels[ numBSPModels ].maxs );
2418                 }
2419
2420                 /* debug color? */
2421                 if ( debugSurfaces ) {
2422                         for ( k = 0; k < MAX_LIGHTMAPS; k++ )
2423                                 VectorCopy( debugColors[ ( ds - mapDrawSurfs ) % 12 ], dv->color[ k ] );
2424                 }
2425         }
2426 }
2427
2428
2429
2430 /*
2431    FindDrawIndexes() - ydnar
2432    this attempts to find a run of indexes in the bsp that match the given indexes
2433    this tends to reduce the size of the bsp index pool by 1/3 or more
2434    returns numIndexes + 1 if the search failed
2435  */
2436
2437 int FindDrawIndexes( int numIndexes, int *indexes ){
2438         int i, j, numTestIndexes;
2439
2440
2441         /* dummy check */
2442         if ( numIndexes < 3 || numBSPDrawIndexes < numIndexes || indexes == NULL ) {
2443                 return numBSPDrawIndexes;
2444         }
2445
2446         /* set limit */
2447         numTestIndexes = 1 + numBSPDrawIndexes - numIndexes;
2448
2449         /* handle 3 indexes as a special case for performance */
2450         if ( numIndexes == 3 ) {
2451                 /* run through all indexes */
2452                 for ( i = 0; i < numTestIndexes; i++ )
2453                 {
2454                         /* test 3 indexes */
2455                         if ( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2456                                  indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2457                                  indexes[ 2 ] == bspDrawIndexes[ i + 2 ] ) {
2458                                 numRedundantIndexes += numIndexes;
2459                                 return i;
2460                         }
2461                 }
2462
2463                 /* failed */
2464                 return numBSPDrawIndexes;
2465         }
2466
2467         /* handle 4 or more indexes */
2468         for ( i = 0; i < numTestIndexes; i++ )
2469         {
2470                 /* test first 4 indexes */
2471                 if ( indexes[ 0 ] == bspDrawIndexes[ i ] &&
2472                          indexes[ 1 ] == bspDrawIndexes[ i + 1 ] &&
2473                          indexes[ 2 ] == bspDrawIndexes[ i + 2 ] &&
2474                          indexes[ 3 ] == bspDrawIndexes[ i + 3 ] ) {
2475                         /* handle 4 indexes */
2476                         if ( numIndexes == 4 ) {
2477                                 return i;
2478                         }
2479
2480                         /* test the remainder */
2481                         for ( j = 4; j < numIndexes; j++ )
2482                         {
2483                                 if ( indexes[ j ] != bspDrawIndexes[ i + j ] ) {
2484                                         break;
2485                                 }
2486                                 else if ( j == ( numIndexes - 1 ) ) {
2487                                         numRedundantIndexes += numIndexes;
2488                                         return i;
2489                                 }
2490                         }
2491                 }
2492         }
2493
2494         /* failed */
2495         return numBSPDrawIndexes;
2496 }
2497
2498
2499
2500 /*
2501    EmitDrawIndexes() - ydnar
2502    attempts to find an existing run of drawindexes before adding new ones
2503  */
2504
2505 void EmitDrawIndexes( mapDrawSurface_t *ds, bspDrawSurface_t *out ){
2506         int i;
2507
2508
2509         /* attempt to use redundant indexing */
2510         out->firstIndex = FindDrawIndexes( ds->numIndexes, ds->indexes );
2511         out->numIndexes = ds->numIndexes;
2512         if ( out->firstIndex == numBSPDrawIndexes ) {
2513                 /* copy new unique indexes */
2514                 for ( i = 0; i < ds->numIndexes; i++ )
2515                 {
2516                         AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
2517                         bspDrawIndexes[ numBSPDrawIndexes ] = ds->indexes[ i ];
2518
2519                         /* validate the index */
2520                         if ( ds->type != SURFACE_PATCH ) {
2521                                 if ( bspDrawIndexes[ numBSPDrawIndexes ] < 0 || bspDrawIndexes[ numBSPDrawIndexes ] >= ds->numVerts ) {
2522                                         Sys_FPrintf( SYS_WRN, "WARNING: %d %s has invalid index %d (%d)\n",
2523                                                                 numBSPDrawSurfaces,
2524                                                                 ds->shaderInfo->shader,
2525                                                                 bspDrawIndexes[ numBSPDrawIndexes ],
2526                                                                 i );
2527                                         bspDrawIndexes[ numBSPDrawIndexes ] = 0;
2528                                 }
2529                         }
2530
2531                         /* increment index count */
2532                         numBSPDrawIndexes++;
2533                 }
2534         }
2535 }
2536
2537
2538
2539
2540 /*
2541    EmitFlareSurface()
2542    emits a bsp flare drawsurface
2543  */
2544
2545 void EmitFlareSurface( mapDrawSurface_t *ds ){
2546         int i;
2547         bspDrawSurface_t        *out;
2548
2549
2550         /* ydnar: nuking useless flare drawsurfaces */
2551         if ( emitFlares == qfalse && ds->type != SURFACE_SHADER ) {
2552                 return;
2553         }
2554
2555         /* limit check */
2556         if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2557                 Error( "MAX_MAP_DRAW_SURFS" );
2558         }
2559
2560         /* allocate a new surface */
2561         if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2562                 Error( "MAX_MAP_DRAW_SURFS" );
2563         }
2564         out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2565         ds->outputNum = numBSPDrawSurfaces;
2566         numBSPDrawSurfaces++;
2567         memset( out, 0, sizeof( *out ) );
2568
2569         /* set it up */
2570         out->surfaceType = MST_FLARE;
2571         out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2572         out->fogNum = ds->fogNum;
2573
2574         /* RBSP */
2575         for ( i = 0; i < MAX_LIGHTMAPS; i++ )
2576         {
2577                 out->lightmapNum[ i ] = -3;
2578                 out->lightmapStyles[ i ] = LS_NONE;
2579                 out->vertexStyles[ i ] = LS_NONE;
2580         }
2581         out->lightmapStyles[ 0 ] = ds->lightStyle;
2582         out->vertexStyles[ 0 ] = ds->lightStyle;
2583
2584         VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );          /* origin */
2585         VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );    /* color */
2586         VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2587         VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );    /* normal */
2588
2589         /* add to count */
2590         numSurfacesByType[ ds->type ]++;
2591 }
2592
2593 /*
2594    EmitPatchSurface()
2595    emits a bsp patch drawsurface
2596  */
2597
2598 void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds ){
2599         int i, j;
2600         bspDrawSurface_t    *out;
2601         int surfaceFlags, contentFlags;
2602         int forcePatchMeta;
2603
2604         /* vortex: _patchMeta support */
2605         forcePatchMeta = IntForKey( e, "_patchMeta" );
2606         if ( !forcePatchMeta ) {
2607                 forcePatchMeta = IntForKey( e, "patchMeta" );
2608         }
2609
2610         /* invert the surface if necessary */
2611         if ( ds->backSide || ds->shaderInfo->invert ) {
2612                 bspDrawVert_t   *dv1, *dv2, temp;
2613
2614                 /* walk the verts, flip the normal */
2615                 for ( i = 0; i < ds->numVerts; i++ )
2616                         VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2617
2618                 /* walk the verts again, but this time reverse their order */
2619                 for ( j = 0; j < ds->patchHeight; j++ )
2620                 {
2621                         for ( i = 0; i < ( ds->patchWidth / 2 ); i++ )
2622                         {
2623                                 dv1 = &ds->verts[ j * ds->patchWidth + i ];
2624                                 dv2 = &ds->verts[ j * ds->patchWidth + ( ds->patchWidth - i - 1 ) ];
2625                                 memcpy( &temp, dv1, sizeof( bspDrawVert_t ) );
2626                                 memcpy( dv1, dv2, sizeof( bspDrawVert_t ) );
2627                                 memcpy( dv2, &temp, sizeof( bspDrawVert_t ) );
2628                         }
2629                 }
2630
2631                 /* invert facing */
2632                 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2633         }
2634
2635         /* allocate a new surface */
2636         if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2637                 Error( "MAX_MAP_DRAW_SURFS" );
2638         }
2639         out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2640         ds->outputNum = numBSPDrawSurfaces;
2641         numBSPDrawSurfaces++;
2642         memset( out, 0, sizeof( *out ) );
2643
2644         /* set it up */
2645         out->surfaceType = MST_PATCH;
2646         if ( debugSurfaces ) {
2647                 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2648         }
2649         else if ( patchMeta || forcePatchMeta ) {
2650                 /* patch meta requires that we have nodraw patches for collision */
2651                 surfaceFlags = ds->shaderInfo->surfaceFlags;
2652                 contentFlags = ds->shaderInfo->contentFlags;
2653                 ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, NULL );
2654                 ApplySurfaceParm( "pointlight", &contentFlags, &surfaceFlags, NULL );
2655
2656                 /* we don't want this patch getting lightmapped */
2657                 VectorClear( ds->lightmapVecs[ 2 ] );
2658                 VectorClear( ds->lightmapAxis );
2659                 ds->sampleSize = 0;
2660
2661                 /* emit the new fake shader */
2662                 out->shaderNum = EmitShader( ds->shaderInfo->shader, &contentFlags, &surfaceFlags );
2663         }
2664         else{
2665                 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2666         }
2667         out->patchWidth = ds->patchWidth;
2668         out->patchHeight = ds->patchHeight;
2669         out->fogNum = ds->fogNum;
2670
2671         /* RBSP */
2672         for ( i = 0; i < MAX_LIGHTMAPS; i++ )
2673         {
2674                 out->lightmapNum[ i ] = -3;
2675                 out->lightmapStyles[ i ] = LS_NONE;
2676                 out->vertexStyles[ i ] = LS_NONE;
2677         }
2678         out->lightmapStyles[ 0 ] = LS_NORMAL;
2679         out->vertexStyles[ 0 ] = LS_NORMAL;
2680
2681         /* ydnar: gs mods: previously, the lod bounds were stored in lightmapVecs[ 0 ] and [ 1 ], moved to bounds[ 0 ] and [ 1 ] */
2682         VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2683         VectorCopy( ds->bounds[ 0 ], out->lightmapVecs[ 0 ] );
2684         VectorCopy( ds->bounds[ 1 ], out->lightmapVecs[ 1 ] );
2685         VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2686
2687         /* ydnar: gs mods: clear out the plane normal */
2688         if ( ds->planar == qfalse ) {
2689                 VectorClear( out->lightmapVecs[ 2 ] );
2690         }
2691
2692         /* emit the verts and indexes */
2693         EmitDrawVerts( ds, out );
2694         EmitDrawIndexes( ds, out );
2695
2696         /* add to count */
2697         numSurfacesByType[ ds->type ]++;
2698 }
2699
2700 /*
2701    OptimizeTriangleSurface() - ydnar
2702    optimizes the vertex/index data in a triangle surface
2703  */
2704
2705 #define VERTEX_CACHE_SIZE   16
2706
2707 static void OptimizeTriangleSurface( mapDrawSurface_t *ds ){
2708         int i, j, k, temp, first, best, bestScore, score;
2709         int vertexCache[ VERTEX_CACHE_SIZE + 1 ];       /* one more for optimizing insert */
2710         int     *indexes;
2711
2712
2713         /* certain surfaces don't get optimized */
2714         if ( ds->numIndexes <= VERTEX_CACHE_SIZE ||
2715                  ds->shaderInfo->autosprite ) {
2716                 return;
2717         }
2718
2719         /* create index scratch pad */
2720         indexes = safe_malloc( ds->numIndexes * sizeof( *indexes ) );
2721         memcpy( indexes, ds->indexes, ds->numIndexes * sizeof( *indexes ) );
2722
2723         /* setup */
2724         for ( i = 0; i <= VERTEX_CACHE_SIZE && i < ds->numIndexes; i++ )
2725                 vertexCache[ i ] = indexes[ i ];
2726
2727         /* add triangles in a vertex cache-aware order */
2728         for ( i = 0; i < ds->numIndexes; i += 3 )
2729         {
2730                 /* find best triangle given the current vertex cache */
2731                 first = -1;
2732                 best = -1;
2733                 bestScore = -1;
2734                 for ( j = 0; j < ds->numIndexes; j += 3 )
2735                 {
2736                         /* valid triangle? */
2737                         if ( indexes[ j ] != -1 ) {
2738                                 /* set first if necessary */
2739                                 if ( first < 0 ) {
2740                                         first = j;
2741                                 }
2742
2743                                 /* score the triangle */
2744                                 score = 0;
2745                                 for ( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2746                                 {
2747                                         if ( indexes[ j ] == vertexCache[ k ] || indexes[ j + 1 ] == vertexCache[ k ] || indexes[ j + 2 ] == vertexCache[ k ] ) {
2748                                                 score++;
2749                                         }
2750                                 }
2751
2752                                 /* better triangle? */
2753                                 if ( score > bestScore ) {
2754                                         bestScore = score;
2755                                         best = j;
2756                                 }
2757
2758                                 /* a perfect score of 3 means this triangle's verts are already present in the vertex cache */
2759                                 if ( score == 3 ) {
2760                                         break;
2761                                 }
2762                         }
2763                 }
2764
2765                 /* check if no decent triangle was found, and use first available */
2766                 if ( best < 0 ) {
2767                         best = first;
2768                 }
2769
2770                 /* valid triangle? */
2771                 if ( best >= 0 ) {
2772                         /* add triangle to vertex cache */
2773                         for ( j = 0; j < 3; j++ )
2774                         {
2775                                 for ( k = 0; k < VERTEX_CACHE_SIZE; k++ )
2776                                 {
2777                                         if ( indexes[ best + j ] == vertexCache[ k ] ) {
2778                                                 break;
2779                                         }
2780                                 }
2781
2782                                 if ( k >= VERTEX_CACHE_SIZE ) {
2783                                         /* pop off top of vertex cache */
2784                                         for ( k = VERTEX_CACHE_SIZE; k > 0; k-- )
2785                                                 vertexCache[ k ] = vertexCache[ k - 1 ];
2786
2787                                         /* add vertex */
2788                                         vertexCache[ 0 ] = indexes[ best + j ];
2789                                 }
2790                         }
2791
2792                         /* add triangle to surface */
2793                         ds->indexes[ i ] = indexes[ best ];
2794                         ds->indexes[ i + 1 ] = indexes[ best + 1 ];
2795                         ds->indexes[ i + 2 ] = indexes[ best + 2 ];
2796
2797                         /* clear from input pool */
2798                         indexes[ best ] = -1;
2799                         indexes[ best + 1 ] = -1;
2800                         indexes[ best + 2 ] = -1;
2801
2802                         /* sort triangle windings (312 -> 123) */
2803                         while ( ds->indexes[ i ] > ds->indexes[ i + 1 ] || ds->indexes[ i ] > ds->indexes[ i + 2 ] )
2804                         {
2805                                 temp = ds->indexes[ i ];
2806                                 ds->indexes[ i ] = ds->indexes[ i + 1 ];
2807                                 ds->indexes[ i + 1 ] = ds->indexes[ i + 2 ];
2808                                 ds->indexes[ i + 2 ] = temp;
2809                         }
2810                 }
2811         }
2812
2813         /* clean up */
2814         free( indexes );
2815 }
2816
2817
2818
2819 /*
2820    EmitTriangleSurface()
2821    creates a bsp drawsurface from arbitrary triangle surfaces
2822  */
2823
2824 void EmitTriangleSurface( mapDrawSurface_t *ds ){
2825         int i, temp;
2826         bspDrawSurface_t        *out;
2827
2828         /* invert the surface if necessary */
2829         if ( ds->backSide || ds->shaderInfo->invert ) {
2830                 /* walk the indexes, reverse the triangle order */
2831                 for ( i = 0; i < ds->numIndexes; i += 3 )
2832                 {
2833                         temp = ds->indexes[ i ];
2834                         ds->indexes[ i ] = ds->indexes[ i + 1 ];
2835                         ds->indexes[ i + 1 ] = temp;
2836                 }
2837
2838                 /* walk the verts, flip the normal */
2839                 for ( i = 0; i < ds->numVerts; i++ )
2840                         VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal );
2841
2842                 /* invert facing */
2843                 VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] );
2844         }
2845
2846         /* allocate a new surface */
2847         if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) {
2848                 Error( "MAX_MAP_DRAW_SURFS" );
2849         }
2850         out = &bspDrawSurfaces[ numBSPDrawSurfaces ];
2851         ds->outputNum = numBSPDrawSurfaces;
2852         numBSPDrawSurfaces++;
2853         memset( out, 0, sizeof( *out ) );
2854
2855         /* ydnar/sd: handle wolf et foliage surfaces */
2856         if ( ds->type == SURFACE_FOLIAGE ) {
2857                 out->surfaceType = MST_FOLIAGE;
2858         }
2859
2860         /* ydnar: gs mods: handle lightmapped terrain (force to planar type) */
2861         //%     else if( VectorLength( ds->lightmapAxis ) <= 0.0f || ds->type == SURFACE_TRIANGLES || ds->type == SURFACE_FOGHULL || debugSurfaces )
2862         else if ( ( VectorLength( ds->lightmapAxis ) <= 0.0f && ds->planar == qfalse ) ||
2863                           ds->type == SURFACE_TRIANGLES ||
2864                           ds->type == SURFACE_FOGHULL ||
2865                           ds->numVerts > maxLMSurfaceVerts ||
2866                           debugSurfaces ) {
2867                 out->surfaceType = MST_TRIANGLE_SOUP;
2868         }
2869
2870         /* set to a planar face */
2871         else{
2872                 out->surfaceType = MST_PLANAR;
2873         }
2874
2875         /* set it up */
2876         if ( debugSurfaces ) {
2877                 out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL );
2878         }
2879         else{
2880                 out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
2881         }
2882         out->patchWidth = ds->patchWidth;
2883         out->patchHeight = ds->patchHeight;
2884         out->fogNum = ds->fogNum;
2885
2886         /* debug inset (push each triangle vertex towards the center of each triangle it is on */
2887         if ( debugInset ) {
2888                 bspDrawVert_t   *a, *b, *c;
2889                 vec3_t cent, dir;
2890
2891
2892                 /* walk triangle list */
2893                 for ( i = 0; i < ds->numIndexes; i += 3 )
2894                 {
2895                         /* get verts */
2896                         a = &ds->verts[ ds->indexes[ i ] ];
2897                         b = &ds->verts[ ds->indexes[ i + 1 ] ];
2898                         c = &ds->verts[ ds->indexes[ i + 2 ] ];
2899
2900                         /* calculate centroid */
2901                         VectorCopy( a->xyz, cent );
2902                         VectorAdd( cent, b->xyz, cent );
2903                         VectorAdd( cent, c->xyz, cent );
2904                         VectorScale( cent, 1.0f / 3.0f, cent );
2905
2906                         /* offset each vertex */
2907                         VectorSubtract( cent, a->xyz, dir );
2908                         VectorNormalize( dir, dir );
2909                         VectorAdd( a->xyz, dir, a->xyz );
2910                         VectorSubtract( cent, b->xyz, dir );
2911                         VectorNormalize( dir, dir );
2912                         VectorAdd( b->xyz, dir, b->xyz );
2913                         VectorSubtract( cent, c->xyz, dir );
2914                         VectorNormalize( dir, dir );
2915                         VectorAdd( c->xyz, dir, c->xyz );
2916                 }
2917         }
2918
2919         /* RBSP */
2920         for ( i = 0; i < MAX_LIGHTMAPS; i++ )
2921         {
2922                 out->lightmapNum[ i ] = -3;
2923                 out->lightmapStyles[ i ] = LS_NONE;
2924                 out->vertexStyles[ i ] = LS_NONE;
2925         }
2926         out->lightmapStyles[ 0 ] = LS_NORMAL;
2927         out->vertexStyles[ 0 ] = LS_NORMAL;
2928
2929         /* lightmap vectors (lod bounds for patches */
2930         VectorCopy( ds->lightmapOrigin, out->lightmapOrigin );
2931         VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );
2932         VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
2933         VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
2934
2935         /* ydnar: gs mods: clear out the plane normal */
2936         if ( ds->planar == qfalse ) {
2937                 VectorClear( out->lightmapVecs[ 2 ] );
2938         }
2939
2940         /* optimize the surface's triangles */
2941         OptimizeTriangleSurface( ds );
2942
2943         /* emit the verts and indexes */
2944         EmitDrawVerts( ds, out );
2945         EmitDrawIndexes( ds, out );
2946
2947         /* add to count */
2948         numSurfacesByType[ ds->type ]++;
2949 }
2950
2951
2952
2953 /*
2954    EmitFaceSurface()
2955    emits a bsp planar winding (brush face) drawsurface
2956  */
2957
2958 static void EmitFaceSurface( mapDrawSurface_t *ds ){
2959         /* strip/fan finding was moved elsewhere */
2960         if ( maxAreaFaceSurface ) {
2961                 MaxAreaFaceSurface( ds );
2962         }
2963         else{
2964                 StripFaceSurface( ds );
2965         }
2966         EmitTriangleSurface( ds );
2967 }
2968
2969
2970 /*
2971    MakeDebugPortalSurfs_r() - ydnar
2972    generates drawsurfaces for passable portals in the bsp
2973  */
2974
2975 static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si ){
2976         int i, k, c, s;
2977         portal_t            *p;
2978         winding_t           *w;
2979         mapDrawSurface_t    *ds;
2980         bspDrawVert_t       *dv;
2981
2982
2983         /* recurse if decision node */
2984         if ( node->planenum != PLANENUM_LEAF ) {
2985                 MakeDebugPortalSurfs_r( node->children[ 0 ], si );
2986                 MakeDebugPortalSurfs_r( node->children[ 1 ], si );
2987                 return;
2988         }
2989
2990         /* don't bother with opaque leaves */
2991         if ( node->opaque ) {
2992                 return;
2993         }
2994
2995         /* walk the list of portals */
2996         for ( c = 0, p = node->portals; p != NULL; c++, p = p->next[ s ] )
2997         {
2998                 /* get winding and side even/odd */
2999                 w = p->winding;
3000                 s = ( p->nodes[ 1 ] == node );
3001
3002                 /* is this a valid portal for this leaf? */
3003                 if ( w && p->nodes[ 0 ] == node ) {
3004                         /* is this portal passable? */
3005                         if ( PortalPassable( p ) == qfalse ) {
3006                                 continue;
3007                         }
3008
3009                         /* check max points */
3010                         if ( w->numpoints > 64 ) {
3011                                 Error( "MakePortalSurfs_r: w->numpoints = %d", w->numpoints );
3012                         }
3013
3014                         /* allocate a drawsurface */
3015                         ds = AllocDrawSurface( SURFACE_FACE );
3016                         ds->shaderInfo = si;
3017                         ds->planar = qtrue;
3018                         ds->sideRef = AllocSideRef( p->side, NULL );
3019                         ds->planeNum = FindFloatPlane( p->plane.normal, p->plane.dist, 0, NULL );
3020                         VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] );
3021                         ds->fogNum = -1;
3022                         ds->numVerts = w->numpoints;
3023                         ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
3024                         memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
3025
3026                         /* walk the winding */
3027                         for ( i = 0; i < ds->numVerts; i++ )
3028                         {
3029                                 /* get vert */
3030                                 dv = ds->verts + i;
3031
3032                                 /* set it */
3033                                 VectorCopy( w->p[ i ], dv->xyz );
3034                                 VectorCopy( p->plane.normal, dv->normal );
3035                                 dv->st[ 0 ] = 0;
3036                                 dv->st[ 1 ] = 0;
3037                                 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
3038                                 {
3039                                         VectorCopy( debugColors[ c % 12 ], dv->color[ k ] );
3040                                         dv->color[ k ][ 3 ] = 32;
3041                                 }
3042                         }
3043                 }
3044         }
3045 }
3046
3047
3048
3049 /*
3050    MakeDebugPortalSurfs() - ydnar
3051    generates drawsurfaces for passable portals in the bsp
3052  */
3053
3054 void MakeDebugPortalSurfs( tree_t *tree ){
3055         shaderInfo_t    *si;
3056
3057
3058         /* note it */
3059         Sys_FPrintf( SYS_VRB, "--- MakeDebugPortalSurfs ---\n" );
3060
3061         /* get portal debug shader */
3062         si = ShaderInfoForShader( "debugportals" );
3063
3064         /* walk the tree */
3065         MakeDebugPortalSurfs_r( tree->headnode, si );
3066 }
3067
3068
3069
3070 /*
3071    MakeFogHullSurfs()
3072    generates drawsurfaces for a foghull (this MUST use a sky shader)
3073  */
3074
3075 void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader ){
3076         shaderInfo_t        *si;
3077         mapDrawSurface_t    *ds;
3078         vec3_t fogMins, fogMaxs;
3079         int i, indexes[] =
3080         {
3081                 0, 1, 2, 0, 2, 3,
3082                 4, 7, 5, 5, 7, 6,
3083                 1, 5, 6, 1, 6, 2,
3084                 0, 4, 5, 0, 5, 1,
3085                 2, 6, 7, 2, 7, 3,
3086                 3, 7, 4, 3, 4, 0
3087         };
3088
3089
3090         /* dummy check */
3091         if ( shader == NULL || shader[ 0 ] == '\0' ) {
3092                 return;
3093         }
3094
3095         /* note it */
3096         Sys_FPrintf( SYS_VRB, "--- MakeFogHullSurfs ---\n" );
3097
3098         /* get hull bounds */
3099         VectorCopy( mapMins, fogMins );
3100         VectorCopy( mapMaxs, fogMaxs );
3101         for ( i = 0; i < 3; i++ )
3102         {
3103                 fogMins[ i ] -= 128;
3104                 fogMaxs[ i ] += 128;
3105         }
3106
3107         /* get foghull shader */
3108         si = ShaderInfoForShader( shader );
3109
3110         /* allocate a drawsurface */
3111         ds = AllocDrawSurface( SURFACE_FOGHULL );
3112         ds->shaderInfo = si;
3113         ds->fogNum = -1;
3114         ds->numVerts = 8;
3115         ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
3116         memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
3117         ds->numIndexes = 36;
3118         ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
3119         memset( ds->indexes, 0, ds->numIndexes * sizeof( *ds->indexes ) );
3120
3121         /* set verts */
3122         VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
3123         VectorSet( ds->verts[ 1 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
3124         VectorSet( ds->verts[ 2 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] );
3125         VectorSet( ds->verts[ 3 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );
3126
3127         VectorSet( ds->verts[ 4 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
3128         VectorSet( ds->verts[ 5 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
3129         VectorSet( ds->verts[ 6 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] );
3130         VectorSet( ds->verts[ 7 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] );
3131
3132         /* set indexes */
3133         memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( *ds->indexes ) );
3134 }
3135
3136
3137
3138 /*
3139    BiasSurfaceTextures()
3140    biases a surface's texcoords as close to 0 as possible
3141  */
3142
3143 void BiasSurfaceTextures( mapDrawSurface_t *ds ){
3144         int i;
3145
3146
3147         /* calculate the surface texture bias */
3148         CalcSurfaceTextureRange( ds );
3149
3150         /* don't bias globaltextured shaders */
3151         if ( ds->shaderInfo->globalTexture ) {
3152                 return;
3153         }
3154
3155         /* bias the texture coordinates */
3156         for ( i = 0; i < ds->numVerts; i++ )
3157         {
3158                 ds->verts[ i ].st[ 0 ] += ds->bias[ 0 ];
3159                 ds->verts[ i ].st[ 1 ] += ds->bias[ 1 ];
3160         }
3161 }
3162
3163
3164
3165 /*
3166    AddSurfaceModelsToTriangle_r()
3167    adds models to a specified triangle, returns the number of models added
3168  */
3169
3170 int AddSurfaceModelsToTriangle_r( mapDrawSurface_t *ds, surfaceModel_t *model, bspDrawVert_t **tri ){
3171         bspDrawVert_t mid, *tri2[ 3 ];
3172         int max, n, localNumSurfaceModels;
3173
3174
3175         /* init */
3176         localNumSurfaceModels = 0;
3177
3178         /* subdivide calc */
3179         {
3180                 int i;
3181                 float       *a, *b, dx, dy, dz, dist, maxDist;
3182
3183
3184                 /* find the longest edge and split it */
3185                 max = -1;
3186                 maxDist = 0.0f;
3187                 for ( i = 0; i < 3; i++ )
3188                 {
3189                         /* get verts */
3190                         a = tri[ i ]->xyz;
3191                         b = tri[ ( i + 1 ) % 3 ]->xyz;
3192
3193                         /* get dists */
3194                         dx = a[ 0 ] - b[ 0 ];
3195                         dy = a[ 1 ] - b[ 1 ];
3196                         dz = a[ 2 ] - b[ 2 ];
3197                         dist = ( dx * dx ) + ( dy * dy ) + ( dz * dz );
3198
3199                         /* longer? */
3200                         if ( dist > maxDist ) {
3201                                 maxDist = dist;
3202                                 max = i;
3203                         }
3204                 }
3205
3206                 /* is the triangle small enough? */
3207                 if ( max < 0 || maxDist <= ( model->density * model->density ) ) {
3208                         float odds, r, angle;
3209                         vec3_t origin, normal, scale, axis[ 3 ], angles;
3210                         m4x4_t transform, temp;
3211
3212
3213                         /* roll the dice (model's odds scaled by vertex alpha) */
3214                         odds = model->odds * ( tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] ) / 765.0f;
3215                         r = Random();
3216                         if ( r > odds ) {
3217                                 return 0;
3218                         }
3219
3220                         /* calculate scale */
3221                         r = model->minScale + Random() * ( model->maxScale - model->minScale );
3222                         VectorSet( scale, r, r, r );
3223
3224                         /* calculate angle */
3225                         angle = model->minAngle + Random() * ( model->maxAngle - model->minAngle );
3226
3227                         /* calculate average origin */
3228                         VectorCopy( tri[ 0 ]->xyz, origin );
3229                         VectorAdd( origin, tri[ 1 ]->xyz, origin );
3230                         VectorAdd( origin, tri[ 2 ]->xyz, origin );
3231                         VectorScale( origin, ( 1.0f / 3.0f ), origin );
3232
3233                         /* clear transform matrix */
3234                         m4x4_identity( transform );
3235
3236                         /* handle oriented models */
3237                         if ( model->oriented ) {
3238                                 /* set angles */
3239                                 VectorSet( angles, 0.0f, 0.0f, angle );
3240
3241                                 /* calculate average normal */
3242                                 VectorCopy( tri[ 0 ]->normal, normal );
3243                                 VectorAdd( normal, tri[ 1 ]->normal, normal );
3244                                 VectorAdd( normal, tri[ 2 ]->normal, normal );
3245                                 if ( VectorNormalize( normal, axis[ 2 ] ) == 0.0f ) {
3246                                         VectorCopy( tri[ 0 ]->normal, axis[ 2 ] );
3247                                 }
3248
3249                                 /* make perpendicular vectors */
3250                                 MakeNormalVectors( axis[ 2 ], axis[ 1 ], axis[ 0 ] );
3251
3252                                 /* copy to matrix */
3253                                 m4x4_identity( temp );
3254                                 temp[ 0 ] = axis[ 0 ][ 0 ]; temp[ 1 ] = axis[ 0 ][ 1 ]; temp[ 2 ] = axis[ 0 ][ 2 ];
3255                                 temp[ 4 ] = axis[ 1 ][ 0 ]; temp[ 5 ] = axis[ 1 ][ 1 ]; temp[ 6 ] = axis[ 1 ][ 2 ];
3256                                 temp[ 8 ] = axis[ 2 ][ 0 ]; temp[ 9 ] = axis[ 2 ][ 1 ]; temp[ 10 ] = axis[ 2 ][ 2 ];
3257
3258                                 /* scale */
3259                                 m4x4_scale_by_vec3( temp, scale );
3260
3261                                 /* rotate around z axis */
3262                                 m4x4_rotate_by_vec3( temp, angles, eXYZ );
3263
3264                                 /* translate */
3265                                 m4x4_translate_by_vec3( transform, origin );
3266
3267                                 /* tranform into axis space */
3268                                 m4x4_multiply_by_m4x4( transform, temp );
3269                         }
3270
3271                         /* handle z-up models */
3272                         else
3273                         {
3274                                 /* set angles */
3275                                 VectorSet( angles, 0.0f, 0.0f, angle );
3276
3277                                 /* set matrix */
3278                                 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
3279                         }
3280
3281                         /* insert the model */
3282                         InsertModel( (char *) model->model, 0, 0, transform, NULL, ds->celShader, ds->entityNum, ds->castShadows, ds->recvShadows, 0, ds->lightmapScale, 0, 0 );
3283
3284                         /* return to sender */
3285                         return 1;
3286                 }
3287         }
3288
3289         /* split the longest edge and map it */
3290         LerpDrawVert( tri[ max ], tri[ ( max + 1 ) % 3 ], &mid );
3291
3292         /* recurse to first triangle */
3293         VectorCopy( tri, tri2 );
3294         tri2[ max ] = &mid;
3295         n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3296         if ( n < 0 ) {
3297                 return n;
3298         }
3299         localNumSurfaceModels += n;
3300
3301         /* recurse to second triangle */
3302         VectorCopy( tri, tri2 );
3303         tri2[ ( max + 1 ) % 3 ] = &mid;
3304         n = AddSurfaceModelsToTriangle_r( ds, model, tri2 );
3305         if ( n < 0 ) {
3306                 return n;
3307         }
3308         localNumSurfaceModels += n;
3309
3310         /* return count */
3311         return localNumSurfaceModels;
3312 }
3313
3314
3315
3316 /*
3317    AddSurfaceModels()
3318    adds a surface's shader models to the surface
3319  */
3320
3321 int AddSurfaceModels( mapDrawSurface_t *ds ){
3322         surfaceModel_t  *model;
3323         int i, x, y, n, pw[ 5 ], r, localNumSurfaceModels, iterations;
3324         mesh_t src, *mesh, *subdivided;
3325         bspDrawVert_t centroid, *tri[ 3 ];
3326         float alpha;
3327
3328
3329         /* dummy check */
3330         if ( ds == NULL || ds->shaderInfo == NULL || ds->shaderInfo->surfaceModel == NULL ) {
3331                 return 0;
3332         }
3333
3334         /* init */
3335         localNumSurfaceModels = 0;
3336
3337         /* walk the model list */
3338         for ( model = ds->shaderInfo->surfaceModel; model != NULL; model = model->next )
3339         {
3340                 /* switch on type */
3341                 switch ( ds->type )
3342                 {
3343                 /* handle brush faces and decals */
3344                 case SURFACE_FACE:
3345                 case SURFACE_DECAL:
3346                         /* calculate centroid */
3347                         memset( &centroid, 0, sizeof( centroid ) );
3348                         alpha = 0.0f;
3349
3350                         /* walk verts */
3351                         for ( i = 0; i < ds->numVerts; i++ )
3352                         {
3353                                 VectorAdd( centroid.xyz, ds->verts[ i ].xyz, centroid.xyz );
3354                                 VectorAdd( centroid.normal, ds->verts[ i ].normal, centroid.normal );
3355                                 centroid.st[ 0 ] += ds->verts[ i ].st[ 0 ];
3356                                 centroid.st[ 1 ] += ds->verts[ i ].st[ 1 ];
3357                                 alpha += ds->verts[ i ].color[ 0 ][ 3 ];
3358                         }
3359
3360                         /* average */
3361                         centroid.xyz[ 0 ] /= ds->numVerts;
3362                         centroid.xyz[ 1 ] /= ds->numVerts;
3363                         centroid.xyz[ 2 ] /= ds->numVerts;
3364                         if ( VectorNormalize( centroid.normal, centroid.normal ) == 0.0f ) {
3365                                 VectorCopy( ds->verts[ 0 ].normal, centroid.normal );
3366                         }
3367                         centroid.st[ 0 ]  /= ds->numVerts;
3368                         centroid.st[ 1 ]  /= ds->numVerts;
3369                         alpha /= ds->numVerts;
3370                         centroid.color[ 0 ][ 0 ] = 0xFF;
3371                         centroid.color[ 0 ][ 1 ] = 0xFF;
3372                         centroid.color[ 0 ][ 2 ] = 0xFF;
3373                         centroid.color[ 0 ][ 2 ] = ( alpha > 255.0f ? 0xFF : alpha );
3374
3375                         /* head vert is centroid */
3376                         tri[ 0 ] = &centroid;
3377
3378                         /* walk fanned triangles */
3379                         for ( i = 0; i < ds->numVerts; i++ )
3380                         {
3381                                 /* set triangle */
3382                                 tri[ 1 ] = &ds->verts[ i ];
3383                                 tri[ 2 ] = &ds->verts[ ( i + 1 ) % ds->numVerts ];
3384
3385                                 /* create models */
3386                                 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3387                                 if ( n < 0 ) {
3388                                         return n;
3389                                 }
3390                                 localNumSurfaceModels += n;
3391                         }
3392                         break;
3393
3394                 /* handle patches */
3395                 case SURFACE_PATCH:
3396                         /* subdivide the surface */
3397                         src.width = ds->patchWidth;
3398                         src.height = ds->patchHeight;
3399                         src.verts = ds->verts;
3400                         //%     subdivided = SubdivideMesh( src, 8.0f, 512 );
3401                         iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions );
3402                         subdivided = SubdivideMesh2( src, iterations );
3403
3404                         /* fit it to the curve and remove colinear verts on rows/columns */
3405                         PutMeshOnCurve( *subdivided );
3406                         mesh = RemoveLinearMeshColumnsRows( subdivided );
3407                         FreeMesh( subdivided );
3408
3409                         /* subdivide each quad to place the models */
3410                         for ( y = 0; y < ( mesh->height - 1 ); y++ )
3411                         {
3412                                 for ( x = 0; x < ( mesh->width - 1 ); x++ )
3413                                 {
3414                                         /* set indexes */
3415                                         pw[ 0 ] = x + ( y * mesh->width );
3416                                         pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
3417                                         pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
3418                                         pw[ 3 ] = x + 1 + ( y * mesh->width );
3419                                         pw[ 4 ] = x + ( y * mesh->width );      /* same as pw[ 0 ] */
3420
3421                                         /* set radix */
3422                                         r = ( x + y ) & 1;
3423
3424                                         /* triangle 1 */
3425                                         tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3426                                         tri[ 1 ] = &mesh->verts[ pw[ r + 1 ] ];
3427                                         tri[ 2 ] = &mesh->verts[ pw[ r + 2 ] ];
3428                                         n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3429                                         if ( n < 0 ) {
3430                                                 return n;
3431                                         }
3432                                         localNumSurfaceModels += n;
3433
3434                                         /* triangle 2 */
3435                                         tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
3436                                         tri[ 1 ] = &mesh->verts[ pw[ r + 2 ] ];
3437                                         tri[ 2 ] = &mesh->verts[ pw[ r + 3 ] ];
3438                                         n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3439                                         if ( n < 0 ) {
3440                                                 return n;
3441                                         }
3442                                         localNumSurfaceModels += n;
3443                                 }
3444                         }
3445
3446                         /* free the subdivided mesh */
3447                         FreeMesh( mesh );
3448                         break;
3449
3450                 /* handle triangle surfaces */
3451                 case SURFACE_TRIANGLES:
3452                 case SURFACE_FORCED_META:
3453                 case SURFACE_META:
3454                         /* walk the triangle list */
3455                         for ( i = 0; i < ds->numIndexes; i += 3 )
3456                         {
3457                                 tri[ 0 ] = &ds->verts[ ds->indexes[ i ] ];
3458                                 tri[ 1 ] = &ds->verts[ ds->indexes[ i + 1 ] ];
3459                                 tri[ 2 ] = &ds->verts[ ds->indexes[ i + 2 ] ];
3460                                 n = AddSurfaceModelsToTriangle_r( ds, model, tri );
3461                                 if ( n < 0 ) {
3462                                         return n;
3463                                 }
3464                                 localNumSurfaceModels += n;
3465                         }
3466                         break;
3467
3468                 /* no support for flares, foghull, etc */
3469                 default:
3470                         break;
3471                 }
3472         }
3473
3474         /* return count */
3475         return localNumSurfaceModels;
3476 }
3477
3478
3479
3480 /*
3481    AddEntitySurfaceModels() - ydnar
3482    adds surfacemodels to an entity's surfaces
3483  */
3484
3485 void AddEntitySurfaceModels( entity_t *e ){
3486         int i;
3487
3488
3489         /* note it */
3490         Sys_FPrintf( SYS_VRB, "--- AddEntitySurfaceModels ---\n" );
3491
3492         /* walk the surface list */
3493         for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3494                 numSurfaceModels += AddSurfaceModels( &mapDrawSurfs[ i ] );
3495 }
3496
3497
3498
3499 /*
3500    VolumeColorMods() - ydnar
3501    applies brush/volumetric color/alpha modulation to vertexes
3502  */
3503
3504 static void VolumeColorMods( entity_t *e, mapDrawSurface_t *ds ){
3505         int i, j;
3506         float d;
3507         brush_t     *b;
3508         plane_t     *plane;
3509
3510
3511         /* early out */
3512         if ( e->colorModBrushes == NULL ) {
3513                 return;
3514         }
3515
3516         /* iterate brushes */
3517         for ( b = e->colorModBrushes; b != NULL; b = b->nextColorModBrush )
3518         {
3519                 /* worldspawn alpha brushes affect all, grouped ones only affect original entity */
3520                 if ( b->entityNum != 0 && b->entityNum != ds->entityNum ) {
3521                         continue;
3522                 }
3523
3524                 /* test bbox */
3525                 if ( b->mins[ 0 ] > ds->maxs[ 0 ] || b->maxs[ 0 ] < ds->mins[ 0 ] ||
3526                          b->mins[ 1 ] > ds->maxs[ 1 ] || b->maxs[ 1 ] < ds->mins[ 1 ] ||
3527                          b->mins[ 2 ] > ds->maxs[ 2 ] || b->maxs[ 2 ] < ds->mins[ 2 ] ) {
3528                         continue;
3529                 }
3530
3531                 /* iterate verts */
3532                 for ( i = 0; i < ds->numVerts; i++ )
3533                 {
3534                         /* iterate planes */
3535                         for ( j = 0; j < b->numsides; j++ )
3536                         {
3537                                 /* point-plane test */
3538                                 plane = &mapplanes[ b->sides[ j ].planenum ];
3539                                 d = DotProduct( ds->verts[ i ].xyz, plane->normal ) - plane->dist;
3540                                 if ( d > 1.0f ) {
3541                                         break;
3542                                 }
3543                         }
3544
3545                         /* apply colormods */
3546                         if ( j == b->numsides ) {
3547                                 ColorMod( b->contentShader->colorMod, 1, &ds->verts[ i ] );
3548                         }
3549                 }
3550         }
3551 }
3552
3553
3554
3555 /*
3556    FilterDrawsurfsIntoTree()
3557    upon completion, all drawsurfs that actually generate a reference
3558    will have been emited to the bspfile arrays, and the references
3559    will have valid final indexes
3560  */
3561
3562 void FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree ){
3563         int i, j;
3564         mapDrawSurface_t    *ds;
3565         shaderInfo_t        *si;
3566         vec3_t origin, mins, maxs;
3567         int refs;
3568         int numSurfs, numRefs, numSkyboxSurfaces;
3569         qboolean sb;
3570
3571
3572         /* note it */
3573         Sys_FPrintf( SYS_VRB, "--- FilterDrawsurfsIntoTree ---\n" );
3574
3575         /* filter surfaces into the tree */
3576         numSurfs = 0;
3577         numRefs = 0;
3578         numSkyboxSurfaces = 0;
3579         for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
3580         {
3581                 /* get surface and try to early out */
3582                 ds = &mapDrawSurfs[ i ];
3583                 if ( ds->numVerts == 0 && ds->type != SURFACE_FLARE && ds->type != SURFACE_SHADER ) {
3584                         continue;
3585                 }
3586
3587                 /* get shader */
3588                 si = ds->shaderInfo;
3589
3590                 /* ydnar: skybox surfaces are special */
3591                 if ( ds->skybox ) {
3592                         refs = AddReferenceToTree_r( ds, tree->headnode, qtrue );
3593                         ds->skybox = qfalse;
3594                         sb = qtrue;
3595                 }
3596                 else
3597                 {
3598                         sb = qfalse;
3599
3600                         /* refs initially zero */
3601                         refs = 0;
3602
3603                         /* apply texture coordinate mods */
3604                         for ( j = 0; j < ds->numVerts; j++ )
3605                                 TCMod( si->mod, ds->verts[ j ].st );
3606
3607                         /* ydnar: apply shader colormod */
3608                         ColorMod( ds->shaderInfo->colorMod, ds->numVerts, ds->verts );
3609
3610                         /* ydnar: apply brush colormod */
3611                         VolumeColorMods( e, ds );
3612
3613                         /* ydnar: make fur surfaces */
3614                         if ( si->furNumLayers > 0 ) {
3615                                 Fur( ds );
3616                         }
3617
3618                         /* ydnar/sd: make foliage surfaces */
3619                         if ( si->foliage != NULL ) {
3620                                 Foliage( ds );
3621                         }
3622
3623                         /* create a flare surface if necessary */
3624                         if ( si->flareShader != NULL && si->flareShader[ 0 ] ) {
3625                                 AddSurfaceFlare( ds, e->origin );
3626                         }
3627
3628                         /* ydnar: don't emit nodraw surfaces (like nodraw fog) */
3629                         if ( ( si->compileFlags & C_NODRAW ) && ds->type != SURFACE_PATCH ) {
3630                                 continue;
3631                         }
3632
3633                         /* ydnar: bias the surface textures */
3634                         BiasSurfaceTextures( ds );
3635
3636                         /* ydnar: globalizing of fog volume handling (eek a hack) */
3637                         if ( e != entities && si->noFog == qfalse ) {
3638                                 /* find surface origin and offset by entity origin */
3639                                 VectorAdd( ds->mins, ds->maxs, origin );
3640                                 VectorScale( origin, 0.5f, origin );
3641                                 VectorAdd( origin, e->origin, origin );
3642
3643                                 VectorAdd( ds->mins, e->origin, mins );
3644                                 VectorAdd( ds->maxs, e->origin, maxs );
3645
3646                                 /* set the fog number for this surface */
3647                                 ds->fogNum = FogForBounds( mins, maxs, 1.0f );  //%     FogForPoint( origin, 0.0f );
3648                         }
3649                 }
3650
3651                 /* ydnar: remap shader */
3652                 if ( ds->shaderInfo->remapShader && ds->shaderInfo->remapShader[ 0 ] ) {
3653                         ds->shaderInfo = ShaderInfoForShader( ds->shaderInfo->remapShader );
3654                 }
3655
3656                 /* ydnar: gs mods: handle the various types of surfaces */
3657                 switch ( ds->type )
3658                 {
3659                 /* handle brush faces */
3660                 case SURFACE_FACE:
3661                 case SURFACE_DECAL:
3662                         if ( refs == 0 ) {
3663                                 refs = FilterFaceIntoTree( ds, tree );
3664                         }
3665                         if ( refs > 0 ) {
3666                                 EmitFaceSurface( ds );
3667                         }
3668                         break;
3669
3670                 /* handle patches */
3671                 case SURFACE_PATCH:
3672                         if ( refs == 0 ) {
3673                                 refs = FilterPatchIntoTree( ds, tree );
3674                         }
3675                         if ( refs > 0 ) {
3676                                 EmitPatchSurface( e, ds );
3677                         }
3678                         break;
3679
3680                 /* handle triangle surfaces */
3681                 case SURFACE_TRIANGLES:
3682                 case SURFACE_FORCED_META:
3683                 case SURFACE_META:
3684                         //%     Sys_FPrintf( SYS_VRB, "Surface %4d: [%1d] %4d verts %s\n", numSurfs, ds->planar, ds->numVerts, si->shader );
3685                         if ( refs == 0 ) {
3686                                 refs = FilterTrianglesIntoTree( ds, tree );
3687                         }
3688                         if ( refs > 0 ) {
3689                                 EmitTriangleSurface( ds );
3690                         }
3691                         break;
3692
3693                 /* handle foliage surfaces (splash damage/wolf et) */
3694                 case SURFACE_FOLIAGE:
3695                         //%     Sys_FPrintf( SYS_VRB, "Surface %4d: [%d] %4d verts %s\n", numSurfs, ds->numFoliageInstances, ds->numVerts, si->shader );
3696                         if ( refs == 0 ) {
3697                                 refs = FilterFoliageIntoTree( ds, tree );
3698                         }
3699                         if ( refs > 0 ) {
3700                                 EmitTriangleSurface( ds );
3701                         }
3702                         break;
3703
3704                 /* handle foghull surfaces */
3705                 case SURFACE_FOGHULL:
3706                         if ( refs == 0 ) {
3707                                 refs = AddReferenceToTree_r( ds, tree->headnode, qfalse );
3708                         }
3709                         if ( refs > 0 ) {
3710                                 EmitTriangleSurface( ds );
3711                         }
3712                         break;
3713
3714                 /* handle flares */
3715                 case SURFACE_FLARE:
3716                         if ( refs == 0 ) {
3717                                 refs = FilterFlareSurfIntoTree( ds, tree );
3718                         }
3719                         if ( refs > 0 ) {
3720                                 EmitFlareSurface( ds );
3721                         }
3722                         break;
3723
3724                 /* handle shader-only surfaces */
3725                 case SURFACE_SHADER:
3726                         refs = 1;
3727                         EmitFlareSurface( ds );
3728                         break;
3729
3730                 /* no references */
3731                 default:
3732                         refs = 0;
3733                         break;
3734                 }
3735
3736                 /* maybe surface got marked as skybox again */
3737                 /* if we keep that flag, it will get scaled up AGAIN */
3738                 if ( sb ) {
3739                         ds->skybox = qfalse;
3740                 }
3741
3742                 /* tot up the references */
3743                 if ( refs > 0 ) {
3744                         /* tot up counts */
3745                         numSurfs++;
3746                         numRefs += refs;
3747
3748                         /* emit extra surface data */
3749                         SetSurfaceExtra( ds, numBSPDrawSurfaces - 1 );
3750                         //%     Sys_FPrintf( SYS_VRB, "%d verts %d indexes\n", ds->numVerts, ds->numIndexes );
3751
3752                         /* one last sanity check */
3753                         {
3754                                 bspDrawSurface_t    *out;
3755                                 out = &bspDrawSurfaces[ numBSPDrawSurfaces - 1 ];
3756                                 if ( out->numVerts == 3 && out->numIndexes > 3 ) {
3757                                         Sys_FPrintf( SYS_WRN, "WARNING: Potentially bad %s surface (%d: %d, %d)\n     %s\n",
3758                                                                 surfaceTypes[ ds->type ],
3759                                                                 numBSPDrawSurfaces - 1, out->numVerts, out->numIndexes, si->shader );
3760                                 }
3761                         }
3762
3763                         /* ydnar: handle skybox surfaces */
3764                         if ( ds->skybox ) {
3765                                 MakeSkyboxSurface( ds );
3766                                 numSkyboxSurfaces++;
3767                         }
3768                 }
3769         }
3770
3771         /* emit some statistics */
3772         Sys_FPrintf( SYS_VRB, "%9d references\n", numRefs );
3773         Sys_FPrintf( SYS_VRB, "%9d (%d) emitted drawsurfs\n", numSurfs, numBSPDrawSurfaces );
3774         Sys_FPrintf( SYS_VRB, "%9d stripped face surfaces\n", numStripSurfaces );
3775         Sys_FPrintf( SYS_VRB, "%9d fanned face surfaces\n", numFanSurfaces );
3776         Sys_FPrintf( SYS_VRB, "%9d maxarea'd face surfaces\n", numMaxAreaSurfaces );
3777         Sys_FPrintf( SYS_VRB, "%9d surface models generated\n", numSurfaceModels );
3778         Sys_FPrintf( SYS_VRB, "%9d skybox surfaces generated\n", numSkyboxSurfaces );
3779         for ( i = 0; i < NUM_SURFACE_TYPES; i++ )
3780                 Sys_FPrintf( SYS_VRB, "%9d %s surfaces\n", numSurfacesByType[ i ], surfaceTypes[ i ] );
3781
3782         Sys_FPrintf( SYS_VRB, "%9d redundant indexes supressed, saving %d Kbytes\n", numRedundantIndexes, ( numRedundantIndexes * 4 / 1024 ) );
3783 }