File: AASBuild_ledge.cpp

package info (click to toggle)
dhewm3 1.5.1~pre%2Bgit20200905%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 21,664 kB
  • sloc: cpp: 408,868; ansic: 1,188; objc: 1,034; python: 330; sh: 94; makefile: 11
file content (574 lines) | stat: -rw-r--r-- 14,243 bytes parent folder | download | duplicates (4)
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
===========================================================================

Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.

This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").

Doom 3 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 3 of the License, or
(at your option) any later version.

Doom 3 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 Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/

#include "sys/platform.h"

#include "tools/compilers/aas/AASBuild_local.h"

#define LEDGE_EPSILON		0.1f

//===============================================================
//
//	idLedge
//
//===============================================================

/*
============
idLedge::idLedge
============
*/
idLedge::idLedge( void ) {
}

/*
============
idLedge::idLedge
============
*/
idLedge::idLedge( const idVec3 &v1, const idVec3 &v2, const idVec3 &gravityDir, idBrushBSPNode *n ) {
	start = v1;
	end = v2;
	node = n;
	numPlanes = 4;
	planes[0].SetNormal( (v1 - v2).Cross( gravityDir ) );
	planes[0].Normalize();
	planes[0].FitThroughPoint( v1 );
	planes[1].SetNormal( (v1 - v2).Cross( planes[0].Normal() ) );
	planes[1].Normalize();
	planes[1].FitThroughPoint( v1 );
	planes[2].SetNormal( v1 - v2 );
	planes[2].Normalize();
	planes[2].FitThroughPoint( v1 );
	planes[3].SetNormal( v2 - v1 );
	planes[3].Normalize();
	planes[3].FitThroughPoint( v2 );
}

/*
============
idLedge::AddPoint
============
*/
void idLedge::AddPoint( const idVec3 &v ) {
	if ( planes[2].Distance( v ) > 0.0f ) {
		start = v;
		planes[2].FitThroughPoint( start );
	}
	if ( planes[3].Distance( v ) > 0.0f ) {
		end = v;
		planes[3].FitThroughPoint( end );
	}
}

/*
============
idLedge::CreateBevels

  NOTE: this assumes the gravity is vertical
============
*/
void idLedge::CreateBevels( const idVec3 &gravityDir ) {
	int i, j;
	idBounds bounds;
	idVec3 size, normal;

	bounds.Clear();
	bounds.AddPoint( start );
	bounds.AddPoint( end );
	size = bounds[1] - bounds[0];

	// plane through ledge
	planes[0].SetNormal( (start - end).Cross( gravityDir ) );
	planes[0].Normalize();
	planes[0].FitThroughPoint( start );
	// axial bevels at start and end point
	i = size[1] > size[0];
	normal = vec3_origin;
	normal[i] = 1.0f;
	j = end[i] > start[i];
	planes[1+j].SetNormal( normal );
	planes[1+!j].SetNormal( -normal );
	planes[1].FitThroughPoint( start );
	planes[2].FitThroughPoint( end );
	numExpandedPlanes = 3;
	// if additional bevels are required
	if ( idMath::Fabs( size[!i] ) > 0.01f ) {
		normal = vec3_origin;
		normal[!i] = 1.0f;
		j = end[!i] > start[!i];
		planes[3+j].SetNormal( normal );
		planes[3+!j].SetNormal( -normal );
		planes[3].FitThroughPoint( start );
		planes[4].FitThroughPoint( end );
		numExpandedPlanes = 5;
	}
	// opposite of first
	planes[numExpandedPlanes+0] = -planes[0];
	// number of planes used for splitting
	numSplitPlanes = numExpandedPlanes + 1;
	// top plane
	planes[numSplitPlanes+0].SetNormal( (start - end).Cross( planes[0].Normal() ) );
	planes[numSplitPlanes+0].Normalize();
	planes[numSplitPlanes+0].FitThroughPoint( start );
	// bottom plane
	planes[numSplitPlanes+1] = -planes[numSplitPlanes+0];
	// total number of planes
	numPlanes = numSplitPlanes + 2;
}

/*
============
idLedge::Expand
============
*/
void idLedge::Expand( const idBounds &bounds, float maxStepHeight ) {
	int i, j;
	idVec3 v;

	for ( i = 0; i < numExpandedPlanes; i++ ) {

		for ( j = 0; j < 3; j++ ) {
			if ( planes[i].Normal()[j] > 0.0f ) {
				v[j] = bounds[0][j];
			}
			else {
				v[j] = bounds[1][j];
			}
		}

		planes[i].SetDist( planes[i].Dist() + v * -planes[i].Normal() );
	}

	planes[numSplitPlanes+0].SetDist( planes[numSplitPlanes+0].Dist() + maxStepHeight );
	planes[numSplitPlanes+1].SetDist( planes[numSplitPlanes+1].Dist() + 1.0f );
}

/*
============
idLedge::ChopWinding
============
*/
idWinding *idLedge::ChopWinding( const idWinding *winding ) const {
	int i;
	idWinding *w;

	w = winding->Copy();
	for ( i = 0; i < numPlanes && w; i++ ) {
		w = w->Clip( -planes[i], ON_EPSILON, true );
	}
	return w;
}

/*
============
idLedge::PointBetweenBounds
============
*/
bool idLedge::PointBetweenBounds( const idVec3 &v ) const {
	return ( planes[2].Distance( v ) < LEDGE_EPSILON ) && ( planes[3].Distance( v ) < LEDGE_EPSILON );
}


//===============================================================
//
//	idAASBuild
//
//===============================================================

/*
============
idAASBuild::LedgeSubdivFlood_r
============
*/
void idAASBuild::LedgeSubdivFlood_r( idBrushBSPNode *node, const idLedge *ledge ) {
	int s1, i;
	idBrushBSPPortal *p1;
	idWinding *w;
	idList<idBrushBSPNode *> nodeList;

	if ( node->GetFlags() & NODE_VISITED ) {
		return;
	}

	// if this is not already a ledge area
	if ( !( node->GetFlags() & AREA_LEDGE ) ) {
		for ( p1 = node->GetPortals(); p1; p1 = p1->Next(s1) ) {
			s1 = (p1->GetNode(1) == node);

			if ( !(p1->GetFlags() & FACE_FLOOR) ) {
				continue;
			}

			// split the area if some part of the floor portal is inside the expanded ledge
			w = ledge->ChopWinding( p1->GetWinding() );
			if ( !w ) {
				continue;
			}
			delete w;

			for ( i = 0; i < ledge->numSplitPlanes; i++ ) {
				if ( node->PlaneSide( ledge->planes[i], 0.1f ) != SIDE_CROSS ) {
					continue;
				}
				if ( !node->Split( ledge->planes[i], -1 ) ) {
					continue;
				}
				numLedgeSubdivisions++;
				DisplayRealTimeString( "\r%6d", numLedgeSubdivisions );
				node->GetChild(0)->SetFlag( NODE_VISITED );
				LedgeSubdivFlood_r( node->GetChild(1), ledge );
				return;
			}

			node->SetFlag( AREA_LEDGE );
			break;
		}
	}

	node->SetFlag( NODE_VISITED );

	// get all nodes we might need to flood into
	for ( p1 = node->GetPortals(); p1; p1 = p1->Next(s1) ) {
		s1 = (p1->GetNode(1) == node);

		if ( p1->GetNode( !s1 )->GetContents() & AREACONTENTS_SOLID ) {
			continue;
		}

		// flood through this portal if the portal is partly inside the expanded ledge
		w = ledge->ChopWinding( p1->GetWinding() );
		if ( !w ) {
			continue;
		}
		delete w;
		// add to list, cannot flood directly cause portals might be split on the way
		nodeList.Append( p1->GetNode( !s1 ) );
	}

	// flood into other nodes
	for ( i = 0; i < nodeList.Num(); i++ ) {
		LedgeSubdivLeafNodes_r( nodeList[i], ledge );
	}
}

/*
============
idAASBuild::LedgeSubdivLeafNodes_r

  The node the ledge was originally part of might be split by other ledges.
  Here we recurse down the tree from the original node to find all the new leaf nodes the ledge might be part of.
============
*/
void idAASBuild::LedgeSubdivLeafNodes_r( idBrushBSPNode *node, const idLedge *ledge ) {
	if ( !node ) {
		return;
	}
	if ( !node->GetChild(0) && !node->GetChild(1) ) {
		LedgeSubdivFlood_r( node, ledge );
		return;
	}
	LedgeSubdivLeafNodes_r( node->GetChild(0), ledge );
	LedgeSubdivLeafNodes_r( node->GetChild(1), ledge );
}

