summaryrefslogtreecommitdiff
path: root/engine/r_areaportal.cpp
blob: 4773fc1e6b25bccf3e377bd969dae8656a9c8696 (plain) (blame)
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "render_pch.h"
#include "client.h"
#include "debug_leafvis.h"
#include "con_nprint.h"
#include "tier0/fasttimer.h"
#include "r_areaportal.h"
#include "cmodel_engine.h"
#include "con_nprint.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

ConVar r_ClipAreaPortals( "r_ClipAreaPortals", "1", FCVAR_CHEAT );
ConVar r_DrawPortals( "r_DrawPortals", "0", FCVAR_CHEAT );

CUtlVector<CPortalRect> g_PortalRects;
static bool				g_bViewerInSolidSpace = false;

			  

// ------------------------------------------------------------------------------------ //
// Classes.
// ------------------------------------------------------------------------------------ //

#define MAX_PORTAL_VERTS	32


class CAreaCullInfo
{
public:
	CAreaCullInfo()
	{
		m_GlobalCounter = 0;
		memset( &m_Frustum, 0, sizeof( m_Frustum ) );
		memset( &m_Rect, 0, sizeof( m_Rect ) );
	}
	Frustum_t		m_Frustum;
	CPortalRect		m_Rect;
	unsigned short	m_GlobalCounter; // Used to tell if it's been touched yet this frame.
};



// ------------------------------------------------------------------------------------ //
// Globals.
// ------------------------------------------------------------------------------------ //

// Visible areas from the client DLL + occluded areas using area portals.
unsigned char g_RenderAreaBits[32];

// Used to prevent it from coming back into portals while flowing through them.
static unsigned char g_AreaStack[32];

// Frustums for each area for the current frame. Used to cull out leaves.
static CUtlVector<CAreaCullInfo> g_AreaCullInfo;

// List of areas marked visible this frame.
static unsigned short g_VisibleAreas[MAX_MAP_AREAS];
static int g_nVisibleAreas;

// Tied to CAreaCullInfo::m_GlobalCounter.
static unsigned short g_GlobalCounter = 1;


// A copy of the current view setup, but possibly nobbled a bit.
static CViewSetup g_viewSetup;
static CPortalRect g_viewWindow;
// Maps from world space to normalised (-1,1) screen coords.
static VMatrix g_ScreenFromWorldProjection;


// ------------------------------------------------------------------------------------ //
// Functions.
// ------------------------------------------------------------------------------------ //
void R_Areaportal_LevelInit()
{
	g_AreaCullInfo.SetCount( host_state.worldbrush->m_nAreas );
}

void R_Areaportal_LevelShutdown()
{
	g_AreaCullInfo.Purge();
	g_PortalRects.Purge();
}

static inline void R_SetBit( unsigned char *pBits, int bit )
{
	pBits[bit>>3] |= (1 << (bit&7));
}

static inline void R_ClearBit( unsigned char *pBits, int bit )
{
	pBits[bit>>3] &= ~(1 << (bit&7));
}

static inline unsigned char R_TestBit( unsigned char *pBits, int bit )
{
	return pBits[bit>>3] & (1 << (bit&7));
}

struct portalclip_t
{
	portalclip_t()
	{
		lists[0] = v0;
		lists[1] = v1;
	}
	Vector v0[MAX_PORTAL_VERTS];
	Vector v1[MAX_PORTAL_VERTS];
	Vector *lists[2];
};

