Debian Codegraph @

code/qcommon/cm_patch.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "cm_local.h"
24 #include "cm_patch.h"
25
26 /*
27
28 This file does not reference any globals, and has these entry points:
29
30 void CM_ClearLevelPatches( void );
31 struct patchCollide_s *CM_GeneratePatchCollide( int width, int height, const vec3_t *points );
32 void CM_TraceThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc );
33 qboolean CM_PositionTestInPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc );
34 void CM_DrawDebugSurface( void (*drawPoly)(int color, int numPoints, flaot *points) );
35
36
37 WARNING: this may misbehave with meshes that have rows or columns that only
38 degenerate a few triangles. Completely degenerate rows and columns are handled
39 properly.
40 */
41
42 int c_totalPatchBlocks;
43 int c_totalPatchSurfaces;
44 int c_totalPatchEdges;
45
46 static const patchCollide_t *debugPatchCollide;
47 static const facet_t *debugFacet;
48 static qboolean debugBlock;
49 static vec3_t debugBlockPoints[4];
50
51 /*
52 =================
53 CM_ClearLevelPatches
54 =================
55 */
56 void CM_ClearLevelPatches( void ) {
57 debugPatchCollide = NULL;
58 debugFacet = NULL;
59 }
60
61 /*
62 =================
63 CM_SignbitsForNormal
64 =================
65 */
66 static int CM_SignbitsForNormal( vec3_t normal ) {
67 int bits, j;
68
69 bits = 0;
70 for (j=0 ; j<3 ; j++) {
71 if ( normal[j] < 0 ) {
72 bits |= 1<<j;
73 }
74 }
75 return bits;
76 }
77
78 /*
79 =====================
80 CM_PlaneFromPoints
81
82 Returns false if the triangle is degenrate.
83 The normal will point out of the clock for clockwise ordered points
84 =====================
85 */
86 static qboolean CM_PlaneFromPoints( vec4_t plane, vec3_t a, vec3_t b, vec3_t c ) {
87 vec3_t d1, d2;
88
89 VectorSubtract( b, a, d1 );
90 VectorSubtract( c, a, d2 );
91 CrossProduct( d2, d1, plane );
92 if ( VectorNormalize( plane ) == 0 ) {
93 return qfalse;
94 }
95
96 plane[3] = DotProduct( a, plane );
97 return qtrue;
98 }
99
100
101 /*
102 ================================================================================
103
104 GRID SUBDIVISION
105
106 ================================================================================
107 */
108
109 /*
110 =================
111 CM_NeedsSubdivision
112
113 Returns true if the given quadratic curve is not flat enough for our
114 collision detection purposes
115 =================
116 */
117 static qboolean CM_NeedsSubdivision( vec3_t a, vec3_t b, vec3_t c ) {
118 vec3_t cmid;
119 vec3_t lmid;
120 vec3_t delta;
121 float dist;
122 int i;
123
124 // calculate the linear midpoint
125 for ( i = 0 ; i < 3 ; i++ ) {
126 lmid[i] = 0.5*(a[i] + c[i]);
127 }
128
129 // calculate the exact curve midpoint
130 for ( i = 0 ; i < 3 ; i++ ) {
131 cmid[i] = 0.5 * ( 0.5*(a[i] + b[i]) + 0.5*(b[i] + c[i]) );
132 }
133
134 // see if the curve is far enough away from the linear mid
135 VectorSubtract( cmid, lmid, delta );
136 dist = VectorLength( delta );
137
138 return dist >= SUBDIVIDE_DISTANCE;
139 }
140
141 /*
142 ===============
143 CM_Subdivide
144
145 a, b, and c are control points.
146 the subdivided sequence will be: a, out1, out2, out3, c
147 ===============
148 */
149 static void CM_Subdivide( vec3_t a, vec3_t b, vec3_t c, vec3_t out1, vec3_t out2, vec3_t out3 ) {
150 int i;
151
152 for ( i = 0 ; i < 3 ; i++ ) {
153 out1[i] = 0.5 * (a[i] + b[i]);
154 out3[i] = 0.5 * (b[i] + c[i]);
155 out2[i] = 0.5 * (out1[i] + out3[i]);
156 }
157 }
158
159 /*
160 =================
161 CM_TransposeGrid
162
163 Swaps the rows and columns in place
164 =================
165 */
166 static void CM_TransposeGrid( cGrid_t *grid ) {
167 int i, j, l;
168 vec3_t temp;
169 qboolean tempWrap;
170
171 if ( grid->width > grid->height ) {
172 for ( i = 0 ; i < grid->height ; i++ ) {
173 for ( j = i + 1 ; j < grid->width ; j++ ) {
174 if ( j < grid->height ) {
175 // swap the value
176 VectorCopy( grid->points[i][j], temp );
177 VectorCopy( grid->points[j][i], grid->points[i][j] );
178 VectorCopy( temp, grid->points[j][i] );
179 } else {
180 // just copy
181 VectorCopy( grid->points[j][i], grid->points[i][j] );
182 }
183 }
184 }
185 } else {
186 for ( i = 0 ; i < grid->width ; i++ ) {
187 for ( j = i + 1 ; j < grid->height ; j++ ) {
188 if ( j < grid->width ) {
189 // swap the value
190 VectorCopy( grid->points[j][i], temp );
191 VectorCopy( grid->points[i][j], grid->points[j][i] );
192 VectorCopy( temp, grid->points[i][j] );
193 } else {
194 // just copy
195 VectorCopy( grid->points[i][j], grid->points[j][i] );
196 }
197 }
198 }
199 }
200
201 l = grid->width;
202 grid->width = grid->height;
203 grid->height = l;
204
205 tempWrap = grid->wrapWidth;
206 grid->wrapWidth = grid->wrapHeight;
207 grid->wrapHeight = tempWrap;
208 }
209
210 /*
211 ===================
212 CM_SetGridWrapWidth
213
214 If the left and right columns are exactly equal, set grid->wrapWidth qtrue
215 ===================
216 */
217 static void CM_SetGridWrapWidth( cGrid_t *grid ) {
218 int i, j;
219 float d;
220
221 for ( i = 0 ; i < grid->height ; i++ ) {
222 for ( j = 0 ; j < 3 ; j++ ) {
223 d = grid->points[0][i][j] - grid->points[grid->width-1][i][j];
224 if ( d < -WRAP_POINT_EPSILON || d > WRAP_POINT_EPSILON ) {
225 break;
226 }
227 }
228 if ( j != 3 ) {
229 break;
230 }
231 }
232 if ( i == grid->height ) {
233 grid->wrapWidth = qtrue;
234 } else {
235 grid->wrapWidth = qfalse;
236 }
237 }
238
239 /*
240 =================
241 CM_SubdivideGridColumns
242
243 Adds columns as necessary to the grid until
244 all the aproximating points are within SUBDIVIDE_DISTANCE
245 from the true curve
246 =================
247 */
248 static void CM_SubdivideGridColumns( cGrid_t *grid ) {
249 int i, j, k;
250
251 for ( i = 0 ; i < grid->width - 2 ; ) {
252 // grid->points[i][x] is an interpolating control point
253 // grid->points[i+1][x] is an aproximating control point
254 // grid->points[i+2][x] is an interpolating control point
255
256 //
257 // first see if we can collapse the aproximating collumn away
258 //
259 for ( j = 0 ; j < grid->height ; j++ ) {
260 if ( CM_NeedsSubdivision( grid->points[i][j], grid->points[i+1][j], grid->points[i+2][j] ) ) {
261 break;
262 }
263 }
264 if ( j == grid->height ) {
265 // all of the points were close enough to the linear midpoints
266 // that we can collapse the entire column away
267 for ( j = 0 ; j < grid->height ; j++ ) {
268 // remove the column
269 for ( k = i + 2 ; k < grid->width ; k++ ) {
270 VectorCopy( grid->points[k][j], grid->points[k-1][j] );
271 }
272 }
273
274 grid->width--;
275
276 // go to the next curve segment
277 i++;
278 continue;
279 }
280
281 //
282 // we need to subdivide the curve
283 //
284 for ( j = 0 ; j < grid->height ; j++ ) {
285 vec3_t prev, mid, next;
286
287 // save the control points now
288 VectorCopy( grid->points[i][j], prev );
289 VectorCopy( grid->points[i+1][j], mid );
290 VectorCopy( grid->points[i+2][j], next );
291
292 // make room for two additional columns in the grid
293 // columns i+1 will be replaced, column i+2 will become i+4
294 // i+1, i+2, and i+3 will be generated
295 for ( k = grid->width - 1 ; k > i + 1 ; k-- ) {
296 VectorCopy( grid->points[k][j], grid->points[k+2][j] );
297 }
298
299 // generate the subdivided points
300 CM_Subdivide( prev, mid, next, grid->points[i+1][j], grid->points[i+2][j], grid->points[i+3][j] );
301 }
302
303 grid->width += 2;
304
305 // the new aproximating point at i+1 may need to be removed
306 // or subdivided farther, so don't advance i
307 }
308 }
309
310 /*
311 ======================
312 CM_ComparePoints
313 ======================
314 */
315 #define POINT_EPSILON 0.1
316 static qboolean CM_ComparePoints( float *a, float *b ) {
317 float d;
318
319 d = a[0] - b[0];
320 if ( d < -POINT_EPSILON || d > POINT_EPSILON ) {
321 return qfalse;
322 }
323 d = a[1] - b[1];
324 if ( d < -POINT_EPSILON || d > POINT_EPSILON ) {
325 return qfalse;
326 }
327 d = a[2] - b[2];
328 if ( d < -POINT_EPSILON || d > POINT_EPSILON ) {
329 return qfalse;
330 }
331 return qtrue;
332 }
333
334 /*
335 =================
336 CM_RemoveDegenerateColumns
337
338 If there are any identical columns, remove them
339 =================
340 */
341 static void CM_RemoveDegenerateColumns( cGrid_t *grid ) {
342 int i, j, k;
343
344 for ( i = 0 ; i < grid->width - 1 ; i++ ) {
345 for ( j = 0 ; j < grid->height ; j++ ) {
346 if ( !CM_ComparePoints( grid->points[i][j], grid->points[i+1][j] ) ) {
347 break;
348 }
349 }
350
351 if ( j != grid->height ) {
352 continue; // not degenerate
353 }
354
355 for ( j = 0 ; j < grid->height ; j++ ) {
356 // remove the column
357 for ( k = i + 2 ; k < grid->width ; k++ ) {
358 VectorCopy( grid->points[k][j], grid->points[k-1][j] );
359 }
360 }
361 grid->width--;
362
363 // check against the next column
364 i--;
365 }
366 }
367
368 /*
369 ================================================================================
370
371 PATCH COLLIDE GENERATION
372
373 ================================================================================
374 */
375
376 static int numPlanes;
377 static patchPlane_t planes[MAX_PATCH_PLANES];
378
379 static int numFacets;
380 static facet_t facets[MAX_FACETS];
381
382 #define NORMAL_EPSILON 0.0001
383 #define DIST_EPSILON 0.02
384
385 /*
386 ==================
387 CM_PlaneEqual
388 ==================
389 */
390 int CM_PlaneEqual(patchPlane_t *p, float plane[4], int *flipped) {
391 float invplane[4];
392
393 if (
394 fabs(p->plane[0] - plane[0]) < NORMAL_EPSILON
395 && fabs(p->plane[1] - plane[1]) < NORMAL_EPSILON
396 && fabs(p->plane[2] - plane[2]) < NORMAL_EPSILON
397 && fabs(p->plane[3] - plane[3]) < DIST_EPSILON )
398 {
399 *flipped = qfalse;
400 return qtrue;
401 }
402
403 VectorNegate(plane, invplane);
404 invplane[3] = -plane[3];
405
406 if (
407 fabs(p->plane[0] - invplane[0]) < NORMAL_EPSILON
408 && fabs(p->plane[1] - invplane[1]) < NORMAL_EPSILON
409 && fabs(p->plane[2] - invplane[2]) < NORMAL_EPSILON
410 && fabs(p->plane[3] - invplane[3]) < DIST_EPSILON )
411 {
412 *flipped = qtrue;
413 return qtrue;
414 }
415
416 return qfalse;
417 }
418
419 /*
420 ==================
421 CM_SnapVector
422 ==================
423 */
424 void CM_SnapVector(vec3_t normal) {
425 int i;
426
427 for (i=0 ; i<3 ; i++)
428 {
429 if ( fabs(normal[i] - 1) < NORMAL_EPSILON )
430 {
431 VectorClear (normal);
432 normal[i] = 1;
433 break;
434 }
435 if ( fabs(normal[i] - -1) < NORMAL_EPSILON )
436 {
437 VectorClear (normal);
438 normal[i] = -1;
439 break;
440 }
441 }
442 }
443
444 /*
445 ==================
446 CM_FindPlane2
447 ==================
448 */
449 int CM_FindPlane2(float plane[4], int *flipped) {
450 int i;
451
452 // see if the points are close enough to an existing plane
453 for ( i = 0 ; i < numPlanes ; i++ ) {
454 if (CM_PlaneEqual(&planes[i], plane, flipped)) return i;
455 }
456
457 // add a new plane
458 if ( numPlanes == MAX_PATCH_PLANES ) {
459 Com_Error( ERR_DROP, "MAX_PATCH_PLANES" );
460 }
461
462 Vector4Copy( plane, planes[numPlanes].plane );
463 planes[numPlanes].signbits = CM_SignbitsForNormal( plane );
464
465 numPlanes++;
466
467 *flipped = qfalse;
468
469 return numPlanes-1;
470 }
471
472 /*
473 ==================
474 CM_FindPlane
475 ==================
476 */
477 static int CM_FindPlane( float *p1, float *p2, float *p3 ) {
478 float plane[4];
479 int i;
480 float d;
481
482 if ( !CM_PlaneFromPoints( plane, p1, p2, p3 ) ) {
483 return -1;
484 }
485
486 // see if the points are close enough to an existing plane
487 for ( i = 0 ; i < numPlanes ; i++ ) {
488 if ( DotProduct( plane, planes[i].plane ) < 0 ) {
489 continue; // allow backwards planes?
490 }
491
492 d = DotProduct( p1, planes[i].plane ) - planes[i].plane[3];
493 if ( d < -PLANE_TRI_EPSILON || d > PLANE_TRI_EPSILON ) {
494 continue;
495 }
496
497 d = DotProduct( p2, planes[i].plane ) - planes[i].plane[3];
498 if ( d < -PLANE_TRI_EPSILON || d > PLANE_TRI_EPSILON ) {
499 continue;
500 }
501
502 d = DotProduct( p3, planes[i].plane ) - planes[i].plane[3];
503 if ( d < -PLANE_TRI_EPSILON || d > PLANE_TRI_EPSILON ) {
504 continue;
505 }
506
507 // found it
508 return i;
509 }
510
511 // add a new plane
512 if ( numPlanes == MAX_PATCH_PLANES ) {
513 Com_Error( ERR_DROP, "MAX_PATCH_PLANES" );
514 }
515
516 Vector4Copy( plane, planes[numPlanes].plane );
517 planes[numPlanes].signbits = CM_SignbitsForNormal( plane );
518
519 numPlanes++;
520
521 return numPlanes-1;
522 }
523
524 /*
525 ==================
526 CM_PointOnPlaneSide
527 ==================
528 */
529 static int CM_PointOnPlaneSide( float *p, int planeNum ) {
530 float *plane;
531 float d;
532
533 if ( planeNum == -1 ) {
534 return SIDE_ON;
535 }
536 plane = planes[ planeNum ].plane;
537
538 d = DotProduct( p, plane ) - plane[3];
539
540 if ( d > PLANE_TRI_EPSILON ) {
541 return SIDE_FRONT;
542 }
543
544 if ( d < -PLANE_TRI_EPSILON ) {
545 return SIDE_BACK;
546 }
547
548 return SIDE_ON;
549 }
550
551 /*
552 ==================
553 CM_GridPlane
554 ==================
555 */
556 static int CM_GridPlane( int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2], int i, int j, int tri ) {
557 int p;
558
559 p = gridPlanes[i][j][tri];
560 if ( p != -1 ) {
561 return p;
562 }
563 p = gridPlanes[i][j][!tri];
564 if ( p != -1 ) {
565 return p;
566 }
567
568 // should never happen
569 Com_Printf( "WARNING: CM_GridPlane unresolvable\n" );
570 return -1;
571 }
572
573 /*
574 ==================
575 CM_EdgePlaneNum
576 ==================
577 */
578 static int CM_EdgePlaneNum( cGrid_t *grid, int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2], int i, int j, int k ) {
579 float *p1, *p2;
580 vec3_t up;
581 int p;
582
583 switch ( k ) {
584 case 0: // top border
585 p1 = grid->points[i][j];
586 p2 = grid->points[i+1][j];
587 p = CM_GridPlane( gridPlanes, i, j, 0 );
588 if ( p == -1 ) {
589 return -1;
590 }
591 VectorMA( p1, 4, planes[ p ].plane, up );
592 return CM_FindPlane( p1, p2, up );
593
594 case 2: // bottom border
595 p1 = grid->points[i][j+1];
596 p2 = grid->points[i+1][j+1];
597 p = CM_GridPlane( gridPlanes, i, j, 1 );
598 if ( p == -1 ) {
599 return -1;
600 }
601 VectorMA( p1, 4, planes[ p ].plane, up );
602 return CM_FindPlane( p2, p1, up );
603
604 case 3: // left border
605 p1 = grid->points[i][j];
606 p2 = grid->points[i][j+1];
607 p = CM_GridPlane( gridPlanes, i, j, 1 );
608 if ( p == -1 ) {
609 return -1;
610 }
611 VectorMA( p1, 4, planes[ p ].plane, up );
612 return CM_FindPlane( p2, p1, up );
613
614 case 1: // right border
615 p1 = grid->points[i+1][j];
616 p2 = grid->points[i+1][j+1];
617 p = CM_GridPlane( gridPlanes, i, j, 0 );
618 if ( p == -1 ) {
619 return -1;
620 }
621 VectorMA( p1, 4, planes[ p ].plane, up );
622 return CM_FindPlane( p1, p2, up );
623
624 case 4: // diagonal out of triangle 0
625 p1 = grid->points[i+1][j+1];
626 p2 = grid->points[i][j];
627 p = CM_GridPlane( gridPlanes, i, j, 0 );
628 if ( p == -1 ) {
629 return -1;
630 }
631 VectorMA( p1, 4, planes[ p ].plane, up );
632 return CM_FindPlane( p1, p2, up );
633
634 case 5: // diagonal out of triangle 1
635 p1 = grid->points[i][j];
636 p2 = grid->points[i+1][j+1];
637 p = CM_GridPlane( gridPlanes, i, j, 1 );
638 if ( p == -1 ) {
639 return -1;
640 }
641 VectorMA( p1, 4, planes[ p ].plane, up );
642 return CM_FindPlane( p1, p2, up );
643
644 }
645
646 Com_Error( ERR_DROP, "CM_EdgePlaneNum: bad k" );
647 return -1;
648 }
649
650 /*
651 ===================
652 CM_SetBorderInward
653 ===================
654 */
655 static void CM_SetBorderInward( facet_t *facet, cGrid_t *grid, int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2],
656 int i, int j, int which ) {
657 int k, l;
658 float *points[4];
659 int numPoints;
660
661 switch ( which ) {
662 case -1:
663 points[0] = grid->points[i][j];
664 points[1] = grid->points[i+1][j];
665 points[2] = grid->points[i+1][j+1];
666 points[3] = grid->points[i][j+1];
667 numPoints = 4;
668 break;
669 case 0:
670 points[0] = grid->points[i][j];
671 points[1] = grid->points[i+1][j];
672 points[2] = grid->points[i+1][j+1];
673 numPoints = 3;
674 break;
675 case 1:
676 points[0] = grid->points[i+1][j+1];
677 points[1] = grid->points[i][j+1];
678 points[2] = grid->points[i][j];
679 numPoints = 3;
680 break;
681 default:
682 Com_Error( ERR_FATAL, "CM_SetBorderInward: bad parameter" );
683 numPoints = 0;
684 break;
685 }
686
687 for ( k = 0 ; k < facet->numBorders ; k++ ) {
688 int front, back;
689
690 front = 0;
691 back = 0;
692
693 for ( l = 0 ; l < numPoints ; l++ ) {
694 int side;
695
696 side = CM_PointOnPlaneSide( points[l], facet->borderPlanes[k] );
697 if ( side == SIDE_FRONT ) {
698 front++;
699 } if ( side == SIDE_BACK ) {
700 back++;
701 }
702 }
703
704 if ( front && !back ) {
705 facet->borderInward[k] = qtrue;
706 } else if ( back && !front ) {
707 facet->borderInward[k] = qfalse;
708 } else if ( !front && !back ) {
709 // flat side border
710 facet->borderPlanes[k] = -1;
711 } else {
712 // bisecting side border
713 Com_DPrintf( "WARNING: CM_SetBorderInward: mixed plane sides\n" );
714 facet->borderInward[k] = qfalse;
715 if ( !debugBlock ) {
716 debugBlock = qtrue;
717 VectorCopy( grid->points[i][j], debugBlockPoints[0] );
718 VectorCopy( grid->points[i+1][j], debugBlockPoints[1] );
719 VectorCopy( grid->points[i+1][j+1], debugBlockPoints[2] );
720 VectorCopy( grid->points[i][j+1], debugBlockPoints[3] );
721 }
722 }
723 }
724 }
725
726 /*
727 ==================
728 CM_ValidateFacet
729
730 If the facet isn't bounded by its borders, we screwed up.
731 ==================
732 */
733 static qboolean CM_ValidateFacet( facet_t *facet ) {
734 float plane[4];
735 int j;
736 winding_t *w;
737 vec3_t bounds[2];
738
739 if ( facet->surfacePlane == -1 ) {
740 return qfalse;
741 }
742
743 Vector4Copy( planes[ facet->surfacePlane ].plane, plane );
744 w = BaseWindingForPlane( plane, plane[3] );
745 for ( j = 0 ; j < facet->numBorders && w ; j++ ) {
746 if ( facet->borderPlanes[j] == -1 ) {
747 FreeWinding( w );
748 return qfalse;
749 }
750 Vector4Copy( planes[ facet->borderPlanes[j] ].plane, plane );
751 if ( !facet->borderInward[j] ) {
752 VectorSubtract( vec3_origin, plane, plane );
753 plane[3] = -plane[3];
754 }
755 ChopWindingInPlace( &w, plane, plane[3], 0.1f );
756 }
757
758 if ( !w ) {
759 return qfalse; // winding was completely chopped away
760 }
761
762 // see if the facet is unreasonably large
763 WindingBounds( w, bounds[0], bounds[1] );
764 FreeWinding( w );
765
766 for ( j = 0 ; j < 3 ; j++ ) {
767 if ( bounds[1][j] - bounds[0][j] > MAX_MAP_BOUNDS ) {
768 return qfalse; // we must be missing a plane
769 }
770 if ( bounds[0][j] >= MAX_MAP_BOUNDS ) {
771 return qfalse;
772 }
773 if ( bounds[1][j] <= -MAX_MAP_BOUNDS ) {
774 return qfalse;
775 }
776 }
777 return qtrue; // winding is fine
778 }
779
780 /*
781 ==================
782 CM_AddFacetBevels
783 ==================
784 */
785 void CM_AddFacetBevels( facet_t *facet ) {
786
787 int i, j, k, l;
788 int axis, dir, flipped;
789 float plane[4], d, newplane[4];
790 winding_t *w, *w2;
791 vec3_t mins, maxs, vec, vec2;
792
793 Vector4Copy( planes[ facet->surfacePlane ].plane, plane );
794
795 w = BaseWindingForPlane( plane, plane[3] );
796 for ( j = 0 ; j < facet->numBorders && w ; j++ ) {
797 if (facet->borderPlanes[j] == facet->surfacePlane) continue;
798 Vector4Copy( planes[ facet->borderPlanes[j] ].plane, plane );
799
800 if ( !facet->borderInward[j] ) {
801 VectorSubtract( vec3_origin, plane, plane );
802 plane[3] = -plane[3];
803 }
804
805 ChopWindingInPlace( &w, plane, plane[3], 0.1f );
806 }
807 if ( !w ) {
808 return;
809 }
810
811 WindingBounds(w, mins, maxs);
812
813 // add the axial planes
814 for ( axis = 0 ; axis < 3 ; axis++ )
815 {
816 for ( dir = -1 ; dir <= 1 ; dir += 2 )
817 {
818 VectorClear(plane);
819 plane[axis] = dir;
820 if (dir == 1) {
821 plane[3] = maxs[axis];
822 }
823 else {
824 plane[3] = -mins[axis];
825 }
826 //if it's the surface plane
827 if (CM_PlaneEqual(&planes[facet->surfacePlane], plane, &flipped)) {
828 continue;
829 }
830 // see if the plane is already present
831 for ( i = 0 ; i < facet->numBorders ; i++ ) {
832 if (CM_PlaneEqual(&planes[facet->borderPlanes[i]], plane, &flipped))
833 break;
834 }
835
836 if ( i == facet->numBorders ) {
837 if ( facet->numBorders >= 4 + 6 + 16 ) {
838 Com_Printf( "ERROR: too many bevels\n" );
839 continue;
840 }
841 facet->borderPlanes[facet->numBorders] = CM_FindPlane2(plane, &flipped);
842 facet->borderNoAdjust[facet->numBorders] = 0;
843 facet->borderInward[facet->numBorders] = flipped;
844 facet->numBorders++;
845 }
846 }
847 }
848 //
849 // add the edge bevels
850 //
851 // test the non-axial plane edges
852 for ( j = 0 ; j < w->numpoints ; j++ )
853 {
854 k = (j+1)%w->numpoints;
855 VectorSubtract (w->p[j], w->p[k], vec);
856 //if it's a degenerate edge
857 if (VectorNormalize (vec) < 0.5)
858 continue;
859 CM_SnapVector(vec);
860 for ( k = 0; k < 3 ; k++ )
861 if ( vec[k] == -1 || vec[k] == 1 )
862 break; // axial
863 if ( k < 3 )
864 continue; // only test non-axial edges
865
866 // try the six possible slanted axials from this edge
867 for ( axis = 0 ; axis < 3 ; axis++ )
868 {
869 for ( dir = -1 ; dir <= 1 ; dir += 2 )
870 {
871 // construct a plane
872 VectorClear (vec2);
873 vec2[axis] = dir;
874 CrossProduct (vec, vec2, plane);
875 if (VectorNormalize (plane) < 0.5)
876 continue;
877 plane[3] = DotProduct (w->p[j], plane);
878
879 // if all the points of the facet winding are
880 // behind this plane, it is a proper edge bevel
881 for ( l = 0 ; l < w->numpoints ; l++ )
882 {
883 d = DotProduct (w->p[l], plane) - plane[3];
884 if (d > 0.1)
885 break; // point in front
886 }
887 if ( l < w->numpoints )
888 continue;
889
890 //if it's the surface plane
891 if (CM_PlaneEqual(&planes[facet->surfacePlane], plane, &flipped)) {
892 continue;
893 }
894 // see if the plane is already present
895 for ( i = 0 ; i < facet->numBorders ; i++ ) {
896 if (CM_PlaneEqual(&planes[facet->borderPlanes[i]], plane, &flipped)) {
897 break;
898 }
899 }
900
901 if ( i == facet->numBorders ) {
902 if ( facet->numBorders >= 4 + 6 + 16 ) {
903 Com_Printf( "ERROR: too many bevels\n" );
904 continue;
905 }
906 facet->borderPlanes[facet->numBorders] = CM_FindPlane2(plane, &flipped);
907
908 for ( k = 0 ; k < facet->numBorders ; k++ ) {
909 if (facet->borderPlanes[facet->numBorders] ==
910 facet->borderPlanes[k]) Com_Printf("WARNING: bevel plane already used\n");
911 }
912
913 facet->borderNoAdjust[facet->numBorders] = 0;
914 facet->borderInward[facet->numBorders] = flipped;
915 //
916 w2 = CopyWinding(w);
917 Vector4Copy(planes[facet->borderPlanes[facet->numBorders]].plane, newplane);
918 if (!facet->borderInward[facet->numBorders])
919 {
920 VectorNegate(newplane, newplane);
921 newplane[3] = -newplane[3];
922 } //end if
923 ChopWindingInPlace( &w2, newplane, newplane[3], 0.1f );
924 if (!w2) {
925 Com_DPrintf("WARNING: CM_AddFacetBevels... invalid bevel\n");
926 continue;
927 }
928 else {
929 FreeWinding(w2);
930 }
931 //
932 facet->numBorders++;
933 //already got a bevel
934 // break;
935 }
936 }
937 }
938 }
939 FreeWinding( w );
940
941 #ifndef BSPC
942 //add opposite plane
943 if ( facet->numBorders >= 4 + 6 + 16 ) {
944 Com_Printf( "ERROR: too many bevels\n" );
945 return;
946 }
947 facet->borderPlanes[facet->numBorders] = facet->surfacePlane;
948 facet->borderNoAdjust[facet->numBorders] = 0;
949 facet->borderInward[facet->numBorders] = qtrue;
950 facet->numBorders++;
951 #endif //BSPC
952
953 }
954
955 typedef enum {
956 EN_TOP,
957 EN_RIGHT,
958 EN_BOTTOM,
959 EN_LEFT
960 } edgeName_t;
961
962 /*
963 ==================
964 CM_PatchCollideFromGrid
965 ==================
966 */
967 static void CM_PatchCollideFromGrid( cGrid_t *grid, patchCollide_t *pf ) {
968 int i, j;
969 float *p1, *p2, *p3;
970 int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2];
971 facet_t *facet;
972 int borders[4];
973 int noAdjust[4];
974
975 numPlanes = 0;
976 numFacets = 0;
977
978 // find the planes for each triangle of the grid
979 for ( i = 0 ; i < grid->width - 1 ; i++ ) {
980 for ( j = 0 ; j < grid->height - 1 ; j++ ) {
981 p1 = grid->points[i][j];
982 p2 = grid->points[i+1][j];
983 p3 = grid->points[i+1][j+1];
984 gridPlanes[i][j][0] = CM_FindPlane( p1, p2, p3 );
985
986 p1 = grid->points[i+1][j+1];
987 p2 = grid->points[i][j+1];
988 p3 = grid->points[i][j];
989 gridPlanes[i][j][1] = CM_FindPlane( p1, p2, p3 );
990 }
991 }
992
993 // create the borders for each facet
994 for ( i = 0 ; i < grid->width - 1 ; i++ ) {
995 for ( j = 0 ; j < grid->height - 1 ; j++ ) {
996
997 borders[EN_TOP] = -1;
998 if ( j > 0 ) {
999 borders[EN_TOP] = gridPlanes[i][j-1][1];
1000 } else if ( grid->wrapHeight ) {
1001 borders[EN_TOP] = gridPlanes[i][grid->height-2][1];
1002 }
1003 noAdjust[EN_TOP] = ( borders[EN_TOP] == gridPlanes[i][j][0] );
1004 if ( borders[EN_TOP] == -1 || noAdjust[EN_TOP] ) {
1005 borders[EN_TOP] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 0 );
1006 }
1007
1008 borders[EN_BOTTOM] = -1;
1009 if ( j < grid->height - 2 ) {
1010 borders[EN_BOTTOM] = gridPlanes[i][j+1][0];
1011 } else if ( grid->wrapHeight ) {
1012 borders[EN_BOTTOM] = gridPlanes[i][0][0];
1013 }
1014 noAdjust[EN_BOTTOM] = ( borders[EN_BOTTOM] == gridPlanes[i][j][1] );
1015 if ( borders[EN_BOTTOM] == -1 || noAdjust[EN_BOTTOM] ) {
1016 borders[EN_BOTTOM] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 2 );
1017 }
1018
1019 borders[EN_LEFT] = -1;
1020 if ( i > 0 ) {
1021 borders[EN_LEFT] = gridPlanes[i-1][j][0];
1022 } else if ( grid->wrapWidth ) {
1023 borders[EN_LEFT] = gridPlanes[grid->width-2][j][0];
1024 }
1025 noAdjust[EN_LEFT] = ( borders[EN_LEFT] == gridPlanes[i][j][1] );
1026 if ( borders[EN_LEFT] == -1 || noAdjust[EN_LEFT] ) {
1027 borders[EN_LEFT] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 3 );
1028 }
1029
1030 borders[EN_RIGHT] = -1;
1031 if ( i < grid->width - 2 ) {
1032 borders[EN_RIGHT] = gridPlanes[i+1][j][1];
1033 } else if ( grid->wrapWidth ) {
1034 borders[EN_RIGHT] = gridPlanes[0][j][1];
1035 }
1036 noAdjust[EN_RIGHT] = ( borders[EN_RIGHT] == gridPlanes[i][j][0] );
1037 if ( borders[EN_RIGHT] == -1 || noAdjust[EN_RIGHT] ) {
1038 borders[EN_RIGHT] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 1 );
1039 }
1040
1041 if ( numFacets == MAX_FACETS ) {
1042 Com_Error( ERR_DROP, "MAX_FACETS" );
1043 }
1044 facet = &facets[numFacets];
1045 Com_Memset( facet, 0, sizeof( *facet ) );
1046
1047 if ( gridPlanes[i][j][0] == gridPlanes[i][j][1] ) {
1048 if ( gridPlanes[i][j][0] == -1 ) {
1049 continue; // degenrate
1050 }
1051 facet->surfacePlane = gridPlanes[i][j][0];
1052 facet->numBorders = 4;
1053 facet->borderPlanes[0] = borders[EN_TOP];
1054 facet->borderNoAdjust[0] = noAdjust[EN_TOP];
1055 facet->borderPlanes[1] = borders[EN_RIGHT];
1056 facet->borderNoAdjust[1] = noAdjust[EN_RIGHT];
1057 facet->borderPlanes[2] = borders[EN_BOTTOM];
1058 facet->borderNoAdjust[2] = noAdjust[EN_BOTTOM];
1059 facet->borderPlanes[3] = borders[EN_LEFT];
1060 facet->borderNoAdjust[3] = noAdjust[EN_LEFT];
1061 CM_SetBorderInward( facet, grid, gridPlanes, i, j, -1 );
1062 if ( CM_ValidateFacet( facet ) ) {
1063 CM_AddFacetBevels( facet );
1064 numFacets++;
1065 }
1066 } else {
1067 // two separate triangles
1068 facet->surfacePlane = gridPlanes[i][j][0];
1069 facet->numBorders = 3;
1070 facet->borderPlanes[0] = borders[EN_TOP];
1071 facet->borderNoAdjust[0] = noAdjust[EN_TOP];
1072 facet->borderPlanes[1] = borders[EN_RIGHT];
1073 facet->borderNoAdjust[1] = noAdjust[EN_RIGHT];
1074 facet->borderPlanes[2] = gridPlanes[i][j][1];
1075 if ( facet->borderPlanes[2] == -1 ) {
1076 facet->borderPlanes[2] = borders[EN_BOTTOM];
1077 if ( facet->borderPlanes[2] == -1 ) {
1078 facet->borderPlanes[2] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 4 );
1079 }
1080 }
1081 CM_SetBorderInward( facet, grid, gridPlanes, i, j, 0 );
1082 if ( CM_ValidateFacet( facet ) ) {
1083 CM_AddFacetBevels( facet );
1084 numFacets++;
1085 }
1086
1087 if ( numFacets == MAX_FACETS ) {
1088 Com_Error( ERR_DROP, "MAX_FACETS" );
1089 }
1090 facet = &facets[numFacets];
1091 Com_Memset( facet, 0, sizeof( *facet ) );
1092
1093 facet->surfacePlane = gridPlanes[i][j][1];
1094 facet->numBorders = 3;
1095 facet->borderPlanes[0] = borders[EN_BOTTOM];
1096 facet->borderNoAdjust[0] = noAdjust[EN_BOTTOM];
1097 facet->borderPlanes[1] = borders[EN_LEFT];
1098 facet->borderNoAdjust[1] = noAdjust[EN_LEFT];
1099 facet->borderPlanes[2] = gridPlanes[i][j][0];
1100 if ( facet->borderPlanes[2] == -1 ) {
1101 facet->borderPlanes[2] = borders[EN_TOP];
1102 if ( facet->borderPlanes[2] == -1 ) {
1103 facet->borderPlanes[2] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 5 );
1104 }
1105 }
1106 CM_SetBorderInward( facet, grid, gridPlanes, i, j, 1 );
1107 if ( CM_ValidateFacet( facet ) ) {
1108 CM_AddFacetBevels( facet );
1109 numFacets++;
1110 }
1111 }
1112 }
1113 }
1114
1115 // copy the results out
1116 pf->numPlanes = numPlanes;
1117 pf->numFacets = numFacets;
1118 pf->facets = Hunk_Alloc( numFacets * sizeof( *pf->facets ), h_high );
1119 Com_Memcpy( pf->facets, facets, numFacets * sizeof( *pf->facets ) );
1120 pf->planes = Hunk_Alloc( numPlanes * sizeof( *pf->planes ), h_high );
1121 Com_Memcpy( pf->planes, planes, numPlanes * sizeof( *pf->planes ) );
1122 }
1123
1124
1125 /*
1126 ===================
1127 CM_GeneratePatchCollide
1128
1129 Creates an internal structure that will be used to perform
1130 collision detection with a patch mesh.
1131
1132 Points is packed as concatenated rows.
1133 ===================
1134 */
1135 struct patchCollide_s *CM_GeneratePatchCollide( int width, int height, vec3_t *points ) {
1136 patchCollide_t *pf;
1137 cGrid_t grid;
1138 int i, j;
1139
1140 if ( width <= 2 || height <= 2 || !points ) {
1141 Com_Error( ERR_DROP, "CM_GeneratePatchFacets: bad parameters: (%i, %i, %p)",
1142 width, height, (void *)points );
1143 }
1144
1145 if ( !(width & 1) || !(height & 1) ) {
1146 Com_Error( ERR_DROP, "CM_GeneratePatchFacets: even sizes are invalid for quadratic meshes" );
1147 }
1148
1149 if ( width > MAX_GRID_SIZE || height > MAX_GRID_SIZE ) {
1150 Com_Error( ERR_DROP, "CM_GeneratePatchFacets: source is > MAX_GRID_SIZE" );
1151 }
1152
1153 // build a grid
1154 grid.width = width;
1155 grid.height = height;
1156 grid.wrapWidth = qfalse;
1157 grid.wrapHeight = qfalse;
1158 for ( i = 0 ; i < width ; i++ ) {
1159 for ( j = 0 ; j < height ; j++ ) {
1160 VectorCopy( points[j*width + i], grid.points[i][j] );
1161 }
1162 }
1163
1164 // subdivide the grid
1165 CM_SetGridWrapWidth( &grid );
1166 CM_SubdivideGridColumns( &grid );
1167 CM_RemoveDegenerateColumns( &grid );
1168
1169 CM_TransposeGrid( &grid );
1170
1171 CM_SetGridWrapWidth( &grid );
1172 CM_SubdivideGridColumns( &grid );
1173 CM_RemoveDegenerateColumns( &grid );
1174
1175 // we now have a grid of points exactly on the curve
1176 // the approximate surface defined by these points will be
1177 // collided against
1178 pf = Hunk_Alloc( sizeof( *pf ), h_high );
1179 ClearBounds( pf->bounds[0], pf->bounds[1] );
1180 for ( i = 0 ; i < grid.width ; i++ ) {
1181 for ( j = 0 ; j < grid.height ; j++ ) {
1182 AddPointToBounds( grid.points[i][j], pf->bounds[0], pf->bounds[1] );
1183 }
1184 }
1185
1186 c_totalPatchBlocks += ( grid.width - 1 ) * ( grid.height - 1 );
1187
1188 // generate a bsp tree for the surface
1189 CM_PatchCollideFromGrid( &grid, pf );
1190
1191 // expand by one unit for epsilon purposes
1192 pf->bounds[0][0] -= 1;
1193 pf->bounds[0][1] -= 1;
1194 pf->bounds[0][2] -= 1;
1195
1196 pf->bounds[1][0] += 1;
1197 pf->bounds[1][1] += 1;
1198 pf->bounds[1][2] += 1;
1199
1200 return pf;
1201 }
1202
1203 /*
1204 ================================================================================
1205
1206 TRACE TESTING
1207
1208 ================================================================================
1209 */
1210
1211 /*
1212 ====================
1213 CM_TracePointThroughPatchCollide
1214
1215 special case for point traces because the patch collide "brushes" have no volume
1216 ====================
1217 */
1218 void CM_TracePointThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc ) {
1219 qboolean frontFacing[MAX_PATCH_PLANES];
1220 float intersection[MAX_PATCH_PLANES];
1221 float intersect;
1222 const patchPlane_t *pcPlanes;
1223 const facet_t *facet;
1224 int i, j, k;
1225 float offset;
1226 float d1, d2;
1227 #ifndef BSPC
1228 static cvar_t *cv;
1229 #endif //BSPC
1230
1231 #ifndef BSPC
1232 if ( !cm_playerCurveClip->integer || !tw->isPoint ) {
1233 return;
1234 }
1235 #endif
1236
1237 // determine the trace's relationship to all planes
1238 pcPlanes = pc->planes;
1239 for ( i = 0 ; i < pc->numPlanes ; i++, pcPlanes++ ) {
1240 offset = DotProduct( tw->offsets[ pcPlanes->signbits ], pcPlanes->plane );
1241 d1 = DotProduct( tw->start, pcPlanes->plane ) - pcPlanes->plane[3] + offset;
1242 d2 = DotProduct( tw->end, pcPlanes->plane ) - pcPlanes->plane[3] + offset;
1243 if ( d1 <= 0 ) {
1244 frontFacing[i] = qfalse;
1245 } else {
1246 frontFacing[i] = qtrue;
1247 }
1248 if ( d1 == d2 ) {
1249 intersection[i] = 99999;
1250 } else {
1251 intersection[i] = d1 / ( d1 - d2 );
1252 if ( intersection[i] <= 0 ) {
1253 intersection[i] = 99999;
1254 }
1255 }
1256 }
1257
1258
1259 // see if any of the surface planes are intersected
1260 facet = pc->facets;
1261 for ( i = 0 ; i < pc->numFacets ; i++, facet++ ) {
1262 if ( !frontFacing[facet->surfacePlane] ) {
1263 continue;
1264 }
1265 intersect = intersection[facet->surfacePlane];
1266 if ( intersect < 0 ) {
1267 continue; // surface is behind the starting point
1268 }
1269 if ( intersect > tw->trace.fraction ) {
1270 continue; // already hit something closer
1271 }
1272 for ( j = 0 ; j < facet->numBorders ; j++ ) {
1273 k = facet->borderPlanes[j];
1274 if ( frontFacing[k] ^ facet->borderInward[j] ) {
1275 if ( intersection[k] > intersect ) {
1276 break;
1277 }
1278 } else {
1279 if ( intersection[k] < intersect ) {
1280 break;
1281 }
1282 }
1283 }
1284 if ( j == facet->numBorders ) {
1285 // we hit this facet
1286 #ifndef BSPC
1287 if (!cv) {
1288 cv = Cvar_Get( "r_debugSurfaceUpdate", "1", 0 );
1289 }
1290 if (cv->integer) {
1291 debugPatchCollide = pc;
1292 debugFacet = facet;
1293 }
1294 #endif //BSPC
1295 pcPlanes = &pc->planes[facet->surfacePlane];
1296
1297 // calculate intersection with a slight pushoff
1298 offset = DotProduct( tw->offsets[ pcPlanes->signbits ], pcPlanes->plane );
1299 d1 = DotProduct( tw->start, pcPlanes->plane ) - pcPlanes->plane[3] + offset;
1300 d2 = DotProduct( tw->end, pcPlanes->plane ) - pcPlanes->plane[3] + offset;
1301 tw->trace.fraction = ( d1 - SURFACE_CLIP_EPSILON ) / ( d1 - d2 );
1302
1303 if ( tw->trace.fraction < 0 ) {
1304 tw->trace.fraction = 0;
1305 }
1306
1307 VectorCopy( pcPlanes->plane, tw->trace.plane.normal );
1308 tw->trace.plane.dist = pcPlanes->plane[3];
1309 }
1310 }
1311 }
1312
1313 /*
1314 ====================
1315 CM_CheckFacetPlane
1316 ====================
1317 */
1318 int CM_CheckFacetPlane(float *plane, vec3_t start, vec3_t end, float *enterFrac, float *leaveFrac, int *hit) {
1319 float d1, d2, f;
1320
1321 *hit = qfalse;
1322
1323 d1 = DotProduct( start, plane ) - plane[3];
1324 d2 = DotProduct( end, plane ) - plane[3];
1325
1326 // if completely in front of face, no intersection with the entire facet
1327 if (d1 > 0 && ( d2 >= SURFACE_CLIP_EPSILON || d2 >= d1 ) ) {
1328 return qfalse;
1329 }
1330
1331 // if it doesn't cross the plane, the plane isn't relevant
1332 if (d1 <= 0 && d2 <= 0 ) {
1333 return qtrue;
1334 }
1335
1336 // crosses face
1337 if (d1 > d2) { // enter
1338 f = (d1-SURFACE_CLIP_EPSILON) / (d1-d2);
1339 if ( f < 0 ) {
1340 f = 0;
1341 }
1342 //always favor previous plane hits and thus also the surface plane hit
1343 if (f > *enterFrac) {
1344 *enterFrac = f;
1345 *hit = qtrue;
1346 }
1347 } else { // leave
1348 f = (d1+SURFACE_CLIP_EPSILON) / (d1-d2);
1349 if ( f > 1 ) {
1350 f = 1;
1351 }
1352 if (f < *leaveFrac) {
1353 *leaveFrac = f;
1354 }
1355 }
1356 return qtrue;
1357 }
1358
1359 /*
1360 ====================
1361 CM_TraceThroughPatchCollide
1362 ====================
1363 */
1364 void CM_TraceThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc ) {
1365 int i, j, hit, hitnum;
1366 float offset, enterFrac, leaveFrac, t;
1367 patchPlane_t *pcPlanes;
1368 facet_t *facet;
1369 float plane[4] = {0, 0, 0, 0}, bestplane[4] = {0, 0, 0, 0};
1370 vec3_t startp, endp;
1371 #ifndef BSPC
1372 static cvar_t *cv;
1373 #endif //BSPC
1374
1375 if ( !CM_BoundsIntersect( tw->bounds[0], tw->bounds[1],
1376 pc->bounds[0], pc->bounds[1] ) ) {
1377 return;
1378 }
1379
1380 if (tw->isPoint) {
1381 CM_TracePointThroughPatchCollide( tw, pc );
1382 return;
1383 }
1384
1385 facet = pc->facets;
1386 for ( i = 0 ; i < pc->numFacets ; i++, facet++ ) {
1387 enterFrac = -1.0;
1388 leaveFrac = 1.0;
1389 hitnum = -1;
1390 //
1391 pcPlanes = &pc->planes[ facet->surfacePlane ];
1392 VectorCopy(pcPlanes->plane, plane);
1393 plane[3] = pcPlanes->plane[3];
1394 if ( tw->sphere.use ) {
1395 // adjust the plane distance appropriately for radius
1396 plane[3] += tw->sphere.radius;
1397
1398 // find the closest point on the capsule to the plane
1399 t = DotProduct( plane, tw->sphere.offset );
1400 if ( t > 0.0f ) {
1401 VectorSubtract( tw->start, tw->sphere.offset, startp );
1402 VectorSubtract( tw->end, tw->sphere.offset, endp );
1403 }
1404 else {
1405 VectorAdd( tw->start, tw->sphere.offset, startp );
1406 VectorAdd( tw->end, tw->sphere.offset, endp );
1407 }
1408 }
1409 else {
1410 offset = DotProduct( tw->offsets[ pcPlanes->signbits ], plane);
1411 plane[3] -= offset;
1412 VectorCopy( tw->start, startp );
1413 VectorCopy( tw->end, endp );
1414 }
1415
1416 if (!CM_CheckFacetPlane(plane, startp, endp, &enterFrac, &leaveFrac, &hit)) {
1417 continue;
1418 }
1419 if (hit) {
1420 Vector4Copy(plane, bestplane);
1421 }
1422
1423 for ( j = 0; j < facet->numBorders; j++ ) {
1424 pcPlanes = &pc->planes[ facet->borderPlanes[j] ];
1425 if (facet->borderInward[j]) {
1426 VectorNegate(pcPlanes->plane, plane);
1427 plane[3] = -pcPlanes->plane[3];
1428 }
1429 else {
1430 VectorCopy(pcPlanes->plane, plane);
1431 plane[3] = pcPlanes->plane[3];
1432 }
1433 if ( tw->sphere.use ) {
1434 // adjust the plane distance appropriately for radius
1435 plane[3] += tw->sphere.radius;
1436
1437 // find the closest point on the capsule to the plane
1438 t = DotProduct( plane, tw->sphere.offset );
1439 if ( t > 0.0f ) {
1440 VectorSubtract( tw->start, tw->sphere.offset, startp );
1441 VectorSubtract( tw->end, tw->sphere.offset, endp );
1442 }
1443 else {
1444 VectorAdd( tw->start, tw->sphere.offset, startp );
1445 VectorAdd( tw->end, tw->sphere.offset, endp );
1446 }
1447 }
1448 else {
1449 // NOTE: this works even though the plane might be flipped because the bbox is centered
1450 offset = DotProduct( tw->offsets[ pcPlanes->signbits ], plane);
1451 plane[3] += fabs(offset);
1452 VectorCopy( tw->start, startp );
1453 VectorCopy( tw->end, endp );
1454 }
1455
1456 if (!CM_CheckFacetPlane(plane, startp, endp, &enterFrac, &leaveFrac, &hit)) {
1457 break;
1458 }
1459 if (hit) {
1460 hitnum = j;
1461 Vector4Copy(plane, bestplane);
1462 }
1463 }
1464 if (j < facet->numBorders) continue;
1465 //never clip against the back side
1466 if (hitnum == facet->numBorders - 1) continue;
1467
1468 if (enterFrac < leaveFrac && enterFrac >= 0) {
1469 if (enterFrac < tw->trace.fraction) {
1470 if (enterFrac < 0) {
1471 enterFrac = 0;
1472 }
1473 #ifndef BSPC
1474 if (!cv) {
1475 cv = Cvar_Get( "r_debugSurfaceUpdate", "1", 0 );
1476 }
1477 if (cv && cv->integer) {
1478 debugPatchCollide = pc;
1479 debugFacet = facet;
1480 }
1481 #endif //BSPC
1482
1483 tw->trace.fraction = enterFrac;
1484 VectorCopy( bestplane, tw->trace.plane.normal );
1485 tw->trace.plane.dist = bestplane[3];
1486 }
1487 }
1488 }
1489 }
1490
1491
1492 /*
1493 =======================================================================
1494
1495 POSITION TEST
1496
1497 =======================================================================
1498 */
1499
1500 /*
1501 ====================
1502 CM_PositionTestInPatchCollide
1503 ====================
1504 */
1505 qboolean CM_PositionTestInPatchCollide( traceWork_t *tw, const struct patchCollide_s *pc ) {
1506 int i, j;
1507 float offset, t;
1508 patchPlane_t *pcPlanes;
1509 facet_t *facet;
1510 float plane[4];
1511 vec3_t startp;
1512
1513 if (tw->isPoint) {
1514 return qfalse;
1515 }
1516 //
1517 facet = pc->facets;
1518 for ( i = 0 ; i < pc->numFacets ; i++, facet++ ) {
1519 pcPlanes = &pc->planes[ facet->surfacePlane ];
1520 VectorCopy(pcPlanes->plane, plane);
1521 plane[3] = pcPlanes->plane[3];
1522 if ( tw->sphere.use ) {
1523 // adjust the plane distance appropriately for radius
1524 plane[3] += tw->sphere.radius;
1525
1526 // find the closest point on the capsule to the plane
1527 t = DotProduct( plane, tw->sphere.offset );
1528 if ( t > 0 ) {
1529 VectorSubtract( tw->start, tw->sphere.offset, startp );
1530 }
1531 else {
1532 VectorAdd( tw->start, tw->sphere.offset, startp );
1533 }
1534 }
1535 else {
1536 offset = DotProduct( tw->offsets[ pcPlanes->signbits ], plane);
1537 plane[3] -= offset;
1538 VectorCopy( tw->start, startp );
1539 }
1540
1541 if ( DotProduct( plane, startp ) - plane[3] > 0.0f ) {
1542 continue;
1543 }
1544
1545 for ( j = 0; j < facet->numBorders; j++ ) {
1546 pcPlanes = &pc->planes[ facet->borderPlanes[j] ];
1547 if (facet->borderInward[j]) {
1548 VectorNegate(pcPlanes->plane, plane);
1549 plane[3] = -pcPlanes->plane[3];
1550 }
1551 else {
1552 VectorCopy(pcPlanes->plane, plane);
1553 plane[3] = pcPlanes->plane[3];
1554 }
1555 if ( tw->sphere.use ) {
1556 // adjust the plane distance appropriately for radius
1557 plane[3] += tw->sphere.radius;
1558
1559 // find the closest point on the capsule to the plane
1560 t = DotProduct( plane, tw->sphere.offset );
1561 if ( t > 0.0f ) {
1562 VectorSubtract( tw->start, tw->sphere.offset, startp );
1563 }
1564 else {
1565 VectorAdd( tw->start, tw->sphere.offset, startp );
1566 }
1567 }
1568 else {
1569 // NOTE: this works even though the plane might be flipped because the bbox is centered
1570 offset = DotProduct( tw->offsets[ pcPlanes->signbits ], plane);
1571 plane[3] += fabs(offset);
1572 VectorCopy( tw->start, startp );
1573 }
1574
1575 if ( DotProduct( plane, startp ) - plane[3] > 0.0f ) {
1576 break;
1577 }
1578 }
1579 if (j < facet->numBorders) {
1580 continue;
1581 }
1582 // inside this patch facet
1583 return qtrue;
1584 }
1585 return qfalse;
1586 }
1587
1588 /*
1589 =======================================================================
1590
1591 DEBUGGING
1592
1593 =======================================================================
1594 */
1595
1596
1597 /*
1598 ==================
1599 CM_DrawDebugSurface
1600
1601 Called from the renderer
1602 ==================
1603 */
1604 #ifndef BSPC
1605 void BotDrawDebugPolygons(void (*drawPoly)(int color, int numPoints, float *points), int value);
1606 #endif
1607
1608 void CM_DrawDebugSurface( void (*drawPoly)(int color, int numPoints, float *points) ) {
1609 static cvar_t *cv;
1610 #ifndef BSPC
1611 static cvar_t *cv2;
1612 #endif
1613 const patchCollide_t *pc;
1614 facet_t *facet;
1615 winding_t *w;
1616 int i, j, k, n;
1617 int curplanenum, planenum, curinward, inward;
1618 float plane[4];
1619 vec3_t mins = {-15, -15, -28}, maxs = {15, 15, 28};
1620 //vec3_t mins = {0, 0, 0}, maxs = {0, 0, 0};
1621 vec3_t v1, v2;
1622
1623 #ifndef BSPC
1624 if ( !cv2 )
1625 {
1626 cv2 = Cvar_Get( "r_debugSurface", "0", 0 );
1627 }
1628
1629 if (cv2->integer != 1)
1630 {
1631 BotDrawDebugPolygons(drawPoly, cv2->integer);
1632 return;
1633 }
1634 #endif
1635
1636 if ( !debugPatchCollide ) {
1637 return;
1638 }
1639
1640 #ifndef BSPC
1641 if ( !cv ) {
1642 cv = Cvar_Get( "cm_debugSize", "2", 0 );
1643 }
1644 #endif
1645 pc = debugPatchCollide;
1646
1647 for ( i = 0, facet = pc->facets ; i < pc->numFacets ; i++, facet++ ) {
1648
1649 for ( k = 0 ; k < facet->numBorders + 1; k++ ) {
1650 //
1651 if (k < facet->numBorders) {
1652 planenum = facet->borderPlanes[k];
1653 inward = facet->borderInward[k];
1654 }
1655 else {
1656 planenum = facet->surfacePlane;
1657 inward = qfalse;
1658 //continue;
1659 }
1660
1661 Vector4Copy( pc->planes[ planenum ].plane, plane );
1662
1663 //planenum = facet->surfacePlane;
1664 if ( inward ) {
1665 VectorSubtract( vec3_origin, plane, plane );
1666 plane[3] = -plane[3];
1667 }
1668
1669 plane[3] += cv->value;
1670 //*
1671 for (n = 0; n < 3; n++)
1672 {
1673 if (plane[n] > 0) v1[n] = maxs[n];
1674 else v1[n] = mins[n];
1675 } //end for
1676 VectorNegate(plane, v2);
1677 plane[3] += fabs(DotProduct(v1, v2));
1678 //*/
1679
1680 w = BaseWindingForPlane( plane, plane[3] );
1681 for ( j = 0 ; j < facet->numBorders + 1 && w; j++ ) {
1682 //
1683 if (j < facet->numBorders) {
1684 curplanenum = facet->borderPlanes[j];
1685 curinward = facet->borderInward[j];
1686 }
1687 else {
1688 curplanenum = facet->surfacePlane;
1689 curinward = qfalse;
1690 //continue;
1691 }
1692 //
1693 if (curplanenum == planenum) continue;
1694
1695 Vector4Copy( pc->planes[ curplanenum ].plane, plane );
1696 if ( !curinward ) {
1697 VectorSubtract( vec3_origin, plane, plane );
1698 plane[3] = -plane[3];
1699 }
1700 // if ( !facet->borderNoAdjust[j] ) {
1701 plane[3] -= cv->value;
1702 // }
1703 for (n = 0; n < 3; n++)
1704 {
1705 if (plane[n] > 0) v1[n] = maxs[n];
1706 else v1[n] = mins[n];
1707 } //end for
1708 VectorNegate(plane, v2);
1709 plane[3] -= fabs(DotProduct(v1, v2));
1710
1711 ChopWindingInPlace( &w, plane, plane[3], 0.1f );
1712 }
1713 if ( w ) {
1714 if ( facet == debugFacet ) {
1715 drawPoly( 4, w->numpoints, w->p[0] );
1716 //Com_Printf("blue facet has %d border planes\n", facet->numBorders);
1717 } else {
1718 drawPoly( 1, w->numpoints, w->p[0] );
1719 }
1720 FreeWinding( w );
1721 }
1722 else
1723 Com_Printf("winding chopped away by border planes\n");
1724 }
1725 }
1726
1727 // draw the debug block
1728 {
1729 vec3_t v[3];
1730
1731 VectorCopy( debugBlockPoints[0], v[0] );
1732 VectorCopy( debugBlockPoints[1], v[1] );
1733 VectorCopy( debugBlockPoints[2], v[2] );
1734 drawPoly( 2, 3, v[0] );
1735
1736 VectorCopy( debugBlockPoints[2], v[0] );
1737 VectorCopy( debugBlockPoints[3], v[1] );
1738 VectorCopy( debugBlockPoints[0], v[2] );
1739 drawPoly( 2, 3, v[0] );
1740 }
1741
1742 #if 0
1743 vec3_t v[4];
1744
1745 v[0][0] = pc->bounds[1][0];
1746 v[0][1] = pc->bounds[1][1];
1747 v[0][2] = pc->bounds[1][2];
1748
1749 v[1][0] = pc->bounds[1][0];
1750 v[1][1] = pc->bounds[0][1];
1751 v[1][2] = pc->bounds[1][2];
1752
1753 v[2][0] = pc->bounds[0][0];
1754 v[2][1] = pc->bounds[0][1];
1755 v[2][2] = pc->bounds[1][2];
1756
1757 v[3][0] = pc->bounds[0][0];
1758 v[3][1] = pc->bounds[1][1];
1759 v[3][2] = pc->bounds[1][2];
1760
1761 drawPoly( 4, v[0] );
1762 #endif
1763 }