world: move the shared 'box hull' onto the stack

We initialise the shared clipnodes statically and only provide const
pointers to them. We then provide a const template for the planes and
hull which can be quickly memcpy'd and just the planes pointer + plane
dists updated.

Signed-off-by: Kevin Shanahan <kmshanah@disenchant.net>
This commit is contained in:
Kevin Shanahan 2013-03-19 14:57:34 +10:30
parent cf33822658
commit 4a76fa3f79

View File

@ -68,63 +68,59 @@ HULL BOXES
===============================================================================
*/
static const mclipnode_t box_clipnodes[6] = {
{ .planenum = 0, .children = { CONTENTS_EMPTY, 1 } },
{ .planenum = 1, .children = { 2, CONTENTS_EMPTY } },
{ .planenum = 2, .children = { CONTENTS_EMPTY, 3 } },
{ .planenum = 3, .children = { 4, CONTENTS_EMPTY } },
{ .planenum = 4, .children = { CONTENTS_EMPTY, 5 } },
{ .planenum = 5, .children = { CONTENTS_SOLID, CONTENTS_EMPTY } }
};
typedef struct {
hull_t hull;
mplane_t planes[6];
} boxhull_t;
static const boxhull_t boxhull_template = {
.hull = {
.clipnodes = box_clipnodes,
.firstclipnode = 0,
.lastclipnode = 5
},
.planes = {
{ .normal = { 1, 0, 0 }, .dist = 0, .type = 0 },
{ .normal = { 1, 0, 0 }, .dist = 0, .type = 0 },
{ .normal = { 0, 1, 0 }, .dist = 0, .type = 1 },
{ .normal = { 0, 1, 0 }, .dist = 0, .type = 1 },
{ .normal = { 0, 0, 1 }, .dist = 0, .type = 2 },
{ .normal = { 0, 0, 1 }, .dist = 0, .type = 2 }
}
};
/*
===================
SV_InitBoxHull
Set up the planes and clipnodes so that the six floats of a bounding box
can just be stored out and get a proper hull_t structure.
To keep everything totally uniform, bounding boxes are turned into small
BSP trees instead of being compared directly.
Set up the planes and clipnodes using the template so that the six floats
of a bounding box can just be stored out and get a proper hull_t structure.
===================
*/
static void
SV_InitBoxHull(hull_t *hull, mplane_t planes[6], mclipnode_t clipnodes[6])
SV_InitBoxhull(const vec3_t mins, const vec3_t maxs, boxhull_t *boxhull)
{
int i, side;
memcpy(boxhull, &boxhull_template, sizeof(boxhull_template));
hull->clipnodes = clipnodes;
hull->planes = planes;
hull->firstclipnode = 0;
hull->lastclipnode = 5;
for (i = 0; i < 6; i++) {
side = i & 1;
clipnodes[i].planenum = i;
clipnodes[i].children[side] = CONTENTS_EMPTY;
clipnodes[i].children[side ^ 1] = (i < 5) ? i + 1 : CONTENTS_SOLID;
planes[i].normal[i >> 1] = 1;
planes[i].type = i >> 1;
}
}
/*
===================
SV_HullForBox
To keep everything totally uniform, bounding boxes are turned into small
BSP trees instead of being compared directly.
===================
*/
static const hull_t *
SV_HullForBox(vec3_t mins, vec3_t maxs)
{
static qboolean initialised = false;
static hull_t box_hull;
static mplane_t box_planes[6];
static mclipnode_t box_clipnodes[6];
if (!initialised) {
SV_InitBoxHull(&box_hull, box_planes, box_clipnodes);
initialised = true;
}
box_planes[0].dist = maxs[0];
box_planes[1].dist = mins[0];
box_planes[2].dist = maxs[1];
box_planes[3].dist = mins[1];
box_planes[4].dist = maxs[2];
box_planes[5].dist = mins[2];
return &box_hull;
boxhull->hull.planes = boxhull->planes;
boxhull->planes[0].dist = maxs[0];
boxhull->planes[1].dist = mins[0];
boxhull->planes[2].dist = maxs[1];
boxhull->planes[3].dist = mins[1];
boxhull->planes[4].dist = maxs[2];
boxhull->planes[5].dist = mins[2];
}
@ -141,7 +137,7 @@ testing object's origin to get a point to use with the returned hull.
*/
static const hull_t *
SV_HullForEntity(const edict_t *ent, const vec3_t mins, const vec3_t maxs,
vec3_t offset)
vec3_t offset, boxhull_t *boxhull)
{
const model_t *model;
const hull_t *hull;
@ -172,7 +168,8 @@ SV_HullForEntity(const edict_t *ent, const vec3_t mins, const vec3_t maxs,
/* create a temp hull from bounding box sizes */
VectorSubtract(ent->v.mins, maxs, hullmins);
VectorSubtract(ent->v.maxs, mins, hullmaxs);
hull = SV_HullForBox(hullmins, hullmaxs);
SV_InitBoxhull(hullmins, hullmaxs, boxhull);
hull = &boxhull->hull;
VectorCopy(ent->v.origin, offset);
}
@ -595,6 +592,7 @@ static void
SV_ClipToEntity(const edict_t *ent, const vec3_t start, const vec3_t mins,
const vec3_t maxs, const vec3_t end, trace_t *trace)
{
boxhull_t boxhull;
const hull_t *hull;
vec3_t offset;
vec3_t start_l, end_l;
@ -606,7 +604,7 @@ SV_ClipToEntity(const edict_t *ent, const vec3_t start, const vec3_t mins,
VectorCopy(end, trace->endpos);
/* get the clipping hull */
hull = SV_HullForEntity(ent, mins, maxs, offset);
hull = SV_HullForEntity(ent, mins, maxs, offset, &boxhull);
VectorSubtract(start, offset, start_l);
VectorSubtract(end, offset, end_l);