quakeforge/libs/video/renderer/r_efrag.c

266 lines
4.9 KiB
C
Raw Normal View History

/*
r_efrag.c
2001-05-15 20:03:56 +00:00
(description)
Copyright (C) 1996-1997 Id Software, Inc.
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:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "QF/console.h"
2001-05-20 01:58:59 +00:00
#include "QF/render.h"
#include "QF/sys.h"
#include "r_local.h"
mnode_t *r_pefragtopnode;
2001-05-20 01:58:59 +00:00
efrag_t *r_free_efrags;
// FIXME: put this on hunk?
efrag_t r_efrags[MAX_EFRAGS];
/*
2001-08-26 16:54:33 +00:00
ENTITY FRAGMENT FUNCTIONS
*/
efrag_t **lastlink;
vec3_t r_emins, r_emaxs;
entity_t *r_addent;
/*
2001-08-26 16:54:33 +00:00
R_RemoveEfrags
2001-08-26 16:54:33 +00:00
Call when removing an object from the world or moving it to another position
*/
void
R_RemoveEfrags (entity_t *ent)
{
efrag_t *ef, *old, *walk, **prev;
ef = ent->efrag;
while (ef) {
prev = &ef->leaf->efrags;
while (1) {
walk = *prev;
if (!walk)
break;
if (walk == ef) { // remove this fragment
*prev = ef->leafnext;
break;
} else
prev = &walk->leafnext;
}
old = ef;
ef = ef->entnext;
// put it on the free list
2001-05-20 01:58:59 +00:00
old->entnext = r_free_efrags;
r_free_efrags = old;
}
ent->efrag = NULL;
}
void
R_SplitEntityOnNode (mnode_t *node)
{
efrag_t *ef;
mplane_t *splitplane;
mleaf_t *leaf;
int sides;
if (node->contents == CONTENTS_SOLID) {
return;
}
2001-08-26 16:54:33 +00:00
// add an efrag if the node is a leaf
if (node->contents < 0) {
if (!r_pefragtopnode)
r_pefragtopnode = node;
leaf = (mleaf_t *) node;
2001-08-26 16:54:33 +00:00
// grab an efrag off the free list
2001-05-20 01:58:59 +00:00
ef = r_free_efrags;
if (!ef) {
Con_Printf ("Too many efrags!\n");
return; // no free fragments...
}
2001-05-20 01:58:59 +00:00
r_free_efrags = r_free_efrags->entnext;
ef->entity = r_addent;
2001-08-26 16:54:33 +00:00
// add the entity link
*lastlink = ef;
lastlink = &ef->entnext;
ef->entnext = NULL;
2001-08-26 16:54:33 +00:00
// set the leaf links
ef->leaf = leaf;
ef->leafnext = leaf->efrags;
leaf->efrags = ef;
return;
}
2001-08-26 16:54:33 +00:00
// NODE_MIXED
splitplane = node->plane;
sides = BOX_ON_PLANE_SIDE (r_emins, r_emaxs, splitplane);
if (sides == 3) {
// split on this plane
// if this is the first splitter of this bmodel, remember it
if (!r_pefragtopnode)
r_pefragtopnode = node;
}
2001-08-26 16:54:33 +00:00
// recurse down the contacted sides
if (sides & 1)
R_SplitEntityOnNode (node->children[0]);
if (sides & 2)
R_SplitEntityOnNode (node->children[1]);
}
void
R_SplitEntityOnNode2 (mnode_t *node)
{
mplane_t *splitplane;
int sides;
if (node->visframe != r_visframecount)
return;
if (node->contents < 0) {
if (node->contents != CONTENTS_SOLID)
r_pefragtopnode = node; // we've reached a non-solid leaf, so
2001-05-15 20:03:56 +00:00
// it's visible and not BSP clipped
return;
}
splitplane = node->plane;
sides = BOX_ON_PLANE_SIDE (r_emins, r_emaxs, splitplane);
if (sides == 3) {
// remember first splitter
r_pefragtopnode = node;
return;
}
2001-08-26 16:54:33 +00:00
// not split yet; recurse down the contacted side
if (sides & 1)
R_SplitEntityOnNode2 (node->children[0]);
else
R_SplitEntityOnNode2 (node->children[1]);
}
void
R_AddEfrags (entity_t *ent)
{
model_t *entmodel;
int i;
if (!ent->model)
return;
2001-05-20 01:58:59 +00:00
if (ent == &r_worldentity)
return; // never add the world
r_addent = ent;
lastlink = &ent->efrag;
r_pefragtopnode = NULL;
entmodel = ent->model;
for (i = 0; i < 3; i++) {
r_emins[i] = ent->origin[i] + entmodel->mins[i];
r_emaxs[i] = ent->origin[i] + entmodel->maxs[i];
}
2001-05-20 01:58:59 +00:00
R_SplitEntityOnNode (r_worldentity.model->nodes);
ent->topnode = r_pefragtopnode;
}
/*
2001-05-15 20:03:56 +00:00
R_StoreEfrags
2001-08-26 16:54:33 +00:00
FIXME: a lot of this goes away with edge-based
*/
void
R_StoreEfrags (efrag_t **ppefrag)
{
entity_t *pent;
2001-05-20 01:58:59 +00:00
model_t *model;
efrag_t *pefrag;
while ((pefrag = *ppefrag) != NULL) {
pent = pefrag->entity;
2001-05-20 01:58:59 +00:00
model = pent->model;
2001-05-20 01:58:59 +00:00
switch (model->type) {
case mod_alias:
case mod_brush:
case mod_sprite:
2001-05-15 20:03:56 +00:00
pent = pefrag->entity;
2001-05-15 21:34:54 +00:00
if (pent->visframe != r_framecount) {
2001-05-20 02:39:56 +00:00
entity_t **ent = R_NewEntity ();
2001-05-15 21:34:54 +00:00
if (!ent)
return;
*ent = pent;
2001-05-15 20:03:56 +00:00
// mark that we've recorded this entity for this frame
pent->visframe = r_framecount;
}
2001-05-15 20:03:56 +00:00
ppefrag = &pefrag->leafnext;
break;
default:
2001-05-15 20:03:56 +00:00
Sys_Error ("R_StoreEfrags: Bad entity type %d\n",
2001-05-20 01:58:59 +00:00
model->type);
}
}
}
2001-05-20 01:58:59 +00:00
void
R_ClearEfrags (void)
{
// allocate the efrags and chain together into a free list
int i;
r_free_efrags = r_efrags;
for (i = 0; i < MAX_EFRAGS - 1; i++)
r_free_efrags[i].entnext = &r_free_efrags[i + 1];
r_free_efrags[i].entnext = NULL;
}