// Transforms and clips the portal's verts to the view frustum. Returns false
// if the verts lie outside the frustum.
static inline bool GetPortalScreenExtents( dareaportal_t *pPortal, 
	portalclip_t * RESTRICT clip, CPortalRect &portalRect , float *pReflectionWaterHeight )
{
	portalRect.left = portalRect.bottom = 1e24;
	portalRect.right = portalRect.top   = -1e24;
	bool bValidExtents = false;
	worldbrushdata_t *pBrushData = host_state.worldbrush;
	
	int nStartVerts = min( (int)pPortal->m_nClipPortalVerts, MAX_PORTAL_VERTS );

	// NOTE: We need two passes to deal with reflection. We need to compute
	// the screen extents for both the reflected + non-reflected area portals
	// and make bounds that surrounds them both.
	int nPassCount = ( pReflectionWaterHeight != NULL ) ? 2 : 1;
	for ( int j = 0; j < nPassCount; ++j )
	{
		int i;
		for( i=0; i < nStartVerts; i++ )
		{
			clip->v0[i] = pBrushData->m_pClipPortalVerts[pPortal->m_FirstClipPortalVert+i];

			// 2nd pass is to compute the reflected areaportal position
			if ( j == 1 )
			{
				clip->v0[i].z = 2.0f * ( *pReflectionWaterHeight ) - clip->v0[i].z;
			}
		}

		int iCurList = 0;
		bool bAllClipped = false;
		for( int iPlane=0; iPlane < 4; iPlane++ )
		{
			const cplane_t *pPlane = g_Frustum.GetPlane(iPlane);

			Vector *pIn = clip->lists[iCurList];
			Vector *pOut = clip->lists[!iCurList];

			int nOutVerts = 0;
			int iPrev = nStartVerts - 1;
			float flPrevDot = pPlane->normal.Dot( pIn[iPrev] ) - pPlane->dist;
			for( int iCur=0; iCur < nStartVerts; iCur++ )
			{
				float flCurDot = pPlane->normal.Dot( pIn[iCur] ) - pPlane->dist;

				if( (flCurDot > 0) != (flPrevDot > 0) )
				{
					if( nOutVerts < MAX_PORTAL_VERTS )
					{
						// Add the vert at the intersection.
						float t = flPrevDot / (flPrevDot - flCurDot);
						VectorLerp( pIn[iPrev], pIn[iCur], t, pOut[nOutVerts] );

						++nOutVerts;
					}
				}

				// Add this vert?
				if( flCurDot > 0 )
				{
					if( nOutVerts < MAX_PORTAL_VERTS )
					{
						pOut[nOutVerts] = pIn[iCur];
						++nOutVerts;
					}
				}

				flPrevDot = flCurDot;
				iPrev = iCur;
			}

			if( nOutVerts == 0 )
			{
				// If they're all behind, then this portal is clipped out.
				bAllClipped = true;
				break;
			}

			nStartVerts = nOutVerts;
			iCurList = !iCurList;
		}

		if ( bAllClipped )
			continue;

		// Project all the verts and figure out the screen extents.
		Vector screenPos;
		Assert( iCurList == 0 );
		for( i=0; i < nStartVerts; i++ )
		{
			Vector &point = clip->v0[i];

			g_EngineRenderer->ClipTransformWithProjection ( g_ScreenFromWorldProjection, point, &screenPos );

			portalRect.left   = fpmin( screenPos.x, portalRect.left );
			portalRect.bottom = fpmin( screenPos.y, portalRect.bottom );
			portalRect.top    = fpmax( screenPos.y, portalRect.top );
			portalRect.right  = fpmax( screenPos.x, portalRect.right );
		}
		bValidExtents = true;
	}

	if ( !bValidExtents )
	{
		portalRect.left = portalRect.bottom = 0;
		portalRect.right = portalRect.top   = 0;
	}

	return bValidExtents;
}


// Fill in the intersection between the two rectangles.
inline bool GetRectIntersection( CPortalRect const *pRect1, CPortalRect const *pRect2, CPortalRect *pOut )
{
	pOut->left  = fpmax( pRect1->left, pRect2->left );
	pOut->right = fpmin( pRect1->right, pRect2->right );
	if( pOut->left >= pOut->right )
		return false;

	pOut->bottom = fpmax( pRect1->bottom, pRect2->bottom );
	pOut->top    = fpmin( pRect1->top, pRect2->top );
	if( pOut->bottom >= pOut->top )
		return false;

	return true;
}

