Source

quake3 / q3map / fog.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/
#include "qbsp.h"


int			c_fogFragment;
int			c_fogPatchFragments;

/*
====================
DrawSurfToMesh
====================
*/
mesh_t	*DrawSurfToMesh( mapDrawSurface_t *ds ) {
	mesh_t		*m;

	m = malloc( sizeof( *m ) );
	m->width = ds->patchWidth;
	m->height = ds->patchHeight;
	m->verts = malloc( sizeof(m->verts[0]) * m->width * m->height );
	memcpy( m->verts, ds->verts, sizeof(m->verts[0]) * m->width * m->height );

	return m;
}


/*
====================
SplitMeshByPlane
====================
*/
void SplitMeshByPlane( mesh_t *in, vec3_t normal, float dist, mesh_t **front, mesh_t **back ) {
	int		w, h, split;
	float	d[MAX_PATCH_SIZE][MAX_PATCH_SIZE];
	drawVert_t	*dv, *v1, *v2;
	int		c_front, c_back, c_on;
	mesh_t	*f, *b;
	int		i;
	float	frac;
	int		frontAprox, backAprox;

	for ( i = 0 ; i < 2 ; i++ ) {
		dv = in->verts;
		c_front = 0;
		c_back = 0;
		c_on = 0;
		for ( h = 0 ; h < in->height ; h++ ) {
			for ( w = 0 ; w < in->width ; w++, dv++ ) {
				d[h][w] = DotProduct( dv->xyz, normal ) - dist;
				if ( d[h][w] > ON_EPSILON ) {
					c_front++;
				} else if ( d[h][w] < -ON_EPSILON ) {
					c_back++;
				} else {
					c_on++;
				}
			}
		}

		*front = NULL;
		*back = NULL;

		if ( !c_front ) {
			*back = in;
			return;
		}
		if ( !c_back ) {
			*front = in;
			return;
		}

		// find a split point
		split = -1;
		for ( w = 0 ; w < in->width -1 ; w++ ) {
			if ( ( d[0][w] < 0 ) != ( d[0][w+1] < 0 ) ) {
				if ( split == -1 ) {
					split = w;
					break;
				}
			}
		}

		if ( split == -1 ) {
			if ( i == 1 ) {
				qprintf( "No crossing points in patch\n");
				*front = in;
				return;
			}

			in = TransposeMesh( in );
			InvertMesh( in );
			continue;
		}

		// make sure the split point stays the same for all other rows
		for ( h = 1 ; h < in->height ; h++ ) {
			for ( w = 0 ; w < in->width -1 ; w++ ) {
				if ( ( d[h][w] < 0 ) != ( d[h][w+1] < 0 ) ) {
					if ( w != split ) {
						_printf( "multiple crossing points for patch -- can't clip\n");
						*front = in;
						return;
					}
				}
			}
			if ( ( d[h][split] < 0 ) == ( d[h][split+1] < 0 ) ) {
				_printf( "differing crossing points for patch -- can't clip\n");
				*front = in;
				return;
			}
		}

		break;
	}


	// create two new meshes
	f = malloc( sizeof( *f ) );
	f->width = split + 2;
	if ( ! (f->width & 1) ) {
		f->width++;
		frontAprox = 1;
	} else {
		frontAprox = 0;
	}
	if ( f->width > MAX_PATCH_SIZE ) {
		Error( "MAX_PATCH_SIZE after split");
	}
	f->height = in->height;
	f->verts = malloc( sizeof(f->verts[0]) * f->width * f->height );

	b = malloc( sizeof( *b ) );
	b->width = in->width - split;
	if ( ! (b->width & 1) ) {
		b->width++;
		backAprox = 1;
	} else {
		backAprox = 0;
	}
	if ( b->width > MAX_PATCH_SIZE ) {
		Error( "MAX_PATCH_SIZE after split");
	}
	b->height = in->height;
	b->verts = malloc( sizeof(b->verts[0]) * b->width * b->height );

	if ( d[0][0] > 0 ) {
		*front = f;
		*back = b;
	} else {
		*front = b;
		*back = f;
	}

	// distribute the points
	for ( w = 0 ; w < in->width ; w++ ) {
		for ( h = 0 ; h < in->height ; h++ ) {
			if ( w <= split ) {
				f->verts[ h * f->width + w ] = in->verts[ h * in->width + w ];
			} else {
				b->verts[ h * b->width + w - split + backAprox ] = in->verts[ h * in->width + w ];
			}
		}
	}

	// clip the crossing line
	for ( h = 0 ; h < in->height ; h++ ) {
		dv = &f->verts[ h * f->width + split + 1 ];
		v1 = &in->verts[ h * in->width + split ];
		v2 = &in->verts[ h * in->width + split + 1 ];
		frac = d[h][split] / ( d[h][split] - d[h][split+1] );
		for ( i = 0 ; i < 10 ; i++ ) {
			dv->xyz[i] = v1->xyz[i] + frac * ( v2->xyz[i] - v1->xyz[i] );
		}
		dv->xyz[10] = 0;//set all 4 colors to 0 
		if ( frontAprox ) {
			f->verts[ h * f->width + split + 2 ] = *dv;
		}
		b->verts[ h * b->width ] = *dv;
		if ( backAprox ) {
			b->verts[ h * b->width + 1 ] = *dv;
		}
	}

	/*
PrintMesh( in );
_printf("\n");
PrintMesh( f );
_printf("\n");
PrintMesh( b );
_printf("\n");
	*/

	FreeMesh( in );
}


/*
====================
ChopPatchByBrush
====================
*/
qboolean ChopPatchByBrush( mapDrawSurface_t *ds, bspbrush_t *b ) {
	int			i, j;
	side_t		*s;
	plane_t		*plane;
	mesh_t		*outside[MAX_BRUSH_SIDES];
	int			numOutside;
	mesh_t		*m, *front, *back;
	mapDrawSurface_t	*newds;

	m = DrawSurfToMesh( ds );
	numOutside = 0;

	// only split by the top and bottom planes to avoid
	// some messy patch clipping issues

	for ( i = 4 ; i <= 5 ; i++ ) {
		s = &b->sides[ i ];
		plane = &mapplanes[ s->planenum ];

		SplitMeshByPlane( m, plane->normal, plane->dist, &front, &back );

		if ( !back ) {
			// nothing actually contained inside
			for ( j = 0 ; j < numOutside ; j++ ) {
				FreeMesh( outside[j] );
			}
			return qfalse;
		}
		m = back;

		if ( front ) {
			if ( numOutside == MAX_BRUSH_SIDES ) {
				Error( "MAX_BRUSH_SIDES" );
			}
			outside[ numOutside ] = front;
			numOutside++;
		}
	}

	// all of outside fragments become seperate drawsurfs
	c_fogPatchFragments += numOutside;
	for ( i = 0 ; i < numOutside ; i++ ) {
		newds = DrawSurfaceForMesh( outside[ i ] );
		newds->shaderInfo = ds->shaderInfo;
		FreeMesh( outside[ i ] );
	}

	// replace ds with m
	ds->patchWidth = m->width;
	ds->patchHeight = m->height;
	ds->numVerts = m->width * m->height;
	free( ds->verts );
	ds->verts = malloc( ds->numVerts * sizeof( *ds->verts ) );
	memcpy( ds->verts, m->verts, ds->numVerts * sizeof( *ds->verts ) );

	FreeMesh( m );

	return qtrue;
}

//===============================================================================

/*
====================
WindingFromDrawSurf
====================
*/
winding_t	*WindingFromDrawSurf( mapDrawSurface_t *ds ) {
	winding_t	*w;
	int			i;

	w = AllocWinding( ds->numVerts );
	w->numpoints = ds->numVerts;
	for ( i = 0 ; i < ds->numVerts ; i++ ) {
		VectorCopy( ds->verts[i].xyz, w->p[i] );
	}
	return w;
}

/*
====================
ChopFaceByBrush

There may be a fragment contained in the brush
====================
*/
qboolean ChopFaceByBrush( mapDrawSurface_t *ds, bspbrush_t *b ) {
	int			i, j;
	side_t		*s;
	plane_t		*plane;
	winding_t	*w;
	winding_t	*front, *back;
	winding_t	*outside[MAX_BRUSH_SIDES];
	int			numOutside;
	mapDrawSurface_t	*newds;
	drawVert_t		*dv;
	shaderInfo_t	*si;
	float		mins[2];

	// brush primitive :
	// axis base
	vec3_t		texX,texY;
	vec_t		x,y;

	w = WindingFromDrawSurf( ds );
	numOutside = 0;

	for ( i = 0 ; i < b->numsides ; i++ ) {
		s = &b->sides[ i ];
		if ( s->backSide ) {
			continue;
		}
		plane = &mapplanes[ s->planenum ];

		// handle coplanar outfacing (don't fog)
		if ( ds->side->planenum == s->planenum ) {
			return qfalse;
		}

		// handle coplanar infacing (keep inside)
		if ( ( ds->side->planenum ^ 1 ) == s->planenum ) {
			continue;
		}

		// general case
		ClipWindingEpsilon( w, plane->normal, plane->dist, ON_EPSILON,
			&front, &back );
		FreeWinding( w );
		if ( !back ) {
			// nothing actually contained inside
			for ( j = 0 ; j < numOutside ; j++ ) {
				FreeWinding( outside[j] );
			}
			return qfalse;
		}
		if ( front ) {
			if ( numOutside == MAX_BRUSH_SIDES ) {
				Error( "MAX_BRUSH_SIDES" );
			}
			outside[ numOutside ] = front;
			numOutside++;
		}
		w = back;
	}

	// all of outside fragments become seperate drawsurfs
	// linked to the same side
	c_fogFragment += numOutside;
	s = ds->side;

	for ( i = 0 ; i < numOutside ; i++ ) {
		newds = DrawSurfaceForSide( ds->mapBrush, s, outside[i] );
		FreeWinding( outside[i] );
	}


	// replace ds->verts with the verts for w
	ds->numVerts = w->numpoints;
	free( ds->verts );

	ds->verts = malloc( ds->numVerts * sizeof( *ds->verts ) );
	memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );

	si = s->shaderInfo;

	mins[0] = 9999;
	mins[1] = 9999;

	// compute s/t coordinates from brush primitive texture matrix
	// compute axis base
	ComputeAxisBase( mapplanes[s->planenum].normal, texX, texY );

	for ( j = 0 ; j < w->numpoints ; j++ ) {
		dv = ds->verts + j;
		VectorCopy( w->p[j], dv->xyz );
	
		if (g_bBrushPrimit==BPRIMIT_OLDBRUSHES)
		{
			// calculate texture s/t
			dv->st[0] = s->vecs[0][3] + DotProduct( s->vecs[0],	dv->xyz );
			dv->st[1] = s->vecs[1][3] + DotProduct( s->vecs[1],	dv->xyz );	
			dv->st[0] /= si->width;
			dv->st[1] /= si->height;
		}
		else
		{
			// calculate texture s/t from brush primitive texture matrix
			x = DotProduct( dv->xyz, texX );
			y = DotProduct( dv->xyz, texY );
			dv->st[0]=s->texMat[0][0]*x+s->texMat[0][1]*y+s->texMat[0][2];
			dv->st[1]=s->texMat[1][0]*x+s->texMat[1][1]*y+s->texMat[1][2];
		}

		if ( dv->st[0] < mins[0] ) {
			mins[0] = dv->st[0];
		}
		if ( dv->st[1] < mins[1] ) {
			mins[1] = dv->st[1];
		}

		// copy normal
		VectorCopy ( mapplanes[s->planenum].normal, dv->normal );
	}

	// adjust the texture coordinates to be as close to 0 as possible
	if ( !si->globalTexture ) {
		mins[0] = floor( mins[0] );
		mins[1] = floor( mins[1] );
		for ( i = 0 ; i < w->numpoints ; i++ ) {
			dv = ds->verts + i;
			dv->st[0] -= mins[0];
			dv->st[1] -= mins[1];
		}
	}

	return qtrue;
}

//===============================================================================


/*
=====================
FogDrawSurfs

Call after the surface list has been pruned, 
before tjunction fixing
before lightmap allocation
=====================
*/
void FogDrawSurfs( void ) {
	int					i, j, k;
	mapDrawSurface_t	*ds;
	bspbrush_t			*b;
	vec3_t				mins, maxs;
	int					c_fogged;
	int					numBaseDrawSurfs;
	dfog_t				*fog;

	qprintf("----- FogDrawsurfs -----\n");

	c_fogged = 0;
	c_fogFragment = 0;

	// find all fog brushes
	for ( b = entities[0].brushes ; b ; b = b->next ) {
		if ( !(b->contents & CONTENTS_FOG) ) {
			continue;
		}

		if ( numFogs == MAX_MAP_FOGS ) {
			Error( "MAX_MAP_FOGS" );
		}
		fog = &dfogs[numFogs];
		numFogs++;
		fog->brushNum = b->outputNumber;

		// find a side with a valid shaderInfo
		// non-axial fog columns may have bevel planes that need to be skipped
		for ( i = 0 ; i < b->numsides ; i++ ) {
			if ( b->sides[i].shaderInfo && (b->sides[i].shaderInfo->contents & CONTENTS_FOG) ) {
				strcpy( fog->shader, b->sides[i].shaderInfo->shader );
				break;
			}
		}
		if ( i == b->numsides ) {
			continue;		// shouldn't happen
		}

		fog->visibleSide = -1;

		// clip each surface into this, but don't clip any of
		// the resulting fragments to the same brush
		numBaseDrawSurfs = numMapDrawSurfs;
		for ( i = 0 ; i < numBaseDrawSurfs ; i++ ) {
			ds = &mapDrawSurfs[i];

			// bound the drawsurf
			ClearBounds( mins, maxs );
			for ( j = 0 ; j < ds->numVerts ; j++ ) {
				AddPointToBounds( ds->verts[j].xyz, mins, maxs );
			}

			// check against the fog brush
			for ( k = 0 ; k < 3 ; k++ ) {
				if ( mins[k] > b->maxs[k] ) {
					break;
				}
				if ( maxs[k] < b->mins[k] ) {
					break;
				}
			}
			if ( k < 3 ) {
				continue;		// bboxes don't intersect
			}

			if ( ds->mapBrush == b ) {
				int		s;

				s = ds->side - b->sides;
				if ( s <= 6 ) {	// not one of the reversed inside faces
					// this is a visible fog plane
					if ( fog->visibleSide != -1 ) {
						_printf( "WARNING: fog brush %i has multiple visible sides\n", b->brushnum );
					}
					fog->visibleSide = s;
				}
			}

			if ( ds->miscModel ) {
				// we could write splitting code for trimodels if we wanted to...
				c_fogged++;
				ds->fogNum = numFogs - 1;
			} else if ( ds->patch ) {
				if ( ChopPatchByBrush( ds, b ) ) {
					c_fogged++;
					ds->fogNum = numFogs - 1;
				}
			} else {
				if ( ChopFaceByBrush( ds, b ) ) {
					c_fogged++;
					ds->fogNum = numFogs - 1;
				}
			}
		}
	}

	// split the drawsurfs by the fog brushes

	qprintf( "%5i fogs\n", numFogs );
	qprintf( "%5i fog polygon fragments\n", c_fogFragment );
	qprintf( "%5i fog patch fragments\n", c_fogPatchFragments );
	qprintf( "%5i fogged drawsurfs\n", c_fogged );
}