quakeforge/tools/qfbsp/source/solidbsp.c

740 lines
14 KiB
C
Raw Normal View History

/* Copyright (C) 1996-1997 Id Software, Inc.
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 the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See file, 'COPYING', for details.
*/
// solidbsp.c
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#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 17:14:23 +00:00
int c_solid, c_empty, c_water;
2002-09-19 17:14:23 +00:00
qboolean usemidsplit;
//============================================================================
/*
==================
FaceSide
For BSP hueristic
==================
*/
2002-09-19 17:14:23 +00:00
int
FaceSide (face_t * in, plane_t *split)
{
2002-09-19 17:14:23 +00:00
int frontcount, backcount;
vec_t dot;
int i;
vec_t *p;
frontcount = backcount = 0;
2002-09-19 17:14:23 +00:00
// axial planes are fast
if (split->type < 3)
2002-09-19 17:14:23 +00:00
for (i = 0, p = in->pts[0] + split->type; i < in->numpoints;
i++, p += 3) {
if (*p > split->dist + ON_EPSILON) {
if (backcount)
return SIDE_ON;
frontcount = 1;
2002-09-19 17:14:23 +00:00
} else if (*p < split->dist - ON_EPSILON) {
if (frontcount)
return SIDE_ON;
backcount = 1;
}
2002-09-19 17:14:23 +00:00
} else
// sloping planes take longer
2002-09-19 17:14:23 +00:00
for (i = 0, p = in->pts[0]; i < in->numpoints; i++, p += 3) {
dot = DotProduct (p, split->normal);
dot -= split->dist;
2002-09-19 17:14:23 +00:00
if (dot > ON_EPSILON) {
if (backcount)
return SIDE_ON;
frontcount = 1;
2002-09-19 17:14:23 +00:00
} else if (dot < -ON_EPSILON) {
if (frontcount)
return SIDE_ON;
backcount = 1;
}
}
2002-09-19 17:14:23 +00:00
if (!frontcount)
return SIDE_BACK;
if (!backcount)
return SIDE_FRONT;
2002-09-19 17:14:23 +00:00
return SIDE_ON;
}
/*
==================
ChooseMidPlaneFromList
The clipping hull BSP doesn't worry about avoiding splits
==================
*/
2002-09-19 17:14:23 +00:00
surface_t *
ChooseMidPlaneFromList (surface_t * surfaces, vec3_t mins, vec3_t maxs)
{
2002-09-19 17:14:23 +00:00
int j, l;
surface_t *p, *bestsurface;
vec_t bestvalue, value, dist;
plane_t *plane;
//
// pick the plane that splits the least
//
2002-09-19 17:14:23 +00:00
bestvalue = 6 * 8192 * 8192;
bestsurface = NULL;
2002-09-19 17:14:23 +00:00
for (p = surfaces; p; p = p->next) {
if (p->onnode)
continue;
plane = &planes[p->planenum];
2002-09-19 17:14:23 +00:00
// check for axis aligned surfaces
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
//
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 17:14:23 +00:00
if (value > bestvalue)
continue;
2002-09-19 17:14:23 +00:00
//
// currently the best!
//
bestvalue = value;
bestsurface = p;
}
2002-09-19 17:14:23 +00:00
if (!bestsurface) {
for (p = surfaces; p; p = p->next)
if (!p->onnode)
2002-09-19 17:14:23 +00:00
return p; // first valid surface
Sys_Error ("ChooseMidPlaneFromList: no valid planes");
}
2002-09-19 17:14:23 +00:00
return bestsurface;
}
/*
==================
ChoosePlaneFromList
The real BSP hueristic
==================
*/
2002-09-19 17:14:23 +00:00
surface_t *
ChoosePlaneFromList (surface_t * surfaces, vec3_t mins, vec3_t maxs,
qboolean usefloors)
{
2002-09-19 17:14:23 +00:00
int j, k, l;
surface_t *p, *p2, *bestsurface;
vec_t bestvalue, bestdistribution, value, dist;
plane_t *plane;
face_t *f;
//
// pick the plane that splits the least
//
bestvalue = 99999;
bestsurface = NULL;
bestdistribution = 9e30;
2002-09-19 17:14:23 +00:00
for (p = surfaces; p; p = p->next) {
if (p->onnode)
continue;
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) {
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) {
k++;
if (k >= bestvalue)
break;
}
2002-09-19 17:14:23 +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
l = plane->type;
2002-09-19 17:14:23 +00:00
if (l <= PLANE_Z) { // axial aligned
//
//
// calculate the split metric along axis l
//
value = 0;
2002-09-19 17:14:23 +00:00
for (j = 0; j < 3; j++) {
if (j == l) {
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 17:14:23 +00:00
if (value > bestdistribution && k == bestvalue)
continue;
bestdistribution = value;
}
2002-09-19 17:14:23 +00:00
//
// currently the best!
//
bestvalue = k;
bestsurface = p;
}
}
return bestsurface;
}
/*
==================
SelectPartition
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 17:14:23 +00:00
surface_t *
SelectPartition (surface_t * surfaces)
{
2002-09-19 17:14:23 +00:00
int i, j;
vec3_t mins, maxs;
surface_t *p, *bestsurface;
//
// count onnode surfaces
//
i = 0;
bestsurface = NULL;
2002-09-19 17:14:23 +00:00
for (p = surfaces; p; p = p->next)
if (!p->onnode) {
i++;
bestsurface = p;
}
2002-09-19 17:14:23 +00:00
if (i == 0)
return NULL;
2002-09-19 17:14:23 +00:00
if (i == 1)
return bestsurface; // this is a final split
//
// calculate a bounding box of the entire surfaceset
//
2002-09-19 17:14:23 +00:00
for (i = 0; i < 3; i++) {
mins[i] = 99999;
maxs[i] = -99999;
}
2002-09-19 17:14:23 +00:00
for (p = surfaces; p; p = p->next)
for (j = 0; j < 3; j++) {
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
return ChooseMidPlaneFromList (surfaces, mins, maxs);
2002-09-19 17:14:23 +00:00
// do slow way to save poly splits for drawing hull
#if 0
bestsurface = ChoosePlaneFromList (surfaces, mins, maxs, false);
2002-09-19 17:14:23 +00:00
if (bestsurface)
return bestsurface;
2002-09-19 17:14:23 +00:00
#endif
return ChoosePlaneFromList (surfaces, mins, maxs, true);
}
//============================================================================
/*
=================
CalcSurfaceInfo
Calculates the bounding box
=================
*/
2002-09-19 17:14:23 +00:00
void
CalcSurfaceInfo (surface_t * surf)
{
2002-09-19 17:14:23 +00:00
int i, j;
face_t *f;
if (!surf->faces)
Sys_Error ("CalcSurfaceInfo: surface without a face");
2002-09-19 17:14:23 +00:00
//
// calculate a bounding box
//
2002-09-19 17:14:23 +00:00
for (i = 0; i < 3; i++) {
surf->mins[i] = 99999;
surf->maxs[i] = -99999;
}
2002-09-19 17:14:23 +00:00
for (f = surf->faces; f; f = f->next) {
if (f->contents[0] >= 0 || f->contents[1] >= 0)
Sys_Error ("Bad contents");
for (i = 0; i < f->numpoints; i++)
for (j = 0; j < 3; j++) {
if (f->pts[i][j] < surf->mins[j])
surf->mins[j] = f->pts[i][j];
if (f->pts[i][j] > surf->maxs[j])
surf->maxs[j] = f->pts[i][j];
}
}
}
/*
==================
DividePlane
==================
*/
2002-09-19 17:14:23 +00:00
void
DividePlane (surface_t * in, plane_t *split, surface_t ** front,
surface_t ** back)
{
2002-09-19 17:14:23 +00:00
face_t *facet, *next;
face_t *frontlist, *backlist;
face_t *frontfrag, *backfrag;
surface_t *news;
plane_t *inplane;
inplane = &planes[in->planenum];
2002-09-19 17:14:23 +00:00
// parallel case is easy
2002-09-19 17:14:23 +00:00
if (VectorCompare (inplane->normal, split->normal)) {
// check for exactly on node
2002-09-19 17:14:23 +00:00
if (inplane->dist == split->dist) { // divide the facets to the front
// and back sides
news = AllocSurface ();
*news = *in;
2002-09-19 17:14:23 +00:00
facet = in->faces;
in->faces = NULL;
news->faces = NULL;
in->onnode = news->onnode = true;
2002-09-19 17:14:23 +00:00
for (; facet; facet = next) {
next = facet->next;
2002-09-19 17:14:23 +00:00
if (facet->planeside == 1) {
facet->next = news->faces;
news->faces = facet;
2002-09-19 17:14:23 +00:00
} else {
facet->next = in->faces;
in->faces = facet;
}
}
2002-09-19 17:14:23 +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) {
*front = in;
*back = NULL;
2002-09-19 17:14:23 +00:00
} else {
*front = NULL;
*back = in;
}
return;
}
// do a real split. may still end up entirely on one side
// OPTIMIZE: use bounding box for fast test
frontlist = NULL;
backlist = NULL;
2002-09-19 17:14:23 +00:00
for (facet = in->faces; facet; facet = next) {
next = facet->next;
SplitFace (facet, split, &frontfrag, &backfrag);
2002-09-19 17:14:23 +00:00
if (frontfrag) {
frontfrag->next = frontlist;
frontlist = frontfrag;
}
2002-09-19 17:14:23 +00:00
if (backfrag) {
backfrag->next = backlist;
backlist = backfrag;
}
}
// if nothing actually got split, just move the in plane
2002-09-19 17:14:23 +00:00
if (frontlist == NULL) {
*front = NULL;
*back = in;
in->faces = backlist;
return;
}
2002-09-19 17:14:23 +00:00
if (backlist == NULL) {
*front = in;
*back = NULL;
in->faces = frontlist;
return;
}
// stuff got split, so allocate one new plane and reuse in
news = AllocSurface ();
*news = *in;
news->faces = backlist;
*back = news;
2002-09-19 17:14:23 +00:00
in->faces = frontlist;
*front = in;
2002-09-19 17:14:23 +00:00
// recalc bboxes and flags
CalcSurfaceInfo (news);
2002-09-19 17:14:23 +00:00
CalcSurfaceInfo (in);
}
/*
==================
DivideNodeBounds
==================
*/
2002-09-19 17:14:23 +00:00
void
DivideNodeBounds (node_t * node, plane_t *split)
{
VectorCopy (node->mins, node->children[0]->mins);
VectorCopy (node->mins, node->children[1]->mins);
VectorCopy (node->maxs, node->children[0]->maxs);
VectorCopy (node->maxs, node->children[1]->maxs);
// OPTIMIZE: sloping cuts can give a better bbox than this...
if (split->type > 2)
return;
node->children[0]->mins[split->type] =
2002-09-19 17:14:23 +00:00
node->children[1]->maxs[split->type] = split->dist;
}
/*
==================
LinkConvexFaces
Determines the contents of the leaf and creates the final list of
original faces that have some fragment inside this leaf
==================
*/
2002-09-19 17:14:23 +00:00
void
LinkConvexFaces (surface_t * planelist, node_t * leafnode)
{
2002-09-19 17:14:23 +00:00
face_t *f, *next;
surface_t *surf, *pnext;
int i, count;
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) {
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");
}
//
// write the list of faces, and free the originals
//
leaffaces += count;
2002-09-19 17:14:23 +00:00
leafnode->markfaces = malloc (sizeof (face_t *) * (count + 1));
i = 0;
2002-09-19 17:14:23 +00:00
for (surf = planelist; surf; surf = pnext) {
pnext = surf->next;
2002-09-19 17:14:23 +00:00
for (f = surf->faces; f; f = next) {
next = f->next;
leafnode->markfaces[i] = f->original;
i++;
FreeFace (f);
}
FreeSurface (surf);
}
2002-09-19 17:14:23 +00:00
leafnode->markfaces[i] = NULL; // sentinal
}
/*
==================
LinkNodeFaces
Returns a duplicated list of all faces on surface
==================
*/
2002-09-19 17:14:23 +00:00
face_t *
LinkNodeFaces (surface_t * surface)
{
2002-09-19 17:14:23 +00:00
face_t *f, *new, **prevptr;
face_t *list;
list = NULL;
2002-09-19 17:14:23 +00:00
// subdivide
prevptr = &surface->faces;
2002-09-19 17:14:23 +00:00
while (1) {
f = *prevptr;
if (!f)
break;
SubdivideFace (f, prevptr);
f = *prevptr;
prevptr = &f->next;
}
// copy
2002-09-19 17:14:23 +00:00
for (f = surface->faces; f; f = f->next) {
nodefaces++;
new = AllocFace ();
*new = *f;
f->original = new;
new->next = list;
list = new;
}
return list;
}
/*
==================
PartitionSurfaces
==================
*/
2002-09-19 17:14:23 +00:00
void
PartitionSurfaces (surface_t * surfaces, node_t * node)
{
2002-09-19 17:14:23 +00:00
surface_t *split, *p, *next;
surface_t *frontlist, *backlist;
surface_t *frontfrag, *backfrag;
plane_t *splitplane;
split = SelectPartition (surfaces);
2002-09-19 17:14:23 +00:00
if (!split) { // this is a leaf node
node->planenum = PLANENUM_LEAF;
LinkConvexFaces (surfaces, node);
return;
}
2002-09-19 17:14:23 +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
DivideNodeBounds (node, splitplane);
//
// multiple surfaces, so split all the polysurfaces into front and back lists
//
frontlist = NULL;
backlist = NULL;
2002-09-19 17:14:23 +00:00
for (p = surfaces; p; p = next) {
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
// MergePlaneFaces (frontfrag);
// MergePlaneFaces (backfrag);
}
2002-09-19 17:14:23 +00:00
if (frontfrag) {
if (!frontfrag->faces)
Sys_Error ("surface with no faces");
frontfrag->next = frontlist;
frontlist = frontfrag;
}
2002-09-19 17:14:23 +00:00
if (backfrag) {
if (!backfrag->faces)
Sys_Error ("surface with no faces");
backfrag->next = backlist;
backlist = backfrag;
}
}
PartitionSurfaces (frontlist, node->children[0]);
PartitionSurfaces (backlist, node->children[1]);
}
/*
==================
DrawSurface
==================
*/
2002-09-19 17:14:23 +00:00
void
DrawSurface (surface_t * surf)
{
2002-09-19 17:14:23 +00:00
face_t *f;
for (f = surf->faces; f; f = f->next)
Draw_DrawFace (f);
}
/*
==================
DrawSurfaceList
==================
*/
2002-09-19 17:14:23 +00:00
void
DrawSurfaceList (surface_t * surf)
{
Draw_ClearWindow ();
2002-09-19 17:14:23 +00:00
while (surf) {
DrawSurface (surf);
surf = surf->next;
}
}
/*
==================
SolidBSP
==================
*/
2002-09-19 17:14:23 +00:00
node_t *
SolidBSP (surface_t * surfhead, qboolean midsplit)
{
2002-09-19 17:14:23 +00:00
int i;
node_t *headnode;
qprintf ("----- SolidBSP -----\n");
headnode = AllocNode ();
usemidsplit = midsplit;
2002-09-19 17:14:23 +00:00
//
// calculate a bounding box for the entire model
//
2002-09-19 17:14:23 +00:00
for (i = 0; i < 3; i++) {
headnode->mins[i] = brushset->mins[i] - SIDESPACE;
headnode->maxs[i] = brushset->maxs[i] + SIDESPACE;
}
2002-09-19 17:14:23 +00:00
//
// recursively partition everything
//
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);
qprintf ("%5i nodefaces\n", nodefaces);
2002-09-19 17:14:23 +00:00
return headnode;
}