Debian Codegraph @

tools/quake3/q3map2/visflow.c

raw symbols SCIP
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 ----------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define VISFLOW_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41
42 /*
43
44 each portal will have a list of all possible to see from first portal
45
46 if (!thread->portalmightsee[portalnum])
47
48 portal mightsee
49
50 for p2 = all other portals in leaf
51 get sperating planes
52 for all portals that might be seen by p2
53 mark as unseen if not present in seperating plane
54 flood fill a new mightsee
55 save as passagemightsee
56
57
58 void CalcMightSee (leaf_t *leaf,
59 */
60
61 int CountBits( byte *bits, int numbits ){
62 int i;
63 int c;
64
65 c = 0;
66 for ( i = 0 ; i < numbits ; i++ )
67 if ( bits[i >> 3] & ( 1 << ( i & 7 ) ) ) {
68 c++;
69 }
70
71 return c;
72 }
73
74 int c_fullskip;
75
76 int c_chop, c_nochop;
77
78 int active;
79
80 void CheckStack( leaf_t *leaf, threaddata_t *thread ){
81 pstack_t *p, *p2;
82
83 for ( p = thread->pstack_head.next ; p ; p = p->next )
84 {
85 // Sys_Printf ("=");
86 if ( p->leaf == leaf ) {
87 Error( "CheckStack: leaf recursion" );
88 }
89 for ( p2 = thread->pstack_head.next ; p2 != p ; p2 = p2->next )
90 if ( p2->leaf == p->leaf ) {
91 Error( "CheckStack: late leaf recursion" );
92 }
93 }
94 // Sys_Printf ("\n");
95 }
96
97
98 fixedWinding_t *AllocStackWinding( pstack_t *stack ){
99 int i;
100
101 for ( i = 0 ; i < 3 ; i++ )
102 {
103 if ( stack->freewindings[i] ) {
104 stack->freewindings[i] = 0;
105 return &stack->windings[i];
106 }
107 }
108
109 Error( "AllocStackWinding: failed" );
110
111 return NULL;
112 }
113
114 void FreeStackWinding( fixedWinding_t *w, pstack_t *stack ){
115 int i;
116
117 i = w - stack->windings;
118
119 if ( i < 0 || i > 2 ) {
120 return; // not from local
121
122 }
123 if ( stack->freewindings[i] ) {
124 Error( "FreeStackWinding: already free" );
125 }
126 stack->freewindings[i] = 1;
127 }
128
129 /*
130 ==============
131 VisChopWinding
132
133 ==============
134 */
135 fixedWinding_t *VisChopWinding( fixedWinding_t *in, pstack_t *stack, visPlane_t *split ){
136 vec_t dists[128];
137 int sides[128];
138 int counts[3];
139 vec_t dot;
140 int i, j;
141 vec_t *p1, *p2;
142 vec3_t mid;
143 fixedWinding_t *neww;
144
145 counts[0] = counts[1] = counts[2] = 0;
146
147 // determine sides for each point
148 for ( i = 0 ; i < in->numpoints ; i++ )
149 {
150 dot = DotProduct( in->points[i], split->normal );
151 dot -= split->dist;
152 dists[i] = dot;
153 if ( dot > ON_EPSILON ) {
154 sides[i] = SIDE_FRONT;
155 }
156 else if ( dot < -ON_EPSILON ) {
157 sides[i] = SIDE_BACK;
158 }
159 else
160 {
161 sides[i] = SIDE_ON;
162 }
163 counts[sides[i]]++;
164 }
165
166 if ( !counts[1] ) {
167 return in; // completely on front side
168
169 }
170 if ( !counts[0] ) {
171 FreeStackWinding( in, stack );
172 return NULL;
173 }
174
175 sides[i] = sides[0];
176 dists[i] = dists[0];
177
178 neww = AllocStackWinding( stack );
179
180 neww->numpoints = 0;
181
182 for ( i = 0 ; i < in->numpoints ; i++ )
183 {
184 p1 = in->points[i];
185
186 if ( neww->numpoints == MAX_POINTS_ON_FIXED_WINDING ) {
187 FreeStackWinding( neww, stack );
188 return in; // can't chop -- fall back to original
189 }
190
191 if ( sides[i] == SIDE_ON ) {
192 VectorCopy( p1, neww->points[neww->numpoints] );
193 neww->numpoints++;
194 continue;
195 }
196
197 if ( sides[i] == SIDE_FRONT ) {
198 VectorCopy( p1, neww->points[neww->numpoints] );
199 neww->numpoints++;
200 }
201
202 if ( sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i] ) {
203 continue;
204 }
205
206 if ( neww->numpoints == MAX_POINTS_ON_FIXED_WINDING ) {
207 FreeStackWinding( neww, stack );
208 return in; // can't chop -- fall back to original
209 }
210
211 // generate a split point
212 p2 = in->points[( i + 1 ) % in->numpoints];
213
214 dot = dists[i] / ( dists[i] - dists[i + 1] );
215 for ( j = 0 ; j < 3 ; j++ )
216 { // avoid round off error when possible
217 if ( split->normal[j] == 1 ) {
218 mid[j] = split->dist;
219 }
220 else if ( split->normal[j] == -1 ) {
221 mid[j] = -split->dist;
222 }
223 else{
224 mid[j] = p1[j] + dot * ( p2[j] - p1[j] );
225 }
226 }
227
228 VectorCopy( mid, neww->points[neww->numpoints] );
229 neww->numpoints++;
230 }
231
232 // free the original winding
233 FreeStackWinding( in, stack );
234
235 return neww;
236 }
237
238 /*
239 ==============
240 ClipToSeperators
241
242 Source, pass, and target are an ordering of portals.
243
244 Generates seperating planes canidates by taking two points from source and one
245 point from pass, and clips target by them.
246
247 If target is totally clipped away, that portal can not be seen through.
248
249 Normal clip keeps target on the same side as pass, which is correct if the
250 order goes source, pass, target. If the order goes pass, source, target then
251 flipclip should be set.
252 ==============
253 */
254 fixedWinding_t *ClipToSeperators( fixedWinding_t *source, fixedWinding_t *pass, fixedWinding_t *target, qboolean flipclip, pstack_t *stack ){
255 int i, j, k, l;
256 visPlane_t plane;
257 vec3_t v1, v2;
258 float d;
259 vec_t length;
260 int counts[3];
261 qboolean fliptest;
262
263 // check all combinations
264 for ( i = 0 ; i < source->numpoints ; i++ )
265 {
266 l = ( i + 1 ) % source->numpoints;
267 VectorSubtract( source->points[l], source->points[i], v1 );
268
269 // find a vertex of pass that makes a plane that puts all of the
270 // vertexes of pass on the front side and all of the vertexes of
271 // source on the back side
272 for ( j = 0 ; j < pass->numpoints ; j++ )
273 {
274 VectorSubtract( pass->points[j], source->points[i], v2 );
275
276 plane.normal[0] = v1[1] * v2[2] - v1[2] * v2[1];
277 plane.normal[1] = v1[2] * v2[0] - v1[0] * v2[2];
278 plane.normal[2] = v1[0] * v2[1] - v1[1] * v2[0];
279
280 // if points don't make a valid plane, skip it
281
282 length = plane.normal[0] * plane.normal[0]
283 + plane.normal[1] * plane.normal[1]
284 + plane.normal[2] * plane.normal[2];
285
286 if ( length < ON_EPSILON ) {
287 continue;
288 }
289
290 length = 1 / sqrt( length );
291
292 plane.normal[0] *= length;
293 plane.normal[1] *= length;
294 plane.normal[2] *= length;
295
296 plane.dist = DotProduct( pass->points[j], plane.normal );
297
298 //
299 // find out which side of the generated seperating plane has the
300 // source portal
301 //
302 #if 1
303 fliptest = qfalse;
304 for ( k = 0 ; k < source->numpoints ; k++ )
305 {
306 if ( k == i || k == l ) {
307 continue;
308 }
309 d = DotProduct( source->points[k], plane.normal ) - plane.dist;
310 if ( d < -ON_EPSILON ) { // source is on the negative side, so we want all
311 // pass and target on the positive side
312 fliptest = qfalse;
313 break;
314 }
315 else if ( d > ON_EPSILON ) { // source is on the positive side, so we want all
316 // pass and target on the negative side
317 fliptest = qtrue;
318 break;
319 }
320 }
321 if ( k == source->numpoints ) {
322 continue; // planar with source portal
323 }
324 #else
325 fliptest = flipclip;
326 #endif
327 //
328 // flip the normal if the source portal is backwards
329 //
330 if ( fliptest ) {
331 VectorSubtract( vec3_origin, plane.normal, plane.normal );
332 plane.dist = -plane.dist;
333 }
334 #if 1
335 //
336 // if all of the pass portal points are now on the positive side,
337 // this is the seperating plane
338 //
339 counts[0] = counts[1] = counts[2] = 0;
340 for ( k = 0 ; k < pass->numpoints ; k++ )
341 {
342 if ( k == j ) {
343 continue;
344 }
345 d = DotProduct( pass->points[k], plane.normal ) - plane.dist;
346 if ( d < -ON_EPSILON ) {
347 break;
348 }
349 else if ( d > ON_EPSILON ) {
350 counts[0]++;
351 }
352 else{
353 counts[2]++;
354 }
355 }
356 if ( k != pass->numpoints ) {
357 continue; // points on negative side, not a seperating plane
358
359 }
360 if ( !counts[0] ) {
361 continue; // planar with seperating plane
362 }
363 #else
364 k = ( j + 1 ) % pass->numpoints;
365 d = DotProduct( pass->points[k], plane.normal ) - plane.dist;
366 if ( d < -ON_EPSILON ) {
367 continue;
368 }
369 k = ( j + pass->numpoints - 1 ) % pass->numpoints;
370 d = DotProduct( pass->points[k], plane.normal ) - plane.dist;
371 if ( d < -ON_EPSILON ) {
372 continue;
373 }
374 #endif
375 //
376 // flip the normal if we want the back side
377 //
378 if ( flipclip ) {
379 VectorSubtract( vec3_origin, plane.normal, plane.normal );
380 plane.dist = -plane.dist;
381 }
382
383 #ifdef SEPERATORCACHE
384 stack->seperators[flipclip][stack->numseperators[flipclip]] = plane;
385 if ( ++stack->numseperators[flipclip] >= MAX_SEPERATORS ) {
386 Error( "MAX_SEPERATORS" );
387 }
388 #endif
389 //MrE: fast check first
390 d = DotProduct( stack->portal->origin, plane.normal ) - plane.dist;
391 //if completely at the back of the seperator plane
392 if ( d < -stack->portal->radius ) {
393 return NULL;
394 }
395 //if completely on the front of the seperator plane
396 if ( d > stack->portal->radius ) {
397 break;
398 }
399
400 //
401 // clip target by the seperating plane
402 //
403 target = VisChopWinding( target, stack, &plane );
404 if ( !target ) {
405 return NULL; // target is not visible
406
407 }
408 break; // optimization by Antony Suter
409 }
410 }
411
412 return target;
413 }
414
415 /*
416 ==================
417 RecursiveLeafFlow
418
419 Flood fill through the leafs
420 If src_portal is NULL, this is the originating leaf
421 ==================
422 */
423 void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack ){
424 pstack_t stack;
425 vportal_t *p;
426 visPlane_t backplane;
427 leaf_t *leaf;
428 int i, j, n;
429 long *test, *might, *prevmight, *vis, more;
430 int pnum;
431
432 thread->c_chains++;
433
434 leaf = &leafs[leafnum];
435 // CheckStack (leaf, thread);
436
437 prevstack->next = &stack;
438
439 stack.next = NULL;
440 stack.leaf = leaf;
441 stack.portal = NULL;
442 stack.depth = prevstack->depth + 1;
443
444 #ifdef SEPERATORCACHE
445 stack.numseperators[0] = 0;
446 stack.numseperators[1] = 0;
447 #endif
448
449 might = (long *)stack.mightsee;
450 vis = (long *)thread->base->portalvis;
451
452 // check all portals for flowing into other leafs
453 for ( i = 0; i < leaf->numportals; i++ )
454 {
455 p = leaf->portals[i];
456 if ( p->removed ) {
457 continue;
458 }
459 pnum = p - portals;
460
461 /* MrE: portal trace debug code
462 {
463 int portaltrace[] = {13, 16, 17, 37};
464 pstack_t *s;
465
466 s = &thread->pstack_head;
467 for (j = 0; s->next && j < sizeof(portaltrace)/sizeof(int) - 1; j++, s = s->next)
468 {
469 if (s->portal->num != portaltrace[j])
470 break;
471 }
472 if (j >= sizeof(portaltrace)/sizeof(int) - 1)
473 {
474 if (p->num == portaltrace[j])
475 n = 0; //traced through all the portals
476 }
477 }
478 */
479
480 if ( !( prevstack->mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
481 continue; // can't possibly see it
482 }
483
484 // if the portal can't see anything we haven't already seen, skip it
485 if ( p->status == stat_done ) {
486 test = (long *)p->portalvis;
487 }
488 else
489 {
490 test = (long *)p->portalflood;
491 }
492
493 more = 0;
494 prevmight = (long *)prevstack->mightsee;
495 for ( j = 0 ; j < portallongs ; j++ )
496 {
497 might[j] = prevmight[j] & test[j];
498 more |= ( might[j] & ~vis[j] );
499 }
500
501 if ( !more &&
502 ( thread->base->portalvis[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) { // can't see anything new
503 continue;
504 }
505
506 // get plane of portal, point normal into the neighbor leaf
507 stack.portalplane = p->plane;
508 VectorSubtract( vec3_origin, p->plane.normal, backplane.normal );
509 backplane.dist = -p->plane.dist;
510
511 // c_portalcheck++;
512
513 stack.portal = p;
514 stack.next = NULL;
515 stack.freewindings[0] = 1;
516 stack.freewindings[1] = 1;
517 stack.freewindings[2] = 1;
518
519 #if 1
520 {
521 float d;
522
523 d = DotProduct( p->origin, thread->pstack_head.portalplane.normal );
524 d -= thread->pstack_head.portalplane.dist;
525 if ( d < -p->radius ) {
526 continue;
527 }
528 else if ( d > p->radius ) {
529 stack.pass = p->winding;
530 }
531 else
532 {
533 stack.pass = VisChopWinding( p->winding, &stack, &thread->pstack_head.portalplane );
534 if ( !stack.pass ) {
535 continue;
536 }
537 }
538 }
539 #else
540 stack.pass = VisChopWinding( p->winding, &stack, &thread->pstack_head.portalplane );
541 if ( !stack.pass ) {
542 continue;
543 }
544 #endif
545
546
547 #if 1
548 {
549 float d;
550
551 d = DotProduct( thread->base->origin, p->plane.normal );
552 d -= p->plane.dist;
553 //MrE: vis-bug fix
554 //if (d > p->radius)
555 if ( d > thread->base->radius ) {
556 continue;
557 }
558 //MrE: vis-bug fix
559 //if (d < -p->radius)
560 else if ( d < -thread->base->radius ) {
561 stack.source = prevstack->source;
562 }
563 else
564 {
565 stack.source = VisChopWinding( prevstack->source, &stack, &backplane );
566 //FIXME: shouldn't we create a new source origin and radius for fast checks?
567 if ( !stack.source ) {
568 continue;
569 }
570 }
571 }
572 #else
573 stack.source = VisChopWinding( prevstack->source, &stack, &backplane );
574 if ( !stack.source ) {
575 continue;
576 }
577 #endif
578
579 if ( !prevstack->pass ) { // the second leaf can only be blocked if coplanar
580
581 // mark the portal as visible
582 thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
583
584 RecursiveLeafFlow( p->leaf, thread, &stack );
585 continue;
586 }
587
588 #ifdef SEPERATORCACHE
589 if ( stack.numseperators[0] ) {
590 for ( n = 0; n < stack.numseperators[0]; n++ )
591 {
592 stack.pass = VisChopWinding( stack.pass, &stack, &stack.seperators[0][n] );
593 if ( !stack.pass ) {
594 break; // target is not visible
595 }
596 }
597 if ( n < stack.numseperators[0] ) {
598 continue;
599 }
600 }
601 else
602 {
603 stack.pass = ClipToSeperators( prevstack->source, prevstack->pass, stack.pass, qfalse, &stack );
604 }
605 #else
606 stack.pass = ClipToSeperators( stack.source, prevstack->pass, stack.pass, qfalse, &stack );
607 #endif
608 if ( !stack.pass ) {
609 continue;
610 }
611
612 #ifdef SEPERATORCACHE
613 if ( stack.numseperators[1] ) {
614 for ( n = 0; n < stack.numseperators[1]; n++ )
615 {
616 stack.pass = VisChopWinding( stack.pass, &stack, &stack.seperators[1][n] );
617 if ( !stack.pass ) {
618 break; // target is not visible
619 }
620 }
621 }
622 else
623 {
624 stack.pass = ClipToSeperators( prevstack->pass, prevstack->source, stack.pass, qtrue, &stack );
625 }
626 #else
627 stack.pass = ClipToSeperators( prevstack->pass, stack.source, stack.pass, qtrue, &stack );
628 #endif
629 if ( !stack.pass ) {
630 continue;
631 }
632
633 // mark the portal as visible
634 thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
635
636 // flow through it for real
637 RecursiveLeafFlow( p->leaf, thread, &stack );
638 //
639 stack.next = NULL;
640 }
641
642 prevstack->next = NULL;
643 }
644
645 /*
646 ===============
647 PortalFlow
648
649 generates the portalvis bit vector
650 ===============
651 */
652 void PortalFlow( int portalnum ){
653 threaddata_t data;
654 int i;
655 vportal_t *p;
656 int c_might, c_can;
657
658 #ifdef MREDEBUG
659 Sys_Printf( "\r%6d", portalnum );
660 #endif
661
662 p = sorted_portals[portalnum];
663
664 if ( p->removed ) {
665 p->status = stat_done;
666 return;
667 }
668
669 p->status = stat_working;
670
671 c_might = CountBits( p->portalflood, numportals * 2 );
672
673 memset( &data, 0, sizeof( data ) );
674 data.base = p;
675
676 data.pstack_head.portal = p;
677 data.pstack_head.source = p->winding;
678 data.pstack_head.portalplane = p->plane;
679 data.pstack_head.depth = 0;
680 for ( i = 0 ; i < portallongs ; i++ )
681 ( (long *)data.pstack_head.mightsee )[i] = ( (long *)p->portalflood )[i];
682
683 RecursiveLeafFlow( p->leaf, &data, &data.pstack_head );
684
685 p->status = stat_done;
686
687 c_can = CountBits( p->portalvis, numportals * 2 );
688
689 Sys_FPrintf( SYS_VRB,"portal:%4i mightsee:%4i cansee:%4i (%i chains)\n",
690 (int)( p - portals ), c_might, c_can, data.c_chains );
691 }
692
693 /*
694 ==================
695 RecursivePassageFlow
696 ==================
697 */
698 void RecursivePassageFlow( vportal_t *portal, threaddata_t *thread, pstack_t *prevstack ){
699 pstack_t stack;
700 vportal_t *p;
701 leaf_t *leaf;
702 passage_t *passage, *nextpassage;
703 int i, j;
704 long *might, *vis, *prevmight, *cansee, *portalvis, more;
705 int pnum;
706
707 leaf = &leafs[portal->leaf];
708
709 prevstack->next = &stack;
710
711 stack.next = NULL;
712 stack.depth = prevstack->depth + 1;
713
714 vis = (long *)thread->base->portalvis;
715
716 passage = portal->passages;
717 nextpassage = passage;
718 // check all portals for flowing into other leafs
719 for ( i = 0; i < leaf->numportals; i++, passage = nextpassage )
720 {
721 p = leaf->portals[i];
722 if ( p->removed ) {
723 continue;
724 }
725 nextpassage = passage->next;
726 pnum = p - portals;
727
728 if ( !( prevstack->mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
729 continue; // can't possibly see it
730 }
731
732 // mark the portal as visible
733 thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
734
735 prevmight = (long *)prevstack->mightsee;
736 cansee = (long *)passage->cansee;
737 might = (long *)stack.mightsee;
738 memcpy( might, prevmight, portalbytes );
739 if ( p->status == stat_done ) {
740 portalvis = (long *) p->portalvis;
741 }
742 else{
743 portalvis = (long *) p->portalflood;
744 }
745 more = 0;
746 for ( j = 0; j < portallongs; j++ )
747 {
748 if ( *might ) {
749 *might &= *cansee++ & *portalvis++;
750 more |= ( *might & ~vis[j] );
751 }
752 else
753 {
754 cansee++;
755 portalvis++;
756 }
757 might++;
758 }
759
760 if ( !more ) {
761 // can't see anything new
762 continue;
763 }
764
765 // flow through it for real
766 RecursivePassageFlow( p, thread, &stack );
767
768 stack.next = NULL;
769 }
770
771 prevstack->next = NULL;
772 }
773
774 /*
775 ===============
776 PassageFlow
777 ===============
778 */
779 void PassageFlow( int portalnum ){
780 threaddata_t data;
781 int i;
782 vportal_t *p;
783 // int c_might, c_can;
784
785 #ifdef MREDEBUG
786 Sys_Printf( "\r%6d", portalnum );
787 #endif
788
789 p = sorted_portals[portalnum];
790
791 if ( p->removed ) {
792 p->status = stat_done;
793 return;
794 }
795
796 p->status = stat_working;
797
798 // c_might = CountBits (p->portalflood, numportals*2);
799
800 memset( &data, 0, sizeof( data ) );
801 data.base = p;
802
803 data.pstack_head.portal = p;
804 data.pstack_head.source = p->winding;
805 data.pstack_head.portalplane = p->plane;
806 data.pstack_head.depth = 0;
807 for ( i = 0 ; i < portallongs ; i++ )
808 ( (long *)data.pstack_head.mightsee )[i] = ( (long *)p->portalflood )[i];
809
810 RecursivePassageFlow( p, &data, &data.pstack_head );
811
812 p->status = stat_done;
813
814 /*
815 c_can = CountBits (p->portalvis, numportals*2);
816
817 Sys_FPrintf (SYS_VRB,"portal:%4i mightsee:%4i cansee:%4i (%i chains)\n",
818 (int)(p - portals), c_might, c_can, data.c_chains);
819 */
820 }
821
822 /*
823 ==================
824 RecursivePassagePortalFlow
825 ==================
826 */
827 void RecursivePassagePortalFlow( vportal_t *portal, threaddata_t *thread, pstack_t *prevstack ){
828 pstack_t stack;
829 vportal_t *p;
830 leaf_t *leaf;
831 visPlane_t backplane;
832 passage_t *passage, *nextpassage;
833 int i, j, n;
834 long *might, *vis, *prevmight, *cansee, *portalvis, more;
835 int pnum;
836
837 // thread->c_chains++;
838
839 leaf = &leafs[portal->leaf];
840 // CheckStack (leaf, thread);
841
842 prevstack->next = &stack;
843
844 stack.next = NULL;
845 stack.leaf = leaf;
846 stack.portal = NULL;
847 stack.depth = prevstack->depth + 1;
848
849 #ifdef SEPERATORCACHE
850 stack.numseperators[0] = 0;
851 stack.numseperators[1] = 0;
852 #endif
853
854 vis = (long *)thread->base->portalvis;
855
856 passage = portal->passages;
857 nextpassage = passage;
858 // check all portals for flowing into other leafs
859 for ( i = 0; i < leaf->numportals; i++, passage = nextpassage )
860 {
861 p = leaf->portals[i];
862 if ( p->removed ) {
863 continue;
864 }
865 nextpassage = passage->next;
866 pnum = p - portals;
867
868 if ( !( prevstack->mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
869 continue; // can't possibly see it
870
871 }
872 prevmight = (long *)prevstack->mightsee;
873 cansee = (long *)passage->cansee;
874 might = (long *)stack.mightsee;
875 memcpy( might, prevmight, portalbytes );
876 if ( p->status == stat_done ) {
877 portalvis = (long *) p->portalvis;
878 }
879 else{
880 portalvis = (long *) p->portalflood;
881 }
882 more = 0;
883 for ( j = 0; j < portallongs; j++ )
884 {
885 if ( *might ) {
886 *might &= *cansee++ & *portalvis++;
887 more |= ( *might & ~vis[j] );
888 }
889 else
890 {
891 cansee++;
892 portalvis++;
893 }
894 might++;
895 }
896
897 if ( !more && ( thread->base->portalvis[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) { // can't see anything new
898 continue;
899 }
900
901 // get plane of portal, point normal into the neighbor leaf
902 stack.portalplane = p->plane;
903 VectorSubtract( vec3_origin, p->plane.normal, backplane.normal );
904 backplane.dist = -p->plane.dist;
905
906 // c_portalcheck++;
907
908 stack.portal = p;
909 stack.next = NULL;
910 stack.freewindings[0] = 1;
911 stack.freewindings[1] = 1;
912 stack.freewindings[2] = 1;
913
914 #if 1
915 {
916 float d;
917
918 d = DotProduct( p->origin, thread->pstack_head.portalplane.normal );
919 d -= thread->pstack_head.portalplane.dist;
920 if ( d < -p->radius ) {
921 continue;
922 }
923 else if ( d > p->radius ) {
924 stack.pass = p->winding;
925 }
926 else
927 {
928 stack.pass = VisChopWinding( p->winding, &stack, &thread->pstack_head.portalplane );
929 if ( !stack.pass ) {
930 continue;
931 }
932 }
933 }
934 #else
935 stack.pass = VisChopWinding( p->winding, &stack, &thread->pstack_head.portalplane );
936 if ( !stack.pass ) {
937 continue;
938 }
939 #endif
940
941
942 #if 1
943 {
944 float d;
945
946 d = DotProduct( thread->base->origin, p->plane.normal );
947 d -= p->plane.dist;
948 //MrE: vis-bug fix
949 //if (d > p->radius)
950 if ( d > thread->base->radius ) {
951 continue;
952 }
953 //MrE: vis-bug fix
954 //if (d < -p->radius)
955 else if ( d < -thread->base->radius ) {
956 stack.source = prevstack->source;
957 }
958 else
959 {
960 stack.source = VisChopWinding( prevstack->source, &stack, &backplane );
961 //FIXME: shouldn't we create a new source origin and radius for fast checks?
962 if ( !stack.source ) {
963 continue;
964 }
965 }
966 }
967 #else
968 stack.source = VisChopWinding( prevstack->source, &stack, &backplane );
969 if ( !stack.source ) {
970 continue;
971 }
972 #endif
973
974 if ( !prevstack->pass ) { // the second leaf can only be blocked if coplanar
975
976 // mark the portal as visible
977 thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
978
979 RecursivePassagePortalFlow( p, thread, &stack );
980 continue;
981 }
982
983 #ifdef SEPERATORCACHE
984 if ( stack.numseperators[0] ) {
985 for ( n = 0; n < stack.numseperators[0]; n++ )
986 {
987 stack.pass = VisChopWinding( stack.pass, &stack, &stack.seperators[0][n] );
988 if ( !stack.pass ) {
989 break; // target is not visible
990 }
991 }
992 if ( n < stack.numseperators[0] ) {
993 continue;
994 }
995 }
996 else
997 {
998 stack.pass = ClipToSeperators( prevstack->source, prevstack->pass, stack.pass, qfalse, &stack );
999 }
1000 #else
1001 stack.pass = ClipToSeperators( stack.source, prevstack->pass, stack.pass, qfalse, &stack );
1002 #endif
1003 if ( !stack.pass ) {
1004 continue;
1005 }
1006
1007 #ifdef SEPERATORCACHE
1008 if ( stack.numseperators[1] ) {
1009 for ( n = 0; n < stack.numseperators[1]; n++ )
1010 {
1011 stack.pass = VisChopWinding( stack.pass, &stack, &stack.seperators[1][n] );
1012 if ( !stack.pass ) {
1013 break; // target is not visible
1014 }
1015 }
1016 }
1017 else
1018 {
1019 stack.pass = ClipToSeperators( prevstack->pass, prevstack->source, stack.pass, qtrue, &stack );
1020 }
1021 #else
1022 stack.pass = ClipToSeperators( prevstack->pass, stack.source, stack.pass, qtrue, &stack );
1023 #endif
1024 if ( !stack.pass ) {
1025 continue;
1026 }
1027
1028 // mark the portal as visible
1029 thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
1030
1031 // flow through it for real
1032 RecursivePassagePortalFlow( p, thread, &stack );
1033 //
1034 stack.next = NULL;
1035 }
1036
1037 prevstack->next = NULL;
1038 }
1039
1040 /*
1041 ===============
1042 PassagePortalFlow
1043 ===============
1044 */
1045 void PassagePortalFlow( int portalnum ){
1046 threaddata_t data;
1047 int i;
1048 vportal_t *p;
1049 // int c_might, c_can;
1050
1051 #ifdef MREDEBUG
1052 Sys_Printf( "\r%6d", portalnum );
1053 #endif
1054
1055 p = sorted_portals[portalnum];
1056
1057 if ( p->removed ) {
1058 p->status = stat_done;
1059 return;
1060 }
1061
1062 p->status = stat_working;
1063
1064 // c_might = CountBits (p->portalflood, numportals*2);
1065
1066 memset( &data, 0, sizeof( data ) );
1067 data.base = p;
1068
1069 data.pstack_head.portal = p;
1070 data.pstack_head.source = p->winding;
1071 data.pstack_head.portalplane = p->plane;
1072 data.pstack_head.depth = 0;
1073 for ( i = 0 ; i < portallongs ; i++ )
1074 ( (long *)data.pstack_head.mightsee )[i] = ( (long *)p->portalflood )[i];
1075
1076 RecursivePassagePortalFlow( p, &data, &data.pstack_head );
1077
1078 p->status = stat_done;
1079
1080 /*
1081 c_can = CountBits (p->portalvis, numportals*2);
1082
1083 Sys_FPrintf (SYS_VRB,"portal:%4i mightsee:%4i cansee:%4i (%i chains)\n",
1084 (int)(p - portals), c_might, c_can, data.c_chains);
1085 */
1086 }
1087
1088 fixedWinding_t *PassageChopWinding( fixedWinding_t *in, fixedWinding_t *out, visPlane_t *split ){
1089 vec_t dists[128];
1090 int sides[128];
1091 int counts[3];
1092 vec_t dot;
1093 int i, j;
1094 vec_t *p1, *p2;
1095 vec3_t mid;
1096 fixedWinding_t *neww;
1097
1098 counts[0] = counts[1] = counts[2] = 0;
1099
1100 // determine sides for each point
1101 for ( i = 0 ; i < in->numpoints ; i++ )
1102 {
1103 dot = DotProduct( in->points[i], split->normal );
1104 dot -= split->dist;
1105 dists[i] = dot;
1106 if ( dot > ON_EPSILON ) {
1107 sides[i] = SIDE_FRONT;
1108 }
1109 else if ( dot < -ON_EPSILON ) {
1110 sides[i] = SIDE_BACK;
1111 }
1112 else
1113 {
1114 sides[i] = SIDE_ON;
1115 }
1116 counts[sides[i]]++;
1117 }
1118
1119 if ( !counts[1] ) {
1120 return in; // completely on front side
1121
1122 }
1123 if ( !counts[0] ) {
1124 return NULL;
1125 }
1126
1127 sides[i] = sides[0];
1128 dists[i] = dists[0];
1129
1130 neww = out;
1131
1132 neww->numpoints = 0;
1133
1134 for ( i = 0 ; i < in->numpoints ; i++ )
1135 {
1136 p1 = in->points[i];
1137
1138 if ( neww->numpoints == MAX_POINTS_ON_FIXED_WINDING ) {
1139 return in; // can't chop -- fall back to original
1140 }
1141
1142 if ( sides[i] == SIDE_ON ) {
1143 VectorCopy( p1, neww->points[neww->numpoints] );
1144 neww->numpoints++;
1145 continue;
1146 }
1147
1148 if ( sides[i] == SIDE_FRONT ) {
1149 VectorCopy( p1, neww->points[neww->numpoints] );
1150 neww->numpoints++;
1151 }
1152
1153 if ( sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i] ) {
1154 continue;
1155 }
1156
1157 if ( neww->numpoints == MAX_POINTS_ON_FIXED_WINDING ) {
1158 return in; // can't chop -- fall back to original
1159 }
1160
1161 // generate a split point
1162 p2 = in->points[( i + 1 ) % in->numpoints];
1163
1164 dot = dists[i] / ( dists[i] - dists[i + 1] );
1165 for ( j = 0 ; j < 3 ; j++ )
1166 { // avoid round off error when possible
1167 if ( split->normal[j] == 1 ) {
1168 mid[j] = split->dist;
1169 }
1170 else if ( split->normal[j] == -1 ) {
1171 mid[j] = -split->dist;
1172 }
1173 else{
1174 mid[j] = p1[j] + dot * ( p2[j] - p1[j] );
1175 }
1176 }
1177
1178 VectorCopy( mid, neww->points[neww->numpoints] );
1179 neww->numpoints++;
1180 }
1181
1182 return neww;
1183 }
1184
1185 /*
1186 ===============
1187 AddSeperators
1188 ===============
1189 */
1190 int AddSeperators( fixedWinding_t *source, fixedWinding_t *pass, qboolean flipclip, visPlane_t *seperators, int maxseperators ){
1191 int i, j, k, l;
1192 visPlane_t plane;
1193 vec3_t v1, v2;
1194 float d;
1195 vec_t length;
1196 int counts[3], numseperators;
1197 qboolean fliptest;
1198
1199 numseperators = 0;
1200 // check all combinations
1201 for ( i = 0 ; i < source->numpoints ; i++ )
1202 {
1203 l = ( i + 1 ) % source->numpoints;
1204 VectorSubtract( source->points[l], source->points[i], v1 );
1205
1206 // find a vertex of pass that makes a plane that puts all of the
1207 // vertexes of pass on the front side and all of the vertexes of
1208 // source on the back side
1209 for ( j = 0 ; j < pass->numpoints ; j++ )
1210 {
1211 VectorSubtract( pass->points[j], source->points[i], v2 );
1212
1213 plane.normal[0] = v1[1] * v2[2] - v1[2] * v2[1];
1214 plane.normal[1] = v1[2] * v2[0] - v1[0] * v2[2];
1215 plane.normal[2] = v1[0] * v2[1] - v1[1] * v2[0];
1216
1217 // if points don't make a valid plane, skip it
1218
1219 length = plane.normal[0] * plane.normal[0]
1220 + plane.normal[1] * plane.normal[1]
1221 + plane.normal[2] * plane.normal[2];
1222
1223 if ( length < ON_EPSILON ) {
1224 continue;
1225 }
1226
1227 length = 1 / sqrt( length );
1228
1229 plane.normal[0] *= length;
1230 plane.normal[1] *= length;
1231 plane.normal[2] *= length;
1232
1233 plane.dist = DotProduct( pass->points[j], plane.normal );
1234
1235 //
1236 // find out which side of the generated seperating plane has the
1237 // source portal
1238 //
1239 #if 1
1240 fliptest = qfalse;
1241 for ( k = 0 ; k < source->numpoints ; k++ )
1242 {
1243 if ( k == i || k == l ) {
1244 continue;
1245 }
1246 d = DotProduct( source->points[k], plane.normal ) - plane.dist;
1247 if ( d < -ON_EPSILON ) { // source is on the negative side, so we want all
1248 // pass and target on the positive side
1249 fliptest = qfalse;
1250 break;
1251 }
1252 else if ( d > ON_EPSILON ) { // source is on the positive side, so we want all
1253 // pass and target on the negative side
1254 fliptest = qtrue;
1255 break;
1256 }
1257 }
1258 if ( k == source->numpoints ) {
1259 continue; // planar with source portal
1260 }
1261 #else
1262 fliptest = flipclip;
1263 #endif
1264 //
1265 // flip the normal if the source portal is backwards
1266 //
1267 if ( fliptest ) {
1268 VectorSubtract( vec3_origin, plane.normal, plane.normal );
1269 plane.dist = -plane.dist;
1270 }
1271 #if 1
1272 //
1273 // if all of the pass portal points are now on the positive side,
1274 // this is the seperating plane
1275 //
1276 counts[0] = counts[1] = counts[2] = 0;
1277 for ( k = 0 ; k < pass->numpoints ; k++ )
1278 {
1279 if ( k == j ) {
1280 continue;
1281 }
1282 d = DotProduct( pass->points[k], plane.normal ) - plane.dist;
1283 if ( d < -ON_EPSILON ) {
1284 break;
1285 }
1286 else if ( d > ON_EPSILON ) {
1287 counts[0]++;
1288 }
1289 else{
1290 counts[2]++;
1291 }
1292 }
1293 if ( k != pass->numpoints ) {
1294 continue; // points on negative side, not a seperating plane
1295
1296 }
1297 if ( !counts[0] ) {
1298 continue; // planar with seperating plane
1299 }
1300 #else
1301 k = ( j + 1 ) % pass->numpoints;
1302 d = DotProduct( pass->points[k], plane.normal ) - plane.dist;
1303 if ( d < -ON_EPSILON ) {
1304 continue;
1305 }
1306 k = ( j + pass->numpoints - 1 ) % pass->numpoints;
1307 d = DotProduct( pass->points[k], plane.normal ) - plane.dist;
1308 if ( d < -ON_EPSILON ) {
1309 continue;
1310 }
1311 #endif
1312 //
1313 // flip the normal if we want the back side
1314 //
1315 if ( flipclip ) {
1316 VectorSubtract( vec3_origin, plane.normal, plane.normal );
1317 plane.dist = -plane.dist;
1318 }
1319
1320 if ( numseperators >= maxseperators ) {
1321 Error( "max seperators" );
1322 }
1323 seperators[numseperators] = plane;
1324 numseperators++;
1325 break;
1326 }
1327 }
1328 return numseperators;
1329 }
1330
1331 /*
1332 ===============
1333 CreatePassages
1334
1335 MrE: create passages from one portal to all the portals in the leaf the portal leads to
1336 every passage has a cansee bit string with all the portals that can be
1337 seen through the passage
1338 ===============
1339 */
1340 void CreatePassages( int portalnum ){
1341 int i, j, k, n, numseperators;
1342 // int numsee;
1343 float d;
1344 vportal_t *portal, *p, *target;
1345 leaf_t *leaf;
1346 passage_t *passage, *lastpassage;
1347 visPlane_t seperators[MAX_SEPERATORS * 2];
1348 fixedWinding_t *w;
1349 fixedWinding_t in, out, *res;
1350
1351
1352 #ifdef MREDEBUG
1353 Sys_Printf( "\r%6d", portalnum );
1354 #endif
1355
1356 portal = sorted_portals[portalnum];
1357
1358 if ( portal->removed ) {
1359 portal->status = stat_done;
1360 return;
1361 }
1362
1363 lastpassage = NULL;
1364 leaf = &leafs[portal->leaf];
1365 for ( i = 0; i < leaf->numportals; i++ )
1366 {
1367 target = leaf->portals[i];
1368 if ( target->removed ) {
1369 continue;
1370 }
1371
1372 passage = (passage_t *) safe_malloc0( sizeof( passage_t ) + portalbytes );
1373 numseperators = AddSeperators( portal->winding, target->winding, qfalse, seperators, MAX_SEPERATORS * 2 );
1374 numseperators += AddSeperators( target->winding, portal->winding, qtrue, &seperators[numseperators], MAX_SEPERATORS * 2 - numseperators );
1375
1376 passage->next = NULL;
1377 if ( lastpassage ) {
1378 lastpassage->next = passage;
1379 }
1380 else{
1381 portal->passages = passage;
1382 }
1383 lastpassage = passage;
1384
1385 // numsee = 0;
1386 //create the passage->cansee
1387 for ( j = 0; j < numportals * 2; j++ )
1388 {
1389 p = &portals[j];
1390 if ( p->removed ) {
1391 continue;
1392 }
1393 if ( !( target->portalflood[j >> 3] & ( 1 << ( j & 7 ) ) ) ) {
1394 continue;
1395 }
1396 if ( !( portal->portalflood[j >> 3] & ( 1 << ( j & 7 ) ) ) ) {
1397 continue;
1398 }
1399 for ( k = 0; k < numseperators; k++ )
1400 {
1401 //
1402 d = DotProduct( p->origin, seperators[k].normal ) - seperators[k].dist;
1403 //if completely at the back of the seperator plane
1404 if ( d < -p->radius + ON_EPSILON ) {
1405 break;
1406 }
1407 w = p->winding;
1408 for ( n = 0; n < w->numpoints; n++ )
1409 {
1410 d = DotProduct( w->points[n], seperators[k].normal ) - seperators[k].dist;
1411 //if at the front of the seperator
1412 if ( d > ON_EPSILON ) {
1413 break;
1414 }
1415 }
1416 //if no points are at the front of the seperator
1417 if ( n >= w->numpoints ) {
1418 break;
1419 }
1420 }
1421 if ( k < numseperators ) {
1422 continue;
1423 }
1424
1425 /* explitive deleted */
1426
1427
1428 /* ydnar: prefer correctness to stack overflow */
1429 //% memcpy( &in, p->winding, (int)((fixedWinding_t *)0)->points[p->winding->numpoints] );
1430 if ( p->winding->numpoints <= MAX_POINTS_ON_FIXED_WINDING ) {
1431 memcpy( &in, p->winding, (size_t) &( ( (fixedWinding_t*) 0 )->points[ p->winding->numpoints ] ) );
1432 }
1433 else{
1434 memcpy( &in, p->winding, sizeof( fixedWinding_t ) );
1435 }
1436
1437
1438 for ( k = 0; k < numseperators; k++ )
1439 {
1440 /* ydnar: this is a shitty crutch */
1441 if ( in.numpoints > MAX_POINTS_ON_FIXED_WINDING ) {
1442 //% Sys_Printf( "[%d]", p->winding->numpoints );
1443 in.numpoints = MAX_POINTS_ON_FIXED_WINDING;
1444 }
1445
1446 res = PassageChopWinding( &in, &out, &seperators[ k ] );
1447 if ( res == &out ) {
1448 memcpy( &in, &out, sizeof( fixedWinding_t ) );
1449 }
1450
1451
1452 if ( res == NULL ) {
1453 break;
1454 }
1455 }
1456 if ( k < numseperators ) {
1457 continue;
1458 }
1459 passage->cansee[j >> 3] |= ( 1 << ( j & 7 ) );
1460 // numsee++;
1461 }
1462 }
1463 }
1464
1465 void PassageMemory( void ){
1466 int i, j, totalmem, totalportals;
1467 vportal_t *portal, *target;
1468 leaf_t *leaf;
1469
1470 totalmem = 0;
1471 totalportals = 0;
1472 for ( i = 0; i < numportals; i++ )
1473 {
1474 portal = sorted_portals[i];
1475 if ( portal->removed ) {
1476 continue;
1477 }
1478 leaf = &leafs[portal->leaf];
1479 for ( j = 0; j < leaf->numportals; j++ )
1480 {
1481 target = leaf->portals[j];
1482 if ( target->removed ) {
1483 continue;
1484 }
1485 totalmem += sizeof( passage_t ) + portalbytes;
1486 totalportals++;
1487 }
1488 }
1489 Sys_Printf( "%7i average number of passages per leaf\n", totalportals / numportals );
1490 Sys_Printf( "%7i MB required passage memory\n", totalmem >> 10 >> 10 );
1491 }
1492
1493 /*
1494 ===============================================================================
1495
1496 This is a rough first-order aproximation that is used to trivially reject some
1497 of the final calculations.
1498
1499
1500 Calculates portalfront and portalflood bit vectors
1501
1502 thinking about:
1503
1504 typedef struct passage_s
1505 {
1506 struct passage_s *next;
1507 struct portal_s *to;
1508 stryct sep_s *seperators;
1509 byte *mightsee;
1510 } passage_t;
1511
1512 typedef struct portal_s
1513 {
1514 struct passage_s *passages;
1515 int leaf; // leaf portal faces into
1516 } portal_s;
1517
1518 leaf = portal->leaf
1519 clear
1520 for all portals
1521
1522
1523 calc portal visibility
1524 clear bit vector
1525 for all passages
1526 passage visibility
1527
1528
1529 for a portal to be visible to a passage, it must be on the front of
1530 all seperating planes, and both portals must be behind the new portal
1531
1532 ===============================================================================
1533 */
1534
1535 int c_flood, c_vis;
1536
1537
1538 /*
1539 ==================
1540 SimpleFlood
1541
1542 ==================
1543 */
1544 void SimpleFlood( vportal_t *srcportal, int leafnum ){
1545 int i;
1546 leaf_t *leaf;
1547 vportal_t *p;
1548 int pnum;
1549
1550 leaf = &leafs[leafnum];
1551
1552 for ( i = 0 ; i < leaf->numportals ; i++ )
1553 {
1554 p = leaf->portals[i];
1555 if ( p->removed ) {
1556 continue;
1557 }
1558 pnum = p - portals;
1559 if ( !( srcportal->portalfront[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
1560 continue;
1561 }
1562
1563 if ( srcportal->portalflood[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) {
1564 continue;
1565 }
1566
1567 srcportal->portalflood[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
1568
1569 SimpleFlood( srcportal, p->leaf );
1570 }
1571 }
1572
1573 /*
1574 ==============
1575 BasePortalVis
1576 ==============
1577 */
1578 void BasePortalVis( int portalnum ){
1579 int j, k;
1580 vportal_t *tp, *p;
1581 float d;
1582 fixedWinding_t *w;
1583 vec3_t dir;
1584
1585
1586 p = portals + portalnum;
1587
1588 if ( p->removed ) {
1589 return;
1590 }
1591
1592 p->portalfront = safe_malloc0( portalbytes );
1593
1594 p->portalflood = safe_malloc0( portalbytes );
1595
1596 p->portalvis = safe_malloc0( portalbytes );
1597
1598 for ( j = 0, tp = portals ; j < numportals * 2 ; j++, tp++ )
1599 {
1600 if ( j == portalnum ) {
1601 continue;
1602 }
1603 if ( tp->removed ) {
1604 continue;
1605 }
1606
1607 /* ydnar: this is old farplane vis code from mre */
1608 /*
1609 if (farplanedist >= 0)
1610 {
1611 vec3_t dir;
1612 VectorSubtract(p->origin, tp->origin, dir);
1613 if (VectorLength(dir) > farplanedist - p->radius - tp->radius)
1614 continue;
1615 }
1616 */
1617
1618 if( !p->sky && !tp->sky && farPlaneDist != 0.0f && farPlaneDistMode == 'o' )
1619 {
1620 VectorSubtract( p->origin, tp->origin, dir );
1621 if( VectorLength( dir ) > farPlaneDist )
1622 continue;
1623 }
1624
1625 if( !p->sky && !tp->sky && farPlaneDist != 0.0f && farPlaneDistMode == 'e' )
1626 {
1627 VectorSubtract( p->origin, tp->origin, dir );
1628 if( VectorLength( dir ) + p->radius + tp->radius > 2.0f * farPlaneDist )
1629 continue;
1630 }
1631
1632 if( !p->sky && !tp->sky && farPlaneDist != 0.0f && farPlaneDistMode == 'r' )
1633 {
1634 if( p->radius + tp->radius > farPlaneDist )
1635 continue;
1636 }
1637
1638 /* ydnar: this is known-to-be-working farplane code */
1639 if ( !p->sky && !tp->sky && farPlaneDist != 0.0f ) {
1640 VectorSubtract( p->origin, tp->origin, dir );
1641 if ( VectorLength( dir ) - p->radius - tp->radius > farPlaneDist ) {
1642 continue;
1643 }
1644 }
1645
1646
1647 w = tp->winding;
1648 for ( k = 0 ; k < w->numpoints ; k++ )
1649 {
1650 d = DotProduct( w->points[k], p->plane.normal )
1651 - p->plane.dist;
1652 if ( d > ON_EPSILON ) {
1653 break;
1654 }
1655 }
1656 if ( k == w->numpoints ) {
1657 continue; // no points on front
1658
1659 }
1660 w = p->winding;
1661 for ( k = 0 ; k < w->numpoints ; k++ )
1662 {
1663 d = DotProduct( w->points[k], tp->plane.normal )
1664 - tp->plane.dist;
1665 if ( d < -ON_EPSILON ) {
1666 break;
1667 }
1668 }
1669 if ( k == w->numpoints ) {
1670 continue; // no points on front
1671
1672 }
1673 p->portalfront[j >> 3] |= ( 1 << ( j & 7 ) );
1674 }
1675
1676 SimpleFlood( p, p->leaf );
1677
1678 p->nummightsee = CountBits( p->portalflood, numportals * 2 );
1679 // Sys_Printf ("portal %i: %i mightsee\n", portalnum, p->nummightsee);
1680 c_flood += p->nummightsee;
1681 }
1682
1683
1684
1685
1686
1687 /*
1688 ===============================================================================
1689
1690 This is a second order aproximation
1691
1692 Calculates portalvis bit vector
1693
1694 WAAAAAAY too slow.
1695
1696 ===============================================================================
1697 */
1698
1699 /*
1700 ==================
1701 RecursiveLeafBitFlow
1702
1703 ==================
1704 */
1705 void RecursiveLeafBitFlow( int leafnum, byte *mightsee, byte *cansee ){
1706 vportal_t *p;
1707 leaf_t *leaf;
1708 int i, j;
1709 long more;
1710 int pnum;
1711 byte newmight[MAX_PORTALS / 8];
1712
1713 leaf = &leafs[leafnum];
1714
1715 // check all portals for flowing into other leafs
1716 for ( i = 0 ; i < leaf->numportals ; i++ )
1717 {
1718 p = leaf->portals[i];
1719 if ( p->removed ) {
1720 continue;
1721 }
1722 pnum = p - portals;
1723
1724 // if some previous portal can't see it, skip
1725 if ( !( mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
1726 continue;
1727 }
1728
1729 // if this portal can see some portals we mightsee, recurse
1730 more = 0;
1731 for ( j = 0 ; j < portallongs ; j++ )
1732 {
1733 ( (long *)newmight )[j] = ( (long *)mightsee )[j]
1734 & ( (long *)p->portalflood )[j];
1735 more |= ( (long *)newmight )[j] & ~( (long *)cansee )[j];
1736 }
1737
1738 if ( !more ) {
1739 continue; // can't see anything new
1740
1741 }
1742 cansee[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
1743
1744 RecursiveLeafBitFlow( p->leaf, newmight, cansee );
1745 }
1746 }
1747
1748 /*
1749 ==============
1750 BetterPortalVis
1751 ==============
1752 */
1753 void BetterPortalVis( int portalnum ){
1754 vportal_t *p;
1755
1756 p = portals + portalnum;
1757
1758 if ( p->removed ) {
1759 return;
1760 }
1761
1762 RecursiveLeafBitFlow( p->leaf, p->portalflood, p->portalvis );
1763
1764 // build leaf vis information
1765 p->nummightsee = CountBits( p->portalvis, numportals * 2 );
1766 c_vis += p->nummightsee;
1767 }