more rcon_secure cleanup
[divverent/darkplaces.git] / svbsp.c
1
2 // Shadow Volume BSP code written by Forest "LordHavoc" Hale on 2003-11-06 and placed into public domain.
3 // Modified by LordHavoc (to make it work and other nice things like that) on 2007-01-24 and 2007-01-25
4
5 #include <math.h>
6 #include <string.h>
7 #include "svbsp.h"
8 #include "polygon.h"
9
10 #define MAX_SVBSP_POLYGONPOINTS 64
11 #define SVBSP_CLIP_EPSILON (1.0 / 1024.0)
12
13 #define SVBSP_DotProduct(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
14
15 static void SVBSP_PlaneFromPoints(double *plane4f, const double *p1, const double *p2, const double *p3)
16 {
17         double ilength;
18         // calculate unnormalized plane
19         plane4f[0] = (p1[1] - p2[1]) * (p3[2] - p2[2]) - (p1[2] - p2[2]) * (p3[1] - p2[1]);
20         plane4f[1] = (p1[2] - p2[2]) * (p3[0] - p2[0]) - (p1[0] - p2[0]) * (p3[2] - p2[2]);
21         plane4f[2] = (p1[0] - p2[0]) * (p3[1] - p2[1]) - (p1[1] - p2[1]) * (p3[0] - p2[0]);
22         plane4f[3] = SVBSP_DotProduct(plane4f, p1);
23         // normalize the plane normal and adjust distance accordingly
24         ilength = sqrt(SVBSP_DotProduct(plane4f, plane4f));
25         if (ilength)
26                 ilength = 1.0 / ilength;
27         plane4f[0] *= ilength;
28         plane4f[1] *= ilength;
29         plane4f[2] *= ilength;
30         plane4f[3] *= ilength;
31 }
32
33 void SVBSP_Init(svbsp_t *b, const double *origin, int maxnodes, svbsp_node_t *nodes)
34 {
35         memset(b, 0, sizeof(*b));
36         b->origin[0] = origin[0];
37         b->origin[1] = origin[1];
38         b->origin[2] = origin[2];
39         b->numnodes = 3;
40         b->maxnodes = maxnodes;
41         b->nodes = nodes;
42         b->ranoutofnodes = 0;
43         b->stat_occluders_rejected = 0;
44         b->stat_occluders_accepted = 0;
45         b->stat_occluders_fragments_accepted = 0;
46         b->stat_occluders_fragments_rejected = 0;
47         b->stat_queries_rejected = 0;
48         b->stat_queries_accepted = 0;
49         b->stat_queries_fragments_accepted = 0;
50         b->stat_queries_fragments_rejected = 0;
51
52         // the bsp tree must be initialized to have two perpendicular splits axes
53         // through origin, otherwise the polygon insertions would affect the
54         // opposite side of the tree, which would be disasterous.
55         //
56         // so this code has to make 3 nodes and 4 leafs, and since the leafs are
57         // represented by empty/solid state numbers in this system rather than
58         // actual structs, we only need to make the 3 nodes.
59
60         // root node
61         // this one splits the world into +X and -X sides
62         b->nodes[0].plane[0] = 1;
63         b->nodes[0].plane[1] = 0;
64         b->nodes[0].plane[2] = 0;
65         b->nodes[0].plane[3] = origin[0];
66         b->nodes[0].parent = -1;
67         b->nodes[0].children[0] = 1;
68         b->nodes[0].children[1] = 2;
69
70         // +X side node
71         // this one splits the +X half of the world into +Y and -Y
72         b->nodes[1].plane[0] = 0;
73         b->nodes[1].plane[1] = 1;
74         b->nodes[1].plane[2] = 0;
75         b->nodes[1].plane[3] = origin[1];
76         b->nodes[1].parent = 0;
77         b->nodes[1].children[0] = -1;
78         b->nodes[1].children[1] = -1;
79
80         // -X side node
81         // this one splits the -X half of the world into +Y and -Y
82         b->nodes[2].plane[0] = 0;
83         b->nodes[2].plane[1] = 1;
84         b->nodes[2].plane[2] = 0;
85         b->nodes[2].plane[3] = origin[1];
86         b->nodes[2].parent = 0;
87         b->nodes[2].children[0] = -1;
88         b->nodes[2].children[1] = -1;
89 }
90
91 static void SVBSP_InsertOccluderPolygonNodes(svbsp_t *b, int *parentnodenumpointer, int parentnodenum, int numpoints, const double *points, void (*fragmentcallback)(void *fragmentcallback_pointer1, int fragmentcallback_number1, svbsp_t *b, int numpoints, const double *points), void *fragmentcallback_pointer1, int fragmentcallback_number1)
92 {
93         // now we need to create up to numpoints + 1 new nodes, forming a BSP tree
94         // describing the occluder polygon's shadow volume
95         int i, j, p, basenum;
96         svbsp_node_t *node;
97
98         // if there aren't enough nodes remaining, skip it
99         if (b->numnodes + numpoints + 1 >= b->maxnodes)
100         {
101                 b->ranoutofnodes = 1;
102                 return;
103         }
104
105         // add one node per side, then the actual occluding face node
106
107         // thread safety notes:
108         // DO NOT multithread insertion, it could be made 'safe' but the results
109         // would be inconsistent.
110         //
111         // it is completely safe to multithread queries in all cases.
112         //
113         // if an insertion is occurring the query will give intermediate results,
114         // being blocked by some volumes but not others, which is perfectly okay
115         // for visibility culling intended only to reduce rendering work
116
117         // note down the first available nodenum for the *parentnodenumpointer
118         // line which is done last to allow multithreaded queries during an
119         // insertion
120         basenum = b->numnodes;
121         for (i = 0, p = numpoints - 1;i < numpoints;p = i, i++)
122         {
123 #if 1
124                 // see if a parent plane describes this side
125                 for (j = parentnodenum;j >= 0;j = b->nodes[j].parent)
126                 {
127                         double *parentnodeplane = b->nodes[j].plane;
128                         if (fabs(SVBSP_DotProduct(b->origin     , parentnodeplane) - parentnodeplane[3]) < SVBSP_CLIP_EPSILON
129                          && fabs(SVBSP_DotProduct(points + p * 3, parentnodeplane) - parentnodeplane[3]) < SVBSP_CLIP_EPSILON
130                          && fabs(SVBSP_DotProduct(points + i * 3, parentnodeplane) - parentnodeplane[3]) < SVBSP_CLIP_EPSILON)
131                                 break;
132                 }
133                 if (j >= 0)
134                         continue; // already have a matching parent plane
135 #endif
136
137                 // create a side plane
138                 // anything infront of this is not inside the shadow volume
139                 node = b->nodes + b->numnodes++;
140                 SVBSP_PlaneFromPoints(node->plane, b->origin, points + p * 3, points + i * 3);
141                 // we need to flip the plane if it puts any part of the polygon on the
142                 // wrong side
143                 // (in this way this code treats all polygons as double sided)
144                 //
145                 // because speed is important this stops as soon as it finds proof
146                 // that the orientation is right or wrong
147                 // (we know that the plane is on one edge of the polygon, so there is
148                 // never a case where points lie on both sides, so the first hint is
149                 // sufficient)
150                 for (j = 0;j < numpoints;j++)
151                 {
152                         double d = SVBSP_DotProduct(points + j * 3, node->plane) - node->plane[3];
153                         if (d < -SVBSP_CLIP_EPSILON)
154                                 break;
155                         if (d > SVBSP_CLIP_EPSILON)
156                         {
157                                 node->plane[0] *= -1;
158                                 node->plane[1] *= -1;
159                                 node->plane[2] *= -1;
160                                 node->plane[3] *= -1;
161                                 break;
162                         }
163                 }
164                 node->parent = parentnodenum;
165                 node->children[0] = -1; // empty
166                 node->children[1] = -1; // empty
167                 // link this child into the tree
168                 *parentnodenumpointer = parentnodenum = (int)(node - b->nodes);
169                 // now point to the child pointer for the next node to update later
170                 parentnodenumpointer = &node->children[1];
171         }
172
173 #if 1
174         // see if a parent plane describes the face plane
175         for (j = parentnodenum;j >= 0;j = b->nodes[j].parent)
176         {
177                 double *parentnodeplane = b->nodes[j].plane;
178                 if (fabs(SVBSP_DotProduct(points    , parentnodeplane) - parentnodeplane[3]) < SVBSP_CLIP_EPSILON
179                  && fabs(SVBSP_DotProduct(points + 3, parentnodeplane) - parentnodeplane[3]) < SVBSP_CLIP_EPSILON
180                  && fabs(SVBSP_DotProduct(points + 6, parentnodeplane) - parentnodeplane[3]) < SVBSP_CLIP_EPSILON)
181                         break;
182         }
183         if (j < 0)
184 #endif
185         {
186                 // add the face-plane node
187                 // infront is empty, behind is shadow
188                 node = b->nodes + b->numnodes++;
189                 SVBSP_PlaneFromPoints(node->plane, points, points + 3, points + 6);
190                 // this is a flip check similar to the one above
191                 // this one checks if the plane faces the origin, if not, flip it
192                 if (SVBSP_DotProduct(b->origin, node->plane) - node->plane[3] < -SVBSP_CLIP_EPSILON)
193                 {
194                         node->plane[0] *= -1;
195                         node->plane[1] *= -1;
196                         node->plane[2] *= -1;
197                         node->plane[3] *= -1;
198                 }
199                 node->parent = parentnodenum;
200                 node->children[0] = -1; // empty
201                 node->children[1] = -2; // shadow
202                 // link this child into the tree
203                 // (with the addition of this node, queries will now be culled by it)
204                 *parentnodenumpointer = (int)(node - b->nodes);
205         }
206 }
207
208 static int SVBSP_AddPolygonNode(svbsp_t *b, int *parentnodenumpointer, int parentnodenum, int numpoints, const double *points, int insertoccluder, void (*fragmentcallback)(void *fragmentcallback_pointer1, int fragmentcallback_number1, svbsp_t *b, int numpoints, const double *points), void *fragmentcallback_pointer1, int fragmentcallback_number1)
209 {
210         int i;
211         int frontnumpoints, backnumpoints;
212         double frontpoints[MAX_SVBSP_POLYGONPOINTS * 3], backpoints[MAX_SVBSP_POLYGONPOINTS * 3];
213         if (numpoints < 3)
214                 return 0;
215         // recurse through plane nodes
216         while (*parentnodenumpointer >= 0)
217         {
218                 // do a quick check to see if there is any need to split the polygon
219                 svbsp_node_t *node = b->nodes + *parentnodenumpointer;
220                 parentnodenum = *parentnodenumpointer;
221 #if 1
222                 if (SVBSP_DotProduct(points, node->plane) >= node->plane[3] + SVBSP_CLIP_EPSILON)
223                 {
224                         for (i = 1;i < numpoints && SVBSP_DotProduct(points + i * 3, node->plane) >= node->plane[3] + SVBSP_CLIP_EPSILON;i++);
225                         if (i == numpoints)
226                         {
227                                 // no need to split, just go to one side
228                                 parentnodenumpointer = &node->children[0];
229                                 continue;
230                         }
231                 }
232                 else if (SVBSP_DotProduct(points, node->plane) <= node->plane[3] - SVBSP_CLIP_EPSILON)
233                 {
234                         for (i = 1;i < numpoints && SVBSP_DotProduct(points + i * 3, node->plane) <= node->plane[3] - SVBSP_CLIP_EPSILON;i++);
235                         if (i == numpoints)
236                         {
237                                 // no need to split, just go to one side
238                                 parentnodenumpointer = &node->children[1];
239                                 continue;
240                         }
241                 }
242 #endif
243                 // at this point we know it crosses the plane, so we need to split it
244                 PolygonD_Divide(numpoints, points, node->plane[0], node->plane[1], node->plane[2], node->plane[3], SVBSP_CLIP_EPSILON, MAX_SVBSP_POLYGONPOINTS, frontpoints, &frontnumpoints, MAX_SVBSP_POLYGONPOINTS, backpoints, &backnumpoints, NULL);
245                 if (frontnumpoints > MAX_SVBSP_POLYGONPOINTS)
246                         frontnumpoints = MAX_SVBSP_POLYGONPOINTS;
247                 if (backnumpoints > MAX_SVBSP_POLYGONPOINTS)
248                         backnumpoints = MAX_SVBSP_POLYGONPOINTS;
249                 // recurse the sides and return the resulting bit flags
250                 i = 0;
251                 if (frontnumpoints >= 3)
252                         i |= SVBSP_AddPolygonNode(b, &node->children[0], (int)(node - b->nodes), frontnumpoints, frontpoints, insertoccluder, fragmentcallback, fragmentcallback_pointer1, fragmentcallback_number1);
253                 if (backnumpoints >= 3)
254                         i |= SVBSP_AddPolygonNode(b, &node->children[1], (int)(node - b->nodes), backnumpoints , backpoints , insertoccluder, fragmentcallback, fragmentcallback_pointer1, fragmentcallback_number1);
255                 return i;
256         }
257         // leaf node
258         if (*parentnodenumpointer == -1)
259         {
260                 // empty leaf node; and some geometry survived
261                 // if inserting an occluder, replace this empty leaf with a shadow volume
262 #if 0
263                 for (i = 0;i < numpoints-2;i++)
264                 {
265                         Debug_PolygonBegin(NULL, DRAWFLAG_ADDITIVE);
266                         Debug_PolygonVertex(points[0], points[1], points[2], 0, 0, 0.25, 0, 0, 1);
267                         Debug_PolygonVertex(points[0 + (i + 1) * 3], points[1 + (i + 1) * 3], points[2 + (i + 1) * 3], 0, 0, 0.25, 0, 0, 1);
268                         Debug_PolygonVertex(points[0 + (i + 2) * 3], points[1 + (i + 2) * 3], points[2 + (i + 2) * 3], 0, 0, 0.25, 0, 0, 1);
269                         Debug_PolygonEnd();
270                 }
271 #endif
272                 if (insertoccluder)
273                 {
274                         b->stat_occluders_fragments_accepted++;
275                         SVBSP_InsertOccluderPolygonNodes(b, parentnodenumpointer, parentnodenum, numpoints, points, fragmentcallback, fragmentcallback_pointer1, fragmentcallback_number1);
276                 }
277                 else
278                         b->stat_queries_fragments_accepted++;
279                 if (fragmentcallback)
280                         fragmentcallback(fragmentcallback_pointer1, fragmentcallback_number1, b, numpoints, points);
281                 return 2;
282         }
283         else
284         {
285                 // otherwise it's a solid leaf which destroys all polygons inside it
286                 if (insertoccluder)
287                         b->stat_occluders_fragments_rejected++;
288                 else
289                         b->stat_queries_fragments_rejected++;
290 #if 0
291                 for (i = 0;i < numpoints-2;i++)
292                 {
293                         Debug_PolygonBegin(NULL, DRAWFLAG_ADDITIVE);
294                         Debug_PolygonVertex(points[0], points[1], points[2], 0, 0, 0, 0, 0.25, 1);
295                         Debug_PolygonVertex(points[0 + (i + 1) * 3], points[1 + (i + 1) * 3], points[2 + (i + 1) * 3], 0, 0, 0, 0, 0.25, 1);
296                         Debug_PolygonVertex(points[0 + (i + 2) * 3], points[1 + (i + 2) * 3], points[2 + (i + 2) * 3], 0, 0, 0, 0, 0.25, 1);
297                         Debug_PolygonEnd();
298                 }
299 #endif
300         }
301         return 1;
302 }
303
304 int SVBSP_AddPolygon(svbsp_t *b, int numpoints, const double *points, int insertoccluder, void (*fragmentcallback)(void *fragmentcallback_pointer1, int fragmentcallback_number1, svbsp_t *b, int numpoints, const double *points), void *fragmentcallback_pointer1, int fragmentcallback_number1)
305 {
306         int i;
307         int nodenum;
308         // don't even consider an empty polygon
309         if (numpoints < 3)
310                 return 0;
311 #if 0
312 //if (insertoccluder)
313         for (i = 0;i < numpoints-2;i++)
314         {
315                 Debug_PolygonBegin(NULL, DRAWFLAG_ADDITIVE);
316                 Debug_PolygonVertex(points[0], points[1], points[2], 0, 0, 0, 0.25, 0, 1);
317                 Debug_PolygonVertex(points[0 + (i + 1) * 3], points[1 + (i + 1) * 3], points[2 + (i + 1) * 3], 0, 0, 0, 0.25, 0, 1);
318                 Debug_PolygonVertex(points[0 + (i + 2) * 3], points[1 + (i + 2) * 3], points[2 + (i + 2) * 3], 0, 0, 0, 0.25, 0, 1);
319                 Debug_PolygonEnd();
320         }
321 #endif
322         nodenum = 0;
323         i = SVBSP_AddPolygonNode(b, &nodenum, -1, numpoints, points, insertoccluder, fragmentcallback, fragmentcallback_pointer1, fragmentcallback_number1);
324         if (insertoccluder)
325         {
326                 if (i & 2)
327                         b->stat_occluders_accepted++;
328                 else
329                         b->stat_occluders_rejected++;
330         }
331         else
332         {
333                 if (i & 2)
334                         b->stat_queries_accepted++;
335                 else
336                         b->stat_queries_rejected++;
337         }
338         return i;
339 }
340