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
10 #define MAX_SVBSP_POLYGONPOINTS 64
11 #define SVBSP_CLIP_EPSILON (1.0 / 1024.0)
13 #define SVBSP_DotProduct(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
15 static void SVBSP_PlaneFromPoints(double *plane4f, const double *p1, const double *p2, const double *p3)
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));
26 ilength = 1.0 / ilength;
27 plane4f[0] *= ilength;
28 plane4f[1] *= ilength;
29 plane4f[2] *= ilength;
30 plane4f[3] *= ilength;
33 void SVBSP_Init(svbsp_t *b, const double *origin, int maxnodes, svbsp_node_t *nodes)
35 memset(b, 0, sizeof(*b));
36 b->origin[0] = origin[0];
37 b->origin[1] = origin[1];
38 b->origin[2] = origin[2];
40 b->maxnodes = maxnodes;
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;
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.
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.
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;
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;
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;
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)
93 // now we need to create up to numpoints + 1 new nodes, forming a BSP tree
94 // describing the occluder polygon's shadow volume
98 // if there aren't enough nodes remaining, skip it
99 if (b->numnodes + numpoints + 1 >= b->maxnodes)
101 b->ranoutofnodes = 1;
105 // add one node per side, then the actual occluding face node
107 // thread safety notes:
108 // DO NOT multithread insertion, it could be made 'safe' but the results
109 // would be inconsistent.
111 // it is completely safe to multithread queries in all cases.
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
117 // note down the first available nodenum for the *parentnodenumpointer
118 // line which is done last to allow multithreaded queries during an
120 basenum = b->numnodes;
121 for (i = 0, p = numpoints - 1;i < numpoints;p = i, i++)
124 // see if a parent plane describes this side
125 for (j = parentnodenum;j >= 0;j = b->nodes[j].parent)
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)
134 continue; // already have a matching parent plane
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
143 // (in this way this code treats all polygons as double sided)
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
150 for (j = 0;j < numpoints;j++)
152 double d = SVBSP_DotProduct(points + j * 3, node->plane) - node->plane[3];
153 if (d < -SVBSP_CLIP_EPSILON)
155 if (d > SVBSP_CLIP_EPSILON)
157 node->plane[0] *= -1;
158 node->plane[1] *= -1;
159 node->plane[2] *= -1;
160 node->plane[3] *= -1;
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];
174 // see if a parent plane describes the face plane
175 for (j = parentnodenum;j >= 0;j = b->nodes[j].parent)
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)
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)
194 node->plane[0] *= -1;
195 node->plane[1] *= -1;
196 node->plane[2] *= -1;
197 node->plane[3] *= -1;
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);
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)
211 int frontnumpoints, backnumpoints;
212 double frontpoints[MAX_SVBSP_POLYGONPOINTS * 3], backpoints[MAX_SVBSP_POLYGONPOINTS * 3];
215 // recurse through plane nodes
216 while (*parentnodenumpointer >= 0)
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;
222 if (SVBSP_DotProduct(points, node->plane) >= node->plane[3] + SVBSP_CLIP_EPSILON)
224 for (i = 1;i < numpoints && SVBSP_DotProduct(points + i * 3, node->plane) >= node->plane[3] + SVBSP_CLIP_EPSILON;i++);
227 // no need to split, just go to one side
228 parentnodenumpointer = &node->children[0];
232 else if (SVBSP_DotProduct(points, node->plane) <= node->plane[3] - SVBSP_CLIP_EPSILON)
234 for (i = 1;i < numpoints && SVBSP_DotProduct(points + i * 3, node->plane) <= node->plane[3] - SVBSP_CLIP_EPSILON;i++);
237 // no need to split, just go to one side
238 parentnodenumpointer = &node->children[1];
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
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);
258 if (*parentnodenumpointer == -1)
260 // empty leaf node; and some geometry survived
261 // if inserting an occluder, replace this empty leaf with a shadow volume
263 for (i = 0;i < numpoints-2;i++)
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);
274 b->stat_occluders_fragments_accepted++;
275 SVBSP_InsertOccluderPolygonNodes(b, parentnodenumpointer, parentnodenum, numpoints, points, fragmentcallback, fragmentcallback_pointer1, fragmentcallback_number1);
278 b->stat_queries_fragments_accepted++;
279 if (fragmentcallback)
280 fragmentcallback(fragmentcallback_pointer1, fragmentcallback_number1, b, numpoints, points);
285 // otherwise it's a solid leaf which destroys all polygons inside it
287 b->stat_occluders_fragments_rejected++;
289 b->stat_queries_fragments_rejected++;
291 for (i = 0;i < numpoints-2;i++)
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);
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)
308 // don't even consider an empty polygon
312 //if (insertoccluder)
313 for (i = 0;i < numpoints-2;i++)
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);
323 i = SVBSP_AddPolygonNode(b, &nodenum, -1, numpoints, points, insertoccluder, fragmentcallback, fragmentcallback_pointer1, fragmentcallback_number1);
327 b->stat_occluders_accepted++;
329 b->stat_occluders_rejected++;
334 b->stat_queries_accepted++;
336 b->stat_queries_rejected++;