]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/light_bounce.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / tools / quake3 / q3map2 / light_bounce.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 LIGHT_BOUNCE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* functions */
42
43 /*
44    RadFreeLights()
45    deletes any existing lights, freeing up memory for the next bounce
46  */
47
48 void RadFreeLights( void ){
49         light_t     *light, *next;
50
51
52         /* delete lights */
53         for ( light = lights; light; light = next )
54         {
55                 next = light->next;
56                 if ( light->w != NULL ) {
57                         FreeWinding( light->w );
58                 }
59                 free( light );
60         }
61         numLights = 0;
62         lights = NULL;
63 }
64
65
66
67 /*
68    RadClipWindingEpsilon()
69    clips a rad winding by a plane
70    based off the regular clip winding code
71  */
72
73 static void RadClipWindingEpsilon( radWinding_t *in, vec3_t normal, vec_t dist,
74                                                                    vec_t epsilon, radWinding_t *front, radWinding_t *back, clipWork_t *cw ){
75         vec_t           *dists;
76         int             *sides;
77         int counts[ 3 ];
78         vec_t dot;                  /* ydnar: changed from static b/c of threading */ /* VC 4.2 optimizer bug if not static? */
79         int i, j, k;
80         radVert_t       *v1, *v2, mid;
81         int maxPoints;
82
83
84         /* crutch */
85         dists = cw->dists;
86         sides = cw->sides;
87
88         /* clear counts */
89         counts[ 0 ] = counts[ 1 ] = counts[ 2 ] = 0;
90
91         /* determine sides for each point */
92         for ( i = 0; i < in->numVerts; i++ )
93         {
94                 dot = DotProduct( in->verts[ i ].xyz, normal );
95                 dot -= dist;
96                 dists[ i ] = dot;
97                 if ( dot > epsilon ) {
98                         sides[ i ] = SIDE_FRONT;
99                 }
100                 else if ( dot < -epsilon ) {
101                         sides[ i ] = SIDE_BACK;
102                 }
103                 else{
104                         sides[ i ] = SIDE_ON;
105                 }
106                 counts[ sides[ i ] ]++;
107         }
108         sides[ i ] = sides[ 0 ];
109         dists[ i ] = dists[ 0 ];
110
111         /* clear front and back */
112         front->numVerts = back->numVerts = 0;
113
114         /* handle all on one side cases */
115         if ( counts[ 0 ] == 0 ) {
116                 memcpy( back, in, sizeof( radWinding_t ) );
117                 return;
118         }
119         if ( counts[ 1 ] == 0 ) {
120                 memcpy( front, in, sizeof( radWinding_t ) );
121                 return;
122         }
123
124         /* setup windings */
125         maxPoints = in->numVerts + 4;
126
127         /* do individual verts */
128         for ( i = 0; i < in->numVerts; i++ )
129         {
130                 /* do simple vertex copies first */
131                 v1 = &in->verts[ i ];
132
133                 if ( sides[ i ] == SIDE_ON ) {
134                         memcpy( &front->verts[ front->numVerts++ ], v1, sizeof( radVert_t ) );
135                         memcpy( &back->verts[ back->numVerts++ ], v1, sizeof( radVert_t ) );
136                         continue;
137                 }
138
139                 if ( sides[ i ] == SIDE_FRONT ) {
140                         memcpy( &front->verts[ front->numVerts++ ], v1, sizeof( radVert_t ) );
141                 }
142
143                 if ( sides[ i ] == SIDE_BACK ) {
144                         memcpy( &back->verts[ back->numVerts++ ], v1, sizeof( radVert_t ) );
145                 }
146
147                 if ( sides[ i + 1 ] == SIDE_ON || sides[ i + 1 ] == sides[ i ] ) {
148                         continue;
149                 }
150
151                 /* generate a split vertex */
152                 v2 = &in->verts[ ( i + 1 ) % in->numVerts ];
153
154                 dot = dists[ i ] / ( dists[ i ] - dists[ i + 1 ] );
155
156                 /* average vertex values */
157                 for ( j = 0; j < 4; j++ )
158                 {
159                         /* color */
160                         if ( j < 4 ) {
161                                 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
162                                         mid.color[ k ][ j ] = v1->color[ k ][ j ] + dot * ( v2->color[ k ][ j ] - v1->color[ k ][ j ] );
163                         }
164
165                         /* xyz, normal */
166                         if ( j < 3 ) {
167                                 mid.xyz[ j ] = v1->xyz[ j ] + dot * ( v2->xyz[ j ] - v1->xyz[ j ] );
168                                 mid.normal[ j ] = v1->normal[ j ] + dot * ( v2->normal[ j ] - v1->normal[ j ] );
169                         }
170
171                         /* st, lightmap */
172                         if ( j < 2 ) {
173                                 mid.st[ j ] = v1->st[ j ] + dot * ( v2->st[ j ] - v1->st[ j ] );
174                                 for ( k = 0; k < MAX_LIGHTMAPS; k++ )
175                                         mid.lightmap[ k ][ j ] = v1->lightmap[ k ][ j ] + dot * ( v2->lightmap[ k ][ j ] - v1->lightmap[ k ][ j ] );
176                         }
177                 }
178
179                 /* normalize the averaged normal */
180                 VectorNormalize( mid.normal, mid.normal );
181
182                 /* copy the midpoint to both windings */
183                 memcpy( &front->verts[ front->numVerts++ ], &mid, sizeof( radVert_t ) );
184                 memcpy( &back->verts[ back->numVerts++ ], &mid, sizeof( radVert_t ) );
185         }
186
187         /* error check */
188         if ( front->numVerts > maxPoints || front->numVerts > maxPoints ) {
189                 Error( "RadClipWindingEpsilon: points exceeded estimate" );
190         }
191         if ( front->numVerts > MAX_POINTS_ON_WINDING || front->numVerts > MAX_POINTS_ON_WINDING ) {
192                 Error( "RadClipWindingEpsilon: MAX_POINTS_ON_WINDING" );
193         }
194 }
195
196
197
198
199
200 /*
201    RadSampleImage()
202    samples a texture image for a given color
203    returns qfalse if pixels are bad
204  */
205
206 qboolean RadSampleImage( byte *pixels, int width, int height, float st[ 2 ], float color[ 4 ] ){
207         float sto[ 2 ];
208         int x, y;
209
210
211         /* clear color first */
212         color[ 0 ] = color[ 1 ] = color[ 2 ] = color[ 3 ] = 255;
213
214         /* dummy check */
215         if ( pixels == NULL || width < 1 || height < 1 ) {
216                 return qfalse;
217         }
218
219         /* bias st */
220         sto[ 0 ] = st[ 0 ];
221         while ( sto[ 0 ] < 0.0f )
222                 sto[ 0 ] += 1.0f;
223         sto[ 1 ] = st[ 1 ];
224         while ( sto[ 1 ] < 0.0f )
225                 sto[ 1 ] += 1.0f;
226
227         /* get offsets */
228         x = ( (float) width * sto[ 0 ] ) + 0.5f;
229         x %= width;
230         y = ( (float) height * sto[ 1 ] )  + 0.5f;
231         y %= height;
232
233         /* get pixel */
234         pixels += ( y * width * 4 ) + ( x * 4 );
235         VectorCopy( pixels, color );
236         color[ 3 ] = pixels[ 3 ];
237         return qtrue;
238 }
239
240
241
242 /*
243    RadSample()
244    samples a fragment's lightmap or vertex color and returns an
245    average color and a color gradient for the sample
246  */
247
248 #define MAX_SAMPLES         150
249 #define SAMPLE_GRANULARITY  6
250
251 static void RadSample( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si, radWinding_t *rw, vec3_t average, vec3_t gradient, int *style ){
252         int i, j, k, l, v, x, y, samples;
253         vec3_t color, mins, maxs;
254         vec4_t textureColor;
255         float alpha, alphaI, bf;
256         vec3_t blend;
257         float st[ 2 ], lightmap[ 2 ], *radLuxel;
258         radVert_t   *rv[ 3 ];
259
260
261         /* initial setup */
262         ClearBounds( mins, maxs );
263         VectorClear( average );
264         VectorClear( gradient );
265         alpha = 0;
266
267         /* dummy check */
268         if ( rw == NULL || rw->numVerts < 3 ) {
269                 return;
270         }
271
272         /* start sampling */
273         samples = 0;
274
275         /* sample vertex colors if no lightmap or this is the initial pass */
276         if ( lm == NULL || lm->radLuxels[ lightmapNum ] == NULL || bouncing == qfalse ) {
277                 for ( samples = 0; samples < rw->numVerts; samples++ )
278                 {
279                         /* multiply by texture color */
280                         if ( !RadSampleImage( si->lightImage->pixels, si->lightImage->width, si->lightImage->height, rw->verts[ samples ].st, textureColor ) ) {
281                                 VectorCopy( si->averageColor, textureColor );
282                                 textureColor[ 4 ] = 255.0f;
283                         }
284                         for ( i = 0; i < 3; i++ )
285                                 color[ i ] = ( textureColor[ i ] / 255 ) * ( rw->verts[ samples ].color[ lightmapNum ][ i ] / 255.0f );
286
287                         AddPointToBounds( color, mins, maxs );
288                         VectorAdd( average, color, average );
289
290                         /* get alpha */
291                         alpha += ( textureColor[ 3 ] / 255.0f ) * ( rw->verts[ samples ].color[ lightmapNum ][ 3 ] / 255.0f );
292                 }
293
294                 /* set style */
295                 *style = ds->vertexStyles[ lightmapNum ];
296         }
297
298         /* sample lightmap */
299         else
300         {
301                 /* fracture the winding into a fan (including degenerate tris) */
302                 for ( v = 1; v < ( rw->numVerts - 1 ) && samples < MAX_SAMPLES; v++ )
303                 {
304                         /* get a triangle */
305                         rv[ 0 ] = &rw->verts[ 0 ];
306                         rv[ 1 ] = &rw->verts[ v ];
307                         rv[ 2 ] = &rw->verts[ v + 1 ];
308
309                         /* this code is embarassing (really should just rasterize the triangle) */
310                         for ( i = 1; i < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; i++ )
311                         {
312                                 for ( j = 1; j < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; j++ )
313                                 {
314                                         for ( k = 1; k < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; k++ )
315                                         {
316                                                 /* create a blend vector (barycentric coordinates) */
317                                                 blend[ 0 ] = i;
318                                                 blend[ 1 ] = j;
319                                                 blend[ 2 ] = k;
320                                                 bf = ( 1.0 / ( blend[ 0 ] + blend[ 1 ] + blend[ 2 ] ) );
321                                                 VectorScale( blend, bf, blend );
322
323                                                 /* create a blended sample */
324                                                 st[ 0 ] = st[ 1 ] = 0.0f;
325                                                 lightmap[ 0 ] = lightmap[ 1 ] = 0.0f;
326                                                 alphaI = 0.0f;
327                                                 for ( l = 0; l < 3; l++ )
328                                                 {
329                                                         st[ 0 ] += ( rv[ l ]->st[ 0 ] * blend[ l ] );
330                                                         st[ 1 ] += ( rv[ l ]->st[ 1 ] * blend[ l ] );
331                                                         lightmap[ 0 ] += ( rv[ l ]->lightmap[ lightmapNum ][ 0 ] * blend[ l ] );
332                                                         lightmap[ 1 ] += ( rv[ l ]->lightmap[ lightmapNum ][ 1 ] * blend[ l ] );
333                                                         alphaI += ( rv[ l ]->color[ lightmapNum ][ 3 ] * blend[ l ] );
334                                                 }
335
336                                                 /* get lightmap xy coords */
337                                                 x = lightmap[ 0 ] / (float) superSample;
338                                                 y = lightmap[ 1 ] / (float) superSample;
339                                                 if ( x < 0 ) {
340                                                         x = 0;
341                                                 }
342                                                 else if ( x >= lm->w ) {
343                                                         x = lm->w - 1;
344                                                 }
345                                                 if ( y < 0 ) {
346                                                         y = 0;
347                                                 }
348                                                 else if ( y >= lm->h ) {
349                                                         y = lm->h - 1;
350                                                 }
351
352                                                 /* get radiosity luxel */
353                                                 radLuxel = RAD_LUXEL( lightmapNum, x, y );
354
355                                                 /* ignore unlit/unused luxels */
356                                                 if ( radLuxel[ 0 ] < 0.0f ) {
357                                                         continue;
358                                                 }
359
360                                                 /* inc samples */
361                                                 samples++;
362
363                                                 /* multiply by texture color */
364                                                 if ( !RadSampleImage( si->lightImage->pixels, si->lightImage->width, si->lightImage->height, st, textureColor ) ) {
365                                                         VectorCopy( si->averageColor, textureColor );
366                                                         textureColor[ 4 ] = 255;
367                                                 }
368                                                 for ( i = 0; i < 3; i++ )
369                                                         color[ i ] = ( textureColor[ i ] / 255 ) * ( radLuxel[ i ] / 255 );
370
371                                                 AddPointToBounds( color, mins, maxs );
372                                                 VectorAdd( average, color, average );
373
374                                                 /* get alpha */
375                                                 alpha += ( textureColor[ 3 ] / 255 ) * ( alphaI / 255 );
376                                         }
377                                 }
378                         }
379                 }
380
381                 /* set style */
382                 *style = ds->lightmapStyles[ lightmapNum ];
383         }
384
385         /* any samples? */
386         if ( samples <= 0 ) {
387                 return;
388         }
389
390         /* average the color */
391         VectorScale( average, ( 1.0 / samples ), average );
392
393         /* create the color gradient */
394         //%     VectorSubtract( maxs, mins, delta );
395
396         /* new: color gradient will always be 0-1.0, expressed as the range of light relative to overall light */
397         //%     gradient[ 0 ] = maxs[ 0 ] > 0.0f ? (maxs[ 0 ] - mins[ 0 ]) / maxs[ 0 ] : 0.0f;
398         //%     gradient[ 1 ] = maxs[ 1 ] > 0.0f ? (maxs[ 1 ] - mins[ 1 ]) / maxs[ 1 ] : 0.0f;
399         //%     gradient[ 2 ] = maxs[ 2 ] > 0.0f ? (maxs[ 2 ] - mins[ 2 ]) / maxs[ 2 ] : 0.0f;
400
401         /* newer: another contrast function */
402         for ( i = 0; i < 3; i++ )
403                 gradient[ i ] = ( maxs[ i ] - mins[ i ] ) * maxs[ i ];
404 }
405
406
407
408 /*
409    RadSubdivideDiffuseLight()
410    subdivides a radiosity winding until it is smaller than subdivide, then generates an area light
411  */
412
413 #define RADIOSITY_MAX_GRADIENT      0.75f   //% 0.25f
414 #define RADIOSITY_VALUE             500.0f
415 #define RADIOSITY_MIN               0.0001f
416 #define RADIOSITY_CLIP_EPSILON      0.125f
417
418 static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si,
419                                                                           float scale, float subdivide, qboolean original, radWinding_t *rw, clipWork_t *cw ){
420         int i, style;
421         float dist, area, value;
422         vec3_t mins, maxs, normal, d1, d2, cross, color, gradient;
423         light_t         *light, *splash;
424         winding_t       *w;
425
426
427         /* dummy check */
428         if ( rw == NULL || rw->numVerts < 3 ) {
429                 return;
430         }
431
432         /* get bounds for winding */
433         ClearBounds( mins, maxs );
434         for ( i = 0; i < rw->numVerts; i++ )
435                 AddPointToBounds( rw->verts[ i ].xyz, mins, maxs );
436
437         /* subdivide if necessary */
438         for ( i = 0; i < 3; i++ )
439         {
440                 if ( maxs[ i ] - mins[ i ] > subdivide ) {
441                         radWinding_t front, back;
442
443
444                         /* make axial plane */
445                         VectorClear( normal );
446                         normal[ i ] = 1;
447                         dist = ( maxs[ i ] + mins[ i ] ) * 0.5f;
448
449                         /* clip the winding */
450                         RadClipWindingEpsilon( rw, normal, dist, RADIOSITY_CLIP_EPSILON, &front, &back, cw );
451
452                         /* recurse */
453                         RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qfalse, &front, cw );
454                         RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qfalse, &back, cw );
455                         return;
456                 }
457         }
458
459         /* check area */
460         area = 0.0f;
461         for ( i = 2; i < rw->numVerts; i++ )
462         {
463                 VectorSubtract( rw->verts[ i - 1 ].xyz, rw->verts[ 0 ].xyz, d1 );
464                 VectorSubtract( rw->verts[ i ].xyz, rw->verts[ 0 ].xyz, d2 );
465                 CrossProduct( d1, d2, cross );
466                 area += 0.5f * VectorLength( cross );
467         }
468         if ( area < 1.0f || area > 20000000.0f ) {
469                 return;
470         }
471
472         /* more subdivision may be necessary */
473         if ( bouncing ) {
474                 /* get color sample for the surface fragment */
475                 RadSample( lightmapNum, ds, lm, si, rw, color, gradient, &style );
476
477                 /* if color gradient is too high, subdivide again */
478                 if ( subdivide > minDiffuseSubdivide &&
479                          ( gradient[ 0 ] > RADIOSITY_MAX_GRADIENT || gradient[ 1 ] > RADIOSITY_MAX_GRADIENT || gradient[ 2 ] > RADIOSITY_MAX_GRADIENT ) ) {
480                         RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, ( subdivide / 2.0f ), qfalse, rw, cw );
481                         return;
482                 }
483         }
484
485         /* create a regular winding and an average normal */
486         w = AllocWinding( rw->numVerts );
487         w->numpoints = rw->numVerts;
488         VectorClear( normal );
489         for ( i = 0; i < rw->numVerts; i++ )
490         {
491                 VectorCopy( rw->verts[ i ].xyz, w->p[ i ] );
492                 VectorAdd( normal, rw->verts[ i ].normal, normal );
493         }
494         VectorScale( normal, ( 1.0f / rw->numVerts ), normal );
495         if ( VectorNormalize( normal, normal ) == 0.0f ) {
496                 return;
497         }
498
499         /* early out? */
500         if ( bouncing && VectorLength( color ) < RADIOSITY_MIN ) {
501                 return;
502         }
503
504         /* debug code */
505         //%     Sys_Printf( "Size: %d %d %d\n", (int) (maxs[ 0 ] - mins[ 0 ]), (int) (maxs[ 1 ] - mins[ 1 ]), (int) (maxs[ 2 ] - mins[ 2 ]) );
506         //%     Sys_Printf( "Grad: %f %f %f\n", gradient[ 0 ], gradient[ 1 ], gradient[ 2 ] );
507
508         /* increment counts */
509         numDiffuseLights++;
510         switch ( ds->surfaceType )
511         {
512         case MST_PLANAR:
513                 numBrushDiffuseLights++;
514                 break;
515
516         case MST_TRIANGLE_SOUP:
517                 numTriangleDiffuseLights++;
518                 break;
519
520         case MST_PATCH:
521                 numPatchDiffuseLights++;
522                 break;
523         }
524
525         /* create a light */
526         light = safe_malloc( sizeof( *light ) );
527         memset( light, 0, sizeof( *light ) );
528
529         /* attach it */
530         ThreadLock();
531         light->next = lights;
532         lights = light;
533         ThreadUnlock();
534
535         /* initialize the light */
536         light->flags = LIGHT_AREA_DEFAULT;
537         light->type = EMIT_AREA;
538         light->si = si;
539         light->fade = 1.0f;
540         light->w = w;
541
542         /* set falloff threshold */
543         light->falloffTolerance = falloffTolerance;
544
545         /* bouncing light? */
546         if ( bouncing == qfalse ) {
547                 /* handle first-pass lights in normal q3a style */
548                 value = si->value;
549                 light->photons = value * area * areaScale;
550                 light->add = value * formFactorValueScale * areaScale;
551                 VectorCopy( si->color, light->color );
552                 VectorScale( light->color, light->add, light->emitColor );
553                 light->style = noStyles ? LS_NORMAL : si->lightStyle;
554                 if ( light->style < LS_NORMAL || light->style >= LS_NONE ) {
555                         light->style = LS_NORMAL;
556                 }
557
558                 /* set origin */
559                 VectorAdd( mins, maxs, light->origin );
560                 VectorScale( light->origin, 0.5f, light->origin );
561
562                 /* nudge it off the plane a bit */
563                 VectorCopy( normal, light->normal );
564                 VectorMA( light->origin, 1.0f, light->normal, light->origin );
565                 light->dist = DotProduct( light->origin, normal );
566
567                 /* optionally create a point splashsplash light for first pass */
568                 if ( original && si->backsplashFraction > 0 ) {
569                         /* allocate a new point light */
570                         splash = safe_malloc( sizeof( *splash ) );
571                         memset( splash, 0, sizeof( *splash ) );
572                         splash->next = lights;
573                         lights = splash;
574
575                         /* set it up */
576                         splash->flags = LIGHT_Q3A_DEFAULT;
577                         splash->type = EMIT_POINT;
578                         splash->photons = light->photons * si->backsplashFraction;
579                         splash->fade = 1.0f;
580                         splash->si = si;
581                         VectorMA( light->origin, si->backsplashDistance, normal, splash->origin );
582                         VectorCopy( si->color, splash->color );
583                         splash->falloffTolerance = falloffTolerance;
584                         splash->style = noStyles ? LS_NORMAL : light->style;
585
586                         /* add to counts */
587                         numPointLights++;
588                 }
589         }
590         else
591         {
592                 /* handle bounced light (radiosity) a little differently */
593                 value = RADIOSITY_VALUE * si->bounceScale * 0.375f;
594                 light->photons = value * area * bounceScale;
595                 light->add = value * formFactorValueScale * bounceScale;
596                 VectorCopy( color, light->color );
597                 VectorScale( light->color, light->add, light->emitColor );
598                 light->style = noStyles ? LS_NORMAL : style;
599                 if ( light->style < LS_NORMAL || light->style >= LS_NONE ) {
600                         light->style = LS_NORMAL;
601                 }
602
603                 /* set origin */
604                 WindingCenter( w, light->origin );
605
606                 /* nudge it off the plane a bit */
607                 VectorCopy( normal, light->normal );
608                 VectorMA( light->origin, 1.0f, light->normal, light->origin );
609                 light->dist = DotProduct( light->origin, normal );
610         }
611
612         /* emit light from both sides? */
613         if ( si->compileFlags & C_FOG || si->twoSided ) {
614                 light->flags |= LIGHT_TWOSIDED;
615         }
616
617         //%     Sys_Printf( "\nAL: C: (%6f, %6f, %6f) [%6f] N: (%6f, %6f, %6f) %s\n",
618         //%             light->color[ 0 ], light->color[ 1 ], light->color[ 2 ], light->add,
619         //%             light->normal[ 0 ], light->normal[ 1 ], light->normal[ 2 ],
620         //%             light->si->shader );
621 }
622
623
624
625 /*
626    RadLightForTriangles()
627    creates unbounced diffuse lights for triangle soup (misc_models, etc)
628  */
629
630 void RadLightForTriangles( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw ){
631         int i, j, k, v;
632         bspDrawSurface_t    *ds;
633         surfaceInfo_t       *info;
634         float               *radVertexLuxel;
635         radWinding_t rw;
636
637
638         /* get surface */
639         ds = &bspDrawSurfaces[ num ];
640         info = &surfaceInfos[ num ];
641
642         /* each triangle is a potential emitter */
643         rw.numVerts = 3;
644         for ( i = 0; i < ds->numIndexes; i += 3 )
645         {
646                 /* copy each vert */
647                 for ( j = 0; j < 3; j++ )
648                 {
649                         /* get vertex index and rad vertex luxel */
650                         v = ds->firstVert + bspDrawIndexes[ ds->firstIndex + i + j ];
651
652                         /* get most everything */
653                         memcpy( &rw.verts[ j ], &yDrawVerts[ v ], sizeof( bspDrawVert_t ) );
654
655                         /* fix colors */
656                         for ( k = 0; k < MAX_LIGHTMAPS; k++ )
657                         {
658                                 radVertexLuxel = RAD_VERTEX_LUXEL( k, ds->firstVert + bspDrawIndexes[ ds->firstIndex + i + j ] );
659                                 VectorCopy( radVertexLuxel, rw.verts[ j ].color[ k ] );
660                                 rw.verts[ j ].color[ k ][ 3 ] = yDrawVerts[ v ].color[ k ][ 3 ];
661                         }
662                 }
663
664                 /* subdivide into area lights */
665                 RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qtrue, &rw, cw );
666         }
667 }
668
669
670
671 /*
672    RadLightForPatch()
673    creates unbounced diffuse lights for patches
674  */
675
676 #define PLANAR_EPSILON  0.1f
677
678 void RadLightForPatch( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw ){
679         int i, x, y, v, t, pw[ 5 ], r;
680         bspDrawSurface_t    *ds;
681         surfaceInfo_t       *info;
682         bspDrawVert_t       *bogus;
683         bspDrawVert_t       *dv[ 4 ];
684         mesh_t src, *subdivided, *mesh;
685         float               *radVertexLuxel;
686         float dist;
687         vec4_t plane;
688         qboolean planar;
689         radWinding_t rw;
690
691
692         /* get surface */
693         ds = &bspDrawSurfaces[ num ];
694         info = &surfaceInfos[ num ];
695
696         /* construct a bogus vert list with color index stuffed into color[ 0 ] */
697         bogus = safe_malloc( ds->numVerts * sizeof( bspDrawVert_t ) );
698         memcpy( bogus, &yDrawVerts[ ds->firstVert ], ds->numVerts * sizeof( bspDrawVert_t ) );
699         for ( i = 0; i < ds->numVerts; i++ )
700                 bogus[ i ].color[ 0 ][ 0 ] = i;
701
702         /* build a subdivided mesh identical to shadow facets for this patch */
703         /* this MUST MATCH FacetsForPatch() identically! */
704         src.width = ds->patchWidth;
705         src.height = ds->patchHeight;
706         src.verts = bogus;
707         //%     subdivided = SubdivideMesh( src, 8, 512 );
708         subdivided = SubdivideMesh2( src, info->patchIterations );
709         PutMeshOnCurve( *subdivided );
710         //%     MakeMeshNormals( *subdivided );
711         mesh = RemoveLinearMeshColumnsRows( subdivided );
712         FreeMesh( subdivided );
713         free( bogus );
714
715         /* FIXME: build interpolation table into color[ 1 ] */
716
717         /* fix up color indexes */
718         for ( i = 0; i < ( mesh->width * mesh->height ); i++ )
719         {
720                 dv[ 0 ] = &mesh->verts[ i ];
721                 if ( dv[ 0 ]->color[ 0 ][ 0 ] >= ds->numVerts ) {
722                         dv[ 0 ]->color[ 0 ][ 0 ] = ds->numVerts - 1;
723                 }
724         }
725
726         /* iterate through the mesh quads */
727         for ( y = 0; y < ( mesh->height - 1 ); y++ )
728         {
729                 for ( x = 0; x < ( mesh->width - 1 ); x++ )
730                 {
731                         /* set indexes */
732                         pw[ 0 ] = x + ( y * mesh->width );
733                         pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
734                         pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
735                         pw[ 3 ] = x + 1 + ( y * mesh->width );
736                         pw[ 4 ] = x + ( y * mesh->width );    /* same as pw[ 0 ] */
737
738                         /* set radix */
739                         r = ( x + y ) & 1;
740
741                         /* get drawverts */
742                         dv[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
743                         dv[ 1 ] = &mesh->verts[ pw[ r + 1 ] ];
744                         dv[ 2 ] = &mesh->verts[ pw[ r + 2 ] ];
745                         dv[ 3 ] = &mesh->verts[ pw[ r + 3 ] ];
746
747                         /* planar? */
748                         planar = PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz );
749                         if ( planar ) {
750                                 dist = DotProduct( dv[ 1 ]->xyz, plane ) - plane[ 3 ];
751                                 if ( fabs( dist ) > PLANAR_EPSILON ) {
752                                         planar = qfalse;
753                                 }
754                         }
755
756                         /* generate a quad */
757                         if ( planar ) {
758                                 rw.numVerts = 4;
759                                 for ( v = 0; v < 4; v++ )
760                                 {
761                                         /* get most everything */
762                                         memcpy( &rw.verts[ v ], dv[ v ], sizeof( bspDrawVert_t ) );
763
764                                         /* fix colors */
765                                         for ( i = 0; i < MAX_LIGHTMAPS; i++ )
766                                         {
767                                                 radVertexLuxel = RAD_VERTEX_LUXEL( i, ds->firstVert + dv[ v ]->color[ 0 ][ 0 ] );
768                                                 VectorCopy( radVertexLuxel, rw.verts[ v ].color[ i ] );
769                                                 rw.verts[ v ].color[ i ][ 3 ] = dv[ v ]->color[ i ][ 3 ];
770                                         }
771                                 }
772
773                                 /* subdivide into area lights */
774                                 RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qtrue, &rw, cw );
775                         }
776
777                         /* generate 2 tris */
778                         else
779                         {
780                                 rw.numVerts = 3;
781                                 for ( t = 0; t < 2; t++ )
782                                 {
783                                         for ( v = 0; v < 3 + t; v++ )
784                                         {
785                                                 /* get "other" triangle (stupid hacky logic, but whatevah) */
786                                                 if ( v == 1 && t == 1 ) {
787                                                         v++;
788                                                 }
789
790                                                 /* get most everything */
791                                                 memcpy( &rw.verts[ v ], dv[ v ], sizeof( bspDrawVert_t ) );
792
793                                                 /* fix colors */
794                                                 for ( i = 0; i < MAX_LIGHTMAPS; i++ )
795                                                 {
796                                                         radVertexLuxel = RAD_VERTEX_LUXEL( i, ds->firstVert + dv[ v ]->color[ 0 ][ 0 ] );
797                                                         VectorCopy( radVertexLuxel, rw.verts[ v ].color[ i ] );
798                                                         rw.verts[ v ].color[ i ][ 3 ] = dv[ v ]->color[ i ][ 3 ];
799                                                 }
800                                         }
801
802                                         /* subdivide into area lights */
803                                         RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qtrue, &rw, cw );
804                                 }
805                         }
806                 }
807         }
808
809         /* free the mesh */
810         FreeMesh( mesh );
811 }
812
813
814
815
816 /*
817    RadLight()
818    creates unbounced diffuse lights for a given surface
819  */
820
821 void RadLight( int num ){
822         int lightmapNum;
823         float scale, subdivide;
824         int contentFlags, surfaceFlags, compileFlags;
825         bspDrawSurface_t    *ds;
826         surfaceInfo_t       *info;
827         rawLightmap_t       *lm;
828         shaderInfo_t        *si;
829         clipWork_t cw;
830
831
832         /* get drawsurface, lightmap, and shader info */
833         ds = &bspDrawSurfaces[ num ];
834         info = &surfaceInfos[ num ];
835         lm = info->lm;
836         si = info->si;
837         scale = si->bounceScale;
838
839         /* find nodraw bit */
840         contentFlags = surfaceFlags = compileFlags = 0;
841         ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, &compileFlags );
842
843         /* early outs? */
844         if ( scale <= 0.0f || ( si->compileFlags & C_SKY ) || si->autosprite ||
845                  ( bspShaders[ ds->shaderNum ].contentFlags & contentFlags ) || ( bspShaders[ ds->shaderNum ].surfaceFlags & surfaceFlags ) ||
846                  ( si->compileFlags & compileFlags ) ) {
847                 return;
848         }
849
850         /* determine how much we need to chop up the surface */
851         if ( si->lightSubdivide ) {
852                 subdivide = si->lightSubdivide;
853         }
854         else{
855                 subdivide = diffuseSubdivide;
856         }
857
858         /* inc counts */
859         numDiffuseSurfaces++;
860
861         /* iterate through styles (this could be more efficient, yes) */
862         for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
863         {
864                 /* switch on type */
865                 if ( ds->lightmapStyles[ lightmapNum ] != LS_NONE && ds->lightmapStyles[ lightmapNum ] != LS_UNUSED ) {
866                         switch ( ds->surfaceType )
867                         {
868                         case MST_PLANAR:
869                         case MST_TRIANGLE_SOUP:
870                                 RadLightForTriangles( num, lightmapNum, lm, si, scale, subdivide, &cw );
871                                 break;
872
873                         case MST_PATCH:
874                                 RadLightForPatch( num, lightmapNum, lm, si, scale, subdivide, &cw );
875                                 break;
876
877                         default:
878                                 break;
879                         }
880                 }
881         }
882 }
883
884
885
886 /*
887    RadCreateDiffuseLights()
888    creates lights for unbounced light on surfaces in the bsp
889  */
890
891 int iterations = 0;
892
893 void RadCreateDiffuseLights( void ){
894         /* startup */
895         Sys_FPrintf( SYS_VRB, "--- RadCreateDiffuseLights ---\n" );
896         numDiffuseSurfaces = 0;
897         numDiffuseLights = 0;
898         numBrushDiffuseLights = 0;
899         numTriangleDiffuseLights = 0;
900         numPatchDiffuseLights = 0;
901         numAreaLights = 0;
902
903         /* hit every surface (threaded) */
904         RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, RadLight );
905
906         /* dump the lights generated to a file */
907         if ( dump ) {
908                 char dumpName[ 1024 ], ext[ 64 ];
909                 FILE    *file;
910                 light_t *light;
911
912                 strcpy( dumpName, source );
913                 StripExtension( dumpName );
914                 sprintf( ext, "_bounce_%03d.map", iterations );
915                 strcat( dumpName, ext );
916                 file = fopen( dumpName, "wb" );
917                 Sys_Printf( "Writing %s...\n", dumpName );
918                 if ( file ) {
919                         for ( light = lights; light; light = light->next )
920                         {
921                                 fprintf( file,
922                                                  "{\n"
923                                                  "\"classname\" \"light\"\n"
924                                                  "\"light\" \"%d\"\n"
925                                                  "\"origin\" \"%.0f %.0f %.0f\"\n"
926                                                  "\"_color\" \"%.3f %.3f %.3f\"\n"
927                                                  "}\n",
928
929                                                  (int) light->add,
930
931                                                  light->origin[ 0 ],
932                                                  light->origin[ 1 ],
933                                                  light->origin[ 2 ],
934
935                                                  light->color[ 0 ],
936                                                  light->color[ 1 ],
937                                                  light->color[ 2 ] );
938                         }
939                         fclose( file );
940                 }
941         }
942
943         /* increment */
944         iterations++;
945
946         /* print counts */
947         Sys_Printf( "%8d diffuse surfaces\n", numDiffuseSurfaces );
948         Sys_FPrintf( SYS_VRB, "%8d total diffuse lights\n", numDiffuseLights );
949         Sys_FPrintf( SYS_VRB, "%8d brush diffuse lights\n", numBrushDiffuseLights );
950         Sys_FPrintf( SYS_VRB, "%8d patch diffuse lights\n", numPatchDiffuseLights );
951         Sys_FPrintf( SYS_VRB, "%8d triangle diffuse lights\n", numTriangleDiffuseLights );
952 }