quakeforge/tools/qfbsp/source/region.c

276 lines
5.6 KiB
C
Raw Normal View History

2002-09-19 18:51:19 +00:00
/*
Copyright (C) 1996-1997 Id Software, Inc.
2002-09-19 18:51:19 +00:00
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.
2002-09-19 18:51:19 +00:00
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.
2002-09-19 18:51:19 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2002-09-19 18:51:19 +00:00
See file, 'COPYING', for details.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include "QF/sys.h"
#include "compat.h"
#include "bsp5.h"
#include "region.h"
#include "surfaces.h"
/** \addtogroup qfbsp_region
*/
//@{
/*
input
-----
vertexes
edges
faces
output
------
smaller set of vertexes
smaller set of edges
regions
? triangulated regions
face to region mapping numbers
*/
#define MAX_EDGES_IN_REGION 32
2002-09-19 17:14:23 +00:00
int firstedge;
vec3_t region_mins, region_maxs;
2008-07-19 05:40:57 +00:00
/*
static void
2002-09-19 17:14:23 +00:00
AddPointToRegion (vec3_t p)
{
2002-09-19 17:14:23 +00:00
int i;
for (i = 0; i < 3; i++) {
if (p[i] < region_mins[i])
region_mins[i] = p[i];
if (p[i] > region_maxs[i])
region_maxs[i] = p[i];
2002-09-19 17:14:23 +00:00
}
}
static void
2002-09-19 18:51:19 +00:00
AddFaceToRegionSize (face_t *f)
{
2002-09-19 17:14:23 +00:00
int i;
2003-09-08 03:00:53 +00:00
for (i = 0; i < f->points->numpoints; i++)
AddPointToRegion (f->points->points[i]);
}
static qboolean
2002-09-19 18:51:19 +00:00
CanJoinFaces (face_t *f, face_t *f2)
{
2002-09-19 17:14:23 +00:00
int i;
2002-09-19 18:51:19 +00:00
vec3_t oldmins, oldmaxs;
if (f2->planenum != f->planenum
2002-09-19 17:14:23 +00:00
|| f2->planeside != f->planeside || f2->texturenum != f->texturenum)
return false;
if (f2->outputnumber != -1)
return false;
2002-09-19 17:14:23 +00:00
if (f2->contents[0] != f->contents[0]) { // does this ever happen?
2002-09-19 18:51:19 +00:00
// they shouldn't share.
printf ("CanJoinFaces: edge with different contents");
return false;
}
2002-09-23 16:27:17 +00:00
// check size constraints
2002-09-19 17:14:23 +00:00
if (!(bsp->texinfo[f->texturenum].flags & TEX_SPECIAL)) {
VectorCopy (region_mins, oldmins);
VectorCopy (region_maxs, oldmaxs);
AddFaceToRegionSize (f2);
2002-09-19 17:14:23 +00:00
for (i = 0; i < 3; i++) {
if (region_maxs[i] - region_mins[i] > options.subdivide_size) {
VectorCopy (oldmins, region_mins);
VectorCopy (oldmaxs, region_maxs);
return false;
}
}
2002-09-19 17:14:23 +00:00
} else {
2003-09-08 03:00:53 +00:00
if (bsp->numsurfedges - firstedge + f2->points->numpoints
2002-09-19 18:51:19 +00:00
> MAX_EDGES_IN_REGION)
2002-09-19 17:14:23 +00:00
return false; // a huge water or sky polygon
}
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// check edge count constraints
return true;
}
static void
2002-09-19 18:51:19 +00:00
RecursiveGrowRegion (dface_t *r, face_t *f)
{
2002-09-19 17:14:23 +00:00
face_t *f2;
2002-09-19 18:51:19 +00:00
int e, i;
2002-09-19 17:14:23 +00:00
if (f->outputnumber == bsp->numfaces)
return;
if (f->outputnumber != -1)
Sys_Error ("RecursiveGrowRegion: region collision");
f->outputnumber = bsp->numfaces;
2002-09-19 17:14:23 +00:00
2010-08-30 06:12:20 +00:00
// add edges
2003-09-08 03:00:53 +00:00
for (i = 0; i < f->points->numpoints; i++) {
e = f->edges[i];
if (!edgefaces[abs (e)].f[0])
2002-09-19 17:14:23 +00:00
continue; // edge has allready been removed
if (e > 0)
f2 = edgefaces[e].f[1];
else
f2 = edgefaces[-e].f[0];
2002-09-19 17:14:23 +00:00
if (f2 && f2->outputnumber == bsp->numfaces) {
edgefaces[abs (e)].f[0] = NULL;
edgefaces[abs (e)].f[1] = NULL;
2002-09-19 17:14:23 +00:00
continue; // allready merged
}
2002-09-19 17:14:23 +00:00
if (f2 && CanJoinFaces (f, f2)) { // remove the edge and merge the
// faces
edgefaces[abs (e)].f[0] = NULL;
edgefaces[abs (e)].f[1] = NULL;
RecursiveGrowRegion (r, f2);
2002-09-19 17:14:23 +00:00
} else {
// emit a surfedge
BSP_AddSurfEdge (bsp, e);
}
}
}
2008-07-19 05:40:57 +00:00
*/
2002-09-19 18:51:19 +00:00
2002-09-19 17:14:23 +00:00
typedef struct {
int numedges;
int edges[2];
} checkpoint_t;
2002-09-19 17:14:23 +00:00
checkpoint_t checkpoints[MAX_MAP_VERTS];
static void
2002-09-19 17:14:23 +00:00
CountRealNumbers (void)
{
2002-09-19 18:51:19 +00:00
int c, i;
2002-09-19 17:14:23 +00:00
qprintf ("%5i regions\n", bsp->numfaces - firstmodelface);
c = 0;
2002-09-19 17:14:23 +00:00
for (i = firstmodelface; i < bsp->numfaces; i++)
c += bsp->faces[i].numedges;
qprintf ("%5i real marksurfaces\n", c);
2002-09-19 17:14:23 +00:00
c = 0;
2002-09-19 17:14:23 +00:00
for (i = firstmodeledge; i < bsp->numedges; i++)
if (edgefaces[i].f[0])
2002-09-19 17:14:23 +00:00
c++; // not removed
qprintf ("%5i real edges\n", c);
}
static void
2002-09-19 17:14:23 +00:00
GrowNodeRegion_r (node_t * node)
{
dface_t r;
2002-09-19 17:14:23 +00:00
face_t *f;
int i;
if (node->planenum == PLANENUM_LEAF)
return;
node->firstface = bsp->numfaces;
2002-09-19 17:14:23 +00:00
for (f = node->faces; f; f = f->next) {
if (f->texturenum < 0)
continue;
2002-09-19 18:51:19 +00:00
// if (f->outputnumber != -1)
// continue; // allready grown into an earlier region
2002-09-19 17:14:23 +00:00
// emit a region
if (bsp->numfaces == MAX_MAP_FACES)
Sys_Error ("MAX_MAP_FACES");
f->outputnumber = bsp->numfaces;
r.planenum = node->outputplanenum;
r.side = f->planeside;
r.texinfo = f->texturenum;
2002-09-19 17:14:23 +00:00
for (i = 0; i < MAXLIGHTMAPS; i++)
r.styles[i] = 255;
r.lightofs = -1;
2002-09-19 17:14:23 +00:00
// add the face and mergable neighbors to it
#if 0
ClearRegionSize ();
AddFaceToRegionSize (f);
RecursiveGrowRegion (r, f);
#endif
r.firstedge = firstedge = bsp->numsurfedges;
2003-09-08 03:00:53 +00:00
for (i = 0; i < f->points->numpoints; i++) {
BSP_AddSurfEdge (bsp, f->edges[i]);
}
2002-09-19 17:14:23 +00:00
r.numedges = bsp->numsurfedges - r.firstedge;
BSP_AddFace (bsp, &r);
}
node->numfaces = bsp->numfaces - node->firstface;
GrowNodeRegion_r (node->children[0]);
GrowNodeRegion_r (node->children[1]);
}
2002-09-19 17:14:23 +00:00
void
2002-09-19 18:51:19 +00:00
GrowNodeRegions (node_t *headnode)
{
qprintf ("---- GrowRegions ----\n");
2002-09-19 17:14:23 +00:00
GrowNodeRegion_r (headnode);
2002-09-19 17:14:23 +00:00
2002-09-19 18:51:19 +00:00
// RemoveColinearEdges ();
CountRealNumbers ();
}
/*
2002-09-23 16:27:17 +00:00
Turn the faces on a plane into optimal non-convex regions
The edges may still be split later as a result of tjunctions
typedef struct
{
vec3_t dir;
vec3_t origin;
vec3_t p[2];
2010-08-30 06:12:20 +00:00
}
2002-09-23 16:27:17 +00:00
for all faces
for all edges
for all edges so far
if overlap
split
*/
//@}