static void R_FlowThroughArea( int area, const Vector &vecVisOrigin, const CPortalRect *pClipRect, 
	const VisOverrideData_t* pVisData, float *pReflectionWaterHeight )
{
#ifndef SWDS
	// Update this area's frustum.
	if( g_AreaCullInfo[area].m_GlobalCounter != g_GlobalCounter )
	{
		g_VisibleAreas[g_nVisibleAreas] = area;
		++g_nVisibleAreas;

		g_AreaCullInfo[area].m_GlobalCounter = g_GlobalCounter;
		g_AreaCullInfo[area].m_Rect = *pClipRect;
	}
	else
	{
		// Expand the areaportal's rectangle to include the new cliprect.
		CPortalRect *pFrustumRect = &g_AreaCullInfo[area].m_Rect;
		pFrustumRect->left   = fpmin( pFrustumRect->left, pClipRect->left );
		pFrustumRect->bottom = fpmin( pFrustumRect->bottom, pClipRect->bottom );
		pFrustumRect->top    = fpmax( pFrustumRect->top, pClipRect->top );
		pFrustumRect->right  = fpmax( pFrustumRect->right, pClipRect->right );
	}
	
	// Mark this area as visible.
	R_SetBit( g_RenderAreaBits, area );

	// Set that we're in this area on the stack.
	R_SetBit( g_AreaStack, area );

	worldbrushdata_t *pBrushData = host_state.worldbrush;

	Assert( area < host_state.worldbrush->m_nAreas );
	darea_t *pArea = &host_state.worldbrush->m_pAreas[area];
	// temp buffer for clipping
	portalclip_t clipTmp;

	// Check all areas that connect to this area.
	for( int iAreaPortal=0; iAreaPortal < pArea->numareaportals; iAreaPortal++ )
	{
		Assert( pArea->firstareaportal + iAreaPortal < pBrushData->m_nAreaPortals );
		dareaportal_t *pAreaPortal = &pBrushData->m_pAreaPortals[ pArea->firstareaportal + iAreaPortal ];

		// Don't flow back into a portal on the stack.
		if( R_TestBit( g_AreaStack, pAreaPortal->otherarea ) )
			continue;

		// If this portal is closed, don't go through it.
		if ( !R_TestBit( cl.m_chAreaPortalBits, pAreaPortal->m_PortalKey ) )
			continue;

		// Make sure the viewer is on the right side of the portal to see through it.
		cplane_t *pPlane = &pBrushData->planes[ pAreaPortal->planenum ];
		// Use the specified vis origin to test backface culling, or the main view if none was specified
		float flDist = pPlane->normal.Dot( vecVisOrigin ) - pPlane->dist;
		if( flDist < -0.1f )
			continue;

		// If the client doesn't want this area visible, don't try to flow into it.
		if( !R_TestBit( cl.m_chAreaBits, pAreaPortal->otherarea ) )
			continue;

		CPortalRect portalRect;
		bool portalVis = true;

		// don't try to clip portals if the viewer is practically in the plane
		float fDistTolerance = (pVisData)?(pVisData->m_fDistToAreaPortalTolerance):(0.1f);
		if ( flDist > fDistTolerance )
		{
			portalVis = GetPortalScreenExtents( pAreaPortal, &clipTmp, portalRect, pReflectionWaterHeight );
		}
		else
		{
			portalRect.left = -1;
			portalRect.top = 1;
			portalRect.right = 1;
			portalRect.bottom = -1;	// note top/bottom reversed!
			//portalVis=true - not needed, default
		}
		if( portalVis )
		{
			CPortalRect intersection;
			if( GetRectIntersection( &portalRect, pClipRect, &intersection ) )
			{
#ifdef USE_CONVARS
				if( r_DrawPortals.GetInt() )
				{
					g_PortalRects.AddToTail( intersection );
				}
#endif

				// Ok, we can see into this area.
				R_FlowThroughArea( pAreaPortal->otherarea, vecVisOrigin, &intersection, pVisData, pReflectionWaterHeight );
			}
		}
	}
	
	// Mark that we're leaving this area.
	R_ClearBit( g_AreaStack, area );
#endif
}


