improved plane distance epsilon checking and improved precision when converting brush...
[divverent/darkplaces.git] / collision.c
1
2 #include "quakedef.h"
3 #include "polygon.h"
4
5 #define COLLISION_SNAPSCALE (32.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
7 #define COLLISION_PLANE_DIST_EPSILON (1.0f / 32.0f)
8
9 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
10 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
11 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
12 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
13 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
14
15 void Collision_Init (void)
16 {
17         Cvar_RegisterVariable(&collision_impactnudge);
18         Cvar_RegisterVariable(&collision_startnudge);
19         Cvar_RegisterVariable(&collision_endnudge);
20         Cvar_RegisterVariable(&collision_enternudge);
21         Cvar_RegisterVariable(&collision_leavenudge);
22 }
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
38 {
39         int i;
40         Con_Printf("3 %s\n%i\n", name, brush->numpoints);
41         for (i = 0;i < brush->numpoints;i++)
42                 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
43         // FIXME: optimize!
44         Con_Printf("4\n%i\n", brush->numplanes);
45         for (i = 0;i < brush->numplanes;i++)
46                 Con_Printf("%f %f %f %f\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
47 }
48
49 void Collision_ValidateBrush(colbrushf_t *brush)
50 {
51         int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
52         float d;
53         printbrush = false;
54         if (!brush->numpoints)
55         {
56                 Con_Print("Collision_ValidateBrush: brush with no points!\n");
57                 printbrush = true;
58         }
59 #if 0
60         // it's ok for a brush to have one point and no planes...
61         if (brush->numplanes == 0 && brush->numpoints != 1)
62         {
63                 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
64                 printbrush = true;
65         }
66 #endif
67         if (brush->numplanes)
68         {
69                 pointsoffplanes = 0;
70                 pointswithinsufficientplanes = 0;
71                 for (k = 0;k < brush->numplanes;k++)
72                         if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
73                                 Con_Printf("Collision_ValidateBrush: plane #%i (%f %f %f %f) is degenerate\n", k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
74                 for (j = 0;j < brush->numpoints;j++)
75                 {
76                         pointonplanes = 0;
77                         for (k = 0;k < brush->numplanes;k++)
78                         {
79                                 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
80                                 if (d > COLLISION_PLANE_DIST_EPSILON)
81                                 {
82                                         Con_Printf("Collision_ValidateBrush: point #%i (%f %f %f) infront of plane #%i (%f %f %f %f)\n", j, brush->points[j].v[0], brush->points[j].v[1], brush->points[j].v[2], k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
83                                         printbrush = true;
84                                 }
85                                 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
86                                         pointsoffplanes++;
87                                 else
88                                         pointonplanes++;
89                         }
90                         if (pointonplanes < 3)
91                                 pointswithinsufficientplanes++;
92                 }
93                 if (pointswithinsufficientplanes)
94                 {
95                         Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
96                         printbrush = true;
97                 }
98                 if (pointsoffplanes == 0) // all points are on all planes
99                 {
100                         Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
101                         printbrush = true;
102                 }
103         }
104         if (printbrush)
105                 Collision_PrintBrushAsQHull(brush, "unnamed");
106 }
107
108 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
109 {
110         float dist, bestdist;
111         bestdist = DotProduct(points->v, normal);
112         points++;
113         while(--numpoints)
114         {
115                 dist = DotProduct(points->v, normal);
116                 bestdist = min(bestdist, dist);
117                 points++;
118         }
119         return bestdist;
120 }
121
122 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
123 {
124         float dist, bestdist;
125         bestdist = DotProduct(points->v, normal);
126         points++;
127         while(--numpoints)
128         {
129                 dist = DotProduct(points->v, normal);
130                 bestdist = max(bestdist, dist);
131                 points++;
132         }
133         return bestdist;
134 }
135
136
137 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents)
138 {
139         // TODO: planesbuf could be replaced by a remapping table
140         int j, k, m, w;
141         int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
142         double maxdist;
143         colbrushf_t *brush;
144         colpointf_t pointsbuf[256];
145         colplanef_t planesbuf[256];
146         int elementsbuf[1024];
147         int polypointbuf[256];
148         int pmaxpoints = 64;
149         int pnumpoints;
150         double p[2][3*64];
151 #if 0
152         // enable these if debugging to avoid seeing garbage in unused data
153         memset(pointsbuf, 0, sizeof(pointsbuf));
154         memset(planesbuf, 0, sizeof(planesbuf));
155         memset(elementsbuf, 0, sizeof(elementsbuf));
156         memset(polypointbuf, 0, sizeof(polypointbuf));
157         memset(p, 0, sizeof(p));
158 #endif
159         // figure out how large a bounding box we need to properly compute this brush
160         maxdist = 0;
161         for (j = 0;j < numoriginalplanes;j++)
162                 maxdist = max(maxdist, originalplanes[j].dist);
163         // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
164         maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
165         // construct a collision brush (points, planes, and renderable mesh) from
166         // a set of planes, this also optimizes out any unnecessary planes (ones
167         // whose polygon is clipped away by the other planes)
168         for (j = 0;j < numoriginalplanes;j++)
169         {
170                 // add the plane uniquely (no duplicates)
171                 for (k = 0;k < numplanesbuf;k++)
172                         if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
173                                 break;
174                 // if the plane is a duplicate, skip it
175                 if (k < numplanesbuf)
176                         continue;
177                 // check if there are too many and skip the brush
178                 if (numplanesbuf >= maxplanesbuf)
179                 {
180                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
181                         return NULL;
182                 }
183
184                 // create a large polygon from the plane
185                 w = 0;
186                 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
187                 pnumpoints = 4;
188                 // clip it by all other planes
189                 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
190                 {
191                         if (k != j)
192                         {
193                                 // we want to keep the inside of the brush plane so we flip
194                                 // the cutting plane
195                                 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, COLLISION_PLANE_DIST_EPSILON, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL, NULL);
196                                 w = !w;
197                         }
198                 }
199                 // if nothing is left, skip it
200                 if (pnumpoints < 3)
201                 {
202                         //Con_Printf("Collision_NewBrushFromPlanes: warning: polygon for plane %f %f %f %f clipped away\n", originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
203                         continue;
204                 }
205
206                 for (k = 0;k < pnumpoints;k++)
207                 {
208                         int l, m;
209                         m = 0;
210                         for (l = 0;l < numoriginalplanes;l++)
211                                 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
212                                         m++;
213                         if (m < 3)
214                                 break;
215                 }
216                 if (k < pnumpoints)
217                 {
218                         Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
219                         //return NULL;
220                 }
221
222                 // check if there are too many polygon vertices for buffer
223                 if (pnumpoints > pmaxpoints)
224                 {
225                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
226                         return NULL;
227                 }
228
229                 // check if there are too many triangle elements for buffer
230                 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
231                 {
232                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
233                         return NULL;
234                 }
235
236                 for (k = 0;k < pnumpoints;k++)
237                 {
238                         // check if there is already a matching point (no duplicates)
239                         for (m = 0;m < numpointsbuf;m++)
240                                 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
241                                         break;
242
243                         // if there is no match, add a new one
244                         if (m == numpointsbuf)
245                         {
246                                 // check if there are too many and skip the brush
247                                 if (numpointsbuf >= maxpointsbuf)
248                                 {
249                                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
250                                         return NULL;
251                                 }
252                                 // add the new one
253                                 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
254                                 numpointsbuf++;
255                         }
256
257                         // store the index into a buffer
258                         polypointbuf[k] = m;
259                 }
260
261                 // add the triangles for the polygon
262                 // (this particular code makes a triangle fan)
263                 for (k = 0;k < pnumpoints - 2;k++)
264                 {
265                         elementsbuf[numelementsbuf++] = polypointbuf[0];
266                         elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
267                         elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
268                 }
269
270                 // add the new plane
271                 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
272                 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
273                 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
274                 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
275                 numplanesbuf++;
276         }
277
278         // validate plane distances
279         for (j = 0;j < numplanesbuf;j++)
280         {
281                 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
282                 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
283                         Con_Printf("plane %f %f %f %f mismatches dist %f\n", planesbuf[j].normal[0], planesbuf[j].normal[1], planesbuf[j].normal[2], planesbuf[j].dist, d);
284         }
285
286         // if nothing is left, there's nothing to allocate
287         if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
288         {
289                 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
290                 return NULL;
291         }
292
293         // allocate the brush and copy to it
294         brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
295         brush->supercontents = supercontents;
296         brush->numplanes = numplanesbuf;
297         brush->numpoints = numpointsbuf;
298         brush->numtriangles = numelementsbuf / 3;
299         brush->planes = (colplanef_t *)(brush + 1);
300         brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
301         brush->elements = (int *)(brush->points + brush->numpoints);
302         for (j = 0;j < brush->numpoints;j++)
303         {
304                 brush->points[j].v[0] = pointsbuf[j].v[0];
305                 brush->points[j].v[1] = pointsbuf[j].v[1];
306                 brush->points[j].v[2] = pointsbuf[j].v[2];
307         }
308         for (j = 0;j < brush->numplanes;j++)
309         {
310                 brush->planes[j].normal[0] = planesbuf[j].normal[0];
311                 brush->planes[j].normal[1] = planesbuf[j].normal[1];
312                 brush->planes[j].normal[2] = planesbuf[j].normal[2];
313                 brush->planes[j].dist = planesbuf[j].dist;
314                 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
315                 brush->planes[j].texture = planesbuf[j].texture;
316         }
317         for (j = 0;j < brush->numtriangles * 3;j++)
318                 brush->elements[j] = elementsbuf[j];
319         VectorCopy(brush->points[0].v, brush->mins);
320         VectorCopy(brush->points[0].v, brush->maxs);
321         for (j = 1;j < brush->numpoints;j++)
322         {
323                 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
324                 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
325                 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
326                 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
327                 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
328                 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
329         }
330         brush->mins[0] -= 1;
331         brush->mins[1] -= 1;
332         brush->mins[2] -= 1;
333         brush->maxs[0] += 1;
334         brush->maxs[1] += 1;
335         brush->maxs[2] += 1;
336         Collision_ValidateBrush(brush);
337         return brush;
338 }
339
340
341
342 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
343 {
344         int i;
345         float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
346         colpointf_t *p, *p2;
347
348         // FIXME: these probably don't actually need to be normalized if the collision code does not care
349         if (brush->numpoints == 3)
350         {
351                 // optimized triangle case
352                 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
353                 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
354                 {
355                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
356                         brush->numplanes = 0;
357                         return;
358                 }
359                 else
360                 {
361                         brush->numplanes = 5;
362                         VectorNormalize(brush->planes[0].normal);
363                         brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
364                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
365                         brush->planes[1].dist = -brush->planes[0].dist;
366                         VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
367                         VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
368                         VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
369 #if 1
370                         {
371                                 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
372                                 int i, best;
373                                 float dist, bestdist;
374                                 bestdist = fabs(brush->planes[0].normal[0]);
375                                 best = 0;
376                                 for (i = 1;i < 3;i++)
377                                 {
378                                         dist = fabs(brush->planes[0].normal[i]);
379                                         if (bestdist < dist)
380                                         {
381                                                 bestdist = dist;
382                                                 best = i;
383                                         }
384                                 }
385                                 VectorClear(projectionnormal);
386                                 if (brush->planes[0].normal[best] < 0)
387                                         projectionnormal[best] = -1;
388                                 else
389                                         projectionnormal[best] = 1;
390                                 VectorCopy(edge0, projectionedge0);
391                                 VectorCopy(edge1, projectionedge1);
392                                 VectorCopy(edge2, projectionedge2);
393                                 projectionedge0[best] = 0;
394                                 projectionedge1[best] = 0;
395                                 projectionedge2[best] = 0;
396                                 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
397                                 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
398                                 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
399                         }
400 #else
401                         CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
402                         CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
403                         CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
404 #endif
405                         VectorNormalize(brush->planes[2].normal);
406                         VectorNormalize(brush->planes[3].normal);
407                         VectorNormalize(brush->planes[4].normal);
408                         brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
409                         brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
410                         brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
411
412                         if (developer.integer >= 100)
413                         {
414                                 // validation code
415 #if 0
416                                 float temp[3];
417
418                                 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
419                                 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
420                                 CrossProduct(edge0, edge1, normal);
421                                 VectorNormalize(normal);
422                                 VectorSubtract(normal, brush->planes[0].normal, temp);
423                                 if (VectorLength(temp) > 0.01f)
424                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: TriangleNormal gave wrong answer (%f %f %f != correct answer %f %f %f)\n", brush->planes->normal[0], brush->planes->normal[1], brush->planes->normal[2], normal[0], normal[1], normal[2]);
425                                 if (fabs(DotProduct(brush->planes[1].normal, brush->planes[0].normal) - -1.0f) > 0.01f || fabs(brush->planes[1].dist - -brush->planes[0].dist) > 0.01f)
426                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 1 (%f %f %f %f) is not opposite plane 0 (%f %f %f %f)\n", brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
427 #if 0
428                                 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
429                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[2].dist);
430                                 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
431                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[3].dist);
432                                 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
433                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[4].dist);
434                                 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
435                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to edge 0 (%f %f %f to %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2]);
436                                 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
437                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to edge 1 (%f %f %f to %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2]);
438                                 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
439                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to edge 2 (%f %f %f to %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2]);
440 #endif
441 #endif
442                                 if (fabs(DotProduct(brush->points[0].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f)
443                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off front plane 0 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
444                                 if (fabs(DotProduct(brush->points[0].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f)
445                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off back plane 1 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist);
446                                 if (fabs(DotProduct(brush->points[2].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f || fabs(DotProduct(brush->points[0].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f)
447                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist);
448                                 if (fabs(DotProduct(brush->points[0].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f)
449                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist);
450                                 if (fabs(DotProduct(brush->points[1].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f)
451                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist);
452                         }
453                 }
454         }
455         else
456         {
457                 // choose best surface normal for polygon's plane
458                 bestdist = 0;
459                 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
460                 {
461                         VectorSubtract(p[-1].v, p[0].v, edge0);
462                         VectorSubtract(p[1].v, p[0].v, edge1);
463                         CrossProduct(edge0, edge1, normal);
464                         //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
465                         dist = DotProduct(normal, normal);
466                         if (i == 0 || bestdist < dist)
467                         {
468                                 bestdist = dist;
469                                 VectorCopy(normal, brush->planes->normal);
470                         }
471                 }
472                 if (bestdist < 0.0001f)
473                 {
474                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
475                         brush->numplanes = 0;
476                         return;
477                 }
478                 else
479                 {
480                         brush->numplanes = brush->numpoints + 2;
481                         VectorNormalize(brush->planes->normal);
482                         brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
483
484                         // negate plane to create other side
485                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
486                         brush->planes[1].dist = -brush->planes[0].dist;
487                         for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
488                         {
489                                 VectorSubtract(p->v, p2->v, edge0);
490                                 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
491                                 VectorNormalize(brush->planes[i + 2].normal);
492                                 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
493                         }
494                 }
495         }
496
497         if (developer.integer >= 100)
498         {
499                 // validity check - will be disabled later
500                 Collision_ValidateBrush(brush);
501                 for (i = 0;i < brush->numplanes;i++)
502                 {
503                         int j;
504                         for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
505                                 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
506                                         Con_Printf("Error in brush plane generation, plane %i\n", i);
507                 }
508         }
509 }
510
511 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
512 {
513         colbrushf_t *brush;
514         brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
515         brush->supercontents = supercontents;
516         brush->numpoints = numpoints;
517         brush->numplanes = numpoints + 2;
518         brush->planes = (colplanef_t *)(brush + 1);
519         brush->points = (colpointf_t *)points;
520         Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
521         return brush;
522 }
523
524 // NOTE: start and end of each brush pair must have same numplanes/numpoints
525 void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
526 {
527         int nplane, nplane2, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
528         float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
529         const colplanef_t *startplane, *endplane;
530         texture_t *hittexture = NULL;
531
532         VectorClear(newimpactnormal);
533
534         for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
535         {
536                 nplane2 = nplane;
537                 if (nplane2 >= thatbrush_start->numplanes)
538                 {
539                         nplane2 -= thatbrush_start->numplanes;
540                         startplane = thisbrush_start->planes + nplane2;
541                         endplane = thisbrush_end->planes + nplane2;
542                         if (developer.integer >= 100)
543                         {
544                                 // any brush with degenerate planes is not worth handling
545                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
546                                 {
547                                         Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
548                                         return;
549                                 }
550                                 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
551                                 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
552                                         Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
553                         }
554                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
555                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
556                 }
557                 else
558                 {
559                         startplane = thatbrush_start->planes + nplane2;
560                         endplane = thatbrush_end->planes + nplane2;
561                         if (developer.integer >= 100)
562                         {
563                                 // any brush with degenerate planes is not worth handling
564                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
565                                 {
566                                         Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
567                                         return;
568                                 }
569                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
570                                 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
571                                         Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
572                         }
573                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
574                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
575                 }
576                 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
577
578                 if(d1 > d2)
579                 {
580                         // moving into brush
581                         if(d2 > 0)
582                                 return;
583                         if(d1 > 0)
584                         {
585                                 // enter
586                                 fstartsolid = false;
587                                 imove = 1 / (d1 - d2);
588                                 f = (d1 - collision_enternudge.value) * imove;
589                                 if (enterfrac < f)
590                                 {
591                                         enterfrac = f;
592                                         enterfrac2 = f - collision_impactnudge.value * imove;
593                                         VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
594                                         hitq3surfaceflags = startplane->q3surfaceflags;
595                                         hittexture = startplane->texture;
596                                 }
597                         }
598                 }
599                 else
600                 {
601                         // moving out of brush
602                         if(d1 > 0)
603                                 return;
604                         if(d2 > 0)
605                         {
606                                 // leave
607                                 fendsolid = false;
608                                 f = (d1 + collision_leavenudge.value) / (d1 - d2);
609                                 if (leavefrac > f)
610                                         leavefrac = f;
611                         }
612                 }
613         }
614
615         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
616         if (fstartsolid)
617         {
618                 trace->startsupercontents |= thatbrush_start->supercontents;
619                 if (brushsolid)
620                 {
621                         trace->startsolid = true;
622                         if (fendsolid)
623                                 trace->allsolid = true;
624                 }
625         }
626
627         // LordHavoc: we need an epsilon nudge here because for a point trace the
628         // penetrating line segment is normally zero length if this brush was
629         // generated from a polygon (infinitely thin), and could even be slightly
630         // positive or negative due to rounding errors in that case.
631         if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
632         {
633 #if 0
634                 // broken
635                 if (thatbrush_start->ispolygon)
636                 {
637                         d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
638                         d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
639                         move = d1 - d2;
640                         if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
641                                 return;
642                         // enter
643                         imove = 1 / move;
644                         enterfrac = (d1 - collision_enternudge.value) * imove;
645                         if (enterfrac < trace->realfraction)
646                         {
647                                 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
648                                 trace->hitsupercontents = thatbrush_start->supercontents;
649                                 trace->hitq3surfaceflags = thatbrush_start->planes[0].q3surfaceflags;
650                                 trace->hittexture = thatbrush_start->planes[0].texture;
651                                 trace->realfraction = bound(0, enterfrac, 1);
652                                 trace->fraction = bound(0, enterfrac2, 1);
653                                 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
654                         }
655                 }
656                 else
657 #endif
658                 {
659                         trace->hitsupercontents = thatbrush_start->supercontents;
660                         trace->hitq3surfaceflags = hitq3surfaceflags;
661                         trace->hittexture = hittexture;
662                         trace->realfraction = bound(0, enterfrac, 1);
663                         trace->fraction = bound(0, enterfrac2, 1);
664                         VectorCopy(newimpactnormal, trace->plane.normal);
665                 }
666         }
667 }
668
669 // NOTE: start and end brush pair must have same numplanes/numpoints
670 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
671 {
672         int nplane, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
673         float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
674         const colplanef_t *startplane, *endplane;
675         texture_t *hittexture = NULL;
676
677         VectorClear(newimpactnormal);
678
679         for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
680         {
681                 startplane = thatbrush_start->planes + nplane;
682                 endplane = thatbrush_end->planes + nplane;
683                 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
684                 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
685                 if (developer.integer >= 100)
686                 {
687                         // any brush with degenerate planes is not worth handling
688                         if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
689                         {
690                                 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
691                                 return;
692                         }
693                         if (thatbrush_start->numpoints)
694                         {
695                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
696                                 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
697                                         Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
698                         }
699                 }
700
701                 if (d1 > d2)
702                 {
703                         // moving into brush
704                         if (d2 > 0)
705                                 return;
706                         if (d1 > 0)
707                         {
708                                 // enter
709                                 fstartsolid = false;
710                                 imove = 1 / (d1 - d2);
711                                 f = (d1 - collision_enternudge.value) * imove;
712                                 if (enterfrac < f)
713                                 {
714                                         enterfrac = f;
715                                         enterfrac2 = f - collision_impactnudge.value * imove;
716                                         VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
717                                         hitq3surfaceflags = startplane->q3surfaceflags;
718                                         hittexture = startplane->texture;
719                                 }
720                         }
721                 }
722                 else
723                 {
724                         // moving out of brush
725                         if (d1 > 0)
726                                 return;
727                         if (d2 > 0)
728                         {
729                                 // leave
730                                 fendsolid = false;
731                                 f = (d1 + collision_leavenudge.value) / (d1 - d2);
732                                 if (leavefrac > f)
733                                         leavefrac = f;
734                         }
735                 }
736         }
737
738         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
739         if (fstartsolid)
740         {
741                 trace->startsupercontents |= thatbrush_start->supercontents;
742                 if (brushsolid)
743                 {
744                         trace->startsolid = true;
745                         if (fendsolid)
746                                 trace->allsolid = true;
747                 }
748         }
749
750         // LordHavoc: we need an epsilon nudge here because for a point trace the
751         // penetrating line segment is normally zero length if this brush was
752         // generated from a polygon (infinitely thin), and could even be slightly
753         // positive or negative due to rounding errors in that case.
754         if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac <= leavefrac)
755         {
756 #if 0
757                 // broken
758                 if (thatbrush_start->ispolygon)
759                 {
760                         d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
761                         d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
762                         move = d1 - d2;
763                         if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
764                                 return;
765                         // enter
766                         imove = 1 / move;
767                         enterfrac = (d1 - collision_enternudge.value) * imove;
768                         if (enterfrac < trace->realfraction)
769                         {
770                                 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
771                                 trace->hitsupercontents = thatbrush_start->supercontents;
772                                 trace->hitq3surfaceflags = hitq3surfaceflags;
773                                 trace->hittexture = hittexture;
774                                 trace->realfraction = bound(0, enterfrac, 1);
775                                 trace->fraction = bound(0, enterfrac2, 1);
776                                 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
777                         }
778                 }
779                 else
780 #endif
781                 {
782                         trace->hitsupercontents = thatbrush_start->supercontents;
783                         trace->hitq3surfaceflags = hitq3surfaceflags;
784                         trace->hittexture = hittexture;
785                         trace->realfraction = bound(0, enterfrac, 1);
786                         trace->fraction = bound(0, enterfrac2, 1);
787                         VectorCopy(newimpactnormal, trace->plane.normal);
788                 }
789         }
790 }
791
792 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
793 {
794         int nplane;
795         const colplanef_t *plane;
796
797         for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
798                 if (DotProduct(plane->normal, point) > plane->dist)
799                         return;
800
801         trace->startsupercontents |= thatbrush->supercontents;
802         if (trace->hitsupercontentsmask & thatbrush->supercontents)
803         {
804                 trace->startsolid = true;
805                 trace->allsolid = true;
806         }
807 }
808
809 static colpointf_t polyf_points[256];
810 static colplanef_t polyf_planes[256 + 2];
811 static colbrushf_t polyf_brush;
812
813 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
814 {
815         while (numpoints--)
816         {
817                 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
818                 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
819                 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
820         }
821 }
822
823 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
824 {
825         if (numpoints > 256)
826         {
827                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
828                 return;
829         }
830         polyf_brush.numpoints = numpoints;
831         polyf_brush.numplanes = numpoints + 2;
832         //polyf_brush.points = (colpointf_t *)points;
833         polyf_brush.planes = polyf_planes;
834         polyf_brush.supercontents = supercontents;
835         polyf_brush.points = polyf_points;
836         Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
837         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
838         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
839         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
840 }
841
842 void Collision_TraceBrushTriangleMeshFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, int q3surfaceflags, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
843 {
844         int i;
845         float facemins[3], facemaxs[3];
846         polyf_brush.numpoints = 3;
847         polyf_brush.numplanes = 5;
848         polyf_brush.points = polyf_points;
849         polyf_brush.planes = polyf_planes;
850         polyf_brush.supercontents = supercontents;
851         for (i = 0;i < polyf_brush.numplanes;i++)
852         {
853                 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
854                 polyf_brush.planes[i].texture = texture;
855         }
856         for (i = 0;i < numtriangles;i++, element3i += 3)
857         {
858                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
859                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
860                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
861                 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
862                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
863                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
864                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
865                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
866                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
867                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
868                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
869                 {
870                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
871                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
872                         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
873                 }
874         }
875 }
876
877 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
878 {
879         if (numpoints > 256)
880         {
881                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
882                 return;
883         }
884         polyf_brush.numpoints = numpoints;
885         polyf_brush.numplanes = numpoints + 2;
886         //polyf_brush.points = (colpointf_t *)points;
887         polyf_brush.points = polyf_points;
888         Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
889         polyf_brush.planes = polyf_planes;
890         polyf_brush.supercontents = supercontents;
891         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
892         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
893         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
894 }
895
896 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, int q3surfaceflags, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
897 {
898         int i;
899 #if 1
900         // FIXME: snap vertices?
901         for (i = 0;i < numtriangles;i++, element3i += 3)
902                 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
903 #else
904         polyf_brush.numpoints = 3;
905         polyf_brush.numplanes = 5;
906         polyf_brush.points = polyf_points;
907         polyf_brush.planes = polyf_planes;
908         polyf_brush.supercontents = supercontents;
909         for (i = 0;i < polyf_brush.numplanes;i++)
910         {
911                 polyf_brush.planes[i].supercontents = supercontents;
912                 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
913                 polyf_brush.planes[i].texture = texture;
914         }
915         for (i = 0;i < numtriangles;i++, element3i += 3)
916         {
917                 float facemins[3], facemaxs[3];
918                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
919                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
920                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
921                 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
922                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
923                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
924                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
925                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
926                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
927                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
928                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
929                 {
930                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
931                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
932                         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
933                 }
934         }
935 #endif
936 }
937
938
939 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
940 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
941 static colbrushf_t polyf_brushstart, polyf_brushend;
942
943 void Collision_TraceBrushPolygonTransformFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, const matrix4x4_t *polygonmatrixstart, const matrix4x4_t *polygonmatrixend, int supercontents, int q3surfaceflags, texture_t *texture)
944 {
945         int i;
946         if (numpoints > 256)
947         {
948                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
949                 return;
950         }
951         polyf_brushstart.numpoints = numpoints;
952         polyf_brushstart.numplanes = numpoints + 2;
953         polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
954         polyf_brushstart.planes = polyf_planesstart;
955         polyf_brushstart.supercontents = supercontents;
956         for (i = 0;i < numpoints;i++)
957                 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
958         polyf_brushend.numpoints = numpoints;
959         polyf_brushend.numplanes = numpoints + 2;
960         polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
961         polyf_brushend.planes = polyf_planesend;
962         polyf_brushend.supercontents = supercontents;
963         for (i = 0;i < numpoints;i++)
964                 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
965         for (i = 0;i < polyf_brushstart.numplanes;i++)
966         {
967                 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
968                 polyf_brushstart.planes[i].texture = texture;
969         }
970         Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
971         Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
972         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
973         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
974
975         //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
976         //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
977
978         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
979 }
980
981
982
983 #define MAX_BRUSHFORBOX 16
984 static int brushforbox_index = 0;
985 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
986 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
987 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
988 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
989
990 void Collision_InitBrushForBox(void)
991 {
992         int i;
993         for (i = 0;i < MAX_BRUSHFORBOX;i++)
994         {
995                 brushforbox_brush[i].numpoints = 8;
996                 brushforbox_brush[i].numplanes = 6;
997                 brushforbox_brush[i].points = brushforbox_point + i * 8;
998                 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
999                 brushforpoint_brush[i].numpoints = 1;
1000                 brushforpoint_brush[i].numplanes = 0;
1001                 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1002                 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1003         }
1004 }
1005
1006 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1007 {
1008         int i, j;
1009         vec3_t v;
1010         colbrushf_t *brush;
1011         if (brushforbox_brush[0].numpoints == 0)
1012                 Collision_InitBrushForBox();
1013         // FIXME: these probably don't actually need to be normalized if the collision code does not care
1014         if (VectorCompare(mins, maxs))
1015         {
1016                 // point brush
1017                 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1018                 VectorCopy(mins, brush->points->v);
1019         }
1020         else
1021         {
1022                 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1023                 // FIXME: optimize
1024                 for (i = 0;i < 8;i++)
1025                 {
1026                         v[0] = i & 1 ? maxs[0] : mins[0];
1027                         v[1] = i & 2 ? maxs[1] : mins[1];
1028                         v[2] = i & 4 ? maxs[2] : mins[2];
1029                         Matrix4x4_Transform(matrix, v, brush->points[i].v);
1030                 }
1031                 // FIXME: optimize!
1032                 for (i = 0;i < 6;i++)
1033                 {
1034                         VectorClear(v);
1035                         v[i >> 1] = i & 1 ? 1 : -1;
1036                         Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1037                         VectorNormalize(brush->planes[i].normal);
1038                 }
1039         }
1040         brush->supercontents = supercontents;
1041         for (j = 0;j < brush->numplanes;j++)
1042         {
1043                 brush->planes[j].q3surfaceflags = q3surfaceflags;
1044                 brush->planes[j].texture = texture;
1045                 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1046         }
1047         VectorCopy(brush->points[0].v, brush->mins);
1048         VectorCopy(brush->points[0].v, brush->maxs);
1049         for (j = 1;j < brush->numpoints;j++)
1050         {
1051                 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1052                 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1053                 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1054                 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1055                 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1056                 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1057         }
1058         brush->mins[0] -= 1;
1059         brush->mins[1] -= 1;
1060         brush->mins[2] -= 1;
1061         brush->maxs[0] += 1;
1062         brush->maxs[1] += 1;
1063         brush->maxs[2] += 1;
1064         Collision_ValidateBrush(brush);
1065         return brush;
1066 }
1067
1068 void Collision_ClipTrace_BrushBox(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask, int supercontents, int q3surfaceflags, texture_t *texture)
1069 {
1070         colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1071         vec3_t startmins, startmaxs, endmins, endmaxs;
1072
1073         // create brushes for the collision
1074         VectorAdd(start, mins, startmins);
1075         VectorAdd(start, maxs, startmaxs);
1076         VectorAdd(end, mins, endmins);
1077         VectorAdd(end, maxs, endmaxs);
1078         boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1079         thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1080         thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1081
1082         memset(trace, 0, sizeof(trace_t));
1083         trace->hitsupercontentsmask = hitsupercontentsmask;
1084         trace->fraction = 1;
1085         trace->realfraction = 1;
1086         trace->allsolid = true;
1087         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1088 }
1089
1090 //pseudocode for detecting line/sphere overlap without calculating an impact point
1091 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1092
1093 // LordHavoc: currently unused, but tested
1094 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1095 // by simply adding the moving sphere's radius to the sphereradius parameter,
1096 // all the results are correct (impactpoint, impactnormal, and fraction)
1097 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1098 {
1099         double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1100         // make sure the impactpoint and impactnormal are valid even if there is
1101         // no collision
1102         VectorCopy(lineend, impactpoint);
1103         VectorClear(impactnormal);
1104         // calculate line direction
1105         VectorSubtract(lineend, linestart, dir);
1106         // normalize direction
1107         linelength = VectorLength(dir);
1108         if (linelength)
1109         {
1110                 scale = 1.0 / linelength;
1111                 VectorScale(dir, scale, dir);
1112         }
1113         // this dotproduct calculates the distance along the line at which the
1114         // sphere origin is (nearest point to the sphere origin on the line)
1115         impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1116         // calculate point on line at that distance, and subtract the
1117         // sphereorigin from it, so we have a vector to measure for the distance
1118         // of the line from the sphereorigin (deviation, how off-center it is)
1119         VectorMA(linestart, impactdist, dir, v);
1120         VectorSubtract(v, sphereorigin, v);
1121         deviationdist = VectorLength2(v);
1122         // if outside the radius, it's a miss for sure
1123         // (we do this comparison using squared radius to avoid a sqrt)
1124         if (deviationdist > sphereradius*sphereradius)
1125                 return 1; // miss (off to the side)
1126         // nudge back to find the correct impact distance
1127         impactdist += deviationdist - sphereradius;
1128         if (impactdist >= linelength)
1129                 return 1; // miss (not close enough)
1130         if (impactdist < 0)
1131                 return 1; // miss (linestart is past or inside sphere)
1132         // calculate new impactpoint
1133         VectorMA(linestart, impactdist, dir, impactpoint);
1134         // calculate impactnormal (surface normal at point of impact)
1135         VectorSubtract(impactpoint, sphereorigin, impactnormal);
1136         // normalize impactnormal
1137         VectorNormalize(impactnormal);
1138         // return fraction of movement distance
1139         return impactdist / linelength;
1140 }
1141
1142 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2, int supercontents, int q3surfaceflags, texture_t *texture)
1143 {
1144 #if 1
1145         // more optimized
1146         float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1147
1148         // this function executes:
1149         // 32 ops when line starts behind triangle
1150         // 38 ops when line ends infront of triangle
1151         // 43 ops when line fraction is already closer than this triangle
1152         // 72 ops when line is outside edge 01
1153         // 92 ops when line is outside edge 21
1154         // 115 ops when line is outside edge 02
1155         // 123 ops when line impacts triangle and updates trace results
1156
1157         // this code is designed for clockwise triangles, conversion to
1158         // counterclockwise would require swapping some things around...
1159         // it is easier to simply swap the point0 and point2 parameters to this
1160         // function when calling it than it is to rewire the internals.
1161
1162         // calculate the faceplanenormal of the triangle, this represents the front side
1163         // 15 ops
1164         VectorSubtract(point0, point1, edge01);
1165         VectorSubtract(point2, point1, edge21);
1166         CrossProduct(edge01, edge21, faceplanenormal);
1167         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1168         // 6 ops
1169         faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1170         if (faceplanenormallength2 < 0.0001f)
1171                 return;
1172         // calculate the distance
1173         // 5 ops
1174         faceplanedist = DotProduct(point0, faceplanenormal);
1175
1176         // if start point is on the back side there is no collision
1177         // (we don't care about traces going through the triangle the wrong way)
1178
1179         // calculate the start distance
1180         // 6 ops
1181         d1 = DotProduct(faceplanenormal, linestart);
1182         if (d1 <= faceplanedist)
1183                 return;
1184
1185         // calculate the end distance
1186         // 6 ops
1187         d2 = DotProduct(faceplanenormal, lineend);
1188         // if both are in front, there is no collision
1189         if (d2 >= faceplanedist)
1190                 return;
1191
1192         // from here on we know d1 is >= 0 and d2 is < 0
1193         // this means the line starts infront and ends behind, passing through it
1194
1195         // calculate the recipricol of the distance delta,
1196         // so we can use it multiple times cheaply (instead of division)
1197         // 2 ops
1198         d = 1.0f / (d1 - d2);
1199         // calculate the impact fraction by taking the start distance (> 0)
1200         // and subtracting the face plane distance (this is the distance of the
1201         // triangle along that same normal)
1202         // then multiply by the recipricol distance delta
1203         // 2 ops
1204         f = (d1 - faceplanedist) * d;
1205         // skip out if this impact is further away than previous ones
1206         // 1 ops
1207         if (f > trace->realfraction)
1208                 return;
1209         // calculate the perfect impact point for classification of insidedness
1210         // 9 ops
1211         impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1212         impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1213         impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1214
1215         // calculate the edge normal and reject if impact is outside triangle
1216         // (an edge normal faces away from the triangle, to get the desired normal
1217         //  a crossproduct with the faceplanenormal is used, and because of the way
1218         // the insidedness comparison is written it does not need to be normalized)
1219
1220         // first use the two edges from the triangle plane math
1221         // the other edge only gets calculated if the point survives that long
1222
1223         // 20 ops
1224         CrossProduct(edge01, faceplanenormal, edgenormal);
1225         if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1226                 return;
1227
1228         // 20 ops
1229         CrossProduct(faceplanenormal, edge21, edgenormal);
1230         if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1231                 return;
1232
1233         // 23 ops
1234         VectorSubtract(point0, point2, edge02);
1235         CrossProduct(faceplanenormal, edge02, edgenormal);
1236         if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1237                 return;
1238
1239         // 8 ops (rare)
1240
1241         // store the new trace fraction
1242         trace->realfraction = f;
1243
1244         // calculate a nudged fraction to keep it out of the surface
1245         // (the main fraction remains perfect)
1246         trace->fraction = f - collision_impactnudge.value * d;
1247
1248         // store the new trace plane (because collisions only happen from
1249         // the front this is always simply the triangle normal, never flipped)
1250         d = 1.0 / sqrt(faceplanenormallength2);
1251         VectorScale(faceplanenormal, d, trace->plane.normal);
1252         trace->plane.dist = faceplanedist * d;
1253
1254         trace->hitsupercontents = supercontents;
1255         trace->hitq3surfaceflags = q3surfaceflags;
1256         trace->hittexture = texture;
1257 #else
1258         float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1259
1260         // this code is designed for clockwise triangles, conversion to
1261         // counterclockwise would require swapping some things around...
1262         // it is easier to simply swap the point0 and point2 parameters to this
1263         // function when calling it than it is to rewire the internals.
1264
1265         // calculate the unnormalized faceplanenormal of the triangle,
1266         // this represents the front side
1267         TriangleNormal(point0, point1, point2, faceplanenormal);
1268         // there's no point in processing a degenerate triangle
1269         // (GIGO - Garbage In, Garbage Out)
1270         if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1271                 return;
1272         // calculate the unnormalized distance
1273         faceplanedist = DotProduct(point0, faceplanenormal);
1274
1275         // calculate the unnormalized start distance
1276         d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1277         // if start point is on the back side there is no collision
1278         // (we don't care about traces going through the triangle the wrong way)
1279         if (d1 <= 0)
1280                 return;
1281
1282         // calculate the unnormalized end distance
1283         d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1284         // if both are in front, there is no collision
1285         if (d2 >= 0)
1286                 return;
1287
1288         // from here on we know d1 is >= 0 and d2 is < 0
1289         // this means the line starts infront and ends behind, passing through it
1290
1291         // calculate the recipricol of the distance delta,
1292         // so we can use it multiple times cheaply (instead of division)
1293         d = 1.0f / (d1 - d2);
1294         // calculate the impact fraction by taking the start distance (> 0)
1295         // and subtracting the face plane distance (this is the distance of the
1296         // triangle along that same normal)
1297         // then multiply by the recipricol distance delta
1298         f = d1 * d;
1299         // skip out if this impact is further away than previous ones
1300         if (f > trace->realfraction)
1301                 return;
1302         // calculate the perfect impact point for classification of insidedness
1303         impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1304         impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1305         impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1306
1307         // calculate the edge normal and reject if impact is outside triangle
1308         // (an edge normal faces away from the triangle, to get the desired normal
1309         //  a crossproduct with the faceplanenormal is used, and because of the way
1310         // the insidedness comparison is written it does not need to be normalized)
1311
1312         VectorSubtract(point2, point0, edge);
1313         CrossProduct(edge, faceplanenormal, edgenormal);
1314         if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1315                 return;
1316
1317         VectorSubtract(point0, point1, edge);
1318         CrossProduct(edge, faceplanenormal, edgenormal);
1319         if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1320                 return;
1321
1322         VectorSubtract(point1, point2, edge);
1323         CrossProduct(edge, faceplanenormal, edgenormal);
1324         if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1325                 return;
1326
1327         // store the new trace fraction
1328         trace->realfraction = bound(0, f, 1);
1329
1330         // store the new trace plane (because collisions only happen from
1331         // the front this is always simply the triangle normal, never flipped)
1332         VectorNormalize(faceplanenormal);
1333         VectorCopy(faceplanenormal, trace->plane.normal);
1334         trace->plane.dist = DotProduct(point0, faceplanenormal);
1335
1336         // calculate the normalized start and end distances
1337         d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1338         d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1339
1340         // calculate a nudged fraction to keep it out of the surface
1341         // (the main fraction remains perfect)
1342         fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1343         trace->fraction = bound(0, fnudged, 1);
1344
1345         // store the new trace endpos
1346         // not needed, it's calculated later when the trace is finished
1347         //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1348         //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1349         //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1350         trace->hitsupercontents = supercontents;
1351         trace->hitq3surfaceflags = q3surfaceflags;
1352         trace->hittexture = texture;
1353 #endif
1354 }
1355
1356 typedef struct colbspnode_s
1357 {
1358         mplane_t plane;
1359         struct colbspnode_s *children[2];
1360         // the node is reallocated or split if max is reached
1361         int numcolbrushf;
1362         int maxcolbrushf;
1363         colbrushf_t **colbrushflist;
1364         //int numcolbrushd;
1365         //int maxcolbrushd;
1366         //colbrushd_t **colbrushdlist;
1367 }
1368 colbspnode_t;
1369
1370 typedef struct colbsp_s
1371 {
1372         mempool_t *mempool;
1373         colbspnode_t *nodes;
1374 }
1375 colbsp_t;
1376
1377 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1378 {
1379         colbsp_t *bsp;
1380         bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1381         bsp->mempool = mempool;
1382         bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1383         return bsp;
1384 }
1385
1386 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1387 {
1388         if (node->children[0])
1389                 Collision_FreeCollisionBSPNode(node->children[0]);
1390         if (node->children[1])
1391                 Collision_FreeCollisionBSPNode(node->children[1]);
1392         while (--node->numcolbrushf)
1393                 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1394         //while (--node->numcolbrushd)
1395         //      Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1396         Mem_Free(node);
1397 }
1398
1399 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1400 {
1401         Collision_FreeCollisionBSPNode(bsp->nodes);
1402         Mem_Free(bsp);
1403 }
1404
1405 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1406 {
1407         int i;
1408         colpointf_t *ps, *pe;
1409         float tempstart[3], tempend[3];
1410         VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1411         VectorCopy(mins, maxs);
1412         for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1413         {
1414                 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1415                 VectorLerp(ps->v, endfrac, pe->v, tempend);
1416                 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1417                 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1418                 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1419                 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1420                 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1421                 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1422         }
1423         mins[0] -= 1;
1424         mins[1] -= 1;
1425         mins[2] -= 1;
1426         maxs[0] += 1;
1427         maxs[1] += 1;
1428         maxs[2] += 1;
1429 }
1430