aboutsummaryrefslogtreecommitdiffstats
path: root/code/bspc/aas_facemerging.c
blob: ea20ffdfb0a6cfa60678c16e3fa14b4f4deada15 (plain)
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
/*
===========================================================================
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 Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "qbsp.h"
#include "../botlib/aasfile.h"
#include "aas_create.h"

//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_TryMergeFaces(tmp_face_t *face1, tmp_face_t *face2)
{
	winding_t *neww;

#ifdef DEBUG
	if (!face1->winding) Error("face1 %d without winding", face1->num);
	if (!face2->winding) Error("face2 %d without winding", face2->num);
#endif //DEBUG
	//
	if (face1->faceflags != face2->faceflags) return false;
	//NOTE: if the front or back area is zero this doesn't mean there's
	//a real area. It means there's solid at that side of the face
	//if both faces have the same front area
	if (face1->frontarea == face2->frontarea)
	{
		//if both faces have the same back area
		if (face1->backarea == face2->backarea)
		{
			//if the faces are in the same plane
			if (face1->planenum == face2->planenum)
			{
				//if they have both a front and a back area (no solid on either side)
				if (face1->frontarea && face1->backarea)
				{
					neww = MergeWindings(face1->winding, face2->winding,
								mapplanes[face1->planenum].normal);
				} //end if
				else
				{
					//this function is to be found in l_poly.c
					neww = TryMergeWinding(face1->winding, face2->winding,
									mapplanes[face1->planenum].normal);
				} //end else
				if (neww)
				{
					FreeWinding(face1->winding);
					face1->winding = neww;
					if (face2->frontarea) AAS_RemoveFaceFromArea(face2, face2->frontarea);
					if (face2->backarea) AAS_RemoveFaceFromArea(face2, face2->backarea);
					AAS_FreeTmpFace(face2);
					return true;
				} //end if
			} //end if
			else if ((face1->planenum & ~1) == (face2->planenum & ~1))
			{
				Log_Write("face %d and %d, same front and back area but flipped planes\r\n",
							face1->num, face2->num);
			} //end if
		} //end if
	} //end if
	return false;
} //end of the function AAS_TryMergeFaces
/*
int AAS_TryMergeFaces(tmp_face_t *face1, tmp_face_t *face2)
{
	winding_t *neww;

#ifdef DEBUG
	if (!face1->winding) Error("face1 %d without winding", face1->num);
	if (!face2->winding) Error("face2 %d without winding", face2->num);
#endif //DEBUG
	//if the faces are in the same plane
	if ((face1->planenum & ~1) != (face2->planenum & ~1)) return false;
//	if (face1->planenum != face2->planenum) return false;
	//NOTE: if the front or back area is zero this doesn't mean there's
	//a real area. It means there's solid at that side of the face
	//if both faces have the same front area
	if (face1->frontarea != face2->frontarea ||
		face1->backarea != face2->backarea)
	{
		if (!face1->frontarea || !face1->backarea ||
				!face2->frontarea || !face2->backarea) return false;
		else if (face1->frontarea != face2->backarea ||
					face1->backarea != face2->frontarea) return false;
//		return false;
	} //end if
	//this function is to be found in l_poly.c
	neww = TryMergeWinding(face1->winding, face2->winding,
					mapplanes[face1->planenum].normal);
	if (!neww) return false;
	//
	FreeWinding(face1->winding);
	face1->winding = neww;
	//remove face2
	if (face2->frontarea)
		AAS_RemoveFaceFromArea(face2, &tmpaasworld.areas[face2->frontarea]);
	if (face2->backarea)
		AAS_RemoveFaceFromArea(face2, &tmpaasworld.areas[face2->backarea]);
	return true;
} //end of the function AAS_TryMergeFaces*/
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_MergeAreaFaces(void)
{
	int num_facemerges = 0;
	int side1, side2, restart;
	tmp_area_t *tmparea, *lasttmparea;
	tmp_face_t *face1, *face2;

	Log_Write("AAS_MergeAreaFaces\r\n");
	qprintf("%6d face merges", num_facemerges);
	//NOTE: first convex area is a dummy
	lasttmparea = tmpaasworld.areas;
	for (tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next)
	{
		restart = false;
		//
		if (tmparea->invalid) continue;
		//
		for (face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1])
		{
			side1 = face1->frontarea != tmparea;
			for (face2 = face1->next[side1]; face2; face2 = face2->next[side2])
			{
				side2 = face2->frontarea != tmparea;
				//if succesfully merged
				if (AAS_TryMergeFaces(face1, face2))
				{
					//start over again after merging two faces
					restart = true;
					num_facemerges++;
					qprintf("\r%6d", num_facemerges);
					AAS_CheckArea(tmparea);
					break;
				} //end if
			} //end for
			if (restart)
			{
				tmparea = lasttmparea;
				break;
			} //end if
		} //end for
		lasttmparea = tmparea;
	} //end for
	qprintf("\n");
	Log_Write("%6d face merges\r\n", num_facemerges);
} //end of the function AAS_MergeAreaFaces
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_MergePlaneFaces(tmp_area_t *tmparea, int planenum)
{
	tmp_face_t *face1, *face2, *nextface2;
	winding_t *neww;
	int side1, side2;

	for (face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1])
	{
		side1 = face1->frontarea != tmparea;
		if (face1->planenum != planenum) continue;
		//
		for (face2 = face1->next[side1]; face2; face2 = nextface2)
		{
			side2 = face2->frontarea != tmparea;
			nextface2 = face2->next[side2];
			//
			if ((face2->planenum & ~1) != (planenum & ~1)) continue;
			//
			neww = MergeWindings(face1->winding, face2->winding,
								mapplanes[face1->planenum].normal);
			FreeWinding(face1->winding);
			face1->winding = neww;
			if (face2->frontarea) AAS_RemoveFaceFromArea(face2, face2->frontarea);
			if (face2->backarea) AAS_RemoveFaceFromArea(face2, face2->backarea);
			AAS_FreeTmpFace(face2);
			//
			nextface2 = face1->next[side1];
		} //end for
	} //end for
} //end of the function AAS_MergePlaneFaces
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_CanMergePlaneFaces(tmp_area_t *tmparea, int planenum)
{
	tmp_area_t *frontarea, *backarea;
	tmp_face_t *face1;
	int side1, merge, faceflags;

	frontarea = backarea = NULL;
	merge = false;
	for (face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1])
	{
		side1 = face1->frontarea != tmparea;
		if ((face1->planenum & ~1) != (planenum & ~1)) continue;
		if (!frontarea && !backarea)
		{
			frontarea = face1->frontarea;
			backarea = face1->backarea;
			faceflags = face1->faceflags;
		} //end if
		else
		{
			if (frontarea != face1->frontarea) return false;
			if (backarea != face1->backarea) return false;
			if (faceflags != face1->faceflags) return false;
			merge = true;
		} //end else
	} //end for
	return merge;
} //end of the function AAS_CanMergePlaneFaces
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_MergeAreaPlaneFaces(void)
{
	int num_facemerges = 0;
	int side1;
	tmp_area_t *tmparea, *nexttmparea;
	tmp_face_t *face1;

	Log_Write("AAS_MergePlaneFaces\r\n");
	qprintf("%6d plane face merges", num_facemerges);
	//NOTE: first convex area is a dummy
	for (tmparea = tmpaasworld.areas; tmparea; tmparea = nexttmparea)
	{
		nexttmparea = tmparea->l_next;
		//
		if (tmparea->invalid) continue;
		//
		for (face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1])
		{
			side1 = face1->frontarea != tmparea;
			//
			if (AAS_CanMergePlaneFaces(tmparea, face1->planenum))
			{
				AAS_MergePlaneFaces(tmparea, face1->planenum);
				nexttmparea = tmparea;
				num_facemerges++;
				qprintf("\r%6d", num_facemerges);
				break;
			} //end if
		} //end for
	} //end for
	qprintf("\n");
	Log_Write("%6d plane face merges\r\n", num_facemerges);
} //end of the function AAS_MergeAreaPlaneFaces