quakeforge/tools/qfbsp/source/brush.c

814 lines
18 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
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include "QF/sys.h"
#include "QF/va.h"
#include "compat.h"
#include "tools/qfbsp/include/brush.h"
#include "tools/qfbsp/include/bsp5.h"
#include "tools/qfbsp/include/draw.h"
#include "tools/qfbsp/include/options.h"
#include "tools/qfbsp/include/surfaces.h"
/** \addtogroup qfbsp_brush
*/
//@{
planeset_t planes = DARRAY_STATIC_INIT (1024);
2002-09-19 17:14:23 +00:00
int numbrushfaces;
mface_t faces[MAX_FACES]; // beveled clipping hull can generate many extra
static entity_t *CurrentEntity;
brush_t *
AllocBrush (void)
{
brush_t *b;
b = malloc (sizeof (brush_t));
memset (b, 0, sizeof (brush_t));
return b;
}
2010-10-12 10:33:27 +00:00
/** Check the face for validity.
\param f The face to check.
2010-10-12 10:33:27 +00:00
\note Does not catch 0 area polygons.
*/
static void
2010-09-02 04:01:45 +00:00
CheckFace (const face_t *f)
{
2002-09-19 18:51:19 +00:00
int i, j;
vec_t *p1, *p2;
vec_t d, edgedist;
vec3_t dir, edgenormal, facenormal;
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
if (f->points->numpoints < 3)
Sys_Error ("CheckFace: %i points", f->points->numpoints);
2002-09-19 17:14:23 +00:00
VectorCopy (planes.a[f->planenum].normal, facenormal);
2002-09-19 18:51:19 +00:00
if (f->planeside)
VectorNegate (facenormal, facenormal);
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
for (i = 0; i < f->points->numpoints; i++) {
p1 = f->points->points[i];
2002-09-19 17:14:23 +00:00
for (j = 0; j < 3; j++)
if (p1[j] > BOGUS_RANGE || p1[j] < -BOGUS_RANGE)
2002-09-19 17:14:23 +00:00
Sys_Error ("CheckFace: BUGUS_RANGE: %f", p1[j]);
2003-09-08 03:00:53 +00:00
j = i + 1 == f->points->numpoints ? 0 : i + 1;
2002-09-19 17:14:23 +00:00
// check the point is on the face plane
d = PlaneDiff (p1, &planes.a[f->planenum]);
2002-09-19 18:51:19 +00:00
// point off plane autofix
if (d < -ON_EPSILON || d > ON_EPSILON)
if (options.verbosity > 1)
printf ("CheckFace: point off plane: %g @ (%g %g %g)\n", d,
p1[0], p1[1], p1[2]);
VectorMultSub (p1, d, planes.a[f->planenum].normal, p1);
2002-09-19 17:14:23 +00:00
// check the edge isn't degenerate
2003-09-08 03:00:53 +00:00
p2 = f->points->points[j];
VectorSubtract (p2, p1, dir);
2002-09-19 17:14:23 +00:00
if (VectorLength (dir) < ON_EPSILON)
Sys_Error ("CheckFace: degenerate edge");
2002-09-19 17:14:23 +00:00
CrossProduct (facenormal, dir, edgenormal);
_VectorNormalize (edgenormal);
edgedist = DotProduct (p1, edgenormal);
edgedist += ON_EPSILON;
2002-09-19 17:14:23 +00:00
// all other points must be on front side
2003-09-08 03:00:53 +00:00
for (j = 0; j < f->points->numpoints; j++) {
if (j == i)
continue;
2003-09-08 03:00:53 +00:00
d = DotProduct (f->points->points[j], edgenormal);
if (d > edgedist)
Sys_Error ("CheckFace: non-convex");
}
}
}
2010-10-12 10:33:27 +00:00
/** Initialize the bounding box of the brush set.
\param bs The brush set of which to initialize the bounding box.
*/
static void
2002-09-19 18:51:19 +00:00
ClearBounds (brushset_t *bs)
{
2002-09-19 18:51:19 +00:00
int i, j;
2002-09-19 17:14:23 +00:00
2010-10-12 10:33:27 +00:00
for (j = 0; j < NUM_HULLS; j++) {
2002-09-19 17:14:23 +00:00
for (i = 0; i < 3; i++) {
bs->mins[i] = BOGUS_RANGE;
bs->maxs[i] = -BOGUS_RANGE;
}
2010-10-12 10:33:27 +00:00
}
}
2010-10-12 10:33:27 +00:00
/** Grow the bounding box of the brush set to include the vector.
\param bs The brush set of which to grown the bounding box.
\param v The vector to be included in the bounding box.
*/
static void
2010-09-02 04:01:45 +00:00
AddToBounds (brushset_t *bs, const vec3_t v)
{
2002-09-19 18:51:19 +00:00
int i;
2002-09-19 17:14:23 +00:00
for (i = 0; i < 3; i++) {
if (v[i] < bs->mins[i])
bs->mins[i] = v[i];
if (v[i] > bs->maxs[i])
bs->maxs[i] = v[i];
}
}
2002-09-19 17:14:23 +00:00
int
2010-08-27 12:38:09 +00:00
PlaneTypeForNormal (const vec3_t normal)
{
2002-09-19 18:51:19 +00:00
float ax, ay, az;
int type;
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// NOTE: should these have an epsilon around 1.0?
if (normal[0] == 1.0)
return PLANE_X;
if (normal[1] == 1.0)
return PLANE_Y;
if (normal[2] == 1.0)
return PLANE_Z;
2002-09-19 17:14:23 +00:00
if (normal[0] == -1.0 || normal[1] == -1.0 || normal[2] == -1.0)
Sys_Error ("PlaneTypeForNormal: not a canonical vector (%g %g %g)",
normal[0], normal[1], normal[2]);
2002-09-19 17:14:23 +00:00
2002-09-19 18:51:19 +00:00
ax = fabs(normal[0]);
ay = fabs(normal[1]);
az = fabs(normal[2]);
2002-09-19 17:14:23 +00:00
if (az >= ax && az >= ay)
type = PLANE_ANYZ;
else if (ay >= ax && ay >= az)
type = PLANE_ANYY;
else
type = PLANE_ANYX;
if (normal[type - PLANE_ANYX] < 0)
Sys_Error ("PlaneTypeForNormal: not a canonical vector (%g %g %g) %d",
normal[0], normal[1], normal[2], type);
return type;
}
#define DISTEPSILON 0.01
#define ANGLEEPSILON 0.00001
int
2002-09-19 17:14:23 +00:00
NormalizePlane (plane_t *dp)
{
vec_t ax, ay, az;
int flip = 0;
2002-09-19 17:14:23 +00:00
// Make axis aligned planes point to +inf.
2002-09-19 17:14:23 +00:00
if (dp->normal[0] == -1.0) {
dp->normal[0] = 1.0;
dp->dist = -dp->dist;
flip = 1;
} else if (dp->normal[1] == -1.0) {
dp->normal[1] = 1.0;
dp->dist = -dp->dist;
flip = 1;
} else if (dp->normal[2] == -1.0) {
dp->normal[2] = 1.0;
dp->dist = -dp->dist;
flip = 1;
}
// For axis aligned planes, set the plane type and ensure the normal
// vector is mathematically correct.
2002-09-19 17:14:23 +00:00
if (dp->normal[0] == 1.0) {
dp->type = PLANE_X;
dp->normal[1] = dp->normal[2] = 0.0;
return flip;
}
2002-09-19 17:14:23 +00:00
if (dp->normal[1] == 1.0) {
dp->type = PLANE_Y;
dp->normal[0] = dp->normal[2] = 0.0;
return flip;
}
2002-09-19 17:14:23 +00:00
if (dp->normal[2] == 1.0) {
dp->type = PLANE_Z;
dp->normal[0] = dp->normal[1] = 0.0;
return flip;
}
// Find out with which axis the plane is most aligned.
2002-09-19 17:14:23 +00:00
ax = fabs (dp->normal[0]);
ay = fabs (dp->normal[1]);
az = fabs (dp->normal[2]);
if (az >= ax && az >= ay)
dp->type = PLANE_ANYZ;
else if (ay >= ax && ay >= az)
dp->type = PLANE_ANYY;
else
dp->type = PLANE_ANYX;
// Make the plane's normal point towards +inf along its primary axis.
2002-09-19 17:14:23 +00:00
if (dp->normal[dp->type - PLANE_ANYX] < 0) {
VectorNegate (dp->normal, dp->normal);
dp->dist = -dp->dist;
flip = 1;
}
return flip;
}
2002-09-19 17:14:23 +00:00
int
2010-09-02 04:01:45 +00:00
FindPlane (const plane_t *dplane, int *side)
{
2002-09-19 18:51:19 +00:00
plane_t *dp, pl;
vec_t dot;
2002-09-19 17:14:23 +00:00
2002-09-19 18:51:19 +00:00
dot = VectorLength(dplane->normal);
if (dot < 1.0 - ANGLEEPSILON || dot > 1.0 + ANGLEEPSILON)
Sys_Error ("FindPlane: normalization error");
2002-09-19 17:14:23 +00:00
pl = *dplane;
NormalizePlane (&pl);
2002-09-19 17:14:23 +00:00
if (DotProduct (pl.normal, dplane->normal) > 0)
*side = 0;
else
*side = 1;
dp = planes.a;
for (size_t i = 0; i < planes.size; i++, dp++) {
dot = DotProduct (dp->normal, pl.normal);
2010-08-27 13:12:13 +00:00
if (dot > 1.0 - ANGLEEPSILON
2002-09-19 18:51:19 +00:00
&& fabs(dp->dist - pl.dist) < DISTEPSILON) { // regular match
2002-09-19 17:14:23 +00:00
return i;
}
}
DARRAY_APPEND (&planes, pl);
return planes.size - 1;
}
/*
2002-09-23 16:27:17 +00:00
Turn brushes into groups of faces.
*/
2002-09-19 17:14:23 +00:00
vec3_t brush_mins, brush_maxs;
face_t *brush_faces;
2010-10-12 10:33:27 +00:00
/** Find the entity with the matching target name.
\param targetname The target name for which to search.
\return The matching entity or NULL if not found.
*/
static entity_t *
FindTargetEntity (const char *targetname)
{
int entnum;
for (entnum = 0; entnum < num_entities; entnum++)
if (!strcmp (targetname,
ValueForKey (&entities[entnum], "targetname")))
return &entities[entnum];
return 0;
}
#define ZERO_EPSILON 0.001
2010-10-12 10:33:27 +00:00
/** Create the faces of the active brush.
*/
static void
2002-09-19 17:14:23 +00:00
CreateBrushFaces (void)
{
2002-09-19 18:51:19 +00:00
face_t *f;
int i, j, k, rotate;
2002-09-19 18:51:19 +00:00
mface_t *mf;
plane_t plane;
2002-09-19 17:14:23 +00:00
vec_t r;
winding_t *w;
vec3_t offset, point;
2002-09-19 17:14:23 +00:00
VectorZero (offset);
brush_mins[0] = brush_mins[1] = brush_mins[2] = BOGUS_RANGE;
brush_maxs[0] = brush_maxs[1] = brush_maxs[2] = -BOGUS_RANGE;
brush_faces = NULL;
2002-09-19 17:14:23 +00:00
rotate = !strncmp (ValueForKey (CurrentEntity, "classname"), "rotate_", 7);
if (rotate) {
entity_t *FoundEntity;
const char *searchstring;
searchstring = ValueForKey (CurrentEntity, "target");
FoundEntity = FindTargetEntity (searchstring);
if (FoundEntity)
GetVectorForKey (FoundEntity, "origin", offset);
SetKeyValue (CurrentEntity, "origin",
va (0, "%g %g %g", VectorExpand (offset)));
}
2002-09-19 17:14:23 +00:00
for (i = 0; i < numbrushfaces; i++) {
mf = &faces[i];
2002-09-19 17:14:23 +00:00
w = BaseWindingForPlane (&mf->plane);
2002-09-19 17:14:23 +00:00
for (j = 0; j < numbrushfaces && w; j++) {
if (j == i)
continue;
2002-09-19 17:14:23 +00:00
// flip the plane, because we want to keep the back side
VectorNegate (faces[j].plane.normal, plane.normal);
plane.dist = -faces[j].plane.dist;
2002-09-19 17:14:23 +00:00
// keepon was false. avoid clipping away windings on plane?
w = ClipWinding (w, &plane, true);
}
2002-09-19 17:14:23 +00:00
if (!w)
2003-09-08 03:00:53 +00:00
continue; // overconstrained plane
2002-09-19 17:14:23 +00:00
// this face is a keeper
f = AllocFace ();
2003-09-08 03:00:53 +00:00
f->points = w;
2002-09-19 17:14:23 +00:00
for (j = 0; j < w->numpoints; j++) {
2003-09-08 03:00:53 +00:00
vec_t *v = f->points->points[j];
VectorSubtract (v, offset, v);
2002-09-19 17:14:23 +00:00
for (k = 0; k < 3; k++) {
2003-09-08 03:00:53 +00:00
r = RINT (v[k]);
if (fabs (v[k] - r) < ZERO_EPSILON)
v[k] = r;
if (v[k] < brush_mins[k])
brush_mins[k] = v[k];
if (v[k] > brush_maxs[k])
brush_maxs[k] = v[k];
}
2002-09-19 17:14:23 +00:00
}
VectorCopy (mf->plane.normal, plane.normal);
VectorScale (mf->plane.normal, mf->plane.dist, point);
VectorSubtract (point, offset, point);
plane.dist = DotProduct (plane.normal, point);
f->texturenum = mf->texinfo;
f->planenum = FindPlane (&plane, &f->planeside);
f->next = brush_faces;
brush_faces = f;
CheckFace (f);
2002-09-19 17:14:23 +00:00
}
// Rotatable objects have to have a bounding box big enough
// to account for all its rotations.
if (rotate) {
vec_t delta, min, max;
min = brush_mins[0];
if (min > brush_mins[1])
min = brush_mins[1];
if (min > brush_mins[2])
min = brush_mins[2];
max = brush_maxs[0];
if (max < brush_maxs[1])
max = brush_maxs[1];
if (max < brush_maxs[2])
max = brush_maxs[2];
delta = fabs(max);
if (fabs(min) > delta)
delta = fabs(min);
for (k = 0; k < 3; k++) {
brush_mins[k] = -delta;
brush_maxs[k] = delta;
}
}
}
/*
2002-09-23 16:27:17 +00:00
BEVELED CLIPPING HULL GENERATION
2002-09-23 16:27:17 +00:00
This is done by brute force, and could easily get a lot faster if anyone
cares.
*/
2002-09-19 17:14:23 +00:00
vec3_t hull_size[3][2] = {
{{0, 0, 0}, {0, 0, 0}},
{{-16, -16, -32}, {16, 16, 24}},
{{-32, -32, -64}, {32, 32, 24}}
};
#define MAX_HULL_POINTS 256
#define MAX_HULL_EDGES MAX_HULL_POINTS * 2
2002-09-19 17:14:23 +00:00
int num_hull_points;
vec3_t hull_points[MAX_HULL_POINTS];
vec3_t hull_corners[MAX_HULL_POINTS * 8];
int num_hull_edges;
int hull_edges[MAX_HULL_EDGES][2];
static void
2010-09-02 04:01:45 +00:00
AddBrushPlane (const plane_t *plane)
{
2002-09-19 18:51:19 +00:00
float l;
2002-09-19 17:14:23 +00:00
int i;
plane_t *pl;
if (numbrushfaces == MAX_FACES)
Sys_Error ("AddBrushPlane: numbrushfaces == MAX_FACES");
l = VectorLength (plane->normal);
if (l < 0.999 || l > 1.001)
Sys_Error ("AddBrushPlane: bad normal");
2002-09-19 17:14:23 +00:00
for (i = 0; i < numbrushfaces; i++) {
pl = &faces[i].plane;
if (_VectorCompare (pl->normal, plane->normal)
2002-09-19 17:14:23 +00:00
&& fabs (pl->dist - plane->dist) < ON_EPSILON)
return;
}
faces[i].plane = *plane;
faces[i].texinfo = faces[0].texinfo;
numbrushfaces++;
}
/*
2002-09-23 16:27:17 +00:00
TestAddPlane
2002-09-23 16:27:17 +00:00
Adds the given plane to the brush description if all of the original brush
vertexes can be put on the front side
*/
static void
2010-09-02 04:01:45 +00:00
TestAddPlane (const plane_t *plane)
{
2002-09-19 18:51:19 +00:00
int c, i;
int counts[3];
plane_t flip;
plane_t *pl;
2002-09-19 17:14:23 +00:00
vec_t d;
vec_t *corner;
vec3_t inv;
2002-09-23 16:27:17 +00:00
// see if the plane has allready been added
2002-09-19 17:14:23 +00:00
for (i = 0; i < numbrushfaces; i++) {
pl = &faces[i].plane;
if (_VectorCompare (plane->normal, pl->normal)
2002-09-19 17:14:23 +00:00
&& fabs (plane->dist - pl->dist) < ON_EPSILON)
return;
VectorNegate (plane->normal, inv);
if (_VectorCompare (inv, pl->normal)
2002-09-19 17:14:23 +00:00
&& fabs (plane->dist + pl->dist) < ON_EPSILON) return;
}
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// check all the corner points
counts[0] = counts[1] = counts[2] = 0;
c = num_hull_points * 8;
2002-09-19 17:14:23 +00:00
corner = hull_corners[0];
2002-09-19 17:14:23 +00:00
for (i = 0; i < c; i++, corner += 3) {
d = DotProduct (corner, plane->normal) - plane->dist;
2002-09-19 17:14:23 +00:00
if (d < -ON_EPSILON) {
if (counts[0])
return;
counts[1]++;
2002-09-19 17:14:23 +00:00
} else if (d > ON_EPSILON) {
if (counts[1])
return;
counts[0]++;
2002-09-19 17:14:23 +00:00
} else
counts[2]++;
}
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// the plane is a seperator
2002-09-19 17:14:23 +00:00
if (counts[0]) {
VectorNegate (plane->normal, flip.normal);
flip.dist = -plane->dist;
plane = &flip;
}
2002-09-19 17:14:23 +00:00
AddBrushPlane (plane);
}
/*
2002-09-23 16:27:17 +00:00
AddHullPoint
2002-09-23 16:27:17 +00:00
Doesn't add if duplicated
*/
static int
2010-09-02 04:01:45 +00:00
AddHullPoint (const vec3_t p, int hullnum)
{
2002-09-19 18:51:19 +00:00
int i, x, y, z;
vec_t *c;
2002-09-19 17:14:23 +00:00
for (i = 0; i < num_hull_points; i++)
if (_VectorCompare (p, hull_points[i]))
return i;
2002-09-19 17:14:23 +00:00
VectorCopy (p, hull_points[num_hull_points]);
2002-09-19 17:14:23 +00:00
c = hull_corners[i * 8];
for (x = 0; x < 2; x++)
for (y = 0; y < 2; y++)
for (z = 0; z < 2; z++) {
c[0] = p[0] + hull_size[hullnum][x][0];
c[1] = p[1] + hull_size[hullnum][y][1];
c[2] = p[2] + hull_size[hullnum][z][2];
c += 3;
}
2002-09-19 17:14:23 +00:00
if (num_hull_points == MAX_HULL_POINTS)
Sys_Error ("MAX_HULL_POINTS");
num_hull_points++;
2002-09-19 17:14:23 +00:00
return i;
}
/*
2002-09-23 16:27:17 +00:00
AddHullEdge
2002-09-23 16:27:17 +00:00
Creates all of the hull planes around the given edge, if not done already
*/
static void
2010-09-02 04:01:45 +00:00
AddHullEdge (const vec3_t p1, const vec3_t p2, int hullnum)
{
2002-09-19 18:51:19 +00:00
int pt1, pt2, a, b, c, d, e, i;
2002-09-19 17:14:23 +00:00
plane_t plane;
2002-09-19 18:51:19 +00:00
vec3_t edgevec, planeorg, planevec;
2002-09-19 17:14:23 +00:00
vec_t l;
pt1 = AddHullPoint (p1, hullnum);
pt2 = AddHullPoint (p2, hullnum);
2002-09-19 17:14:23 +00:00
for (i = 0; i < num_hull_edges; i++)
if ((hull_edges[i][0] == pt1 && hull_edges[i][1] == pt2)
|| (hull_edges[i][0] == pt2 && hull_edges[i][1] == pt1))
2002-09-19 18:51:19 +00:00
return; // allready added
2002-09-19 17:14:23 +00:00
if (num_hull_edges == MAX_HULL_EDGES)
Sys_Error ("MAX_HULL_EDGES");
hull_edges[i][0] = pt1;
hull_edges[i][1] = pt2;
num_hull_edges++;
2002-09-19 17:14:23 +00:00
VectorSubtract (p1, p2, edgevec);
_VectorNormalize (edgevec);
2002-09-19 17:14:23 +00:00
for (a = 0; a < 3; a++) {
b = (a + 1) % 3;
c = (a + 2) % 3;
for (d = 0; d <= 1; d++)
for (e = 0; e <= 1; e++) {
VectorCopy (p1, planeorg);
planeorg[b] += hull_size[hullnum][d][b];
planeorg[c] += hull_size[hullnum][e][c];
2002-09-19 17:14:23 +00:00
VectorZero (planevec);
planevec[a] = 1;
2002-09-19 17:14:23 +00:00
CrossProduct (planevec, edgevec, plane.normal);
l = VectorLength (plane.normal);
2002-09-19 17:14:23 +00:00
if (l < 1 - ANGLEEPSILON || l > 1 + ANGLEEPSILON)
continue;
2002-09-19 17:14:23 +00:00
plane.dist = DotProduct (planeorg, plane.normal);
TestAddPlane (&plane);
}
}
2002-09-19 17:14:23 +00:00
}
static void
2002-09-19 17:14:23 +00:00
ExpandBrush (int hullnum)
{
2002-09-19 17:14:23 +00:00
face_t *f;
2002-09-19 18:51:19 +00:00
int i, x, s;
2002-09-19 17:14:23 +00:00
plane_t plane, *p;
2002-09-19 18:51:19 +00:00
vec3_t corner;
num_hull_points = 0;
num_hull_edges = 0;
2002-09-23 16:27:17 +00:00
// create all the hull points
2002-09-19 17:14:23 +00:00
for (f = brush_faces; f; f = f->next)
2003-09-08 03:00:53 +00:00
for (i = 0; i < f->points->numpoints; i++)
AddHullPoint (f->points->points[i], hullnum);
2002-09-23 16:27:17 +00:00
// expand all of the planes
2002-09-19 17:14:23 +00:00
for (i = 0; i < numbrushfaces; i++) {
p = &faces[i].plane;
VectorZero (corner);
2002-09-19 17:14:23 +00:00
for (x = 0; x < 3; x++) {
if (p->normal[x] > 0)
corner[x] = hull_size[hullnum][1][x];
else if (p->normal[x] < 0)
corner[x] = hull_size[hullnum][0][x];
}
2002-09-19 17:14:23 +00:00
p->dist += DotProduct (corner, p->normal);
}
2002-09-23 16:27:17 +00:00
// add any axis planes not contained in the brush to bevel off corners
2002-09-19 17:14:23 +00:00
for (x = 0; x < 3; x++)
for (s = -1; s <= 1; s += 2) {
// add the plane
VectorZero (plane.normal);
plane.normal[x] = s;
if (s == -1)
plane.dist = -brush_mins[x] + -hull_size[hullnum][0][x];
else
plane.dist = brush_maxs[x] + hull_size[hullnum][1][x];
AddBrushPlane (&plane);
}
2002-09-23 16:27:17 +00:00
// add all of the edge bevels
2002-09-19 17:14:23 +00:00
for (f = brush_faces; f; f = f->next)
2003-09-08 03:00:53 +00:00
for (i = 0; i < f->points->numpoints; i++)
AddHullEdge (f->points->points[i],
f->points->points[(i + 1) % f->points->numpoints],
hullnum);
}
/*
2002-09-23 16:27:17 +00:00
LoadBrush
2002-09-23 16:27:17 +00:00
Converts a mapbrush to a bsp brush
*/
2010-08-27 13:12:13 +00:00
static brush_t *
2010-09-02 04:01:45 +00:00
LoadBrush (const mbrush_t *mb, int hullnum)
{
2002-09-19 17:14:23 +00:00
brush_t *b;
2010-09-02 04:01:45 +00:00
const char *name;
2002-09-19 18:51:19 +00:00
int contents;
2010-09-02 04:01:45 +00:00
const mface_t *f;
2002-09-23 16:27:17 +00:00
// check texture name for attributes
if (mb->faces->texinfo < 0) {
// ignore HINT and SKIP in clip hulls
if (hullnum)
return NULL;
contents = CONTENTS_EMPTY;
} else {
name = miptexnames[bsp->texinfo[mb->faces->texinfo].miptex];
if (!strcasecmp (name, "clip") && hullnum == 0)
return NULL; // "clip" brushes don't show up in the draw hull
if (name[0] == '*' && worldmodel) { // entities never use water merging
if (!strncasecmp (name + 1, "lava", 4))
contents = CONTENTS_LAVA;
else if (!strncasecmp (name + 1, "slime", 5))
contents = CONTENTS_SLIME;
else
contents = CONTENTS_WATER;
} else if (!strncasecmp (name, "sky", 3) && worldmodel && hullnum == 0)
contents = CONTENTS_SKY;
2002-09-19 17:14:23 +00:00
else
contents = CONTENTS_SOLID;
if (hullnum && contents != CONTENTS_SOLID && contents != CONTENTS_SKY)
return NULL; // water brushes don't show up in clipping hulls
// no seperate textures on clip hull
}
2002-09-23 16:27:17 +00:00
// create the faces
brush_faces = NULL;
2002-09-19 17:14:23 +00:00
numbrushfaces = 0;
2002-09-19 17:14:23 +00:00
for (f = mb->faces; f; f = f->next) {
faces[numbrushfaces] = *f;
if (hullnum)
faces[numbrushfaces].texinfo = 0;
numbrushfaces++;
}
2002-09-19 17:14:23 +00:00
CreateBrushFaces ();
2002-09-19 17:14:23 +00:00
if (!brush_faces) {
printf ("WARNING: couldn't create brush faces\n");
return NULL;
}
2002-09-19 17:14:23 +00:00
if (hullnum) {
ExpandBrush (hullnum);
CreateBrushFaces ();
} else if (mb->detail) {
face_t *f;
2016-10-24 15:10:30 +00:00
for (f = brush_faces; f; f = f->next)
f->detail = 1;
}
2002-09-19 18:51:19 +00:00
2002-09-23 16:27:17 +00:00
// create the brush
b = AllocBrush ();
2002-09-19 17:14:23 +00:00
b->contents = contents;
b->faces = brush_faces;
VectorCopy (brush_mins, b->mins);
VectorCopy (brush_maxs, b->maxs);
return b;
}
static void
2010-09-02 04:01:45 +00:00
Brush_DrawAll (const brushset_t *bs)
{
2002-09-19 17:14:23 +00:00
brush_t *b;
face_t *f;
2002-09-19 17:14:23 +00:00
for (b = bs->brushes; b; b = b->next)
for (f = b->faces; f; f = f->next)
Draw_DrawFace (f);
}
2002-09-19 17:14:23 +00:00
brushset_t *
Brush_LoadEntity (entity_t *ent, int hullnum)
{
2002-09-19 17:14:23 +00:00
brush_t *b, *next, *water, *other;
brushset_t *bset;
2002-09-19 18:51:19 +00:00
int numbrushes;
mbrush_t *mbr;
2002-09-19 17:14:23 +00:00
bset = malloc (sizeof (brushset_t));
memset (bset, 0, sizeof (brushset_t));
ClearBounds (bset);
numbrushes = 0;
other = water = NULL;
qprintf ("--- Brush_LoadEntity ---\n");
CurrentEntity = ent;
2002-09-19 17:14:23 +00:00
for (mbr = ent->brushes; mbr; mbr = mbr->next) {
b = LoadBrush (mbr, hullnum);
if (!b)
continue;
2002-09-19 17:14:23 +00:00
numbrushes++;
2002-09-19 17:14:23 +00:00
if (b->contents != CONTENTS_SOLID) {
b->next = water;
water = b;
2002-09-19 17:14:23 +00:00
} else {
b->next = other;
other = b;
}
2002-09-19 17:14:23 +00:00
AddToBounds (bset, b->mins);
AddToBounds (bset, b->maxs);
}
2002-09-23 16:27:17 +00:00
// add all of the water textures at the start
2002-09-19 17:14:23 +00:00
for (b = water; b; b = next) {
next = b->next;
b->next = other;
other = b;
}
bset->brushes = other;
brushset = bset;
Brush_DrawAll (bset);
2002-09-19 17:14:23 +00:00
qprintf ("%i brushes read\n", numbrushes);
2002-09-19 17:14:23 +00:00
return bset;
}
//@}