static void IncrementGlobalCounter()
{
	if( g_GlobalCounter == 0xFFFF )
	{
		for( int i=0; i < g_AreaCullInfo.Count(); i++ )
			g_AreaCullInfo[i].m_GlobalCounter = 0;
	
		g_GlobalCounter = 1;
	}
	else
	{
		g_GlobalCounter++;
	}
}


static void R_SetupGlobalFrustum()
{
#ifndef SWDS

	// Copy the current view away so that we can play with it if needed.
	g_viewSetup = g_EngineRenderer->ViewGetCurrent();

	if( g_viewSetup.m_bOrtho )
	{
		g_viewWindow.right	= g_viewSetup.m_OrthoRight;
		g_viewWindow.left		= g_viewSetup.m_OrthoLeft;
		g_viewWindow.top		= g_viewSetup.m_OrthoTop;
		g_viewWindow.bottom	= g_viewSetup.m_OrthoBottom;
	}
	else
	{
		// Assuming a view plane distance of 1, figure out the boundaries of a window
		// the view would project into given the FOV.
		float xFOV = g_EngineRenderer->GetFov() * 0.5f;
		float yFOV = g_EngineRenderer->GetFovY() * 0.5f;

		g_viewWindow.right	= tan( DEG2RAD( xFOV ) );
		g_viewWindow.left	= -g_viewWindow.right;
		g_viewWindow.top	= tan( DEG2RAD( yFOV ) );
		g_viewWindow.bottom	= -g_viewWindow.top;

		if ( g_viewSetup.m_bOffCenter )
		{
			// How did this ever work?
			Assert ( !"test m_bOffCenter frustums with area portals" );
		}
		else if ( g_viewSetup.m_bViewToProjectionOverride )
		{
			// ...this has been tested and works!
		}

		// Rather than try to deal with crazy projection matrices (shear, trapezoid, etc), take whatever FOV we're given,
		// assume it's conservative, and then construct a matching projection matrix for it. Then use that consistent
		// hallucination throughout, rather than refer back to the engine's actual proj. matrix for anything.
		VMatrix matrixView;
		VMatrix matrixProjection;
		VMatrix matrixWorldToScreen;
		g_viewSetup.m_bViewToProjectionOverride = false;

		ComputeViewMatrices (
			&matrixView, 
			&matrixProjection,
			&matrixWorldToScreen,
			g_viewSetup );

		g_ScreenFromWorldProjection = matrixWorldToScreen;
	}

#endif //#ifndef SWDS
}

ConVar r_snapportal( "r_snapportal", "-1" );
extern void CSGFrustum( Frustum_t &frustum );

static void R_SetupVisibleAreaFrustums()
{
#ifndef SWDS

	// Now scale the portals as specified in the normalized view frustum (-1,-1,1,1)
	// into our view window and generate planes out of that.
	for( int i=0; i < g_nVisibleAreas; i++ )
	{
		CAreaCullInfo *pInfo = &g_AreaCullInfo[ g_VisibleAreas[i] ];

		CPortalRect portalWindow;
		portalWindow.left    = RemapVal( pInfo->m_Rect.left,   -1, 1, g_viewWindow.left,   g_viewWindow.right );
		portalWindow.right   = RemapVal( pInfo->m_Rect.right,  -1, 1, g_viewWindow.left,   g_viewWindow.right );
		portalWindow.top     = RemapVal( pInfo->m_Rect.top,    -1, 1, g_viewWindow.bottom, g_viewWindow.top );
		portalWindow.bottom  = RemapVal( pInfo->m_Rect.bottom, -1, 1, g_viewWindow.bottom, g_viewWindow.top );
		
		if( g_viewSetup.m_bOrtho )
		{
			// Left and right planes...
			float orgOffset = DotProduct(CurrentViewOrigin(), CurrentViewRight());
			pInfo->m_Frustum.SetPlane( FRUSTUM_LEFT, PLANE_ANYZ, CurrentViewRight(), portalWindow.left + orgOffset );
			pInfo->m_Frustum.SetPlane( FRUSTUM_RIGHT, PLANE_ANYZ, -CurrentViewRight(), -portalWindow.right - orgOffset );

			// Top and bottom planes...
			orgOffset = DotProduct(CurrentViewOrigin(), CurrentViewUp());
			pInfo->m_Frustum.SetPlane( FRUSTUM_TOP, PLANE_ANYZ, CurrentViewUp(), portalWindow.top + orgOffset );
			pInfo->m_Frustum.SetPlane( FRUSTUM_BOTTOM, PLANE_ANYZ, -CurrentViewUp(), -portalWindow.bottom - orgOffset );
		}
		else
		{

			if ( g_viewSetup.m_bOffCenter )
			{
				// How did this ever work?
				Assert ( !"test m_bOffCenter frustums with area portals" );
			}
			else if ( g_viewSetup.m_bViewToProjectionOverride )
			{
				// ...this has been tested and works!
			}

			Vector normal;
			const cplane_t *pTest;

			// right side
			normal = portalWindow.right * CurrentViewForward() - CurrentViewRight();
			VectorNormalize(normal); // OPTIMIZE: This is unnecessary for culling
			pTest = pInfo->m_Frustum.GetPlane( FRUSTUM_RIGHT );
			pInfo->m_Frustum.SetPlane( FRUSTUM_RIGHT, PLANE_ANYZ, normal, DotProduct(normal,CurrentViewOrigin()) );

			// left side
			normal = CurrentViewRight() - portalWindow.left * CurrentViewForward();
			VectorNormalize(normal); // OPTIMIZE: This is unnecessary for culling
			pTest = pInfo->m_Frustum.GetPlane( FRUSTUM_LEFT );
			pInfo->m_Frustum.SetPlane( FRUSTUM_LEFT, PLANE_ANYZ, normal, DotProduct(normal,CurrentViewOrigin()) );

			// top
			normal = portalWindow.top * CurrentViewForward() - CurrentViewUp();
			VectorNormalize(normal); // OPTIMIZE: This is unnecessary for culling
			pTest = pInfo->m_Frustum.GetPlane( FRUSTUM_TOP );
			pInfo->m_Frustum.SetPlane( FRUSTUM_TOP, PLANE_ANYZ, normal, DotProduct(normal,CurrentViewOrigin()) );

			// bottom
			normal = CurrentViewUp() - portalWindow.bottom * CurrentViewForward();
			VectorNormalize(normal); // OPTIMIZE: This is unnecessary for culling
			pTest = pInfo->m_Frustum.GetPlane( FRUSTUM_BOTTOM );
			pInfo->m_Frustum.SetPlane( FRUSTUM_BOTTOM, PLANE_ANYZ, normal, DotProduct(normal,CurrentViewOrigin()) );

			// farz
			pInfo->m_Frustum.SetPlane( FRUSTUM_FARZ, PLANE_ANYZ, -CurrentViewForward(), 
				DotProduct(-CurrentViewForward(), CurrentViewOrigin() + CurrentViewForward()*g_viewSetup.zFar) );
		}

		// DEBUG: Code to visualize the areaportal frustums in 3D
		// Useful for debugging
		extern void CSGFrustum( Frustum_t &frustum );
		if ( r_snapportal.GetInt() >= 0 )
		{
			if ( g_VisibleAreas[i] == r_snapportal.GetInt() )
			{
				pInfo->m_Frustum.SetPlane( FRUSTUM_NEARZ, PLANE_ANYZ, CurrentViewForward(), 
					DotProduct(CurrentViewForward(), CurrentViewOrigin()) );
				pInfo->m_Frustum.SetPlane( FRUSTUM_FARZ, PLANE_ANYZ, -CurrentViewForward(), 
					DotProduct(-CurrentViewForward(), CurrentViewOrigin() + CurrentViewForward()*500) );
				r_snapportal.SetValue( -1 );
				CSGFrustum( pInfo->m_Frustum );
			}
		}
	}
#endif
}




