vvis.cpp
来自「hl2 source code. Do not use it illegal.」· C++ 代码 · 共 1,070 行 · 第 1/2 页
CPP
1,070 行
// vis.c
#include <windows.h>
#include "vis.h"
#include "threads.h"
#include "stdlib.h"
#include "pacifier.h"
#include "vmpi.h"
#include "mpivis.h"
#include "vstdlib/strtools.h"
#include "ivvisdll.h"
#include "collisionutils.h"
#include "vstdlib/icommandline.h"
#include "vmpi_tools_shared.h"
int g_numportals;
int portalclusters;
char inbase[32];
char outbase[32];
portal_t *portals;
leaf_t *leafs;
int c_portaltest, c_portalpass, c_portalcheck;
byte *uncompressedvis;
byte *vismap, *vismap_p, *vismap_end; // past visfile
int originalvismapsize;
int leafbytes; // (portalclusters+63)>>3
int leaflongs;
int portalbytes, portallongs;
bool fastvis;
bool nosort;
int totalvis;
portal_t *sorted_portals[MAX_MAP_PORTALS*2];
bool g_bUseRadius = false;
double g_VisRadius = 4096.0f * 4096.0f;
bool g_bLowPriority = false;
int g_nDXLevel = 90;
//=============================================================================
void PlaneFromWinding (winding_t *w, plane_t *plane)
{
Vector v1, v2;
// calc plane
VectorSubtract (w->points[2], w->points[1], v1);
VectorSubtract (w->points[0], w->points[1], v2);
CrossProduct (v2, v1, plane->normal);
VectorNormalize (plane->normal);
plane->dist = DotProduct (w->points[0], plane->normal);
}
/*
==================
NewWinding
==================
*/
winding_t *NewWinding (int points)
{
winding_t *w;
int size;
if (points > MAX_POINTS_ON_WINDING)
Error ("NewWinding: %i points", points);
size = (int)(&((winding_t *)0)->points[points]);
w = (winding_t*)malloc (size);
memset (w, 0, size);
return w;
}
void pw(winding_t *w)
{
int i;
for (i=0 ; i<w->numpoints ; i++)
Msg ("(%5.1f, %5.1f, %5.1f)\n",w->points[i][0], w->points[i][1],w->points[i][2]);
}
void prl(leaf_t *l)
{
int i;
portal_t *p;
plane_t pl;
for (i=0 ; i<l->numportals ; i++)
{
p = l->portals[i];
pl = p->plane;
Msg ("portal %4i to leaf %4i : %7.1f : (%4.1f, %4.1f, %4.1f)\n",(int)(p-portals),p->leaf,pl.dist, pl.normal[0], pl.normal[1], pl.normal[2]);
}
}
//=============================================================================
/*
=============
SortPortals
Sorts the portals from the least complex, so the later ones can reuse
the earlier information.
=============
*/
int PComp (const void *a, const void *b)
{
if ( (*(portal_t **)a)->nummightsee == (*(portal_t **)b)->nummightsee)
return 0;
if ( (*(portal_t **)a)->nummightsee < (*(portal_t **)b)->nummightsee)
return -1;
return 1;
}
void SortPortals (void)
{
int i;
for (i=0 ; i<g_numportals*2 ; i++)
sorted_portals[i] = &portals[i];
if (nosort)
return;
qsort (sorted_portals, g_numportals*2, sizeof(sorted_portals[0]), PComp);
}
/*
==============
LeafVectorFromPortalVector
==============
*/
int LeafVectorFromPortalVector (byte *portalbits, byte *leafbits)
{
int i;
portal_t *p;
int c_leafs;
memset (leafbits, 0, leafbytes);
for (i=0 ; i<g_numportals*2 ; i++)
{
if ( CheckBit( portalbits, i ) )
{
p = portals+i;
SetBit( leafbits, p->leaf );
}
}
c_leafs = CountBits (leafbits, portalclusters);
return c_leafs;
}
/*
===============
ClusterMerge
Merges the portal visibility for a leaf
===============
*/
void ClusterMerge (int clusternum)
{
leaf_t *leaf;
// byte portalvector[MAX_PORTALS/8];
byte portalvector[MAX_PORTALS/4]; // 4 because portal bytes is * 2
byte uncompressed[MAX_MAP_LEAFS/8];
int i, j;
int numvis;
portal_t *p;
int pnum;
// OR together all the portalvis bits
memset (portalvector, 0, portalbytes);
leaf = &leafs[clusternum];
for (i=0 ; i<leaf->numportals ; i++)
{
p = leaf->portals[i];
if (p->status != stat_done)
Error ("portal not done %d %d %d\n", i, p, portals);
for (j=0 ; j<portallongs ; j++)
((long *)portalvector)[j] |= ((long *)p->portalvis)[j];
pnum = p - portals;
SetBit( portalvector, pnum );
}
// convert portal bits to leaf bits
numvis = LeafVectorFromPortalVector (portalvector, uncompressed);
if ( CheckBit( uncompressed, clusternum ) )
Warning("WARNING: Cluster portals saw into cluster\n");
SetBit( uncompressed, clusternum );
numvis++; // count the leaf itself
// save uncompressed for PHS calculation
memcpy (uncompressedvis + clusternum*leafbytes, uncompressed, leafbytes);
qprintf ("cluster %4i : %4i visible\n", clusternum, numvis);
totalvis += numvis;
}
static int CompressAndCrosscheckClusterVis( int clusternum )
{
int optimized = 0;
byte compressed[MAX_MAP_LEAFS/8];
//
// compress the bit string
//
byte *uncompressed = uncompressedvis + clusternum*leafbytes;
for ( int i = 0; i < portalclusters; i++ )
{
if ( i == clusternum )
continue;
if ( CheckBit( uncompressed, i ) )
{
byte *other = uncompressedvis + i*leafbytes;
if ( !CheckBit( other, clusternum ) )
{
ClearBit( uncompressed, i );
optimized++;
}
}
}
int numbytes = CompressVis( uncompressed, compressed );
byte *dest = vismap_p;
vismap_p += numbytes;
if (vismap_p > vismap_end)
Error ("Vismap expansion overflow");
dvis->bitofs[clusternum][DVIS_PVS] = dest-vismap;
memcpy( dest, compressed, numbytes );
return optimized;
}
/*
==================
CalcPortalVis
==================
*/
void CalcPortalVis (void)
{
int i;
// fastvis just uses mightsee for a very loose bound
if( fastvis )
{
for (i=0 ; i<g_numportals*2 ; i++)
{
portals[i].portalvis = portals[i].portalflood;
portals[i].status = stat_done;
}
return;
}
if (g_bUseMPI)
{
RunMPIPortalFlow();
}
else
{
RunThreadsOnIndividual (g_numportals*2, true, PortalFlow);
}
}
/*
==================
CalcVis
==================
*/
void CalcVis (void)
{
int i;
if (g_bUseMPI)
{
RunMPIBasePortalVis();
}
else
{
RunThreadsOnIndividual (g_numportals*2, true, BasePortalVis);
}
SortPortals ();
CalcPortalVis ();
//
// assemble the leaf vis lists by oring the portal lists
//
for ( i = 0; i < portalclusters; i++ )
{
ClusterMerge( i );
}
int count = 0;
// Now crosscheck each leaf's vis and compress
for ( i = 0; i < portalclusters; i++ )
{
count += CompressAndCrosscheckClusterVis( i );
}
Msg ("Optimized: %d visible clusters (%.2f%%)\n", count, totalvis, count*100/totalvis);
Msg ("Total clusters visible: %i\n", totalvis);
Msg ("Average clusters visible: %i\n", totalvis / portalclusters);
}
void SetPortalSphere (portal_t *p)
{
int i;
Vector total, dist;
winding_t *w;
float r, bestr;
w = p->winding;
VectorCopy (vec3_origin, total);
for (i=0 ; i<w->numpoints ; i++)
{
VectorAdd (total, w->points[i], total);
}
for (i=0 ; i<3 ; i++)
total[i] /= w->numpoints;
bestr = 0;
for (i=0 ; i<w->numpoints ; i++)
{
VectorSubtract (w->points[i], total, dist);
r = VectorLength (dist);
if (r > bestr)
bestr = r;
}
VectorCopy (total, p->origin);
p->radius = bestr;
}
/*
============
LoadPortals
============
*/
void LoadPortals (char *name)
{
int i, j;
portal_t *p;
leaf_t *l;
char magic[80];
int numpoints;
winding_t *w;
int leafnums[2];
plane_t plane;
FILE *f;
// Open the portal file.
if ( g_bUseMPI )
{
// If we're using MPI, copy off the file to a temporary first. This will download the file
// from the MPI master, then we get to use nice functions like fscanf on it.
char tempPath[MAX_PATH], tempFile[MAX_PATH];
if ( GetTempPath( sizeof( tempPath ), tempPath ) == 0 )
{
Error( "LoadPortals: GetTempPath failed.\n" );
}
if ( GetTempFileName( tempPath, "vvis_portal_", 0, tempFile ) == 0 )
{
Error( "LoadPortals: GetTempFileName failed.\n" );
}
// Read all the data from the network file into memory.
FileHandle_t hFile = g_pFileSystem->Open(name, "r");
if ( hFile == FILESYSTEM_INVALID_HANDLE )
Error( "LoadPortals( %s ): couldn't get file from master.\n", name );
CUtlVector<char> data;
data.SetSize( g_pFileSystem->Size( hFile ) );
g_pFileSystem->Read( data.Base(), data.Count(), hFile );
g_pFileSystem->Close( hFile );
// Dump it into a temp file.
f = fopen( tempFile, "wt" );
fwrite( data.Base(), 1, data.Count(), f );
fclose( f );
// Open the temp file up.
f = fopen( tempFile, "r" );
}
else
{
f = fopen( name, "r" );
}
if ( !f )
Error ("LoadPortals: couldn't read %s\n",name);
if (fscanf (f,"%79s\n%i\n%i\n",magic, &portalclusters, &g_numportals) != 3)
Error ("LoadPortals: failed to read header");
if (strcmp(magic,PORTALFILE))
Error ("LoadPortals: not a portal file");
Msg ("%4i portalclusters\n", portalclusters);
Msg ("%4i numportals\n", g_numportals);
if (g_numportals * 2 >= MAX_PORTALS)
{
Error("The map overflows the max portal count (%d of max %d)!\n", g_numportals, MAX_PORTALS / 2 );
}
// these counts should take advantage of 64 bit systems automatically
leafbytes = ((portalclusters+63)&~63)>>3;
leaflongs = leafbytes/sizeof(long);
portalbytes = ((g_numportals*2+63)&~63)>>3;
portallongs = portalbytes/sizeof(long);
// each file portal is split into two memory portals
portals = (portal_t*)malloc(2*g_numportals*sizeof(portal_t));
memset (portals, 0, 2*g_numportals*sizeof(portal_t));
leafs = (leaf_t*)malloc(portalclusters*sizeof(leaf_t));
memset (leafs, 0, portalclusters*sizeof(leaf_t));
originalvismapsize = portalclusters*leafbytes;
uncompressedvis = (byte*)malloc(originalvismapsize);
vismap = vismap_p = dvisdata;
dvis->numclusters = portalclusters;
vismap_p = (byte *)&dvis->bitofs[portalclusters];
vismap_end = vismap + MAX_MAP_VISIBILITY;
for (i=0, p=portals ; i<g_numportals ; i++)
{
if (fscanf (f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1])
!= 3)
Error ("LoadPortals: reading portal %i", i);
if (numpoints > MAX_POINTS_ON_WINDING)
Error ("LoadPortals: portal %i has too many points", i);
if ( (unsigned)leafnums[0] > portalclusters
|| (unsigned)leafnums[1] > portalclusters)
Error ("LoadPortals: reading portal %i", i);
w = p->winding = NewWinding (numpoints);
w->original = true;
w->numpoints = numpoints;
for (j=0 ; j<numpoints ; j++)
{
double v[3];
int k;
// scanf into double, then assign to vec_t
// so we don't care what size vec_t is
if (fscanf (f, "(%lf %lf %lf ) "
, &v[0], &v[1], &v[2]) != 3)
Error ("LoadPortals: reading portal %i", i);
for (k=0 ; k<3 ; k++)
w->points[j][k] = v[k];
}
fscanf (f, "\n");
// calc plane
PlaneFromWinding (w, &plane);
// create forward portal
l = &leafs[leafnums[0]];
if (l->numportals == MAX_PORTALS_ON_LEAF)
Error ("Leaf with too many portals");
l->portals[l->numportals] = p;
l->numportals++;
p->winding = w;
VectorSubtract (vec3_origin, plane.normal, p->plane.normal);
p->plane.dist = -plane.dist;
p->leaf = leafnums[1];
SetPortalSphere (p);
p++;
// create backwards portal
l = &leafs[leafnums[1]];
if (l->numportals == MAX_PORTALS_ON_LEAF)
Error ("Leaf with too many portals");
l->portals[l->numportals] = p;
l->numportals++;
p->winding = NewWinding(w->numpoints);
p->winding->numpoints = w->numpoints;
for (j=0 ; j<w->numpoints ; j++)
{
VectorCopy (w->points[w->numpoints-1-j], p->winding->points[j]);
}
p->plane = plane;
p->leaf = leafnums[0];
SetPortalSphere (p);
p++;
}
fclose (f);
}
/*
================
CalcPAS
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?