2002-09-19 18:51:19 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
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 16:58:48 +00:00
|
|
|
|
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 16:58:48 +00:00
|
|
|
|
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 16:58:48 +00:00
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
See file, 'COPYING', for details.
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2002-09-19 19:12:16 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include "string.h"
|
|
|
|
#endif
|
2002-09-19 16:58:48 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
|
|
|
#include "bsp5.h"
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "csg4.h"
|
2002-09-20 21:48:34 +00:00
|
|
|
#include "options.h"
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "region.h"
|
|
|
|
#include "surfaces.h"
|
|
|
|
#include "winding.h"
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
a surface has all of the faces that could be drawn on a given plane
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
the outside filling stage can remove some of them so a better bsp can be
|
|
|
|
generated
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2002-09-19 18:51:19 +00:00
|
|
|
surface_t newcopy_t;
|
2002-09-19 17:14:23 +00:00
|
|
|
int subdivides;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
SubdivideFace
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
If the face is >256 in either texture direction, carve a valid sized
|
|
|
|
piece off and insert the remainder in the next link
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2002-09-19 17:14:23 +00:00
|
|
|
void
|
2002-09-19 18:51:19 +00:00
|
|
|
SubdivideFace (face_t *f, face_t **prevptr)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 18:51:19 +00:00
|
|
|
face_t *front, *back, *next;
|
2002-09-19 17:14:23 +00:00
|
|
|
float mins, maxs;
|
|
|
|
int axis, i;
|
|
|
|
plane_t plane;
|
|
|
|
texinfo_t *tex;
|
2002-09-19 18:51:19 +00:00
|
|
|
vec_t v;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2003-04-04 21:48:47 +00:00
|
|
|
if (f->texturenum < 0) // don't subdivide HINT or SKIP
|
|
|
|
return;
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// special (non-surface cached) faces don't need subdivision
|
2002-09-19 16:58:48 +00:00
|
|
|
tex = &bsp->texinfo[f->texturenum];
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
if (tex->flags & TEX_SPECIAL)
|
2002-09-19 16:58:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
for (axis = 0; axis < 2; axis++) {
|
|
|
|
while (1) {
|
2003-09-03 22:00:08 +00:00
|
|
|
mins = BOGUS_RANGE;
|
|
|
|
maxs = -BOGUS_RANGE;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
for (i = 0; i < f->points->numpoints; i++) {
|
|
|
|
v = DotProduct (f->points->points[i], tex->vecs[axis]);
|
2002-09-19 16:58:48 +00:00
|
|
|
if (v < mins)
|
|
|
|
mins = v;
|
|
|
|
if (v > maxs)
|
|
|
|
maxs = v;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-20 21:48:34 +00:00
|
|
|
if (maxs - mins <= options.subdivide_size)
|
2002-09-19 16:58:48 +00:00
|
|
|
break;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
// split it
|
2002-09-19 16:58:48 +00:00
|
|
|
subdivides++;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
VectorCopy (tex->vecs[axis], plane.normal);
|
|
|
|
v = VectorLength (plane.normal);
|
2002-09-25 21:35:49 +00:00
|
|
|
_VectorNormalize (plane.normal);
|
2002-09-20 21:48:34 +00:00
|
|
|
plane.dist = (mins + options.subdivide_size - 16) / v;
|
2002-09-19 16:58:48 +00:00
|
|
|
next = f->next;
|
|
|
|
SplitFace (f, &plane, &front, &back);
|
|
|
|
if (!front || !back)
|
|
|
|
Sys_Error ("SubdivideFace: didn't split the polygon");
|
|
|
|
*prevptr = back;
|
|
|
|
back->next = front;
|
|
|
|
front->next = next;
|
|
|
|
f = back;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
GatherNodeFaces
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
Frees the current node tree and returns a new chain of the surfaces that
|
|
|
|
have inside faces.
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-09-19 18:51:19 +00:00
|
|
|
GatherNodeFaces_r (node_t *node)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 18:51:19 +00:00
|
|
|
face_t *next, *f;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
if (node->planenum != PLANENUM_LEAF) {
|
2002-09-23 16:27:17 +00:00
|
|
|
// decision node
|
2002-09-19 17:14:23 +00:00
|
|
|
for (f = node->faces; f; f = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = f->next;
|
2003-09-08 03:00:53 +00:00
|
|
|
if (!f->points) { // face was removed outside
|
2002-09-19 16:58:48 +00:00
|
|
|
FreeFace (f);
|
2002-09-19 17:14:23 +00:00
|
|
|
} else {
|
2002-09-19 16:58:48 +00:00
|
|
|
f->next = validfaces[f->planenum];
|
|
|
|
validfaces[f->planenum] = f;
|
|
|
|
}
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
GatherNodeFaces_r (node->children[0]);
|
|
|
|
GatherNodeFaces_r (node->children[1]);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
free (node);
|
2002-09-19 17:14:23 +00:00
|
|
|
} else {
|
2002-09-23 16:27:17 +00:00
|
|
|
// leaf node
|
2002-09-19 16:58:48 +00:00
|
|
|
free (node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
surface_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
GatherNodeFaces (node_t *headnode)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
memset (validfaces, 0, sizeof (validfaces));
|
2002-09-19 16:58:48 +00:00
|
|
|
GatherNodeFaces_r (headnode);
|
2002-09-19 17:14:23 +00:00
|
|
|
return BuildSurfaces ();
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
typedef struct hashvert_s {
|
|
|
|
struct hashvert_s *next;
|
|
|
|
vec3_t point;
|
|
|
|
int num;
|
|
|
|
int numplanes; // for corner determination
|
|
|
|
int planenums[2];
|
|
|
|
int numedges;
|
2002-09-19 16:58:48 +00:00
|
|
|
} hashvert_t;
|
|
|
|
|
|
|
|
#define POINT_EPSILON 0.01
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
int c_cornerverts;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
hashvert_t hvertex[MAX_MAP_VERTS];
|
|
|
|
hashvert_t *hvert_p;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *edgefaces[MAX_MAP_EDGES][2];
|
|
|
|
int firstmodeledge = 1;
|
|
|
|
int firstmodelface;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#define NUM_HASH 4096
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
hashvert_t *hashverts[NUM_HASH];
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
static vec3_t hash_min, hash_scale;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
static void
|
|
|
|
InitHash (void)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
int i;
|
2002-09-19 18:51:19 +00:00
|
|
|
int newsize[2];
|
|
|
|
vec3_t size;
|
|
|
|
vec_t scale, volume;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
memset (hashverts, 0, sizeof (hashverts));
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
2002-09-19 16:58:48 +00:00
|
|
|
hash_min[i] = -8000;
|
|
|
|
size[i] = 16000;
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
volume = size[0] * size[1];
|
|
|
|
|
|
|
|
scale = sqrt (volume / NUM_HASH);
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
newsize[0] = size[0] / scale;
|
|
|
|
newsize[1] = size[1] / scale;
|
|
|
|
|
|
|
|
hash_scale[0] = newsize[0] / size[0];
|
|
|
|
hash_scale[1] = newsize[1] / size[1];
|
|
|
|
hash_scale[2] = newsize[1];
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
hvert_p = hvertex;
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
static unsigned
|
|
|
|
HashVec (vec3_t vec)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
unsigned h;
|
|
|
|
|
|
|
|
h = hash_scale[0] * (vec[0] - hash_min[0]) * hash_scale[2]
|
2002-09-19 16:58:48 +00:00
|
|
|
+ hash_scale[1] * (vec[1] - hash_min[1]);
|
2002-09-19 17:14:23 +00:00
|
|
|
if (h >= NUM_HASH)
|
2002-09-19 16:58:48 +00:00
|
|
|
return NUM_HASH - 1;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2002-09-19 17:14:23 +00:00
|
|
|
GetVertex (vec3_t in, int planenum)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
hashvert_t *hv;
|
2002-09-19 18:51:19 +00:00
|
|
|
int h, i;
|
2002-09-19 17:14:23 +00:00
|
|
|
vec3_t vert;
|
2002-09-19 20:39:33 +00:00
|
|
|
dvertex_t v;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
2002-09-20 17:02:53 +00:00
|
|
|
if (fabs (in[i] - RINT (in[i])) < 0.001)
|
|
|
|
vert[i] = RINT (in[i]);
|
2002-09-19 16:58:48 +00:00
|
|
|
else
|
|
|
|
vert[i] = in[i];
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
h = HashVec (vert);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (hv = hashverts[h]; hv; hv = hv->next) {
|
|
|
|
if (fabs (hv->point[0] - vert[0]) < POINT_EPSILON
|
|
|
|
&& fabs (hv->point[1] - vert[1]) < POINT_EPSILON
|
|
|
|
&& fabs (hv->point[2] - vert[2]) < POINT_EPSILON) {
|
2002-09-19 16:58:48 +00:00
|
|
|
hv->numedges++;
|
|
|
|
if (hv->numplanes == 3)
|
2002-09-19 18:51:19 +00:00
|
|
|
return hv->num; // already known to be a corner
|
2002-09-19 17:14:23 +00:00
|
|
|
for (i = 0; i < hv->numplanes; i++)
|
2002-09-19 16:58:48 +00:00
|
|
|
if (hv->planenums[i] == planenum)
|
2002-09-19 18:51:19 +00:00
|
|
|
return hv->num; // already know this plane
|
2002-09-19 16:58:48 +00:00
|
|
|
if (hv->numplanes == 2)
|
|
|
|
c_cornerverts++;
|
|
|
|
else
|
|
|
|
hv->planenums[hv->numplanes] = planenum;
|
|
|
|
hv->numplanes++;
|
|
|
|
return hv->num;
|
|
|
|
}
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
hv = hvert_p;
|
|
|
|
hv->numedges = 1;
|
|
|
|
hv->numplanes = 1;
|
|
|
|
hv->planenums[0] = planenum;
|
|
|
|
hv->next = hashverts[h];
|
|
|
|
hashverts[h] = hv;
|
|
|
|
VectorCopy (vert, hv->point);
|
|
|
|
hv->num = bsp->numvertexes;
|
2002-09-19 17:14:23 +00:00
|
|
|
if (hv->num == MAX_MAP_VERTS)
|
2002-09-19 16:58:48 +00:00
|
|
|
Sys_Error ("GetVertex: MAX_MAP_VERTS");
|
|
|
|
hvert_p++;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// emit a vertex
|
2002-09-19 16:58:48 +00:00
|
|
|
if (bsp->numvertexes == MAX_MAP_VERTS)
|
|
|
|
Sys_Error ("numvertexes == MAX_MAP_VERTS");
|
|
|
|
|
2002-09-19 20:39:33 +00:00
|
|
|
v.point[0] = vert[0];
|
|
|
|
v.point[1] = vert[1];
|
|
|
|
v.point[2] = vert[2];
|
|
|
|
|
|
|
|
BSP_AddVertex (bsp, &v);
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
return hv->num;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
int c_tryedges;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
GetEdge
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
Don't allow four way edges
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2002-09-19 18:51:19 +00:00
|
|
|
GetEdge (vec3_t p1, vec3_t p2, face_t *f)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 20:39:33 +00:00
|
|
|
dedge_t edge;
|
2002-09-19 18:51:19 +00:00
|
|
|
int v1, v2, i;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
if (!f->contents[0])
|
|
|
|
Sys_Error ("GetEdge: 0 contents");
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
c_tryedges++;
|
2002-09-19 16:58:48 +00:00
|
|
|
v1 = GetVertex (p1, f->planenum);
|
|
|
|
v2 = GetVertex (p2, f->planenum);
|
2002-09-19 17:14:23 +00:00
|
|
|
for (i = firstmodeledge; i < bsp->numedges; i++) {
|
2002-09-19 20:39:33 +00:00
|
|
|
if (v1 == bsp->edges[i].v[1] && v2 == bsp->edges[i].v[0]
|
2002-09-19 17:14:23 +00:00
|
|
|
&& !edgefaces[i][1]
|
|
|
|
&& edgefaces[i][0]->contents[0] == f->contents[0]) {
|
2002-09-19 16:58:48 +00:00
|
|
|
edgefaces[i][1] = f;
|
|
|
|
return -i;
|
|
|
|
}
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// emit an edge
|
2002-09-19 16:58:48 +00:00
|
|
|
if (bsp->numedges == MAX_MAP_EDGES)
|
|
|
|
Sys_Error ("numedges == MAX_MAP_EDGES");
|
2002-09-19 20:39:33 +00:00
|
|
|
edge.v[0] = v1;
|
|
|
|
edge.v[1] = v2;
|
|
|
|
BSP_AddEdge (bsp, &edge);
|
2002-09-19 16:58:48 +00:00
|
|
|
edgefaces[i][0] = f;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-09-19 18:51:19 +00:00
|
|
|
FindFaceEdges (face_t *face)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
int i;
|
2003-09-08 03:00:53 +00:00
|
|
|
winding_t *facep = face->points;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
face->outputnumber = -1;
|
2003-09-08 03:00:53 +00:00
|
|
|
face->edges = malloc (sizeof (int) * facep->numpoints);
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
for (i = 0; i < facep->numpoints; i++)
|
|
|
|
face->edges[i] = GetEdge (facep->points[i],
|
|
|
|
facep->points[(i + 1) % facep->numpoints],
|
|
|
|
face);
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-09-19 18:51:19 +00:00
|
|
|
MakeFaceEdges_r (node_t *node)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *f;
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (node->planenum == PLANENUM_LEAF)
|
|
|
|
return;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (f = node->faces; f; f = f->next)
|
2003-02-04 23:26:26 +00:00
|
|
|
if (f->texturenum >= 0)
|
|
|
|
FindFaceEdges (f);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
MakeFaceEdges_r (node->children[0]);
|
|
|
|
MakeFaceEdges_r (node->children[1]);
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
void
|
2002-09-19 18:51:19 +00:00
|
|
|
MakeFaceEdges (node_t *headnode)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
|
|
|
qprintf ("----- MakeFaceEdges -----\n");
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
InitHash ();
|
|
|
|
c_tryedges = 0;
|
|
|
|
c_cornerverts = 0;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
MakeFaceEdges_r (headnode);
|
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
// CheckEdges ();
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
GrowNodeRegions (headnode);
|
|
|
|
|
|
|
|
firstmodeledge = bsp->numedges;
|
|
|
|
firstmodelface = bsp->numfaces;
|
|
|
|
}
|