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
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2003-02-04 23:26:26 +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"
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
int leaffaces;
|
|
|
|
int nodefaces;
|
|
|
|
int splitnodes;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
int c_solid, c_empty, c_water;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
qboolean usemidsplit;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
FaceSide
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
For BSP hueristic
|
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
|
|
|
FaceSide (face_t *in, plane_t *split)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 18:51:19 +00:00
|
|
|
int frontcount, backcount, i;
|
2002-09-19 17:14:23 +00:00
|
|
|
vec_t dot;
|
|
|
|
vec_t *p;
|
2003-09-08 03:00:53 +00:00
|
|
|
winding_t *inp = in->points;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
frontcount = backcount = 0;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// axial planes are fast
|
2002-09-19 16:58:48 +00:00
|
|
|
if (split->type < 3)
|
2003-09-08 03:00:53 +00:00
|
|
|
for (i = 0, p = inp->points[0] + split->type; i < inp->numpoints;
|
2002-09-19 17:14:23 +00:00
|
|
|
i++, p += 3) {
|
|
|
|
if (*p > split->dist + ON_EPSILON) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (backcount)
|
|
|
|
return SIDE_ON;
|
|
|
|
frontcount = 1;
|
2002-09-19 17:14:23 +00:00
|
|
|
} else if (*p < split->dist - ON_EPSILON) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (frontcount)
|
|
|
|
return SIDE_ON;
|
|
|
|
backcount = 1;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
} else
|
2002-09-23 16:27:17 +00:00
|
|
|
// sloping planes take longer
|
2003-09-08 03:00:53 +00:00
|
|
|
for (i = 0, p = inp->points[0]; i < inp->numpoints; i++, p += 3) {
|
2002-09-19 16:58:48 +00:00
|
|
|
dot = DotProduct (p, split->normal);
|
|
|
|
dot -= split->dist;
|
2002-09-19 17:14:23 +00:00
|
|
|
if (dot > ON_EPSILON) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (backcount)
|
|
|
|
return SIDE_ON;
|
|
|
|
frontcount = 1;
|
2002-09-19 17:14:23 +00:00
|
|
|
} else if (dot < -ON_EPSILON) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (frontcount)
|
|
|
|
return SIDE_ON;
|
|
|
|
backcount = 1;
|
|
|
|
}
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (!frontcount)
|
|
|
|
return SIDE_BACK;
|
|
|
|
if (!backcount)
|
|
|
|
return SIDE_FRONT;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
return SIDE_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
ChooseMidPlaneFromList
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
The clipping hull BSP doesn't worry about avoiding splits
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static surface_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
ChooseMidPlaneFromList (surface_t *surfaces, vec3_t mins, vec3_t maxs)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
int j, l;
|
2002-09-19 18:51:19 +00:00
|
|
|
plane_t *plane;
|
2002-09-19 17:14:23 +00:00
|
|
|
surface_t *p, *bestsurface;
|
|
|
|
vec_t bestvalue, value, dist;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// pick the plane that splits the least
|
2002-09-19 17:14:23 +00:00
|
|
|
bestvalue = 6 * 8192 * 8192;
|
2002-09-19 16:58:48 +00:00
|
|
|
bestsurface = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (p = surfaces; p; p = p->next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (p->onnode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
plane = &planes[p->planenum];
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
// check for axis aligned surfaces
|
2002-09-19 16:58:48 +00:00
|
|
|
l = plane->type;
|
|
|
|
if (l > PLANE_Z)
|
|
|
|
continue;
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
// calculate the split metric along axis l, smaller values are better
|
2002-09-19 16:58:48 +00:00
|
|
|
value = 0;
|
|
|
|
|
|
|
|
dist = plane->dist * plane->normal[l];
|
2002-09-19 17:14:23 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
if (j == l) {
|
|
|
|
value += (maxs[l] - dist) * (maxs[l] - dist);
|
|
|
|
value += (dist - mins[l]) * (dist - mins[l]);
|
|
|
|
} else
|
|
|
|
value += 2 * (maxs[j] - mins[j]) * (maxs[j] - mins[j]);
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (value > bestvalue)
|
|
|
|
continue;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
// currently the best!
|
2002-09-19 16:58:48 +00:00
|
|
|
bestvalue = value;
|
|
|
|
bestsurface = p;
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
if (!bestsurface) {
|
|
|
|
for (p = surfaces; p; p = p->next)
|
2002-09-19 16:58:48 +00:00
|
|
|
if (!p->onnode)
|
2002-09-19 17:14:23 +00:00
|
|
|
return p; // first valid surface
|
2002-09-19 16:58:48 +00:00
|
|
|
Sys_Error ("ChooseMidPlaneFromList: no valid planes");
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
return bestsurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
ChoosePlaneFromList
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
The real BSP hueristic
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static surface_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
ChoosePlaneFromList (surface_t *surfaces, vec3_t mins, vec3_t maxs,
|
2003-02-04 23:26:26 +00:00
|
|
|
qboolean usefloors, qboolean usedetail)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 18:51:19 +00:00
|
|
|
face_t *f;
|
2003-02-04 23:26:26 +00:00
|
|
|
int j, k, l, ishint;
|
2002-09-19 18:51:19 +00:00
|
|
|
plane_t *plane;
|
2002-09-19 17:14:23 +00:00
|
|
|
surface_t *p, *p2, *bestsurface;
|
|
|
|
vec_t bestvalue, bestdistribution, value, dist;
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// pick the plane that splits the least
|
2003-02-04 23:26:26 +00:00
|
|
|
bestvalue = 999999;
|
2002-09-19 16:58:48 +00:00
|
|
|
bestsurface = NULL;
|
|
|
|
bestdistribution = 9e30;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (p = surfaces; p; p = p->next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (p->onnode)
|
|
|
|
continue;
|
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
for (f = p->faces; f; f = f->next)
|
|
|
|
if (f->texturenum == TEX_HINT)
|
|
|
|
break;
|
|
|
|
ishint = f != 0;
|
|
|
|
|
|
|
|
if (p->has_struct && usedetail)
|
|
|
|
continue;
|
|
|
|
if (!p->has_struct && !usedetail)
|
|
|
|
continue;
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
plane = &planes[p->planenum];
|
|
|
|
k = 0;
|
|
|
|
|
|
|
|
if (!usefloors && plane->normal[2] == 1)
|
|
|
|
continue;
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
for (p2 = surfaces; p2; p2 = p2->next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (p2 == p)
|
|
|
|
continue;
|
|
|
|
if (p2->onnode)
|
|
|
|
continue;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (f = p2->faces; f; f = f->next) {
|
|
|
|
if (FaceSide (f, plane) == SIDE_ON) {
|
2003-02-04 23:26:26 +00:00
|
|
|
if (!ishint && f->texturenum == TEX_HINT)
|
|
|
|
k += 9999;
|
|
|
|
else
|
|
|
|
k++;
|
2002-09-19 16:58:48 +00:00
|
|
|
if (k >= bestvalue)
|
|
|
|
break;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
if (k > bestvalue)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k > bestvalue)
|
|
|
|
continue;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
// if equal numbers, axial planes win, then decide on spatial
|
|
|
|
// subdivision
|
|
|
|
|
|
|
|
if (k < bestvalue || (k == bestvalue && plane->type < PLANE_ANYX)) {
|
|
|
|
// check for axis aligned surfaces
|
2002-09-19 16:58:48 +00:00
|
|
|
l = plane->type;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
if (l <= PLANE_Z) { // axial aligned
|
|
|
|
// calculate the split metric along axis l
|
2002-09-19 16:58:48 +00:00
|
|
|
value = 0;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
if (j == l) {
|
2002-09-19 16:58:48 +00:00
|
|
|
dist = plane->dist * plane->normal[l];
|
2002-09-19 17:14:23 +00:00
|
|
|
value += (maxs[l] - dist) * (maxs[l] - dist);
|
|
|
|
value += (dist - mins[l]) * (dist - mins[l]);
|
|
|
|
} else
|
|
|
|
value += 2 * (maxs[j] - mins[j]) * (maxs[j] - mins[j]);
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (value > bestdistribution && k == bestvalue)
|
|
|
|
continue;
|
|
|
|
bestdistribution = value;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
// currently the best!
|
2002-09-19 16:58:48 +00:00
|
|
|
bestvalue = k;
|
|
|
|
bestsurface = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestsurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
SelectPartition
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
Selects a surface from a linked list of surfaces to split the group on
|
|
|
|
returns NULL if the surface list can not be divided any more (a leaf)
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static surface_t *
|
2003-02-04 23:26:26 +00:00
|
|
|
SelectPartition (surface_t *surfaces, int *detail)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
int i, j;
|
|
|
|
surface_t *p, *bestsurface;
|
2002-09-19 18:51:19 +00:00
|
|
|
vec3_t mins, maxs;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
*detail = 0;
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// count onnode surfaces
|
2002-09-19 16:58:48 +00:00
|
|
|
i = 0;
|
|
|
|
bestsurface = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
for (p = surfaces; p; p = p->next)
|
|
|
|
if (!p->onnode) {
|
2002-09-19 16:58:48 +00:00
|
|
|
i++;
|
|
|
|
bestsurface = p;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
if (i == 0)
|
2002-09-19 16:58:48 +00:00
|
|
|
return NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
if (i == 1) {
|
|
|
|
if (!bestsurface->has_struct && !usemidsplit)
|
|
|
|
*detail = 1;
|
2002-09-19 17:14:23 +00:00
|
|
|
return bestsurface; // this is a final split
|
2003-02-04 23:26:26 +00:00
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// calculate a bounding box of the entire surfaceset
|
2002-09-19 17:14:23 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2003-09-03 22:00:08 +00:00
|
|
|
mins[i] = BOGUS_RANGE;
|
|
|
|
maxs[i] = -BOGUS_RANGE;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
for (p = surfaces; p; p = p->next)
|
|
|
|
for (j = 0; j < 3; j++) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (p->mins[j] < mins[j])
|
|
|
|
mins[j] = p->mins[j];
|
|
|
|
if (p->maxs[j] > maxs[j])
|
|
|
|
maxs[j] = p->maxs[j];
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
if (usemidsplit) // do fast way for clipping hull
|
2002-09-19 16:58:48 +00:00
|
|
|
return ChooseMidPlaneFromList (surfaces, mins, maxs);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// do slow way to save poly splits for drawing hull
|
2002-09-19 16:58:48 +00:00
|
|
|
#if 0
|
|
|
|
bestsurface = ChoosePlaneFromList (surfaces, mins, maxs, false);
|
2002-09-19 17:14:23 +00:00
|
|
|
if (bestsurface)
|
2002-09-19 16:58:48 +00:00
|
|
|
return bestsurface;
|
2002-09-19 17:14:23 +00:00
|
|
|
#endif
|
2003-02-04 23:26:26 +00:00
|
|
|
bestsurface = ChoosePlaneFromList (surfaces, mins, maxs, true, false);
|
|
|
|
if (bestsurface)
|
|
|
|
return bestsurface;
|
|
|
|
*detail = 1;
|
|
|
|
return ChoosePlaneFromList (surfaces, mins, maxs, true, true);
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
CalcSurfaceInfo
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
Calculates the bounding box
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2002-09-19 17:14:23 +00:00
|
|
|
void
|
|
|
|
CalcSurfaceInfo (surface_t * surf)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *f;
|
2002-09-19 18:51:19 +00:00
|
|
|
int i, j;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (!surf->faces)
|
|
|
|
Sys_Error ("CalcSurfaceInfo: surface without a face");
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// calculate a bounding box
|
2002-09-19 17:14:23 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2003-09-03 22:00:08 +00:00
|
|
|
surf->mins[i] = BOGUS_RANGE;
|
|
|
|
surf->maxs[i] = -BOGUS_RANGE;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
for (f = surf->faces; f; f = f->next) {
|
2003-09-08 03:00:53 +00:00
|
|
|
winding_t *fp = f->points;
|
2002-09-19 17:14:23 +00:00
|
|
|
if (f->contents[0] >= 0 || f->contents[1] >= 0)
|
|
|
|
Sys_Error ("Bad contents");
|
2003-09-08 03:00:53 +00:00
|
|
|
for (i = 0; i < fp->numpoints; i++)
|
2002-09-19 17:14:23 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
2003-09-08 03:00:53 +00:00
|
|
|
if (fp->points[i][j] < surf->mins[j])
|
|
|
|
surf->mins[j] = fp->points[i][j];
|
|
|
|
if (fp->points[i][j] > surf->maxs[j])
|
|
|
|
surf->maxs[j] = fp->points[i][j];
|
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
|
|
|
DividePlane (surface_t *in, plane_t *split, surface_t **front,
|
|
|
|
surface_t **back)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *facet, *next;
|
|
|
|
face_t *frontlist, *backlist;
|
|
|
|
face_t *frontfrag, *backfrag;
|
|
|
|
plane_t *inplane;
|
2002-09-19 18:51:19 +00:00
|
|
|
surface_t *news;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
int have[2][2]; // [front|back][detail|struct]
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
inplane = &planes[in->planenum];
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// parallel case is easy
|
2002-09-20 17:02:53 +00:00
|
|
|
if (_VectorCompare (inplane->normal, split->normal)) {
|
2002-09-23 16:27:17 +00:00
|
|
|
// check for exactly on node
|
|
|
|
if (inplane->dist == split->dist) {
|
|
|
|
// divide the facets to the front and back sides
|
2002-09-19 16:58:48 +00:00
|
|
|
news = AllocSurface ();
|
|
|
|
*news = *in;
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
facet = in->faces;
|
2002-09-19 16:58:48 +00:00
|
|
|
in->faces = NULL;
|
|
|
|
news->faces = NULL;
|
|
|
|
in->onnode = news->onnode = true;
|
2003-02-04 23:26:26 +00:00
|
|
|
in->has_detail = in->has_struct = false;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (; facet; facet = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = facet->next;
|
2002-09-19 17:14:23 +00:00
|
|
|
if (facet->planeside == 1) {
|
2002-09-19 16:58:48 +00:00
|
|
|
facet->next = news->faces;
|
|
|
|
news->faces = facet;
|
2003-02-04 23:26:26 +00:00
|
|
|
|
|
|
|
if (facet->detail)
|
|
|
|
news->has_detail = true;
|
|
|
|
else
|
|
|
|
news->has_struct = true;
|
2002-09-19 17:14:23 +00:00
|
|
|
} else {
|
2002-09-19 16:58:48 +00:00
|
|
|
facet->next = in->faces;
|
|
|
|
in->faces = facet;
|
2003-02-04 23:26:26 +00:00
|
|
|
|
|
|
|
if (facet->detail)
|
|
|
|
in->has_detail = true;
|
|
|
|
else
|
|
|
|
in->has_struct = true;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (in->faces)
|
|
|
|
*front = in;
|
|
|
|
else
|
|
|
|
*front = NULL;
|
|
|
|
if (news->faces)
|
|
|
|
*back = news;
|
|
|
|
else
|
|
|
|
*back = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
if (inplane->dist > split->dist) {
|
2002-09-19 16:58:48 +00:00
|
|
|
*front = in;
|
|
|
|
*back = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
} else {
|
2002-09-19 16:58:48 +00:00
|
|
|
*front = NULL;
|
|
|
|
*back = in;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2002-09-23 16:27:17 +00:00
|
|
|
// do a real split. may still end up entirely on one side
|
|
|
|
// OPTIMIZE: use bounding box for fast test
|
2002-09-19 16:58:48 +00:00
|
|
|
frontlist = NULL;
|
|
|
|
backlist = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
memset (have, 0, sizeof (have));
|
2002-09-19 17:14:23 +00:00
|
|
|
for (facet = in->faces; facet; facet = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = facet->next;
|
|
|
|
SplitFace (facet, split, &frontfrag, &backfrag);
|
2002-09-19 17:14:23 +00:00
|
|
|
if (frontfrag) {
|
2002-09-19 16:58:48 +00:00
|
|
|
frontfrag->next = frontlist;
|
|
|
|
frontlist = frontfrag;
|
2003-02-04 23:26:26 +00:00
|
|
|
have[0][frontfrag->detail] = 1;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
if (backfrag) {
|
2002-09-19 16:58:48 +00:00
|
|
|
backfrag->next = backlist;
|
|
|
|
backlist = backfrag;
|
2003-02-04 23:26:26 +00:00
|
|
|
have[1][backfrag->detail] = 1;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// if nothing actually got split, just move the in plane
|
2002-09-19 17:14:23 +00:00
|
|
|
if (frontlist == NULL) {
|
2002-09-19 16:58:48 +00:00
|
|
|
*front = NULL;
|
|
|
|
*back = in;
|
|
|
|
in->faces = backlist;
|
|
|
|
return;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
if (backlist == NULL) {
|
2002-09-19 16:58:48 +00:00
|
|
|
*front = in;
|
|
|
|
*back = NULL;
|
|
|
|
in->faces = frontlist;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// stuff got split, so allocate one new plane and reuse in
|
2002-09-19 16:58:48 +00:00
|
|
|
news = AllocSurface ();
|
|
|
|
*news = *in;
|
|
|
|
news->faces = backlist;
|
|
|
|
*back = news;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
in->faces = frontlist;
|
|
|
|
*front = in;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
in->has_struct = have[0][0];
|
|
|
|
in->has_detail = have[0][1];
|
|
|
|
|
|
|
|
news->has_struct = have[1][0];
|
|
|
|
news->has_detail = have[1][1];
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// recalc bboxes and flags
|
2002-09-19 16:58:48 +00:00
|
|
|
CalcSurfaceInfo (news);
|
2002-09-19 17:14:23 +00:00
|
|
|
CalcSurfaceInfo (in);
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
LinkConvexFaces
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
Determines the contents of the leaf and creates the final list of
|
|
|
|
original faces that have some fragment inside this leaf
|
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
|
|
|
LinkConvexFaces (surface_t *planelist, node_t *leafnode)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *f, *next;
|
|
|
|
int i, count;
|
2002-09-19 18:51:19 +00:00
|
|
|
surface_t *surf, *pnext;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
leafnode->faces = NULL;
|
|
|
|
leafnode->contents = 0;
|
|
|
|
leafnode->planenum = -1;
|
|
|
|
|
|
|
|
count = 0;
|
2002-09-19 17:14:23 +00:00
|
|
|
for (surf = planelist; surf; surf = surf->next) {
|
|
|
|
for (f = surf->faces; f; f = f->next) {
|
2003-02-04 23:26:26 +00:00
|
|
|
if (f->texturenum < 0)
|
|
|
|
continue;
|
2002-09-19 16:58:48 +00:00
|
|
|
count++;
|
|
|
|
if (!leafnode->contents)
|
|
|
|
leafnode->contents = f->contents[0];
|
|
|
|
else if (leafnode->contents != f->contents[0])
|
|
|
|
Sys_Error ("Mixed face contents in leafnode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leafnode->contents)
|
|
|
|
leafnode->contents = CONTENTS_SOLID;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
switch (leafnode->contents) {
|
|
|
|
case CONTENTS_EMPTY:
|
|
|
|
c_empty++;
|
|
|
|
break;
|
|
|
|
case CONTENTS_SOLID:
|
|
|
|
c_solid++;
|
|
|
|
break;
|
|
|
|
case CONTENTS_WATER:
|
|
|
|
case CONTENTS_SLIME:
|
|
|
|
case CONTENTS_LAVA:
|
|
|
|
case CONTENTS_SKY:
|
|
|
|
c_water++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sys_Error ("LinkConvexFaces: bad contents number");
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// write the list of faces, and free the originals
|
2002-09-19 16:58:48 +00:00
|
|
|
leaffaces += count;
|
2002-09-19 17:14:23 +00:00
|
|
|
leafnode->markfaces = malloc (sizeof (face_t *) * (count + 1));
|
2002-09-19 16:58:48 +00:00
|
|
|
i = 0;
|
2002-09-19 17:14:23 +00:00
|
|
|
for (surf = planelist; surf; surf = pnext) {
|
2002-09-19 16:58:48 +00:00
|
|
|
pnext = surf->next;
|
2002-09-19 17:14:23 +00:00
|
|
|
for (f = surf->faces; f; f = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = f->next;
|
2003-02-04 23:26:26 +00:00
|
|
|
if (f->texturenum >= 0) {
|
|
|
|
leafnode->markfaces[i] = f->original;
|
|
|
|
i++;
|
|
|
|
}
|
2002-09-19 16:58:48 +00:00
|
|
|
FreeFace (f);
|
|
|
|
}
|
|
|
|
FreeSurface (surf);
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
leafnode->markfaces[i] = NULL; // sentinal
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-23 16:27:17 +00:00
|
|
|
LinkNodeFaces
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
Returns a duplicated list of all faces on surface
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static face_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
LinkNodeFaces (surface_t *surface)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 18:51:19 +00:00
|
|
|
face_t *list, *new, **prevptr, *f;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
list = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// subdivide
|
2002-09-19 16:58:48 +00:00
|
|
|
prevptr = &surface->faces;
|
2002-09-19 17:14:23 +00:00
|
|
|
while (1) {
|
2002-09-19 16:58:48 +00:00
|
|
|
f = *prevptr;
|
|
|
|
if (!f)
|
|
|
|
break;
|
|
|
|
SubdivideFace (f, prevptr);
|
|
|
|
f = *prevptr;
|
|
|
|
prevptr = &f->next;
|
|
|
|
}
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// copy
|
2002-09-19 17:14:23 +00:00
|
|
|
for (f = surface->faces; f; f = f->next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
nodefaces++;
|
|
|
|
new = AllocFace ();
|
|
|
|
*new = *f;
|
2003-09-08 03:00:53 +00:00
|
|
|
new->points = CopyWinding (f->points);
|
2002-09-19 16:58:48 +00:00
|
|
|
f->original = new;
|
|
|
|
new->next = list;
|
|
|
|
list = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-09-19 18:51:19 +00:00
|
|
|
PartitionSurfaces (surface_t *surfaces, node_t *node)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
surface_t *split, *p, *next;
|
|
|
|
surface_t *frontlist, *backlist;
|
|
|
|
surface_t *frontfrag, *backfrag;
|
|
|
|
plane_t *splitplane;
|
|
|
|
|
2003-02-04 23:26:26 +00:00
|
|
|
split = SelectPartition (surfaces, &node->detail);
|
2002-09-19 17:14:23 +00:00
|
|
|
if (!split) { // this is a leaf node
|
2003-02-04 23:26:26 +00:00
|
|
|
node->detail = 0;
|
2002-09-19 16:58:48 +00:00
|
|
|
node->planenum = PLANENUM_LEAF;
|
|
|
|
LinkConvexFaces (surfaces, node);
|
|
|
|
return;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
splitnodes++;
|
|
|
|
node->faces = LinkNodeFaces (split);
|
|
|
|
node->children[0] = AllocNode ();
|
|
|
|
node->children[1] = AllocNode ();
|
|
|
|
node->planenum = split->planenum;
|
|
|
|
|
|
|
|
splitplane = &planes[split->planenum];
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// multiple surfaces, so split all the polysurfaces into front and back
|
|
|
|
// lists
|
2002-09-19 16:58:48 +00:00
|
|
|
frontlist = NULL;
|
|
|
|
backlist = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (p = surfaces; p; p = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = p->next;
|
|
|
|
DividePlane (p, splitplane, &frontfrag, &backfrag);
|
2002-09-19 17:14:23 +00:00
|
|
|
if (frontfrag && backfrag) {
|
|
|
|
// the plane was split, which may expose oportunities to merge
|
|
|
|
// adjacent faces into a single face
|
2002-09-19 18:51:19 +00:00
|
|
|
// MergePlaneFaces (frontfrag);
|
|
|
|
// MergePlaneFaces (backfrag);
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
if (frontfrag) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (!frontfrag->faces)
|
|
|
|
Sys_Error ("surface with no faces");
|
|
|
|
frontfrag->next = frontlist;
|
|
|
|
frontlist = frontfrag;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
if (backfrag) {
|
2002-09-19 16:58:48 +00:00
|
|
|
if (!backfrag->faces)
|
|
|
|
Sys_Error ("surface with no faces");
|
|
|
|
backfrag->next = backlist;
|
|
|
|
backlist = backfrag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionSurfaces (frontlist, node->children[0]);
|
|
|
|
PartitionSurfaces (backlist, node->children[1]);
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
node_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
SolidBSP (surface_t *surfhead, qboolean midsplit)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
int i;
|
|
|
|
node_t *headnode;
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
qprintf ("----- SolidBSP -----\n");
|
|
|
|
|
|
|
|
headnode = AllocNode ();
|
|
|
|
usemidsplit = midsplit;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// calculate a bounding box for the entire model
|
2002-09-19 17:14:23 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2002-09-19 16:58:48 +00:00
|
|
|
headnode->mins[i] = brushset->mins[i] - SIDESPACE;
|
|
|
|
headnode->maxs[i] = brushset->maxs[i] + SIDESPACE;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// recursively partition everything
|
2002-09-19 16:58:48 +00:00
|
|
|
Draw_ClearWindow ();
|
|
|
|
splitnodes = 0;
|
|
|
|
leaffaces = 0;
|
|
|
|
nodefaces = 0;
|
|
|
|
c_solid = c_empty = c_water = 0;
|
|
|
|
|
|
|
|
PartitionSurfaces (surfhead, headnode);
|
|
|
|
|
|
|
|
qprintf ("%5i split nodes\n", splitnodes);
|
|
|
|
qprintf ("%5i solid leafs\n", c_solid);
|
|
|
|
qprintf ("%5i empty leafs\n", c_empty);
|
2002-09-19 17:14:23 +00:00
|
|
|
qprintf ("%5i water leafs\n", c_water);
|
|
|
|
qprintf ("%5i leaffaces\n", leaffaces);
|
2002-09-19 16:58:48 +00:00
|
|
|
qprintf ("%5i nodefaces\n", nodefaces);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
return headnode;
|
|
|
|
}
|