// Intersection of AABB and a frustum. The frustum may contain 0-32 planes
// (active planes are defined by inClipMask). Returns boolean value indicating
// whether AABB intersects the view frustum or not.
// If AABB intersects the frustum, an output clip mask is returned as well
// (indicating which planes are crossed by the AABB). This information
// can be used to optimize testing of child nodes or objects inside the
// nodes (pass value as 'inClipMask').
inline bool R_CullNodeInternal( mnode_t *pNode, int &nClipMask, const Frustum_t& frustum )
{
	int nOutClipMask = nClipMask & FRUSTUM_CLIP_IN_AREA;	// init outclip mask

	float flCenterDotNormal, flHalfDiagDotAbsNormal;
	if (nClipMask & FRUSTUM_CLIP_RIGHT)
	{
		const cplane_t *pPlane = frustum.GetPlane(FRUSTUM_RIGHT);
		flCenterDotNormal = DotProduct( pNode->m_vecCenter, pPlane->normal ) - pPlane->dist;
		flHalfDiagDotAbsNormal = DotProduct( pNode->m_vecHalfDiagonal, frustum.GetAbsNormal(FRUSTUM_RIGHT) );
		if (flCenterDotNormal + flHalfDiagDotAbsNormal < 0.0f)
			return true;
		if (flCenterDotNormal - flHalfDiagDotAbsNormal < 0.0f)
			nOutClipMask |= FRUSTUM_CLIP_RIGHT;	
	}

	if (nClipMask & FRUSTUM_CLIP_LEFT)
	{
		const cplane_t *pPlane = frustum.GetPlane(FRUSTUM_LEFT);
		flCenterDotNormal = DotProduct( pNode->m_vecCenter, pPlane->normal ) - pPlane->dist;
		flHalfDiagDotAbsNormal = DotProduct( pNode->m_vecHalfDiagonal, frustum.GetAbsNormal(FRUSTUM_LEFT) );
		if (flCenterDotNormal + flHalfDiagDotAbsNormal < 0.0f)
			return true;
		if (flCenterDotNormal - flHalfDiagDotAbsNormal < 0.0f)
			nOutClipMask |= FRUSTUM_CLIP_LEFT;	
	}

	if (nClipMask & FRUSTUM_CLIP_TOP)
	{
		const cplane_t *pPlane = frustum.GetPlane(FRUSTUM_TOP);
		flCenterDotNormal = DotProduct( pNode->m_vecCenter, pPlane->normal ) - pPlane->dist;
		flHalfDiagDotAbsNormal = DotProduct( pNode->m_vecHalfDiagonal, frustum.GetAbsNormal(FRUSTUM_TOP) );
		if (flCenterDotNormal + flHalfDiagDotAbsNormal < 0.0f)
			return true;
		if (flCenterDotNormal - flHalfDiagDotAbsNormal < 0.0f)
			nOutClipMask |= FRUSTUM_CLIP_TOP;	
	}

	if (nClipMask & FRUSTUM_CLIP_BOTTOM)
	{
		const cplane_t *pPlane = frustum.GetPlane(FRUSTUM_BOTTOM);
		flCenterDotNormal = DotProduct( pNode->m_vecCenter, pPlane->normal ) - pPlane->dist;
		flHalfDiagDotAbsNormal = DotProduct( pNode->m_vecHalfDiagonal, frustum.GetAbsNormal(FRUSTUM_BOTTOM) );
		if (flCenterDotNormal + flHalfDiagDotAbsNormal < 0.0f)
			return true;
		if (flCenterDotNormal - flHalfDiagDotAbsNormal < 0.0f)
			nOutClipMask |= FRUSTUM_CLIP_BOTTOM;	
	}

	nClipMask = nOutClipMask;
	return false;						// AABB intersects frustum
}


bool R_CullNode( Frustum_t *pAreaFrustum, mnode_t *pNode, int& nClipMask )
{
	// Now cull to this area's frustum.
	if (( !g_bViewerInSolidSpace ) && ( pNode->area > 0 ))
	{
		// First make sure its whole area is even visible.
		if( !R_IsAreaVisible( pNode->area ) )
			return true;

		// This ConVar access causes a huge perf hit in some cases.
		// If you want to put it back in please cache it's value.
//#ifdef USE_CONVARS
//		if( r_ClipAreaPortals.GetInt() )
//#else
		if( 1 )
//#endif
		{
			if ((nClipMask & FRUSTUM_CLIP_IN_AREA) == 0)
			{
				// In this case, we've never hit this area before and
				// we need to test all planes again because we just changed the frustum
				nClipMask = FRUSTUM_CLIP_IN_AREA | FRUSTUM_CLIP_ALL;
			}

			pAreaFrustum = &g_AreaCullInfo[pNode->area].m_Frustum;
		}
	}

	return R_CullNodeInternal( pNode, nClipMask, *pAreaFrustum );
}


