spingle/source/gl_refrag.c

221 lines
4.7 KiB
C
Raw Permalink Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2010-2014 QuakeSpasm developers
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.
*/
// r_efrag.c
2019-12-02 07:07:37 -08:00
#include "q_defs.h"
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
mnode_t *r_pefragtopnode;
2019-11-24 20:45:15 -08:00
//===========================================================================
/*
===============================================================================
2019-11-25 17:40:18 -08:00
ENTITY FRAGMENT FUNCTIONS
2019-11-24 20:45:15 -08:00
ericw -- GLQuake only uses efrags for static entities, and they're never
2019-12-06 09:07:30 -08:00
removed, so I trimmed out unused functionality and stuff in efrag_t.
2019-11-24 20:45:15 -08:00
Now, efrags are just a linked list for each leaf of the static
entities that touch that leaf. The efrags are hunk-allocated so there is no
fixed limit.
2019-11-25 17:40:18 -08:00
2019-11-24 20:45:15 -08:00
This is inspired by MH's tutorial, and code from RMQEngine.
http://forums.insideqc.com/viewtopic.php?t=1930
2019-11-25 17:40:18 -08:00
2019-11-24 20:45:15 -08:00
===============================================================================
*/
2019-11-25 17:40:18 -08:00
vec3_t r_emins, r_emaxs;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
entity_t *r_addent;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
#define EXTRA_EFRAGS 128
2019-11-24 20:45:15 -08:00
// based on RMQEngine
2019-11-25 17:40:18 -08:00
static efrag_t *R_GetEfrag(void)
2019-11-24 20:45:15 -08:00
{
// we could just allocate a single efrag_t and return it, but since
2019-11-24 20:45:15 -08:00
// the struct is so small (2 pointers) allocate groups of them
// to avoid wasting too much space on the hunk allocation headers.
2019-11-25 17:40:18 -08:00
if(cl.free_efrags)
2019-11-24 20:45:15 -08:00
{
efrag_t *ef = cl.free_efrags;
cl.free_efrags = ef->leafnext;
ef->leafnext = NULL;
2019-11-25 17:40:18 -08:00
2019-11-24 20:45:15 -08:00
cl.num_efrags++;
2019-11-25 17:40:18 -08:00
2019-11-24 20:45:15 -08:00
return ef;
}
else
{
2019-11-25 16:49:58 -08:00
int32_t i;
2019-11-25 17:40:18 -08:00
cl.free_efrags = (efrag_t *) Hunk_AllocName(EXTRA_EFRAGS * sizeof(efrag_t), "efrags");
for(i = 0; i < EXTRA_EFRAGS - 1; i++)
2019-11-24 20:45:15 -08:00
cl.free_efrags[i].leafnext = &cl.free_efrags[i + 1];
2019-11-25 17:40:18 -08:00
2019-11-24 20:45:15 -08:00
cl.free_efrags[i].leafnext = NULL;
2019-11-25 17:40:18 -08:00
2019-11-24 20:45:15 -08:00
// call recursively to get a newly allocated free efrag
2019-11-25 17:40:18 -08:00
return R_GetEfrag();
2019-11-24 20:45:15 -08:00
}
}
/*
===================
R_SplitEntityOnNode
===================
*/
2019-11-25 17:40:18 -08:00
void R_SplitEntityOnNode(mnode_t *node)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
efrag_t *ef;
mplane_t *splitplane;
mleaf_t *leaf;
int32_t sides;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(node->contents == CONTENTS_SOLID)
2019-11-24 20:45:15 -08:00
{
return;
}
// add an efrag if the node is a leaf
2019-11-25 17:40:18 -08:00
if(node->contents < 0)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!r_pefragtopnode)
2019-11-24 20:45:15 -08:00
r_pefragtopnode = node;
leaf = (mleaf_t *)node;
// grab an efrag off the free list
ef = R_GetEfrag();
ef->entity = r_addent;
// set the leaf links
ef->leafnext = leaf->efrags;
leaf->efrags = ef;
return;
}
// NODE_MIXED
splitplane = node->plane;
sides = BOX_ON_PLANE_SIDE(r_emins, r_emaxs, splitplane);
2019-11-25 17:40:18 -08:00
if(sides == 3)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
// split on this plane
// if this is the first splitter of this bmodel, remember it
if(!r_pefragtopnode)
2019-11-24 20:45:15 -08:00
r_pefragtopnode = node;
}
// recurse down the contacted sides
2019-11-25 17:40:18 -08:00
if(sides & 1)
R_SplitEntityOnNode(node->children[0]);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(sides & 2)
R_SplitEntityOnNode(node->children[1]);
2019-11-24 20:45:15 -08:00
}
/*
===========
R_CheckEfrags -- johnfitz -- check for excessive efrag count
===========
*/
2019-11-25 17:40:18 -08:00
void R_CheckEfrags(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(cls.signon < 2)
2019-11-24 20:45:15 -08:00
return; //don't spam when still parsing signon packet full of static ents
2019-11-25 17:40:18 -08:00
if(cl.num_efrags > 640 && dev_peakstats.efrags <= 640)
Con_DWarning("%" PRIi32 " efrags exceeds standard limit of 640.\n", cl.num_efrags);
2019-11-24 20:45:15 -08:00
dev_stats.efrags = cl.num_efrags;
dev_peakstats.efrags = q_max(cl.num_efrags, dev_peakstats.efrags);
}
/*
===========
R_AddEfrags
===========
*/
2019-11-25 17:40:18 -08:00
void R_AddEfrags(entity_t *ent)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
qmodel_t *entmodel;
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!ent->model)
2019-11-24 20:45:15 -08:00
return;
r_addent = ent;
r_pefragtopnode = NULL;
entmodel = ent->model;
2019-11-25 17:40:18 -08:00
for(i = 0 ; i < 3 ; i++)
2019-11-24 20:45:15 -08:00
{
r_emins[i] = ent->origin[i] + entmodel->mins[i];
r_emaxs[i] = ent->origin[i] + entmodel->maxs[i];
}
2019-11-25 17:40:18 -08:00
R_SplitEntityOnNode(cl.worldmodel->nodes);
2019-11-24 20:45:15 -08:00
ent->topnode = r_pefragtopnode;
2019-11-25 17:40:18 -08:00
R_CheckEfrags(); //johnfitz
2019-11-24 20:45:15 -08:00
}
/*
================
R_StoreEfrags -- johnfitz -- pointless switch statement removed.
================
*/
2019-11-25 17:40:18 -08:00
void R_StoreEfrags(efrag_t **ppefrag)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
entity_t *pent;
efrag_t *pefrag;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
while((pefrag = *ppefrag) != NULL)
2019-11-24 20:45:15 -08:00
{
pent = pefrag->entity;
2019-11-25 19:38:21 -08:00
if((pent->visframe != r_framecount) && (cl_numvisedicts < max_visedicts))
2019-11-24 20:45:15 -08:00
{
cl_visedicts[cl_numvisedicts++] = pent;
pent->visframe = r_framecount;
}
ppefrag = &pefrag->leafnext;
}
}