Source

qutils / qbsp / csg4.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <stdarg.h>
#include <unistd.h>
#include "../common/cmdlib.h"
#include "../common/mathlib.h"
#include "../common/bspfile.h"
#include "bsp5.h"
#include "map.h"

/*

NOTES
-----
Brushes that touch still need to be split at the cut point to make a tjunction

*/

face_t *validfaces[MAX_MAP_PLANES];


face_t *inside, *outside;
int brushfaces;
int csgfaces;
int csgmergefaces;

void
DrawList(face_t * list)
{
	for(; list; list = list->next)
		Draw_DrawFace(list);
}


/*
==================
NewFaceFromFace

Duplicates the non point information of a face, used by SplitFace and
MergeFace.
==================
*/
face_t *
NewFaceFromFace(face_t * in)
{
	face_t *newf;

	newf = AllocFace();

	newf->planenum = in->planenum;
	newf->texturenum = in->texturenum;
	newf->planeside = in->planeside;
	newf->original = in->original;
	newf->contents[0] = in->contents[0];
	newf->contents[1] = in->contents[1];

	return newf;
}


/*
==================
SplitFace

==================
*/
void
SplitFace(face_t * in, plane_t * split, face_t ** front, face_t ** back)
{
	vec_t dists[MAXEDGES + 1];
	int sides[MAXEDGES + 1];
	int counts[3];
	vec_t dot;
	int i, j;
	face_t *newf, *new2;
	vec_t *p1, *p2;
	vec3_t mid;

	if(in->numpoints < 0)
		Error("SplitFace: freed face");
	counts[0] = counts[1] = counts[2] = 0;

// determine sides for each point
	for(i = 0; i < in->numpoints; i++) {
		dot = DotProduct(in->pts[i], split->normal);
		dot -= split->dist;
		dists[i] = dot;
		if(dot > ON_EPSILON)
			sides[i] = SIDE_FRONT;
		else if(dot < -ON_EPSILON)
			sides[i] = SIDE_BACK;
		else
			sides[i] = SIDE_ON;
		counts[sides[i]]++;
	}
	sides[i] = sides[0];
	dists[i] = dists[0];

	if(!counts[0]) {
		*front = NULL;
		*back = in;
		return;
	}
	if(!counts[1]) {
		*front = in;
		*back = NULL;
		return;
	}

	*back = newf = NewFaceFromFace(in);
	*front = new2 = NewFaceFromFace(in);

// distribute the points and generate splits

	for(i = 0; i < in->numpoints; i++) {
		if(newf->numpoints > MAXEDGES || new2->numpoints > MAXEDGES)
			Error("SplitFace: numpoints > MAXEDGES");

		p1 = in->pts[i];

		if(sides[i] == SIDE_ON) {
			VectorCopy(p1, newf->pts[newf->numpoints]);
			newf->numpoints++;
			VectorCopy(p1, new2->pts[new2->numpoints]);
			new2->numpoints++;
			continue;
		}

		if(sides[i] == SIDE_FRONT) {
			VectorCopy(p1, new2->pts[new2->numpoints]);
			new2->numpoints++;
		} else {
			VectorCopy(p1, newf->pts[newf->numpoints]);
			newf->numpoints++;
		}

		if(sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i])
			continue;

		// generate a split point
		p2 = in->pts[(i + 1) % in->numpoints];

		dot = dists[i] / (dists[i] - dists[i + 1]);
		for(j = 0; j < 3; j++) {	// avoid round off error when possible
			if(split->normal[j] == 1)
				mid[j] = split->dist;
			else if(split->normal[j] == -1)
				mid[j] = -split->dist;
			else
				mid[j] = p1[j] + dot * (p2[j] - p1[j]);
		}

		VectorCopy(mid, newf->pts[newf->numpoints]);
		newf->numpoints++;
		VectorCopy(mid, new2->pts[new2->numpoints]);
		new2->numpoints++;
	}

	if(newf->numpoints > MAXEDGES || new2->numpoints > MAXEDGES)
		Error("SplitFace: numpoints > MAXEDGES");

#if 0
	CheckFace(newf);
	CheckFace(new2);
#endif

// free the original face now that is is represented by the fragments
	FreeFace(in);
}

/*
=================
ClipInside

Clips all of the faces in the inside list, possibly moving them to the
outside list or spliting it into a piece in each list.

Faces exactly on the plane will stay inside unless overdrawn by later brush

frontside is the side of the plane that holds the outside list
=================
*/
void
ClipInside(int splitplane, int frontside, qboolean precedence)
{
	face_t *f, *next;
	face_t *frags[2];
	face_t *insidelist;
	plane_t *split;

	split = &planes[splitplane];

	insidelist = NULL;
	for(f = inside; f; f = next) {
		next = f->next;

		if(f->planenum == splitplane) {	// exactly on, handle special
			if(frontside != f->planeside || precedence) {	// allways clip off opposite faceing
				frags[frontside] = NULL;
				frags[!frontside] = f;
			} else {			// leave it on the outside
				frags[frontside] = f;
				frags[!frontside] = NULL;
			}
		} else {				// proper split
			SplitFace(f, split, &frags[0], &frags[1]);
		}

		if(frags[frontside]) {
			frags[frontside]->next = outside;
			outside = frags[frontside];
		}
		if(frags[!frontside]) {
			frags[!frontside]->next = insidelist;
			insidelist = frags[!frontside];
		}
	}

	inside = insidelist;
}