static ConVar r_portalscloseall( "r_portalscloseall", "0", FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
static ConVar r_portalsopenall( "r_portalsopenall", "0", FCVAR_CHEAT, "Open all portals" );
static ConVar r_ShowViewerArea( "r_ShowViewerArea", "0" );

void R_SetupAreaBits( int iForceViewLeaf /* = -1 */, const VisOverrideData_t* pVisData /* = NULL */, float *pWaterReflectionHeight /* = NULL */ )
{
	IncrementGlobalCounter();

	g_bViewerInSolidSpace = false;

	// Clear the visible area bits.
	memset( g_RenderAreaBits, 0, sizeof( g_RenderAreaBits ) );
	memset( g_AreaStack, 0, sizeof( g_AreaStack ) );

	// Our initial clip rect is the whole screen.
	CPortalRect rect;
	rect.left = rect.bottom = -1;
	rect.top = rect.right = 1;

	// Flow through areas starting at the one we're in.
	int leaf = iForceViewLeaf;
	// If view point override wasn't specified, use the current view origin
	if ( iForceViewLeaf == -1  ) 
	{
		leaf = CM_PointLeafnum( g_EngineRenderer->ViewOrigin() );
	}

	if( r_portalscloseall.GetBool() )
	{
		if ( cl.m_bAreaBitsValid )
		{
			// Clear the visible area bits.
			memset( g_RenderAreaBits, 0, sizeof( g_RenderAreaBits ) );
			int area = host_state.worldbrush->leafs[leaf].area;
			R_SetBit( g_RenderAreaBits, area );
			
			g_VisibleAreas[0] = area;
			g_nVisibleAreas = 1;

			g_AreaCullInfo[area].m_GlobalCounter = g_GlobalCounter;
			g_AreaCullInfo[area].m_Rect = rect;
			
			R_SetupVisibleAreaFrustums();
		}
		else
		{
			g_bViewerInSolidSpace = true;
		}

		return;
	}

#if defined( REPLAY_ENABLED )
	if ( host_state.worldbrush->leafs[leaf].contents & CONTENTS_SOLID ||
		 cl.ishltv || cl.isreplay || !cl.m_bAreaBitsValid || r_portalsopenall.GetBool()  )
#else
	if ( host_state.worldbrush->leafs[leaf].contents & CONTENTS_SOLID ||
		 cl.ishltv || !cl.m_bAreaBitsValid || r_portalsopenall.GetBool()  )
#endif
	{
		// Draw everything if we're in solid space or if r_portalsopenall is true (used for building cubemaps)
		g_bViewerInSolidSpace = true;

		if ( r_ShowViewerArea.GetInt() )
			Con_NPrintf( 3, "Viewer area: (solid space)" );
	}
	else
	{
		int area = host_state.worldbrush->leafs[leaf].area;
		
		if ( r_ShowViewerArea.GetInt() )
			Con_NPrintf( 3, "Viewer area: %d", area );

		g_nVisibleAreas = 0;		
		Vector vecVisOrigin = (pVisData)?(pVisData->m_vecVisOrigin):(g_EngineRenderer->ViewOrigin());
		R_SetupGlobalFrustum();
		R_FlowThroughArea ( area, vecVisOrigin, &rect, pVisData, pWaterReflectionHeight );
		R_SetupVisibleAreaFrustums();
	}
}


const Frustum_t* GetAreaFrustum( int area )
{
	if ( g_AreaCullInfo[area].m_GlobalCounter == g_GlobalCounter )
		return &g_AreaCullInfo[area].m_Frustum;
	else
		return &g_Frustum;
}