📄 cmodel.c
字号:
if (!buf)
Com_Error (ERR_DROP, "Couldn't load %s", name);
last_checksum = LittleLong (Com_BlockChecksum (buf, length));
*checksum = last_checksum;
header = *(dheader_t *)buf;
for (i=0 ; i<sizeof(dheader_t)/4 ; i++)
((int *)&header)[i] = LittleLong ( ((int *)&header)[i]);
if (header.version != BSPVERSION)
Com_Error (ERR_DROP, "CMod_LoadBrushModel: %s has wrong version number (%i should be %i)"
, name, header.version, BSPVERSION);
cmod_base = (byte *)buf;
// load into heap
CMod_LoadSurfaces (&header.lumps[LUMP_TEXINFO]);
CMod_LoadLeafs (&header.lumps[LUMP_LEAFS]);
CMod_LoadLeafBrushes (&header.lumps[LUMP_LEAFBRUSHES]);
CMod_LoadPlanes (&header.lumps[LUMP_PLANES]);
CMod_LoadBrushes (&header.lumps[LUMP_BRUSHES]);
CMod_LoadBrushSides (&header.lumps[LUMP_BRUSHSIDES]);
CMod_LoadSubmodels (&header.lumps[LUMP_MODELS]);
CMod_LoadNodes (&header.lumps[LUMP_NODES]);
CMod_LoadAreas (&header.lumps[LUMP_AREAS]);
CMod_LoadAreaPortals (&header.lumps[LUMP_AREAPORTALS]);
CMod_LoadVisibility (&header.lumps[LUMP_VISIBILITY]);
CMod_LoadEntityString (&header.lumps[LUMP_ENTITIES]);
FS_FreeFile (buf);
CM_InitBoxHull ();
memset (portalopen, 0, sizeof(portalopen));
FloodAreaConnections ();
strcpy (map_name, name);
return &map_cmodels[0];
}
/*
==================
CM_InlineModel
==================
*/
cmodel_t *CM_InlineModel (char *name)
{
int num;
if (!name || name[0] != '*')
Com_Error (ERR_DROP, "CM_InlineModel: bad name");
num = atoi (name+1);
if (num < 1 || num >= numcmodels)
Com_Error (ERR_DROP, "CM_InlineModel: bad number");
return &map_cmodels[num];
}
int CM_NumClusters (void)
{
return numclusters;
}
int CM_NumInlineModels (void)
{
return numcmodels;
}
char *CM_EntityString (void)
{
return map_entitystring;
}
int CM_LeafContents (int leafnum)
{
if (leafnum < 0 || leafnum >= numleafs)
Com_Error (ERR_DROP, "CM_LeafContents: bad number");
return map_leafs[leafnum].contents;
}
int CM_LeafCluster (int leafnum)
{
if (leafnum < 0 || leafnum >= numleafs)
Com_Error (ERR_DROP, "CM_LeafCluster: bad number");
return map_leafs[leafnum].cluster;
}
int CM_LeafArea (int leafnum)
{
if (leafnum < 0 || leafnum >= numleafs)
Com_Error (ERR_DROP, "CM_LeafArea: bad number");
return map_leafs[leafnum].area;
}
//=======================================================================
cplane_t *box_planes;
int box_headnode;
cbrush_t *box_brush;
cleaf_t *box_leaf;
/*
===================
CM_InitBoxHull
Set up the planes and nodes so that the six floats of a bounding box
can just be stored out and get a proper clipping hull structure.
===================
*/
void CM_InitBoxHull (void)
{
int i;
int side;
cnode_t *c;
cplane_t *p;
cbrushside_t *s;
box_headnode = numnodes;
box_planes = &map_planes[numplanes];
if (numnodes+6 > MAX_MAP_NODES
|| numbrushes+1 > MAX_MAP_BRUSHES
|| numleafbrushes+1 > MAX_MAP_LEAFBRUSHES
|| numbrushsides+6 > MAX_MAP_BRUSHSIDES
|| numplanes+12 > MAX_MAP_PLANES)
Com_Error (ERR_DROP, "Not enough room for box tree");
box_brush = &map_brushes[numbrushes];
box_brush->numsides = 6;
box_brush->firstbrushside = numbrushsides;
box_brush->contents = CONTENTS_MONSTER;
box_leaf = &map_leafs[numleafs];
box_leaf->contents = CONTENTS_MONSTER;
box_leaf->firstleafbrush = numleafbrushes;
box_leaf->numleafbrushes = 1;
map_leafbrushes[numleafbrushes] = numbrushes;
for (i=0 ; i<6 ; i++)
{
side = i&1;
// brush sides
s = &map_brushsides[numbrushsides+i];
s->plane = map_planes + (numplanes+i*2+side);
s->surface = &nullsurface;
// nodes
c = &map_nodes[box_headnode+i];
c->plane = map_planes + (numplanes+i*2);
c->children[side] = -1 - emptyleaf;
if (i != 5)
c->children[side^1] = box_headnode+i + 1;
else
c->children[side^1] = -1 - numleafs;
// planes
p = &box_planes[i*2];
p->type = i>>1;
p->signbits = 0;
VectorClear (p->normal);
p->normal[i>>1] = 1;
p = &box_planes[i*2+1];
p->type = 3 + (i>>1);
p->signbits = 0;
VectorClear (p->normal);
p->normal[i>>1] = -1;
}
}
/*
===================
CM_HeadnodeForBox
To keep everything totally uniform, bounding boxes are turned into small
BSP trees instead of being compared directly.
===================
*/
int CM_HeadnodeForBox (vec3_t mins, vec3_t maxs)
{
box_planes[0].dist = maxs[0];
box_planes[1].dist = -maxs[0];
box_planes[2].dist = mins[0];
box_planes[3].dist = -mins[0];
box_planes[4].dist = maxs[1];
box_planes[5].dist = -maxs[1];
box_planes[6].dist = mins[1];
box_planes[7].dist = -mins[1];
box_planes[8].dist = maxs[2];
box_planes[9].dist = -maxs[2];
box_planes[10].dist = mins[2];
box_planes[11].dist = -mins[2];
return box_headnode;
}
/*
==================
CM_PointLeafnum_r
==================
*/
int CM_PointLeafnum_r (vec3_t p, int num)
{
float d;
cnode_t *node;
cplane_t *plane;
while (num >= 0)
{
node = map_nodes + num;
plane = node->plane;
if (plane->type < 3)
d = p[plane->type] - plane->dist;
else
d = DotProduct (plane->normal, p) - plane->dist;
if (d < 0)
num = node->children[1];
else
num = node->children[0];
}
c_pointcontents++; // optimize counter
return -1 - num;
}
int CM_PointLeafnum (vec3_t p)
{
if (!numplanes)
return 0; // sound may call this without map loaded
return CM_PointLeafnum_r (p, 0);
}
/*
=============
CM_BoxLeafnums
Fills in a list of all the leafs touched
=============
*/
int leaf_count, leaf_maxcount;
int *leaf_list;
float *leaf_mins, *leaf_maxs;
int leaf_topnode;
void CM_BoxLeafnums_r (int nodenum)
{
cplane_t *plane;
cnode_t *node;
int s;
while (1)
{
if (nodenum < 0)
{
if (leaf_count >= leaf_maxcount)
{
// Com_Printf ("CM_BoxLeafnums_r: overflow\n");
return;
}
leaf_list[leaf_count++] = -1 - nodenum;
return;
}
node = &map_nodes[nodenum];
plane = node->plane;
// s = BoxOnPlaneSide (leaf_mins, leaf_maxs, plane);
s = BOX_ON_PLANE_SIDE(leaf_mins, leaf_maxs, plane);
if (s == 1)
nodenum = node->children[0];
else if (s == 2)
nodenum = node->children[1];
else
{ // go down both
if (leaf_topnode == -1)
leaf_topnode = nodenum;
CM_BoxLeafnums_r (node->children[0]);
nodenum = node->children[1];
}
}
}
int CM_BoxLeafnums_headnode (vec3_t mins, vec3_t maxs, int *list, int listsize, int headnode, int *topnode)
{
leaf_list = list;
leaf_count = 0;
leaf_maxcount = listsize;
leaf_mins = mins;
leaf_maxs = maxs;
leaf_topnode = -1;
CM_BoxLeafnums_r (headnode);
if (topnode)
*topnode = leaf_topnode;
return leaf_count;
}
int CM_BoxLeafnums (vec3_t mins, vec3_t maxs, int *list, int listsize, int *topnode)
{
return CM_BoxLeafnums_headnode (mins, maxs, list,
listsize, map_cmodels[0].headnode, topnode);
}
/*
==================
CM_PointContents
==================
*/
int CM_PointContents (vec3_t p, int headnode)
{
int l;
if (!numnodes) // map not loaded
return 0;
l = CM_PointLeafnum_r (p, headnode);
return map_leafs[l].contents;
}
/*
==================
CM_TransformedPointContents
Handles offseting and rotation of the end points for moving and
rotating entities
==================
*/
int CM_TransformedPointContents (vec3_t p, int headnode, vec3_t origin, vec3_t angles)
{
vec3_t p_l;
vec3_t temp;
vec3_t forward, right, up;
int l;
// subtract origin offset
VectorSubtract (p, origin, p_l);
// rotate start and end into the models frame of reference
if (headnode != box_headnode &&
(angles[0] || angles[1] || angles[2]) )
{
AngleVectors (angles, forward, right, up);
VectorCopy (p_l, temp);
p_l[0] = DotProduct (temp, forward);
p_l[1] = -DotProduct (temp, right);
p_l[2] = DotProduct (temp, up);
}
l = CM_PointLeafnum_r (p_l, headnode);
return map_leafs[l].contents;
}
/*
===============================================================================
BOX TRACING
===============================================================================
*/
// 1/32 epsilon to keep floating point happy
#define DIST_EPSILON (0.03125)
vec3_t trace_start, trace_end;
vec3_t trace_mins, trace_maxs;
vec3_t trace_extents;
trace_t trace_trace;
int trace_contents;
qboolean trace_ispoint; // optimized case
/*
================
CM_ClipBoxToBrush
================
*/
void CM_ClipBoxToBrush (vec3_t mins, vec3_t maxs, vec3_t p1, vec3_t p2,
trace_t *trace, cbrush_t *brush)
{
int i, j;
cplane_t *plane, *clipplane;
float dist;
float enterfrac, leavefrac;
vec3_t ofs;
float d1, d2;
qboolean getout, startout;
float f;
cbrushside_t *side, *leadside;
enterfrac = -1;
leavefrac = 1;
clipplane = NULL;
if (!brush->numsides)
return;
c_brush_traces++;
getout = false;
startout = false;
leadside = NULL;
for (i=0 ; i<brush->numsides ; i++)
{
side = &map_brushsides[brush->firstbrushside+i];
plane = side->plane;
// FIXME: special case for axial
if (!trace_ispoint)
{ // general box case
// push the plane out apropriately for mins/maxs
// FIXME: use signbits into 8 way lookup for each mins/maxs
for (j=0 ; j<3 ; j++)
{
if (plane->normal[j] < 0)
ofs[j] = maxs[j];
else
ofs[j] = mins[j];
}
dist = DotProduct (ofs, plane->normal);
dist = plane->dist - dist;
}
else
{ // special point case
dist = plane->dist;
}
d1 = DotProduct (p1, plane->normal) - dist;
d2 = DotProduct (p2, plane->normal) - dist;
if (d2 > 0)
getout = true; // endpoint is not in solid
if (d1 > 0)
startout = true;
// if completely in front of face, no intersection
if (d1 > 0 && d2 >= d1)
return;
if (d1 <= 0 && d2 <= 0)
continue;
// crosses face
if (d1 > d2)
{ // enter
f = (d1-DIST_EPSILON) / (d1-d2);
if (f > enterfrac)
{
enterfrac = f;
clipplane = plane;
leadside = side;
}
}
else
{ // leave
f = (d1+DIST_EPSILON) / (d1-d2);
if (f < leavefrac)
leavefrac = f;
}
}
if (!startout)
{ // original point was inside brush
trace->startsolid = true;
if (!getout)
trace->allsolid = true;
return;
}
if (enterfrac < leavefrac)
{
if (enterfrac > -1 && enterfrac < trace->fraction)
{
if (enterfrac < 0)
enterfrac = 0;
trace->fraction = enterfrac;
trace->plane = *clipplane;
trace->surface = &(leadside->surface->c);
trace->contents = brush->contents;
}
}
}
/*
================
CM_TestBoxInBrush
================
*/
void CM_TestBoxInBrush (vec3_t mins, vec3_t maxs, vec3_t p1,
trace_t *trace, cbrush_t *brush)
{
int i, j;
cplane_t *plane;
float dist;
vec3_t ofs;
float d1;
cbrushside_t *side;
if (!brush->numsides)
return;
for (i=0 ; i<brush->numsides ; i++)
{
side = &map_brushsides[brush->firstbrushside+i];
plane = side->plane;
// FIXME: special case for axial
// general box case
// push the plane out apropriately for mins/maxs
// FIXME: use signbits into 8 way lookup for each mins/maxs
for (j=0 ; j<3 ; j++)
{
if (plane->normal[j] < 0)
ofs[j] = maxs[j];
else
ofs[j] = mins[j];
}
dist = DotProduct (ofs, plane->normal);
dist = plane->dist - dist;
d1 = DotProduct (p1, plane->normal) - dist;
// if completely in front of face, no intersection
if (d1 > 0)
return;
}
// inside this brush
trace->startsolid = trace->allsolid = true;
trace->fraction = 0;
trace->contents = brush->contents;
}
/*
================
CM_TraceToLeaf
================
*/
void CM_TraceToLeaf (int leafnum)
{
int k;
int brushnum;
cleaf_t *leaf;
cbrush_t *b;
leaf = &map_leafs[leafnum];
if ( !(leaf->contents & trace_contents))
return;
// trace line against all brushes in the leaf
for (k=0 ; k<leaf->numleafbrushes ; k++)
{
brushnum = map_leafbrushes[leaf->firstleafbrush+k];
b = &map_brushes[brushnum];
if (b->checkcount == checkcount)
continue; // already checked this brush in another leaf
b->checkcount = checkcount;
if ( !(b->contents & trace_contents))
continue;
CM_ClipBoxToBrush (trace_mins, trace_maxs, trace_start, trace_end, &trace_trace, b);
if (!trace_trace.fraction)
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -