210 lines
4.1 KiB
210 lines
4.1 KiB
7 years ago
|
/*
|
||
|
gl_refrag.c - store entity fragments
|
||
|
Copyright (C) 2010 Uncle Mike
|
||
|
|
||
|
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 3 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.
|
||
|
*/
|
||
|
|
||
6 years ago
|
#include "common.h"
|
||
7 years ago
|
#include "entity_types.h"
|
||
|
#include "studio.h"
|
||
6 years ago
|
#include "world.h" // BOX_ON_PLANE_SIDE
|
||
6 years ago
|
#include "client.h"
|
||
|
#include "mathlib.h"
|
||
7 years ago
|
|
||
|
/*
|
||
|
===============================================================================
|
||
|
|
||
|
ENTITY FRAGMENT FUNCTIONS
|
||
|
|
||
|
===============================================================================
|
||
|
*/
|
||
|
|
||
|
static efrag_t **lastlink;
|
||
|
static mnode_t *r_pefragtopnode;
|
||
|
static vec3_t r_emins, r_emaxs;
|
||
|
static cl_entity_t *r_addent;
|
||
|
|
||
|
/*
|
||
|
================
|
||
|
R_RemoveEfrags
|
||
|
|
||
|
Call when removing an object from the world or moving it to another position
|
||
|
================
|
||
|
*/
|
||
|
void R_RemoveEfrags( cl_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
|
||
6 years ago
|
old->entnext = clgame.free_efrags;
|
||
|
clgame.free_efrags = old;
|
||
7 years ago
|
}
|
||
|
ent->efrag = NULL;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
===================
|
||
|
R_SplitEntityOnNode
|
||
|
===================
|
||
|
*/
|
||
|
static void R_SplitEntityOnNode( mnode_t *node )
|
||
|
{
|
||
|
efrag_t *ef;
|
||
|
mleaf_t *leaf;
|
||
|
int sides;
|
||
|
|
||
|
if( node->contents == CONTENTS_SOLID )
|
||
|
return;
|
||
|
|
||
|
// add an efrag if the node is a leaf
|
||
|
if( node->contents < 0 )
|
||
|
{
|
||
|
if( !r_pefragtopnode )
|
||
|
r_pefragtopnode = node;
|
||
|
|
||
|
leaf = (mleaf_t *)node;
|
||
|
|
||
|
// grab an efrag off the free list
|
||
6 years ago
|
ef = clgame.free_efrags;
|
||
7 years ago
|
if( !ef )
|
||
|
{
|
||
6 years ago
|
Con_Printf( S_ERROR "too many efrags!\n" );
|
||
7 years ago
|
return; // no free fragments...
|
||
|
}
|
||
|
|
||
6 years ago
|
clgame.free_efrags = ef->entnext;
|
||
7 years ago
|
ef->entity = r_addent;
|
||
|
|
||
|
// add the entity link
|
||
|
*lastlink = ef;
|
||
|
lastlink = &ef->entnext;
|
||
|
ef->entnext = NULL;
|
||
|
|
||
|
// set the leaf links
|
||
|
ef->leaf = leaf;
|
||
|
ef->leafnext = leaf->efrags;
|
||
|
leaf->efrags = ef;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// NODE_MIXED
|
||
7 years ago
|
sides = BOX_ON_PLANE_SIDE( r_emins, r_emaxs, node->plane );
|
||
7 years ago
|
|
||
|
if( sides == 3 )
|
||
|
{
|
||
|
// split on this plane
|
||
|
// if this is the first splitter of this bmodel, remember it
|
||
|
if( !r_pefragtopnode ) r_pefragtopnode = node;
|
||
|
}
|
||
|
|
||
|
// recurse down the contacted sides
|
||
|
if( sides & 1 ) R_SplitEntityOnNode( node->children[0] );
|
||
|
if( sides & 2 ) R_SplitEntityOnNode( node->children[1] );
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
===========
|
||
|
R_AddEfrags
|
||
|
===========
|
||
|
*/
|
||
|
void R_AddEfrags( cl_entity_t *ent )
|
||
|
{
|
||
6 years ago
|
matrix3x4 transform;
|
||
|
vec3_t outmins, outmaxs;
|
||
7 years ago
|
int i;
|
||
|
|
||
|
if( !ent->model )
|
||
|
return;
|
||
|
|
||
|
r_addent = ent;
|
||
|
lastlink = &ent->efrag;
|
||
|
r_pefragtopnode = NULL;
|
||
|
|
||
6 years ago
|
// handle entity rotation for right bbox expanding
|
||
|
Matrix3x4_CreateFromEntity( transform, ent->angles, vec3_origin, 1.0f );
|
||
|
Matrix3x4_TransformAABB( transform, ent->model->mins, ent->model->maxs, outmins, outmaxs );
|
||
|
|
||
7 years ago
|
for( i = 0; i < 3; i++ )
|
||
|
{
|
||
6 years ago
|
r_emins[i] = ent->origin[i] + outmins[i];
|
||
|
r_emaxs[i] = ent->origin[i] + outmaxs[i];
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
R_SplitEntityOnNode( cl.worldmodel->nodes );
|
||
7 years ago
|
ent->topnode = r_pefragtopnode;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
================
|
||
|
R_StoreEfrags
|
||
|
|
||
|
================
|
||
|
*/
|
||
|
void R_StoreEfrags( efrag_t **ppefrag, int framecount )
|
||
|
{
|
||
|
cl_entity_t *pent;
|
||
|
model_t *clmodel;
|
||
|
efrag_t *pefrag;
|
||
|
|
||
|
while(( pefrag = *ppefrag ) != NULL )
|
||
|
{
|
||
|
pent = pefrag->entity;
|
||
|
clmodel = pent->model;
|
||
|
|
||
|
switch( clmodel->type )
|
||
|
{
|
||
|
case mod_alias:
|
||
|
case mod_brush:
|
||
|
case mod_studio:
|
||
|
case mod_sprite:
|
||
|
pent = pefrag->entity;
|
||
|
|
||
|
if( pent->visframe != framecount )
|
||
|
{
|
||
6 years ago
|
if( CL_AddVisibleEntity( pent, ET_FRAGMENTED ))
|
||
7 years ago
|
{
|
||
|
// mark that we've recorded this entity for this frame
|
||
6 years ago
|
pent->curstate.messagenum = cl.parsecount;
|
||
7 years ago
|
pent->visframe = framecount;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ppefrag = &pefrag->leafnext;
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
}
|