-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_efrag.c
204 lines (152 loc) · 4.62 KB
/
gl_efrag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// gl_efrag.c
#include "quakedef.h"
//===========================================================================
/*
===============================================================================
ENTITY FRAGMENT FUNCTIONS
ericw -- GLQuake only uses efrags for static entities, and they're never
removed, so I trimmed out unused functionality and fields in efrag_t.
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.
This is inspired by MH's tutorial, and code from RMQEngine.
http://forums.insideqc.com/viewtopic.php?t=1930
===============================================================================
*/
// let's get rid of some more globals...
typedef struct r_efragdef_s
{
vec3_t mins, maxs;
entity_t *addent;
} r_efragdef_t;
#define EXTRA_EFRAGS 128
// based on RMQEngine
static efrag_t *R_GetEfrag (void)
{
// we could just Hunk_Alloc a single efrag_t and return it, but since
// the struct is so small (2 pointers) allocate groups of them
// to avoid wasting too much space on the hunk allocation headers.
if (cl.free_efrags)
{
efrag_t *ef = cl.free_efrags;
cl.free_efrags = ef->leafnext;
ef->leafnext = NULL;
return ef;
}
else
{
int i;
cl.free_efrags = (efrag_t *) Hunk_AllocName (EXTRA_EFRAGS * sizeof (efrag_t), "efrags");
for (i = 0; i < EXTRA_EFRAGS - 1; i++)
cl.free_efrags[i].leafnext = &cl.free_efrags[i + 1];
cl.free_efrags[i].leafnext = NULL;
// call recursively to get a newly allocated free efrag
return R_GetEfrag ();
}
}
/*
===================
R_SplitEntityOnNode
===================
*/
void R_SplitEntityOnNode (mnode_t *node, r_efragdef_t *ed)
{
efrag_t *ef;
mplane_t *splitplane;
mleaf_t *leaf;
int sides;
if (node->contents == CONTENTS_SOLID)
return;
// add an efrag if the node is a leaf
if (node->contents < 0)
{
leaf = (mleaf_t *)node;
// grab an efrag off the free list
ef = R_GetEfrag();
ef->entity = ed->addent;
// set the leaf links
ef->leafnext = leaf->efrags;
leaf->efrags = ef;
return;
}
// NODE_MIXED
// split on this plane
splitplane = node->plane;
sides = BOX_ON_PLANE_SIDE(ed->mins, ed->maxs, splitplane);
// recurse down the contacted sides
if (sides & 1)
R_SplitEntityOnNode (node->children[0], ed);
if (sides & 2)
R_SplitEntityOnNode (node->children[1], ed);
}
/*
===========
R_AddEfrags
===========
*/
void R_AddEfrags (entity_t *ent)
{
r_efragdef_t ed;
model_t *entmodel;
int i;
// entities with no model won't get drawn
if (!ent->model)
return;
// never add the world
if (ent == &cl_entities[0])
return;
// init the efrag definition struct so that we can avoid more ugly globals
ed.addent = ent;
entmodel = ent->model;
for (i=0 ; i<3 ; i++)
{
ed.mins[i] = ent->origin[i] + entmodel->mins[i];
ed.maxs[i] = ent->origin[i] + entmodel->maxs[i];
}
if (!cl.worldmodel)
Host_Error ("R_AddEfrags: NULL worldmodel");
R_SplitEntityOnNode (cl.worldmodel->nodes, &ed);
}
/*
================
R_StoreEfrags
johnfitz -- pointless switch statement removed.
================
*/
void R_StoreEfrags (efrag_t **ppefrag)
{
entity_t *pent;
efrag_t *pefrag;
while ((pefrag = *ppefrag) != NULL)
{
pent = pefrag->entity;
if (!pent)
Host_Error ("R_StoreEfrags: pent is NULL");
// some progs might try to send static ents with no model through here...
if (!pent->model)
continue;
// prevent adding twice in this render frame (or if an entity is in more than one leaf)
if ((pent->visframe != r_framecount) && (cl_numvisedicts < MAX_VISEDICTS))
{
// add it to the visible edicts list
cl_visedicts[cl_numvisedicts++] = pent;
// mark that we've recorded this entity for this frame
pent->visframe = r_framecount;
}
ppefrag = &pefrag->leafnext;
}
}