/*
==================
SaveOutside

Saves all of the faces in the outside list to the bsp plane list
==================
*/
void
SaveOutside(qboolean mirror)
{
	face_t *f, *next, *newf;
	int i;
	int planenum;

	for(f = outside; f; f = next) {
		next = f->next;
		csgfaces++;
		Draw_DrawFace(f);
		planenum = f->planenum;

		if(mirror) {
			newf = NewFaceFromFace(f);

			newf->numpoints = f->numpoints;
			newf->planeside = f->planeside ^ 1;	// reverse side
			newf->contents[0] = f->contents[1];
			newf->contents[1] = f->contents[0];

			for(i = 0; i < f->numpoints; i++)	// add points backwards
			{
				VectorCopy(f->pts[f->numpoints - 1 - i], newf->pts[i]);
			}
		} else
			newf = NULL;

		validfaces[planenum] = MergeFaceToList(f, validfaces[planenum]);
		if(newf)
			validfaces[planenum] =
				MergeFaceToList(newf, validfaces[planenum]);

		validfaces[planenum] = FreeMergeListScraps(validfaces[planenum]);
	}
}

/*
==================
FreeInside

Free all the faces that got clipped out
==================
*/
void
FreeInside(int contents)
{
	face_t *f, *next;

	for(f = inside; f; f = next) {
		next = f->next;

		if(contents != CONTENTS_SOLID) {
			f->contents[0] = contents;
			f->next = outside;
			outside = f;
		} else
			FreeFace(f);
	}
}


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

/*
==================
BuildSurfaces

Returns a chain of all the external surfaces with one or more visible
faces.
==================
*/
surface_t *
BuildSurfaces(void)
{
	face_t **f;
	face_t *count;
	int i;
	surface_t *s;
	surface_t *surfhead;

	surfhead = NULL;

	f = validfaces;
	for(i = 0; i < numbrushplanes; i++, f++) {
		if(!*f)
			continue;			// nothing left on this plane

// create a new surface to hold the faces on this plane
		s = AllocSurface();
		s->planenum = i;
		s->next = surfhead;
		surfhead = s;
		s->faces = *f;
		for(count = s->faces; count; count = count->next)
			csgmergefaces++;
		CalcSurfaceInfo(s);		// bounding box and flags
	}

	return surfhead;
}

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

/*
==================
CopyFacesToOutside
==================
*/
void
CopyFacesToOutside(brush_t * b)
{
	face_t *f, *newf;

	outside = NULL;

	for(f = b->faces; f; f = f->next) {
		brushfaces++;
#if 0
		{
			int i;

			for(i = 0; i < f->numpoints; i++)
				printf("(%f,%f,%f) ", f->pts[i][0], f->pts[i][1],
					   f->pts[i][2]);
			printf("\n");
		}
#endif
		newf = AllocFace();
		*newf = *f;
		newf->next = outside;
		newf->contents[0] = CONTENTS_EMPTY;
		newf->contents[1] = b->contents;
		outside = newf;
	}
}


/*
==================
CSGFaces

Returns a list of surfaces containing aall of the faces
==================
*/
surface_t *
CSGFaces(brushset_t * bs)
{
	brush_t *b1, *b2;
	int i;
	qboolean overwrite;
	face_t *f;
	surface_t *surfhead;

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

	memset(validfaces, 0, sizeof(validfaces));

	csgfaces = brushfaces = csgmergefaces = 0;

	Draw_ClearWindow();

//
// do the solid faces
//
	for(b1 = bs->brushes; b1; b1 = b1->next) {
		// set outside to a copy of the brush's faces
		CopyFacesToOutside(b1);

		overwrite = false;

		for(b2 = bs->brushes; b2; b2 = b2->next) {
			// see if b2 needs to clip a chunk out of b1

			if(b1 == b2) {
				overwrite = true;	// later brushes now overwrite
				continue;
			}
			// check bounding box first
			for(i = 0; i < 3; i++)
				if(b1->mins[i] > b2->maxs[i] || b1->maxs[i] < b2->mins[i])
					break;
			if(i < 3)
				continue;

			// divide faces by the planes of the new brush

			inside = outside;
			outside = NULL;

			for(f = b2->faces; f; f = f->next)
				ClipInside(f->planenum, f->planeside, overwrite);

			// these faces are continued in another brush, so get rid of them
			if(b1->contents == CONTENTS_SOLID
			   && b2->contents <= CONTENTS_WATER)
				FreeInside(b2->contents);
			else
				FreeInside(CONTENTS_SOLID);
		}

		// all of the faces left in outside are real surface faces
		if(b1->contents != CONTENTS_SOLID)
			SaveOutside(true);	// mirror faces for inside view
		else
			SaveOutside(false);
	}

#if 0
	if(!csgfaces)
		Error("No faces");
#endif

	surfhead = BuildSurfaces();

	qprintf("%5i brushfaces\n", brushfaces);
	qprintf("%5i csgfaces\n", csgfaces);
	qprintf("%5i mergedfaces\n", csgmergefaces);

	return surfhead;
}