quakeforge/tools/qfvis/source/qfvis.c

826 lines
18 KiB
C
Raw Normal View History

2002-08-25 23:06:23 +00:00
/*
vis.c
PVS/PHS generation tool
Copyright (C) 1996-1997 Id Software, Inc.
2002-08-25 23:06:23 +00:00
Copyright (C) 2002 Colin Thompson
This program 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.
This program 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 this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
2002-08-25 23:06:23 +00:00
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_IO_H
# include <io.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <getopt.h>
#include <errno.h>
#include <stdlib.h>
2002-09-12 06:48:05 +00:00
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
2002-08-25 23:06:23 +00:00
#include "QF/cmd.h"
#include "QF/bspfile.h"
#include "QF/dstring.h"
2002-08-25 23:06:23 +00:00
#include "QF/mathlib.h"
#include "QF/qtypes.h"
#include "QF/quakefs.h"
#include "QF/sys.h"
2002-08-25 23:06:23 +00:00
#include "vis.h"
#include "options.h"
#define MAX_THREADS 4
2002-09-12 06:48:05 +00:00
#ifdef HAVE_PTHREAD_H
2002-08-25 23:06:23 +00:00
pthread_mutex_t *my_mutex;
#endif
bsp_t *bsp;
options_t options;
2002-08-25 23:06:23 +00:00
int c_chains;
int c_mighttest;
int c_portaltest;
int c_portalpass;
int c_portalcheck;
int c_vistest;
2002-09-23 22:54:28 +00:00
int numportals;
int portalclusters;
int numrealleafs;
int originalvismapsize;
int totalvis;
int count_sep;
int bitbytes; // (portalleafs + 63)>>3
int bitlongs;
int bitbytes_l; // (numrealleafs + 63)>>3
2002-08-25 23:06:23 +00:00
portal_t *portals;
cluster_t *clusters;
dstring_t *visdata;
byte *uncompressed; // [bitbytes * portalleafs]
int *leafcluster; // leaf to cluster mappings as read from .prt file
2002-08-25 23:06:23 +00:00
static void
2002-08-25 23:06:23 +00:00
PlaneFromWinding (winding_t *winding, plane_t *plane)
{
vec3_t v1, v2;
2002-08-25 23:06:23 +00:00
// calc plane
2002-09-19 02:37:52 +00:00
VectorSubtract (winding->points[2], winding->points[1], v1);
VectorSubtract (winding->points[0], winding->points[1], v2);
CrossProduct (v2, v1, plane->normal);
_VectorNormalize (plane->normal);
2002-09-19 02:37:52 +00:00
plane->dist = DotProduct (winding->points[0], plane->normal);
2002-08-25 23:06:23 +00:00
}
winding_t *
2002-08-25 23:06:23 +00:00
NewWinding (int points)
{
winding_t *winding;
int size;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
if (points > MAX_POINTS_ON_WINDING)
2002-09-22 21:32:36 +00:00
Sys_Error ("NewWinding: %i points", points);
2002-08-25 23:06:23 +00:00
2002-11-10 02:50:42 +00:00
size = (long) ((winding_t *) 0)->points[points];
winding = calloc (1, size);
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
return winding;
2002-08-25 23:06:23 +00:00
}
void
FreeWinding (winding_t *winding)
{
2002-09-19 02:37:52 +00:00
if (!winding->original)
2002-08-25 23:06:23 +00:00
free (winding);
}
winding_t *
2002-08-25 23:06:23 +00:00
CopyWinding (winding_t *winding)
{
int size;
winding_t *copy;
2002-09-19 02:37:52 +00:00
2002-11-10 02:50:42 +00:00
size = (long) ((winding_t *) 0)->points[winding->numpoints];
2002-09-19 02:37:52 +00:00
copy = malloc (size);
memcpy (copy, winding, size);
copy->original = false;
return copy;
2002-08-25 23:06:23 +00:00
}
/*
ClipWinding
2002-08-26 15:05:23 +00:00
Clips the winding to the plane, returning the new winding on the positive
side
2002-08-25 23:06:23 +00:00
Frees the input winding.
2002-08-26 15:05:23 +00:00
2002-08-25 23:06:23 +00:00
If keepon is true, an exactly on-plane winding will be saved, otherwise
it will be clipped away.
*/
winding_t *
2002-08-25 23:06:23 +00:00
ClipWinding (winding_t *in, plane_t *split, qboolean keepon)
{
int maxpts, i, j;
int sides[MAX_POINTS_ON_WINDING];
int counts[3];
vec_t dists[MAX_POINTS_ON_WINDING];
vec_t dot;
vec_t *p1, *p2;
vec3_t mid;
winding_t *neww;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
counts[0] = counts[1] = counts[2] = 0;
2002-08-25 23:06:23 +00:00
// determine sides for each point
2002-09-19 02:37:52 +00:00
for (i = 0; i < in->numpoints; i++) {
2002-08-25 23:06:23 +00:00
dot = DotProduct (in->points[i], split->normal);
dot -= split->dist;
dists[i] = dot;
if (dot > ON_EPSILON)
sides[i] = SIDE_FRONT;
else if (dot < -ON_EPSILON)
sides[i] = SIDE_BACK;
else {
sides[i] = SIDE_ON;
}
counts[sides[i]]++;
2002-09-19 02:37:52 +00:00
}
sides[i] = sides[0];
dists[i] = dists[0];
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
if (keepon && !counts[0] && !counts[1])
2002-08-25 23:06:23 +00:00
return in;
2002-09-19 02:37:52 +00:00
if (!counts[0]) {
2002-08-25 23:06:23 +00:00
FreeWinding (in);
return NULL;
2002-09-19 02:37:52 +00:00
}
if (!counts[1])
2002-08-25 23:06:23 +00:00
return in;
2002-09-19 02:37:52 +00:00
maxpts = in->numpoints + 4; // can't use counts[0] + 2 because
2002-08-25 23:06:23 +00:00
// of fp grouping errors
2002-09-19 02:37:52 +00:00
neww = NewWinding (maxpts);
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
for (i = 0; i < in->numpoints; i++) {
2002-08-25 23:06:23 +00:00
p1 = in->points[i];
if (sides[i] == SIDE_ON) {
VectorCopy (p1, neww->points[neww->numpoints]);
neww->numpoints++;
continue;
}
if (sides[i] == SIDE_FRONT) {
VectorCopy (p1, neww->points[neww->numpoints]);
neww->numpoints++;
}
if (sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i])
continue;
// generate a split point
p2 = in->points[(i + 1) % in->numpoints];
dot = dists[i] / (dists[i] - dists[i + 1]);
for (j = 0; j < 3; j++) {
// avoid round off error when possible
if (split->normal[j] == 1)
mid[j] = split->dist;
else if (split->normal[j] == -1)
mid[j] = -split->dist;
else
mid[j] = p1[j] + dot * (p2[j] - p1[j]);
}
VectorCopy (mid, neww->points[neww->numpoints]);
neww->numpoints++;
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
if (neww->numpoints > maxpts)
2002-09-22 21:32:36 +00:00
Sys_Error ("ClipWinding: points exceeded estimate");
2002-08-25 23:06:23 +00:00
// free the original winding
2002-09-19 02:37:52 +00:00
FreeWinding (in);
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
return neww;
2002-08-25 23:06:23 +00:00
}
/*
GetNextPortal
Returns the next portal for a thread to work on
Returns the portals from the least complex, so the later ones can reuse
the earlier information.
*/
static portal_t *
2002-08-25 23:06:23 +00:00
GetNextPortal (void)
{
int j;
portal_t *p, *tp;
int min;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
LOCK;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
min = 99999;
p = NULL;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
for (j = 0, tp = portals; j < numportals * 2; j++, tp++) {
2002-08-25 23:06:23 +00:00
if (tp->nummightsee < min && tp->status == stat_none) {
min = tp->nummightsee;
p = tp;
}
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
2003-09-12 23:13:11 +00:00
if (p) {
static int count;
printf ("%5d / %5d %d\r", count++, numportals * 2, p->nummightsee);
fflush (stdout);
p->status = stat_selected;
2003-09-12 23:13:11 +00:00
}
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
UNLOCK;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
return p;
2002-08-25 23:06:23 +00:00
}
static void *
2002-09-19 02:37:52 +00:00
LeafThread (void *thread)
2002-08-25 23:06:23 +00:00
{
portal_t *portal;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
do {
2002-08-25 23:06:23 +00:00
portal = GetNextPortal ();
if (!portal)
break;
PortalFlow (portal);
if (options.verbosity > 0)
2002-08-25 23:06:23 +00:00
printf ("portal:%4i mightsee:%4i cansee:%4i\n",
(int) (portal - portals),
portal->nummightsee,
portal->numcansee);
2002-09-19 02:37:52 +00:00
} while (1);
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
return NULL;
2002-08-25 23:06:23 +00:00
}
static int
2002-08-25 23:06:23 +00:00
CompressRow (byte *vis, byte *dest)
{
int j;
int rep;
int visrow;
byte *dest_p;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
dest_p = dest;
visrow = (numrealleafs + 7) >> 3;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
for (j = 0; j < visrow; j++) {
2002-08-25 23:06:23 +00:00
*dest_p++ = vis[j];
if (vis[j])
continue;
rep = 1;
for (j++; j < visrow; j++)
if (vis[j] || rep == 255)
break;
else
rep++;
*dest_p++ = rep;
j--;
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
return dest_p - dest;
2002-08-25 23:06:23 +00:00
}
static void
ClusterFlowExpand (byte *src, byte *dest)
{
int i, j;
for (j = 1, i = 0; i < numrealleafs; i++) {
if (src[leafcluster[i] >> 3] & (1 << (leafcluster[i] & 7)))
*dest |= j;
j <<= 1;
if (j == 256) {
j = 1;
dest++;
}
}
}
2002-08-25 23:06:23 +00:00
/*
ClusterFlow
2002-08-25 23:06:23 +00:00
Builds the entire visibility list for a cluster
2002-08-25 23:06:23 +00:00
*/
void
ClusterFlow (int clusternum)
2002-08-25 23:06:23 +00:00
{
byte *outbuffer;
byte compressed[MAX_MAP_LEAFS / 8];
int numvis, i, j;
cluster_t *cluster;
portal_t *portal;
2002-08-25 23:06:23 +00:00
outbuffer = uncompressed + clusternum * bitbytes_l;
cluster = &clusters[clusternum];
2002-08-25 23:06:23 +00:00
// flow through all portals, collecting visible bits
memset (compressed, 0, sizeof (compressed));
for (i = 0; i < cluster->numportals; i++) {
portal = cluster->portals[i];
2002-08-25 23:06:23 +00:00
if (portal->status != stat_done)
2002-09-22 21:32:36 +00:00
Sys_Error ("portal not done");
2002-08-25 23:06:23 +00:00
for (j = 0; j < bitbytes; j++)
compressed[j] |= portal->visbits[j];
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
if (compressed[clusternum >> 3] & (1 << (clusternum & 7)))
Sys_Error ("Cluster portals saw into cluster");
2002-08-25 23:06:23 +00:00
compressed[clusternum >> 3] |= (1 << (clusternum & 7));
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
numvis = 0;
for (i = 0; i < portalclusters; i++)
if (compressed[i >> 3] & (1 << (i & 3)))
2002-08-25 23:06:23 +00:00
numvis++;
// expand to cluster->leaf PVS
ClusterFlowExpand (compressed, outbuffer);
2002-08-25 23:06:23 +00:00
// compress the bit string
2002-09-19 02:37:52 +00:00
if (options.verbosity > 0)
printf ("cluster %4i : %4i visible\n", clusternum, numvis);
2002-09-19 02:37:52 +00:00
totalvis += numvis;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
i = CompressRow (outbuffer, compressed);
2003-03-17 00:52:16 +00:00
cluster->visofs = visdata->size;
dstring_append (visdata, compressed, i);
2002-08-25 23:06:23 +00:00
}
static void
2002-08-25 23:06:23 +00:00
CalcPortalVis (void)
{
2002-11-10 02:50:42 +00:00
long i;
2002-08-25 23:06:23 +00:00
// fastvis just uses mightsee for a very loose bound
2002-09-19 02:37:52 +00:00
if (options.minimal) {
2002-08-25 23:06:23 +00:00
for (i = 0; i < numportals * 2; i++) {
portals[i].visbits = portals[i].mightsee;
portals[i].status = stat_done;
}
return;
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
2002-09-12 06:48:05 +00:00
#ifdef HAVE_PTHREAD_H
2002-09-19 02:37:52 +00:00
{
pthread_t work_threads[MAX_THREADS];
2002-09-19 02:37:52 +00:00
void *status;
pthread_attr_t attrib;
2002-08-25 23:06:23 +00:00
if (options.threads > 1) {
my_mutex = malloc (sizeof (*my_mutex));
if (pthread_mutex_init (my_mutex, 0) == -1)
Sys_Error ("pthread_mutex_init failed");
if (pthread_attr_init (&attrib) == -1)
Sys_Error ("pthread_attr_create failed");
if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
Sys_Error ("pthread_attr_setstacksize failed");
for (i = 0; i < options.threads; i++) {
if (pthread_create (&work_threads[i], &attrib, LeafThread,
(void *) i) == -1)
Sys_Error ("pthread_create failed");
}
for (i = 0; i < options.threads; i++) {
if (pthread_join (work_threads[i], &status) == -1)
Sys_Error ("pthread_join failed");
}
if (pthread_mutex_destroy (my_mutex) == -1)
Sys_Error ("pthread_mutex_destroy failed");
} else {
LeafThread (0);
}
}
2002-08-25 23:06:23 +00:00
#else
2002-09-12 06:48:05 +00:00
LeafThread (0);
2002-08-25 23:06:23 +00:00
#endif
2002-09-19 02:37:52 +00:00
if (options.verbosity > 0) {
2002-08-26 15:05:23 +00:00
printf ("portalcheck: %i portaltest: %i portalpass: %i\n",
c_portalcheck, c_portaltest, c_portalpass);
2002-08-25 23:06:23 +00:00
printf ("c_vistest: %i c_mighttest: %i\n", c_vistest, c_mighttest);
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
}
static void
2002-08-25 23:06:23 +00:00
CalcVis (void)
{
int i;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
BasePortalVis ();
CalcPortalVis ();
2002-08-25 23:06:23 +00:00
// assemble the leaf vis lists by oring and compressing the portal lists
for (i = 0; i < portalclusters; i++)
ClusterFlow (i);
2002-08-25 23:06:23 +00:00
for (i = 0; i < numrealleafs; i++) {
2003-03-17 00:52:16 +00:00
bsp->leafs[i + 1].visofs = clusters[leafcluster[i]].visofs;
}
2002-08-25 23:06:23 +00:00
if (options.verbosity >= 0)
printf ("average clusters visible: %i\n", totalvis / portalclusters);
2002-08-25 23:06:23 +00:00
}
#if 0
static qboolean
2002-08-25 23:06:23 +00:00
PlaneCompare (plane_t *p1, plane_t *p2)
{
int i;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
if (fabs (p1->dist - p2->dist) > 0.01)
2002-08-25 23:06:23 +00:00
return false;
2002-09-19 02:37:52 +00:00
for (i = 0; i < 3; i++)
2002-08-25 23:06:23 +00:00
if (fabs (p1->normal[i] - p2->normal[i]) > 0.001)
return false;
2002-09-19 02:37:52 +00:00
return true;
2002-08-25 23:06:23 +00:00
}
static sep_t *
2002-09-21 21:42:30 +00:00
FindPassages (winding_t *source, winding_t *pass)
2002-08-25 23:06:23 +00:00
{
double length;
float d;
int counts[3];
int i, j, k, l;
plane_t plane;
qboolean fliptest;
sep_t *sep, *list;
vec3_t v1, v2;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
list = NULL;
2002-08-25 23:06:23 +00:00
// check all combinations
2002-09-19 02:37:52 +00:00
for (i = 0; i < source->numpoints; i++) {
2002-08-25 23:06:23 +00:00
l = (i + 1) % source->numpoints;
VectorSubtract (source->points[l], source->points[i], v1);
2002-09-19 02:37:52 +00:00
// find a vertex of pass that makes a plane that puts all of the
2002-08-25 23:06:23 +00:00
// vertexes of pass on the front side and all of the vertexes of
// source on the back side
for (j = 0; j < pass->numpoints; j++) {
VectorSubtract (pass->points[j], source->points[i], v2);
plane.normal[0] = v1[1] * v2[2] - v1[2] * v2[1];
plane.normal[1] = v1[2] * v2[0] - v1[0] * v2[2];
plane.normal[2] = v1[0] * v2[1] - v1[1] * v2[0];
// if points don't make a valid plane, skip it
length = plane.normal[0] * plane.normal[0] +
plane.normal[1] * plane.normal[1] +
plane.normal[2] * plane.normal[2];
if (length < ON_EPSILON)
continue;
length = 1 / sqrt(length);
plane.normal[0] *= length;
plane.normal[1] *= length;
plane.normal[2] *= length;
plane.dist = DotProduct (pass->points[j], plane.normal);
// find out which side of the generated seperating plane has the
// source portal
fliptest = false;
for (k = 0; k < source->numpoints; k++) {
if (k == i || k == l)
continue;
d = DotProduct (source->points[k], plane.normal) - plane.dist;
if (d < -ON_EPSILON) {
// source is on the negative side, so we want all
// pass and target on the positive side
fliptest = false;
break;
} else if (d > ON_EPSILON) {
// source is on the positive side, so we want all
// pass and target on the negative side
fliptest = true;
break;
}
}
if (k == source->numpoints)
continue; // planar with source portal
// flip the normal if the source portal is backwards
if (fliptest) {
VectorNegate (plane.normal, plane.normal);
2002-08-25 23:06:23 +00:00
plane.dist = -plane.dist;
}
// if all of the pass portal points are now on the positive side,
// this is the seperating plane
counts[0] = counts[1] = counts[2] = 0;
for (k = 0; k < pass->numpoints; k++) {
if (k == j)
continue;
d = DotProduct (pass->points[k], plane.normal) - plane.dist;
if (d < -ON_EPSILON)
break;
else if (d > ON_EPSILON)
counts[0]++;
else
counts[2]++;
}
if (k != pass->numpoints)
continue; // points on negative side, not a seperating plane
if (!counts[0])
continue; // planar with pass portal
// save this out
count_sep++;
sep = malloc (sizeof (*sep));
sep->next = list;
list = sep;
sep->plane = plane;
}
2002-09-19 02:37:52 +00:00
}
return list;
2002-08-25 23:06:23 +00:00
}
static void
2002-08-25 23:06:23 +00:00
CalcPassages (void)
{
int count, count2, i, j, k;
leaf_t *leaf;
portal_t *p1, *p2;
sep_t *sep;
passage_t *passages;
2002-08-25 23:06:23 +00:00
if (options.verbosity >= 0)
printf ("building passages...\n");
2002-09-19 02:37:52 +00:00
count = count2 = 0;
for (i = 0; i < portalleafs; i++) {
2002-08-25 23:06:23 +00:00
leaf = &leafs[i];
for (j = 0; j < leaf->numportals; j++) {
p1 = leaf->portals[j];
for (k = 0; k < leaf->numportals; k++) {
if (k == j)
continue;
count++;
p2 = leaf->portals[k];
// definately can't see into a coplanar portal
if (PlaneCompare (&p1->plane, &p2->plane))
continue;
count2++;
2002-09-21 21:42:30 +00:00
sep = FindPassages (p1->winding, p2->winding);
2002-08-25 23:06:23 +00:00
if (!sep) {
count_sep++;
sep = malloc (sizeof (*sep));
sep->next = NULL;
sep->plane = p1->plane;
}
passages = malloc (sizeof (*passages));
passages->planes = sep;
passages->from = p1->leaf;
passages->to = p2->leaf;
passages->next = leaf->passages;
leaf->passages = passages;
}
}
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
if (options.verbosity >= 0) {
printf ("numpassages: %i (%i)\n", count2, count);
printf ("total passages: %i\n", count_sep);
}
}
#endif
static void
2002-08-25 23:06:23 +00:00
LoadPortals (char *name)
{
char magic[80];
FILE *f;
int clusternums[2];
int numpoints, i, j;
cluster_t *cluster;
plane_t plane;
portal_t *portal;
winding_t *winding;
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
if (!strcmp (name, "-"))
2002-08-25 23:06:23 +00:00
f = stdin;
2002-09-19 02:37:52 +00:00
else {
2002-08-25 23:06:23 +00:00
f = fopen (name, "r");
if (!f) {
printf ("LoadPortals: couldn't read %s\n", name);
printf ("No vising performed.\n");
exit (1);
}
2002-09-19 02:37:52 +00:00
}
2002-08-25 23:06:23 +00:00
if (fscanf (f, "%79s\n%i\n%i\n%i\n", magic, &portalclusters, &numportals,
&numrealleafs) != 4)
2002-09-22 21:32:36 +00:00
Sys_Error ("LoadPortals: failed to read header");
2002-09-19 02:37:52 +00:00
if (strcmp (magic, PORTALFILE))
2002-09-22 21:32:36 +00:00
Sys_Error ("LoadPortals: not a portal file");
2002-08-25 23:06:23 +00:00
if (options.verbosity >= 0) {
printf ("%4i portalclusters\n", portalclusters);
2002-08-25 23:06:23 +00:00
printf ("%4i numportals\n", numportals);
printf ("%4i numrealleafs\n", numrealleafs);
2002-08-25 23:06:23 +00:00
}
bitbytes = ((portalclusters + 63) & ~63) >> 3;
2002-09-19 02:37:52 +00:00
bitlongs = bitbytes / sizeof (long);
2002-08-25 23:06:23 +00:00
bitbytes_l = ((numrealleafs + 63) & ~63) >> 3;
// each file portal is split into two memory portals, one for each
// direction
portals = calloc (2 * numportals, sizeof (portal_t));
2002-08-25 23:06:23 +00:00
clusters = calloc (portalclusters, sizeof (cluster_t));
2002-08-25 23:06:23 +00:00
originalvismapsize = numrealleafs * ((numrealleafs + 7) / 8);
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
for (i = 0, portal = portals; i < numportals; i++) {
if (fscanf (f, "%i %i %i ", &numpoints, &clusternums[0],
&clusternums[1]) != 3)
2002-09-22 21:32:36 +00:00
Sys_Error ("LoadPortals: reading portal %i", i);
2002-08-25 23:06:23 +00:00
if (numpoints > MAX_POINTS_ON_WINDING)
2002-09-22 21:32:36 +00:00
Sys_Error ("LoadPortals: portal %i has too many points", i);
2003-04-17 00:01:48 +00:00
if ((unsigned) clusternums[0] > (unsigned) portalclusters
|| (unsigned) clusternums[1] > (unsigned) portalclusters)
2002-09-22 21:32:36 +00:00
Sys_Error ("LoadPortals: reading portal %i", i);
2002-08-25 23:06:23 +00:00
winding = portal->winding = NewWinding (numpoints);
winding->original = true;
winding->numpoints = numpoints;
for (j = 0; j < numpoints; j++) {
double v[3];
int k;
2002-08-25 23:06:23 +00:00
// scanf into double, then assign to vec_t
if (fscanf (f, "(%lf %lf %lf ) ", &v[0], &v[1], &v[2]) != 3)
2002-09-22 21:32:36 +00:00
Sys_Error ("LoadPortals: reading portal %i", i);
2002-08-25 23:06:23 +00:00
for (k = 0; k < 3; k++)
winding->points[j][k] = v[k];
}
fscanf (f, "\n");
// calc plane
PlaneFromWinding (winding, &plane);
// create forward portal
cluster = &clusters[clusternums[0]];
if (cluster->numportals == MAX_PORTALS_ON_CLUSTER)
Sys_Error ("Cluster with too many portals");
cluster->portals[cluster->numportals] = portal;
cluster->numportals++;
2002-08-25 23:06:23 +00:00
portal->winding = winding;
VectorNegate (plane.normal, portal->plane.normal);
2002-08-25 23:06:23 +00:00
portal->plane.dist = -plane.dist;
portal->cluster = clusternums[1];
2002-08-25 23:06:23 +00:00
portal++;
// create backwards portal
cluster = &clusters[clusternums[1]];
if (cluster->numportals == MAX_PORTALS_ON_CLUSTER)
Sys_Error ("Cluster with too many portals");
cluster->portals[cluster->numportals] = portal;
cluster->numportals++;
2002-08-25 23:06:23 +00:00
portal->winding = winding;
portal->plane = plane;
portal->cluster = clusternums[0];
2002-08-25 23:06:23 +00:00
portal++;
2002-09-19 02:37:52 +00:00
}
leafcluster = calloc (numrealleafs, sizeof (int));
for (i = 0; i < numrealleafs; i++)
if (fscanf (f, "%i\n", &leafcluster[i]) != 1)
Sys_Error ("LoadPortals: parse error in leaf->cluster mappings");
2002-09-19 02:37:52 +00:00
fclose (f);
2002-08-25 23:06:23 +00:00
}
int
main (int argc, char **argv)
{
double start, stop;
2002-09-12 06:48:05 +00:00
dstring_t *portalfile = dstring_new ();
QFile *f;
2002-08-25 23:06:23 +00:00
start = Sys_DoubleTime ();
2002-09-19 02:37:52 +00:00
2002-08-25 23:06:23 +00:00
this_program = argv[0];
DecodeArgs (argc, argv);
if (!options.bspfile) {
usage (1);
Sys_Error ("%s: no bsp file specified.", this_program);
2002-08-25 23:06:23 +00:00
}
QFS_StripExtension (options.bspfile, options.bspfile);
QFS_DefaultExtension (options.bspfile, ".bsp");
f = Qopen (options.bspfile, "rb");
if (!f)
Sys_Error ("couldn't open %s for reading.", options.bspfile);
bsp = LoadBSPFile (f, Qfilesize (f));
Qclose (f);
2002-08-25 23:06:23 +00:00
visdata = dstring_new ();
2002-09-12 06:48:05 +00:00
portalfile->size = strlen (options.bspfile) + 1;
dstring_adjust (portalfile);
QFS_StripExtension (options.bspfile, portalfile->str);
2002-09-12 06:48:05 +00:00
dstring_appendstr (portalfile, ".prt");
LoadPortals (portalfile->str);
2002-08-25 23:06:23 +00:00
uncompressed = calloc (bitbytes_l, portalclusters);
2002-08-25 23:06:23 +00:00
CalcVis ();
if (options.verbosity >= 0)
printf ("c_chains: %i%s\n", c_chains,
options.threads > 1 ? " (not reliable)" :"");
2002-08-25 23:06:23 +00:00
BSP_AddVisibility (bsp, visdata->str, visdata->size);
2002-08-25 23:06:23 +00:00
if (options.verbosity >= 0)
printf ("visdatasize:%i compressed from %i\n", bsp->visdatasize,
2002-08-26 15:05:23 +00:00
originalvismapsize);
2002-08-25 23:06:23 +00:00
2002-09-19 02:37:52 +00:00
CalcAmbientSounds ();
2002-08-25 23:06:23 +00:00
f = Qopen (options.bspfile, "wb");
if (!f)
Sys_Error ("couldn't open %s for writing.", options.bspfile);
WriteBSPFile (bsp, f);
Qclose (f);
2002-08-25 23:06:23 +00:00
stop = Sys_DoubleTime ();
if (options.verbosity >= 0)
printf ("%5.1f seconds elapsed\n", stop - start);
2002-09-19 02:37:52 +00:00
return 0;
2002-08-25 23:06:23 +00:00
}