quakeforge/tools/qfvis/source/flow.c

379 lines
9.6 KiB
C
Raw Normal View History

2002-08-25 23:06:23 +00:00
/*
flow.c
PVS PHS generator 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
#include <getopt.h>
#include <stdlib.h>
#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 <string.h>
#include <stdlib.h>
#include "QF/bspfile.h"
#include "QF/cmd.h"
2002-08-25 23:06:23 +00:00
#include "QF/mathlib.h"
#include "QF/quakefs.h"
#include "QF/sys.h"
2002-08-25 23:06:23 +00:00
#include "vis.h"
#include "options.h"
static int
CheckStack (cluster_t *cluster, threaddata_t *thread)
2002-08-25 23:06:23 +00:00
{
pstack_t *portal;
for (portal = thread->pstack_head.next; portal; portal = portal->next)
if (portal->cluster == cluster) {
printf ("CheckStack: cluster recursion\n");
return 1;
}
return 0;
2002-08-25 23:06:23 +00:00
}
/*
ClipToSeparators
2002-08-25 23:06:23 +00:00
Source, pass, and target are an ordering of portals.
2002-08-26 15:05:23 +00:00
Generates seperating planes candidates by taking two points from source and
2002-08-25 23:06:23 +00:00
one point from pass, and clips target by them.
If target is totally clipped away, that portal can not be seen through.
Normal clip keeps target on the same side as pass, which is correct if the
2002-08-26 15:05:23 +00:00
order goes source, pass, target. If the order goes pass, source, target
then flipclip should be set.
2002-08-25 23:06:23 +00:00
*/
static winding_t *
ClipToSeparators (winding_t *source, winding_t *pass, winding_t *target,
2002-09-19 02:37:52 +00:00
qboolean flipclip)
2002-08-25 23:06:23 +00:00
{
2002-09-19 02:37:52 +00:00
float d;
2002-08-25 23:06:23 +00:00
int i, j, k, l;
2002-09-19 02:37:52 +00:00
int counts[3];
qboolean fliptest;
2002-08-25 23:06:23 +00:00
plane_t plane;
vec3_t v1, v2;
vec_t length;
// check all combinations
2002-08-25 23:06:23 +00:00
for (i = 0; i < source->numpoints; i++) {
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];
2002-08-25 23:06:23 +00:00
length = DotProduct (plane.normal, plane.normal);
2002-08-25 23:06:23 +00:00
// if points don't make a valid plane, skip it
2002-08-25 23:06:23 +00:00
if (length < ON_EPSILON)
continue;
length = 1 / sqrt (length);
VectorScale (plane.normal, length, plane.normal);
2002-08-25 23:06:23 +00:00
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) {
2002-08-25 23:06:23 +00:00
// 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) {
2002-08-25 23:06:23 +00:00
// 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 seperating plane
}
// flip the normal if we want the back side
if (flipclip) {
VectorNegate (plane.normal, plane.normal);
2002-08-25 23:06:23 +00:00
plane.dist = -plane.dist;
}
// clip target by the seperating plane
target = ClipWinding (target, &plane, false);
if (!target)
return NULL; // target is not visible
2002-09-22 04:17:30 +00:00
break; //XXX is this correct? big speedup
2002-08-25 23:06:23 +00:00
}
}
return target;
}
/*
RecursiveClusterFlow
2002-08-25 23:06:23 +00:00
Flood fill through the clusters
If src_portal is NULL, this is the originating cluster
2002-08-25 23:06:23 +00:00
*/
static void
RecursiveClusterFlow (int clusternum, threaddata_t *thread, pstack_t *prevstack)
2002-08-25 23:06:23 +00:00
{
int i, j;
long *test, *might, *vis;
2002-09-19 02:37:52 +00:00
qboolean more;
cluster_t *cluster;
2002-08-25 23:06:23 +00:00
pstack_t stack;
portal_t *portal;
2002-08-25 23:06:23 +00:00
plane_t backplane;
winding_t *source, *target;
2002-08-25 23:06:23 +00:00
c_chains++;
cluster = &clusters[clusternum];
if (CheckStack(cluster, thread))
return;
2002-08-25 23:06:23 +00:00
// mark the cluster as visible
if (!(thread->clustervis[clusternum >> 3] & (1 << (clusternum & 7)))) {
thread->clustervis[clusternum >> 3] |= 1 << (clusternum & 7);
2002-08-25 23:06:23 +00:00
thread->base->numcansee++;
}
prevstack->next = &stack;
stack.next = NULL;
stack.cluster = cluster;
2002-08-25 23:06:23 +00:00
stack.portal = NULL;
stack.mightsee = malloc(bitbytes);
might = (long *) stack.mightsee;
vis = (long *) thread->clustervis;
2002-08-25 23:06:23 +00:00
// check all portals for flowing into other clusters
for (i = 0; i < cluster->numportals; i++) {
portal = cluster->portals[i];
2002-08-25 23:06:23 +00:00
if (!(prevstack->mightsee[portal->cluster >> 3]
& (1 << (portal->cluster & 7))))
2002-08-25 23:06:23 +00:00
continue; // can't possibly see it
// if the portal can't see anything we haven't already seen, skip it
if (portal->status == stat_done) {
2002-08-25 23:06:23 +00:00
c_vistest++;
test = (long *) portal->visbits;
2002-08-25 23:06:23 +00:00
} else {
c_mighttest++;
test = (long *) portal->mightsee;
2002-08-25 23:06:23 +00:00
}
more = false;
for (j = 0; j < bitlongs; j++) {
might[j] = ((long *) prevstack->mightsee)[j] & test[j];
if (might[j] & ~vis[j])
more = true;
}
2002-09-23 22:54:28 +00:00
if (!more) // can't see anything new
2002-08-25 23:06:23 +00:00
continue;
2002-09-23 22:54:28 +00:00
// get plane of portal, point normal into the neighbor cluster
stack.portalplane = portal->plane;
VectorNegate (portal->plane.normal, backplane.normal);
backplane.dist = -portal->plane.dist;
2002-08-25 23:06:23 +00:00
if (_VectorCompare (prevstack->portalplane.normal, backplane.normal))
2002-08-25 23:06:23 +00:00
continue; // can't go out a coplanar face
c_portalcheck++;
stack.portal = portal;
2002-08-25 23:06:23 +00:00
stack.next = NULL;
target = ClipWinding (portal->winding,
&thread->pstack_head.portalplane, false);
2002-08-25 23:06:23 +00:00
if (!target)
continue;
2002-08-26 15:05:23 +00:00
if (!prevstack->pass) {
// the second cluster can be blocked only if coplanar
2002-08-25 23:06:23 +00:00
stack.source = prevstack->source;
stack.pass = target;
RecursiveClusterFlow (portal->cluster, thread, &stack);
2002-08-25 23:06:23 +00:00
FreeWinding (target);
continue;
}
target = ClipWinding (target, &prevstack->portalplane, false);
if (!target)
continue;
source = CopyWinding (prevstack->source);
source = ClipWinding (source, &backplane, false);
if (!source) {
FreeWinding (target);
continue;
}
c_portaltest++;
if (options.level > 0) {
// clip target to the image that would be formed by a laser
// pointing from the edges of source passing though the corners of
// pass
target = ClipToSeparators (source, prevstack->pass, target, false);
2002-08-25 23:06:23 +00:00
if (!target) {
FreeWinding (source);
continue;
}
}
if (options.level > 1) {
// now pass the laser along the edges of pass from the corners of
// source. the resulting image will have a smaller aree. The
// resulting shape will be the light image produced by a backlit
// source shining past pass. eg, if source and pass are equilateral
// triangles rotated 60 (or 180) degrees relative to each other,
// parallel and in line, target will wind up being a hexagon.
target = ClipToSeparators (prevstack->pass, source, target, true);
2002-08-25 23:06:23 +00:00
if (!target) {
FreeWinding (source);
continue;
}
}
// now do the same as for levels 1 and 2, but trimming source using
// the trimmed target
2002-08-25 23:06:23 +00:00
if (options.level > 2) {
source = ClipToSeparators (target, prevstack->pass, source, false);
2002-08-25 23:06:23 +00:00
if (!source) {
FreeWinding (target);
continue;
}
}
if (options.level > 3) {
source = ClipToSeparators (prevstack->pass, target, source, true);
2002-08-25 23:06:23 +00:00
if (!source) {
FreeWinding (target);
continue;
}
}
stack.source = source;
stack.pass = target;
c_portalpass++;
// flow through it for real
RecursiveClusterFlow (portal->cluster, thread, &stack);
2002-08-25 23:06:23 +00:00
FreeWinding (source);
FreeWinding (target);
}
free (stack.mightsee);
}
void
PortalFlow (portal_t *portal)
2002-08-25 23:06:23 +00:00
{
threaddata_t data;
LOCK;
if (portal->status != stat_selected)
2002-09-22 21:32:36 +00:00
Sys_Error ("PortalFlow: reflowed");
portal->status = stat_working;
UNLOCK;
2002-08-25 23:06:23 +00:00
portal->visbits = calloc (1, bitbytes);
2002-08-25 23:06:23 +00:00
memset (&data, 0, sizeof (data));
data.clustervis = portal->visbits;
data.base = portal;
2002-08-25 23:06:23 +00:00
data.pstack_head.portal = portal;
data.pstack_head.source = portal->winding;
data.pstack_head.portalplane = portal->plane;
data.pstack_head.mightsee = portal->mightsee;
2002-08-25 23:06:23 +00:00
RecursiveClusterFlow (portal->cluster, &data, &data.pstack_head);
2002-08-25 23:06:23 +00:00
portal->status = stat_done;
2002-08-25 23:06:23 +00:00
}