/*
============
idAASBuild::LedgeSubdiv
============
*/
void idAASBuild::LedgeSubdiv( idBrushBSPNode *root ) {
	int i, j;
	idBrush *brush;
	idList<idBrushSide *> sideList;

	// create ledge bevels and expand ledges
	for ( i = 0; i < ledgeList.Num(); i++ ) {

		ledgeList[i].CreateBevels( aasSettings->gravityDir );
		ledgeList[i].Expand( aasSettings->boundingBoxes[0], aasSettings->maxStepHeight );

		// if we should write out a ledge map
		if ( ledgeMap ) {
			sideList.SetNum( 0 );
			for ( j = 0; j < ledgeList[i].numPlanes; j++ ) {
				sideList.Append( new idBrushSide( ledgeList[i].planes[j], -1 ) );
			}

			brush = new idBrush();
			brush->FromSides( sideList );

			ledgeMap->WriteBrush( brush );

			delete brush;
		}

		// flood tree from the ledge node and subdivide areas with the ledge
		LedgeSubdivLeafNodes_r( ledgeList[i].node, &ledgeList[i] );

		// remove the node visited flags
		ledgeList[i].node->RemoveFlagRecurseFlood( NODE_VISITED );
	}
}

/*
============
idAASBuild::IsLedgeSide_r
============
*/
bool idAASBuild::IsLedgeSide_r( idBrushBSPNode *node, idFixedWinding *w, const idPlane &plane, const idVec3 &normal, const idVec3 &origin, const float radius ) {
	int res, i;
	idFixedWinding back;
	float dist;

	if ( !node ) {
		return false;
	}

	while ( node->GetChild(0) && node->GetChild(1) ) {
		dist = node->GetPlane().Distance( origin );
		if ( dist > radius ) {
			res = SIDE_FRONT;
		}
		else if ( dist < -radius ) {
			res = SIDE_BACK;
		}
		else {
			res = w->Split( &back, node->GetPlane(), LEDGE_EPSILON );
		}
		if ( res == SIDE_FRONT ) {
			node = node->GetChild(0);
		}
		else if ( res == SIDE_BACK ) {
			node = node->GetChild(1);
		}
		else if ( res == SIDE_ON ) {
			// continue with the side the winding faces
			if ( node->GetPlane().Normal() * normal > 0.0f ) {
				node = node->GetChild(0);
			}
			else {
				node = node->GetChild(1);
			}
		}
		else {
			if ( IsLedgeSide_r( node->GetChild(1), &back, plane, normal, origin, radius ) ) {
				return true;
			}
			node = node->GetChild(0);
		}
	}

	if ( node->GetContents() & AREACONTENTS_SOLID ) {
		return false;
	}

	for ( i = 0; i < w->GetNumPoints(); i++ ) {
		if ( plane.Distance( (*w)[i].ToVec3() ) > 0.0f ) {
			return true;
		}
	}

	return false;
}

/*
============
idAASBuild::AddLedge
============
*/
void idAASBuild::AddLedge( const idVec3 &v1, const idVec3 &v2, idBrushBSPNode *node ) {
	int i, j, merged;

	// first try to merge the ledge with existing ledges
	merged = -1;
	for ( i = 0; i < ledgeList.Num(); i++ ) {

		for ( j = 0; j < 2; j++ ) {
			if ( idMath::Fabs( ledgeList[i].planes[j].Distance( v1 ) ) > LEDGE_EPSILON ) {
				break;
			}
			if ( idMath::Fabs( ledgeList[i].planes[j].Distance( v2 ) ) > LEDGE_EPSILON ) {
				break;
			}
		}
		if ( j < 2 ) {
			continue;
		}

		if ( !ledgeList[i].PointBetweenBounds( v1 ) &&
			!ledgeList[i].PointBetweenBounds( v2 ) ) {
			continue;
		}

		if ( merged == -1 ) {
			ledgeList[i].AddPoint( v1 );
			ledgeList[i].AddPoint( v2 );
			merged = i;
		}
		else {
			ledgeList[merged].AddPoint( ledgeList[i].start );
			ledgeList[merged].AddPoint( ledgeList[i].end );
			ledgeList.RemoveIndex(i);
			break;
		}
	}

	// if the ledge could not be merged
	if ( merged == -1 ) {
		ledgeList.Append( idLedge( v1, v2, aasSettings->gravityDir, node ) );
	}
}

/*
============
idAASBuild::FindLeafNodeLedges
============
*/
void idAASBuild::FindLeafNodeLedges( idBrushBSPNode *root, idBrushBSPNode *node ) {
	int s1, i;
	idBrushBSPPortal *p1;
	idWinding *w;
	idVec3 v1, v2, normal, origin;
	idFixedWinding winding;
	idBounds bounds;
	idPlane plane;
	float radius;

	for ( p1 = node->GetPortals(); p1; p1 = p1->Next(s1) ) {
		s1 = (p1->GetNode(1) == node);

		if ( !(p1->GetFlags() & FACE_FLOOR) ) {
			continue;
		}

		if ( s1 ) {
			plane = p1->GetPlane();
			w = p1->GetWinding()->Reverse();
		}
		else {
			plane = -p1->GetPlane();
			w = p1->GetWinding();
		}

		for ( i = 0; i < w->GetNumPoints(); i++ ) {

			v1 = (*w)[i].ToVec3();
			v2 = (*w)[(i+1)%w->GetNumPoints()].ToVec3();
			normal = (v2 - v1).Cross( aasSettings->gravityDir );
			if ( normal.Normalize() < 0.5f ) {
				continue;
			}

			winding.Clear();
			winding += v1 + normal * LEDGE_EPSILON * 0.5f;
			winding += v2 + normal * LEDGE_EPSILON * 0.5f;
			winding += winding[1].ToVec3() + ( aasSettings->maxStepHeight + 1.0f ) * aasSettings->gravityDir;
			winding += winding[0].ToVec3() + ( aasSettings->maxStepHeight + 1.0f ) * aasSettings->gravityDir;

			winding.GetBounds( bounds );
			origin = (bounds[1] - bounds[0]) * 0.5f;
			radius = origin.Length() + LEDGE_EPSILON;
			origin = bounds[0] + origin;

			plane.FitThroughPoint( v1 + aasSettings->maxStepHeight * aasSettings->gravityDir );

			if ( !IsLedgeSide_r( root, &winding, plane, normal, origin, radius ) ) {
				continue;
			}

			AddLedge( v1, v2, node );
		}

		if ( w != p1->GetWinding() ) {
			delete w;
		}
	}
}

/*
============
idAASBuild::FindLedges_r
============
*/
void idAASBuild::FindLedges_r( idBrushBSPNode *root, idBrushBSPNode *node ) {
	if ( !node ) {
		return;
	}

	if ( node->GetContents() & AREACONTENTS_SOLID ) {
		return;
	}

	if ( !node->GetChild(0) && !node->GetChild(1) ) {
		if ( node->GetFlags() & NODE_VISITED ) {
			return;
		}
		FindLeafNodeLedges( root, node );
		node->SetFlag( NODE_VISITED );
		return;
	}

	FindLedges_r( root, node->GetChild(0) );
	FindLedges_r( root, node->GetChild(1) );
}

/*
============
idAASBuild::WriteLedgeMap
============
*/
void idAASBuild::WriteLedgeMap( const idStr &fileName, const idStr &ext ) {
	ledgeMap = new idBrushMap( fileName, ext );
	ledgeMap->SetTexture( "textures/base_trim/bluetex4q_ed" );
}

/*
============
idAASBuild::LedgeSubdivision

  NOTE: this assumes the bounding box is higher than the maximum step height
		only ledges with vertical sides are considered
============
*/
void idAASBuild::LedgeSubdivision( idBrushBSP &bsp ) {
	numLedgeSubdivisions = 0;
	ledgeList.Clear();

	common->Printf( "[Ledge Subdivision]\n" );

	bsp.GetRootNode()->RemoveFlagRecurse( NODE_VISITED );
	FindLedges_r( bsp.GetRootNode(), bsp.GetRootNode() );
	bsp.GetRootNode()->RemoveFlagRecurse( NODE_VISITED );

	common->Printf( "\r%6d ledges\n", ledgeList.Num() );

	LedgeSubdiv( bsp.GetRootNode() );

	common->Printf( "\r%6d subdivisions\n", numLedgeSubdivisions );
}