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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#include "render_pch.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// Leaf visualization routines
struct leafvis_t
{
leafvis_t()
{
leafIndex = 0;
CCollisionBSPData *pBSP = GetCollisionBSPData();
if ( pBSP )
{
numbrushes = pBSP->numbrushes;
numentitychars = pBSP->numentitychars;
}
}
bool IsValid()
{
CCollisionBSPData *pBSP = GetCollisionBSPData();
if ( !pBSP || numbrushes != pBSP->numbrushes || numentitychars != pBSP->numentitychars )
return false;
return true;
}
CUtlVector<Vector> verts;
CUtlVector<int> polyVertCount;
Vector color;
int numbrushes;
int numentitychars;
int leafIndex;
};
const int MAX_LEAF_PVERTS = 128;
// Only allocate this after it is turned on
leafvis_t *g_LeafVis = NULL;
static void AddPlaneToList( CUtlVector<cplane_t> &list, const Vector& normal, float dist, int invert )
{
cplane_t plane;
plane.dist = invert ? -dist : dist;
plane.normal = invert ? -normal : normal;
Vector point = plane.dist * plane.normal;
for ( int i = 0; i < list.Count(); i++ )
{
// same plane, remove or replace
if ( list[i].normal == plane.normal )
{
float d = DotProduct(point, list[i].normal) - list[i].dist;
if ( d > 0 )
{
// new plane is in front of the old one
list[i].dist = plane.dist;
}
// new plane is behind the old one
return;
}
}
list.AddToTail( plane );
}
static void PlaneList( int leafIndex, model_t *model, CUtlVector<cplane_t> &planeList )
{
if (!model || !model->brush.pShared || !model->brush.pShared->nodes)
Sys_Error ("PlaneList: bad model");
mleaf_t *pLeaf = &model->brush.pShared->leafs[leafIndex];
mnode_t *pNode = pLeaf->parent;
mnode_t *pChild = (mnode_t *)pLeaf;
while (pNode)
{
// was the child on the front or back of the plane of this node?
bool front = (pNode->children[0] == pChild) ? true : false;
AddPlaneToList( planeList, pNode->plane->normal, pNode->plane->dist, !front );
pChild = pNode;
pNode = pNode->parent;
}
}
Vector CSGInsidePoint( cplane_t *pPlanes, int planeCount )
{
Vector point = vec3_origin;
for ( int i = 0; i < planeCount; i++ )
{
float d = DotProduct( pPlanes[i].normal, point ) - pPlanes[i].dist;
if ( d < 0 )
{
point -= d * pPlanes[i].normal;
}
}
return point;
}
void TranslatePlaneList( cplane_t *pPlanes, int planeCount, const Vector &offset )
{
for ( int i = 0; i < planeCount; i++ )
{
pPlanes[i].dist += DotProduct( offset, pPlanes[i].normal );
}
}
void CSGPlaneList( leafvis_t *pVis, CUtlVector<cplane_t> &planeList)
{
int planeCount = planeList.Count();
Vector vertsIn[MAX_LEAF_PVERTS], vertsOut[MAX_LEAF_PVERTS];
// compute a point inside the volume defined by these planes
Vector insidePoint = CSGInsidePoint( planeList.Base(), planeList.Count() );
// move the planes so that the inside point is at the origin
// NOTE: This is to maximize precision for the CSG operations
TranslatePlaneList( planeList.Base(), planeList.Count(), -insidePoint );
// Build the CSG solid of this leaf given that the planes in the list define a convex solid
for ( int i = 0; i < planeCount; i++ )
{
// Build a big-ass poly in this plane
int vertCount = PolyFromPlane( vertsIn, planeList[i].normal, planeList[i].dist ); // BaseWindingForPlane()
// Now chop it by every other plane
int j;
for ( j = 0; j < planeCount; j++ )
{
// don't clip planes with themselves
if ( i == j )
continue;
// Less than a poly left, something's wrong, don't bother with this polygon
if ( vertCount < 3 )
continue;
// Chop the polygon against this plane
vertCount = ClipPolyToPlane( vertsIn, vertCount, vertsOut, planeList[j].normal, planeList[j].dist );
// Just copy the verts each time, don't bother swapping pointers (efficiency is not a goal here)
for ( int k = 0; k < vertCount; k++ )
{
VectorCopy( vertsOut[k], vertsIn[k] );
}
}
// We've got a polygon here
if ( vertCount >= 3 )
{
// Copy polygon out
pVis->polyVertCount.AddToTail( vertCount );
for ( j = 0; j < vertCount; j++ )
{
// move the verts back by the initial translation
Vector vert = vertsIn[j] + insidePoint;
pVis->verts.AddToTail( vert );
}
}
}
}
void LeafvisChanged( IConVar *pLeafvisVar, const char *pOld, float flOldValue )
{
if ( g_LeafVis )
{
delete g_LeafVis;
g_LeafVis = NULL;
}
}
void AddLeafPortals( leafvis_t *pLeafvis, int leafIndex )
{
CUtlVector<cplane_t> planeList;
Vector normal;
// Build a list of inward pointing planes of the tree descending to this
PlaneList( leafIndex, host_state.worldmodel, planeList );
VectorCopy( vec3_origin, normal );
// Add world bounding box planes in case the world isn't closed
// x-axis
normal[0] = 1;
AddPlaneToList( planeList, normal, MAX_COORD_INTEGER, true );
AddPlaneToList( planeList, normal, -MAX_COORD_INTEGER, false );
normal[0] = 0;
// y-axis
normal[1] = 1;
AddPlaneToList( planeList, normal, MAX_COORD_INTEGER, true );
AddPlaneToList( planeList, normal, -MAX_COORD_INTEGER, false );
normal[1] = 0;
// z-axis
normal[2] = 1;
AddPlaneToList( planeList, normal, MAX_COORD_INTEGER, true );
AddPlaneToList( planeList, normal, -MAX_COORD_INTEGER, false );
CSGPlaneList( pLeafvis, planeList );
}
ConVar mat_leafvis("mat_leafvis","0", FCVAR_CHEAT, "Draw wireframe of current leaf", LeafvisChanged );
ConVar r_visambient("r_visambient","0", 0, "Draw leaf ambient lighting samples. Needs mat_leafvis 1 to work" );
//-----------------------------------------------------------------------------
// Purpose: Builds a convex polyhedron of the leaf boundary around p
// Input : p - point to classify determining the leaf
//-----------------------------------------------------------------------------
void LeafVisBuild( const Vector &p )
{
if ( !mat_leafvis.GetInt() )
{
Assert( !g_LeafVis );
return;
}
else
{
static int last_leaf = -1;
int leafIndex = CM_PointLeafnum( p );
if ( g_LeafVis && last_leaf == leafIndex )
return;
DevMsg( 1, "Leaf %d, Area %d, Cluster %d\n", leafIndex, CM_LeafArea( leafIndex ), CM_LeafCluster( leafIndex ) );
last_leaf = leafIndex;
delete g_LeafVis;
g_LeafVis = new leafvis_t;
g_LeafVis->color.Init( 1.0f, 0.0f, 0.0f );
g_LeafVis->leafIndex = leafIndex;
switch( mat_leafvis.GetInt() )
{
case 2:
{
const mleaf_t *pLeaf = host_state.worldmodel->brush.pShared->leafs;
int leafCount = host_state.worldmodel->brush.pShared->numleafs;
int visCluster = pLeaf[leafIndex].cluster;
// do entire viscluster
for ( int i = 0; i < leafCount; i++ )
{
if ( pLeaf[i].cluster == visCluster )
{
AddLeafPortals( g_LeafVis, i );
}
}
}
break;
case 3:
{
// do entire pvs
byte pvs[ MAX_MAP_LEAFS/8 ];
const mleaf_t *pLeaf = host_state.worldmodel->brush.pShared->leafs;
int leafCount = host_state.worldmodel->brush.pShared->numleafs;
int visCluster = pLeaf[leafIndex].cluster;
CM_Vis( pvs, sizeof( pvs ), visCluster, DVIS_PVS );
for ( int i = 0; i < leafCount; i++ )
{
int cluster = pLeaf[i].cluster;
if ( cluster >= 0 && (pvs[cluster>>3] & (1<<(cluster&7))) )
{
AddLeafPortals( g_LeafVis, i );
}
}
}
break;
case 0:
default:
AddLeafPortals( g_LeafVis, leafIndex );
break;
}
}
}
#ifndef SWDS
void DrawLeafvis( leafvis_t *pVis )
{
CMatRenderContextPtr pRenderContext( materials );
int vert = 0;
g_materialLeafVisWireframe->ColorModulate( pVis->color[0], pVis->color[1], pVis->color[2] );
pRenderContext->Bind( g_materialLeafVisWireframe );
for ( int i = 0; i < pVis->polyVertCount.Count(); i++ )
{
if ( pVis->polyVertCount[i] >= 3 )
{
IMesh *pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_LINES, pVis->polyVertCount[i] );
for ( int j = 0; j < pVis->polyVertCount[i]; j++ )
{
meshBuilder.Position3fv( pVis->verts[ vert + j ].Base() );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( pVis->verts[ vert + ( ( j + 1 ) % pVis->polyVertCount[i] ) ].Base() );
meshBuilder.AdvanceVertex();
}
meshBuilder.End();
pMesh->Draw();
}
vert += pVis->polyVertCount[i];
}
}
void DrawLeafvis_Solid( leafvis_t *pVis )
{
CMatRenderContextPtr pRenderContext( materials );
int vert = 0;
Vector lightNormal(1,1,1);
VectorNormalize(lightNormal);
pRenderContext->Bind( g_pMaterialDebugFlat );
for ( int i = 0; i < pVis->polyVertCount.Count(); i++ )
{
int vertCount = pVis->polyVertCount[i];
if ( vertCount >= 3 )
{
IMesh *pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder meshBuilder;
int triangleCount = vertCount-2;
meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, triangleCount );
Vector e0 = pVis->verts[vert+1] - pVis->verts[vert];
Vector e1 = pVis->verts[vert+2] - pVis->verts[vert];
Vector normal = CrossProduct(e1,e0);
VectorNormalize( normal );
float light = 0.5f + (DotProduct(normal, lightNormal)*0.5f);
Vector color = pVis->color * light;
for ( int j = 0; j < vertCount; j++ )
{
meshBuilder.Position3fv( pVis->verts[ vert + j ].Base() );
meshBuilder.Color3fv( color.Base() );
meshBuilder.AdvanceVertex();
}
for ( int j = 0; j < triangleCount; j++ )
{
meshBuilder.FastIndex(0);
meshBuilder.FastIndex(j+2);
meshBuilder.FastIndex(j+1);
}
meshBuilder.End();
pMesh->Draw();
}
vert += vertCount;
}
}
leafvis_t *g_FrustumVis = NULL, *g_ClipVis[3] = {NULL,NULL,NULL};
int FindMinBrush( CCollisionBSPData *pBSPData, int nodenum, int brushIndex )
{
while (1)
{
if (nodenum < 0)
{
int leafIndex = -1 - nodenum;
cleaf_t &leaf = pBSPData->map_leafs[leafIndex];
int firstbrush = pBSPData->map_leafbrushes[ leaf.firstleafbrush ];
if ( firstbrush < brushIndex )
{
brushIndex = firstbrush;
}
return brushIndex;
}
cnode_t &node = pBSPData->map_rootnode[nodenum];
brushIndex = FindMinBrush( pBSPData, node.children[0], brushIndex );
nodenum = node.children[1];
}
return brushIndex;
}
void RecomputeClipbrushes( bool bEnabled )
{
for ( int v = 0; v < 3; v++ )
{
delete g_ClipVis[v];
g_ClipVis[v] = NULL;
}
if ( !bEnabled )
return;
for ( int v = 0; v < 3; v++ )
{
int contents[3] = {CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP, CONTENTS_MONSTERCLIP, CONTENTS_PLAYERCLIP};
g_ClipVis[v] = new leafvis_t;
g_ClipVis[v]->color.Init( v != 1 ? 1.0f : 0.5, 0.0f, v != 0 ? 1.0f : 0.0f );
CCollisionBSPData *pBSP = GetCollisionBSPData();
int lastBrush = pBSP->numbrushes;
if ( pBSP->numcmodels > 1 )
{
lastBrush = FindMinBrush( pBSP, pBSP->map_cmodels[1].headnode, lastBrush );
}
for ( int i = 0; i < lastBrush; i++ )
{
cbrush_t *pBrush = &pBSP->map_brushes[i];
if ( (pBrush->contents & (CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP)) == contents[v] )
{
CUtlVector<cplane_t> planeList;
if ( pBrush->IsBox() )
{
cboxbrush_t *pBox = &pBSP->map_boxbrushes[pBrush->GetBox()];
for ( int idxSide = 0; idxSide < 3; idxSide++ )
{
Vector normal = vec3_origin;
normal[idxSide] = 1.0f;
AddPlaneToList( planeList, normal, pBox->maxs[idxSide], true );
AddPlaneToList( planeList, -normal, -pBox->mins[idxSide], true );
}
}
else
{
for ( int j = 0; j < pBrush->numsides; j++ )
{
cbrushside_t *pSide = &pBSP->map_brushsides[pBrush->firstbrushside + j];
if ( pSide->bBevel )
continue;
AddPlaneToList( planeList, pSide->plane->normal, pSide->plane->dist, true );
}
}
CSGPlaneList( g_ClipVis[v], planeList );
}
}
}
}
// NOTE: UNDONE: This doesn't work on brush models - only the world.
void ClipChanged( IConVar *pConVar, const char *pOld, float flOldValue )
{
ConVarRef clipVar( pConVar );
RecomputeClipbrushes( clipVar.GetBool() );
}
static ConVar r_drawclipbrushes( "r_drawclipbrushes", "0", FCVAR_CHEAT, "Draw clip brushes (red=NPC+player, pink=player, purple=NPC)", ClipChanged );
static Vector LeafAmbientSamplePos( int leafIndex, const mleafambientlighting_t &sample )
{
mleaf_t *pLeaf = &host_state.worldbrush->leafs[leafIndex];
Vector out = pLeaf->m_vecCenter - pLeaf->m_vecHalfDiagonal;
out.x += float(sample.x) * pLeaf->m_vecHalfDiagonal.x * (2.0f / 255.0f);
out.y += float(sample.y) * pLeaf->m_vecHalfDiagonal.y * (2.0f / 255.0f);
out.z += float(sample.z) * pLeaf->m_vecHalfDiagonal.z * (2.0f / 255.0f);
return out;
}
// convert color formats
static void ColorRGBExp32ToColor32( const ColorRGBExp32 &color, color32 &out )
{
Vector tmp;
ColorRGBExp32ToVector( color, tmp );
out.r = LinearToScreenGamma(tmp.x);
out.g = LinearToScreenGamma(tmp.y);
out.b = LinearToScreenGamma(tmp.z);
}
// some simple helpers to draw a cube in the special way the ambient visualization wants
static Vector CubeSide( const Vector &pos, float size, int vert )
{
Vector side = pos;
side.x += (vert & 1) ? -size : size;
side.y += (vert & 2) ? -size : size;
side.z += (vert & 4) ? -size : size;
return side;
}
static void CubeFace( CMeshBuilder &meshBuilder, const Vector org, int v0, int v1, int v2, int v3, float size, const color32 &color )
{
meshBuilder.Position3fv( CubeSide(org,size,v0).Base() );
meshBuilder.Color4ubv( (byte *)&color );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( CubeSide(org,size,v1).Base() );
meshBuilder.Color4ubv( (byte *)&color );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( CubeSide(org,size,v2).Base() );
meshBuilder.Color4ubv( (byte *)&color );
meshBuilder.AdvanceVertex();
meshBuilder.Position3fv( CubeSide(org,size,v3).Base() );
meshBuilder.Color4ubv( (byte *)&color );
meshBuilder.AdvanceVertex();
}
//-----------------------------------------------------------------------------
// Purpose: Draw the leaf geometry that was computed by LeafVisBuild()
//-----------------------------------------------------------------------------
void LeafVisDraw( void )
{
if ( g_FrustumVis )
{
DrawLeafvis( g_FrustumVis );
}
if ( g_LeafVis )
{
DrawLeafvis( g_LeafVis );
}
if ( g_ClipVis[0] )
{
if ( !g_ClipVis[0]->IsValid() )
{
RecomputeClipbrushes(true);
}
if ( r_drawclipbrushes.GetInt() == 2 )
{
DrawLeafvis_Solid( g_ClipVis[0] );
DrawLeafvis_Solid( g_ClipVis[1] );
DrawLeafvis_Solid( g_ClipVis[2] );
}
else
{
DrawLeafvis( g_ClipVis[0] );
DrawLeafvis( g_ClipVis[1] );
DrawLeafvis( g_ClipVis[2] );
}
}
if ( g_LeafVis && r_visambient.GetBool() )
{
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->Bind( g_pMaterialDebugFlat );
float cubesize = 12.0f;
int leafIndex = g_LeafVis->leafIndex;
mleafambientindex_t *pAmbient = &host_state.worldbrush->m_pLeafAmbient[leafIndex];
if ( !pAmbient->ambientSampleCount && pAmbient->firstAmbientSample )
{
// this leaf references another leaf, move there (this leaf is a solid leaf so it borrows samples from a neighbor)
leafIndex = pAmbient->firstAmbientSample;
pAmbient = &host_state.worldbrush->m_pLeafAmbient[leafIndex];
}
for ( int i = 0; i < pAmbient->ambientSampleCount; i++ )
{
IMesh *pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_QUADS, 6 );
const mleafambientlighting_t &sample = host_state.worldbrush->m_pAmbientSamples[pAmbient->firstAmbientSample+i];
Vector pos = LeafAmbientSamplePos( leafIndex, sample );
// x axis
color32 color;
ColorRGBExp32ToColor32( sample.cube.m_Color[0], color ); // x
CubeFace( meshBuilder, pos, 4, 6, 2, 0, cubesize, color );
ColorRGBExp32ToColor32( sample.cube.m_Color[1], color ); // -x
CubeFace( meshBuilder, pos, 7, 5, 1, 3, cubesize, color );
ColorRGBExp32ToColor32( sample.cube.m_Color[2], color ); // y
CubeFace( meshBuilder, pos, 0, 1, 5, 4, cubesize, color );
ColorRGBExp32ToColor32( sample.cube.m_Color[3], color ); // -y
CubeFace( meshBuilder, pos, 3, 2, 6, 7, cubesize, color );
ColorRGBExp32ToColor32( sample.cube.m_Color[4], color ); // z
CubeFace( meshBuilder, pos, 2, 3, 1, 0, cubesize, color );
ColorRGBExp32ToColor32( sample.cube.m_Color[5], color ); // -z
CubeFace( meshBuilder, pos, 4, 5, 7, 6, cubesize, color );
meshBuilder.End();
pMesh->Draw();
}
}
}
void CSGFrustum( Frustum_t &frustum )
{
delete g_FrustumVis;
g_FrustumVis = new leafvis_t;
g_FrustumVis->color.Init(1.0f, 1.0f, 1.0f);
CUtlVector<cplane_t> planeList;
for ( int i = 0; i < 6; i++ )
{
planeList.AddToTail( *frustum.GetPlane( i ) );
}
CSGPlaneList( g_FrustumVis, planeList );
}
#endif
|