📄 portals.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "qbsp.h"
#include "l_mem.h"
int c_active_portals;
int c_peak_portals;
int c_boundary;
int c_boundary_sides;
int c_portalmemory;
//portal_t *portallist = NULL;
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
portal_t *AllocPortal (void)
{
portal_t *p;
p = GetMemory(sizeof(portal_t));
memset (p, 0, sizeof(portal_t));
if (numthreads == 1)
{
c_active_portals++;
if (c_active_portals > c_peak_portals)
{
c_peak_portals = c_active_portals;
} //end if
c_portalmemory += MemorySize(p);
} //end if
// p->nextportal = portallist;
// portallist = p;
return p;
} //end of the function AllocPortal
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void FreePortal (portal_t *p)
{
if (p->winding) FreeWinding(p->winding);
if (numthreads == 1)
{
c_active_portals--;
c_portalmemory -= MemorySize(p);
} //end if
FreeMemory(p);
} //end of the function FreePortal
//===========================================================================
// Returns the single content bit of the
// strongest visible content present
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int VisibleContents (int contents)
{
int i;
for (i=1 ; i<=LAST_VISIBLE_CONTENTS ; i<<=1)
if (contents & i )
return i;
return 0;
} //end of the function VisibleContents
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int ClusterContents (node_t *node)
{
int c1, c2, c;
if (node->planenum == PLANENUM_LEAF)
return node->contents;
c1 = ClusterContents(node->children[0]);
c2 = ClusterContents(node->children[1]);
c = c1|c2;
// a cluster may include some solid detail areas, but
// still be seen into
if ( ! (c1&CONTENTS_SOLID) || ! (c2&CONTENTS_SOLID) )
c &= ~CONTENTS_SOLID;
return c;
} //end of the function ClusterContents
//===========================================================================
// Returns true if the portal is empty or translucent, allowing
// the PVS calculation to see through it.
// The nodes on either side of the portal may actually be clusters,
// not leaves, so all contents should be ored together
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean Portal_VisFlood (portal_t *p)
{
int c1, c2;
if (!p->onnode)
return false; // to global outsideleaf
c1 = ClusterContents(p->nodes[0]);
c2 = ClusterContents(p->nodes[1]);
if (!VisibleContents (c1^c2))
return true;
if (c1 & (CONTENTS_Q2TRANSLUCENT|CONTENTS_DETAIL))
c1 = 0;
if (c2 & (CONTENTS_Q2TRANSLUCENT|CONTENTS_DETAIL))
c2 = 0;
if ( (c1|c2) & CONTENTS_SOLID )
return false; // can't see through solid
if (! (c1 ^ c2))
return true; // identical on both sides
if (!VisibleContents (c1^c2))
return true;
return false;
} //end of the function Portal_VisFlood
//===========================================================================
// The entity flood determines which areas are
// "outside" on the map, which are then filled in.
// Flowing from side s to side !s
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean Portal_EntityFlood (portal_t *p, int s)
{
if (p->nodes[0]->planenum != PLANENUM_LEAF
|| p->nodes[1]->planenum != PLANENUM_LEAF)
Error ("Portal_EntityFlood: not a leaf");
// can never cross to a solid
if ( (p->nodes[0]->contents & CONTENTS_SOLID)
|| (p->nodes[1]->contents & CONTENTS_SOLID) )
return false;
// can flood through everything else
return true;
}
//=============================================================================
int c_tinyportals;
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AddPortalToNodes (portal_t *p, node_t *front, node_t *back)
{
if (p->nodes[0] || p->nodes[1])
Error ("AddPortalToNode: allready included");
p->nodes[0] = front;
p->next[0] = front->portals;
front->portals = p;
p->nodes[1] = back;
p->next[1] = back->portals;
back->portals = p;
} //end of the function AddPortalToNodes
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void RemovePortalFromNode (portal_t *portal, node_t *l)
{
portal_t **pp, *t;
int s, i, n;
portal_t *p;
portal_t *portals[4096];
// remove reference to the current portal
pp = &l->portals;
while (1)
{
t = *pp;
if (!t)
Error ("RemovePortalFromNode: portal not in leaf");
if ( t == portal )
break;
if (t->nodes[0] == l)
pp = &t->next[0];
else if (t->nodes[1] == l)
pp = &t->next[1];
else
Error ("RemovePortalFromNode: portal not bounding leaf");
}
if (portal->nodes[0] == l)
{
*pp = portal->next[0];
portal->nodes[0] = NULL;
} //end if
else if (portal->nodes[1] == l)
{
*pp = portal->next[1];
portal->nodes[1] = NULL;
} //end else if
else
{
Error("RemovePortalFromNode: mislinked portal");
} //end else
//#ifdef ME
n = 0;
for (p = l->portals; p; p = p->next[s])
{
for (i = 0; i < n; i++)
{
if (p == portals[i]) Error("RemovePortalFromNode: circular linked\n");
} //end for
if (p->nodes[0] != l && p->nodes[1] != l)
{
Error("RemovePortalFromNodes: portal does not belong to node\n");
} //end if
portals[n] = p;
s = (p->nodes[1] == l);
// if (++n >= 4096) Error("RemovePortalFromNode: more than 4096 portals\n");
} //end for
//#endif
} //end of the function RemovePortalFromNode
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void PrintPortal (portal_t *p)
{
int i;
winding_t *w;
w = p->winding;
for (i=0 ; i<w->numpoints ; i++)
printf ("(%5.0f,%5.0f,%5.0f)\n",w->p[i][0]
, w->p[i][1], w->p[i][2]);
} //end of the function PrintPortal
//===========================================================================
// The created portals will face the global outside_node
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
#define SIDESPACE 8
void MakeHeadnodePortals (tree_t *tree)
{
vec3_t bounds[2];
int i, j, n;
portal_t *p, *portals[6];
plane_t bplanes[6], *pl;
node_t *node;
node = tree->headnode;
// pad with some space so there will never be null volume leaves
for (i=0 ; i<3 ; i++)
{
bounds[0][i] = tree->mins[i] - SIDESPACE;
bounds[1][i] = tree->maxs[i] + SIDESPACE;
if ( bounds[0][i] > bounds[1][i] ) {
Error("empty BSP tree");
}
}
tree->outside_node.planenum = PLANENUM_LEAF;
tree->outside_node.brushlist = NULL;
tree->outside_node.portals = NULL;
tree->outside_node.contents = 0;
for (i=0 ; i<3 ; i++)
for (j=0 ; j<2 ; j++)
{
n = j*3 + i;
p = AllocPortal ();
portals[n] = p;
pl = &bplanes[n];
memset (pl, 0, sizeof(*pl));
if (j)
{
pl->normal[i] = -1;
pl->dist = -bounds[j][i];
}
else
{
pl->normal[i] = 1;
pl->dist = bounds[j][i];
}
p->plane = *pl;
p->winding = BaseWindingForPlane (pl->normal, pl->dist);
AddPortalToNodes (p, node, &tree->outside_node);
}
// clip the basewindings by all the other planes
for (i=0 ; i<6 ; i++)
{
for (j=0 ; j<6 ; j++)
{
if (j == i) continue;
ChopWindingInPlace (&portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON);
} //end for
} //end for
} //end of the function MakeHeadNodePortals
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
#define BASE_WINDING_EPSILON 0.001
#define SPLIT_WINDING_EPSILON 0.001
winding_t *BaseWindingForNode (node_t *node)
{
winding_t *w;
node_t *n;
plane_t *plane;
vec3_t normal;
vec_t dist;
w = BaseWindingForPlane (mapplanes[node->planenum].normal
, mapplanes[node->planenum].dist);
// clip by all the parents
for (n=node->parent ; n && w ; )
{
plane = &mapplanes[n->planenum];
if (n->children[0] == node)
{ // take front
ChopWindingInPlace (&w, plane->normal, plane->dist, BASE_WINDING_EPSILON);
}
else
{ // take back
VectorSubtract (vec3_origin, plane->normal, normal);
dist = -plane->dist;
ChopWindingInPlace (&w, normal, dist, BASE_WINDING_EPSILON);
}
node = n;
n = n->parent;
}
return w;
} //end of the function BaseWindingForNode
//===========================================================================
// create the new portal by taking the full plane winding for the cutting
// plane and clipping it by all of parents of this node
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean WindingIsTiny (winding_t *w);
void MakeNodePortal (node_t *node)
{
portal_t *new_portal, *p;
winding_t *w;
vec3_t normal;
float dist;
int side;
w = BaseWindingForNode (node);
// clip the portal by all the other portals in the node
for (p = node->portals; p && w; p = p->next[side])
{
if (p->nodes[0] == node)
{
side = 0;
VectorCopy (p->plane.normal, normal);
dist = p->plane.dist;
} //end if
else if (p->nodes[1] == node)
{
side = 1;
VectorSubtract (vec3_origin, p->plane.normal, normal);
dist = -p->plane.dist;
} //end else if
else
{
Error ("MakeNodePortal: mislinked portal");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -