forked from ericwa/ericw-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis.cc
1341 lines (1118 loc) · 35.1 KB
/
vis.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vis.c
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <vis/leafbits.hh>
#include <vis/vis.hh>
#include <common/log.hh>
#include <common/threads.hh>
/*
* If the portal file is "PRT2" format, then the leafs we are dealing with are
* really clusters of leaves. So, after the vis job is done we need to expand
* the clusters to the real leaf numbers before writing back to the bsp file.
*/
int numportals;
int portalleafs; /* leafs (PRT1) or clusters (PRT2) */
int portalleafs_real; /* real no. of leafs after expanding PRT2 clusters */
int *clustermap; /* mapping from real leaf to cluster number */
portal_t *portals;
leaf_t *leafs;
int c_portaltest, c_portalpass, c_portalcheck, c_mightseeupdate;
int c_noclip = 0;
qboolean showgetleaf = true;
static byte *vismap;
static byte *vismap_p;
static byte *vismap_end; // past visfile
int originalvismapsize;
byte *uncompressed; // [leafbytes_real*portalleafs]
int leafbytes; // (portalleafs+63)>>3
int leaflongs;
int leafbytes_real; // (portalleafs_real+63)>>3
/* Options - TODO: collect these in a struct */
qboolean fastvis;
static int verbose = 0;
int testlevel = 4;
qboolean ambientsky = true;
qboolean ambientwater = true;
qboolean ambientslime = true;
qboolean ambientlava = true;
#if 0
void
NormalizePlane(plane_t *dp)
{
vec_t ax, ay, az;
if (dp->normal[0] == -1.0) {
dp->normal[0] = 1.0;
dp->dist = -dp->dist;
return;
}
if (dp->normal[1] == -1.0) {
dp->normal[1] = 1.0;
dp->dist = -dp->dist;
return;
}
if (dp->normal[2] == -1.0) {
dp->normal[2] = 1.0;
dp->dist = -dp->dist;
return;
}
ax = fabs(dp->normal[0]);
ay = fabs(dp->normal[1]);
az = fabs(dp->normal[2]);
if (ax >= ay && ax >= az) {
if (dp->normal[0] < 0) {
VectorSubtract(vec3_origin, dp->normal, dp->normal);
dp->dist = -dp->dist;
}
return;
}
if (ay >= ax && ay >= az) {
if (dp->normal[1] < 0) {
VectorSubtract(vec3_origin, dp->normal, dp->normal);
dp->dist = -dp->dist;
}
return;
}
if (dp->normal[2] < 0) {
VectorSubtract(vec3_origin, dp->normal, dp->normal);
dp->dist = -dp->dist;
}
}
#endif
void
PlaneFromWinding(const winding_t * w, plane_t *plane)
{
vec3_t v1, v2;
// calc plane
VectorSubtract(w->points[2], w->points[1], v1);
VectorSubtract(w->points[0], w->points[1], v2);
CrossProduct(v2, v1, plane->normal);
VectorNormalize(plane->normal);
plane->dist = DotProduct(w->points[0], plane->normal);
}
//============================================================================
/*
==================
NewWinding
==================
*/
winding_t *
NewWinding(int points)
{
winding_t *w;
int size;
if (points > MAX_WINDING)
Error("%s: %i points", __func__, points);
size = offsetof(winding_t, points[points]);
w = static_cast<winding_t *>(malloc(size));
memset(w, 0, size);
return w;
}
void
LogWinding(const winding_t *w)
{
int i;
if (!verbose)
return;
for (i = 0; i < w->numpoints; i++)
logprint("(%5.1f, %5.1f, %5.1f)\n",
w->points[i][0], w->points[i][1], w->points[i][2]);
}
void
LogLeaf(const leaf_t *leaf)
{
const portal_t *portal;
const plane_t *plane;
int i;
if (!verbose)
return;
for (i = 0; i < leaf->numportals; i++) {
portal = leaf->portals[i];
plane = &portal->plane;
logprint("portal %4i to leaf %4i : %7.1f : (%4.2f, %4.2f, %4.2f)\n",
(int)(portal - portals), portal->leaf, plane->dist,
plane->normal[0], plane->normal[1], plane->normal[2]);
}
}
/*
==================
CopyWinding
==================
*/
winding_t *
CopyWinding(const winding_t * w)
{
int size;
winding_t *c;
size = offsetof(winding_t, points[w->numpoints]);
c = static_cast<winding_t *>(malloc(size));
memcpy(c, w, size);
return c;
}
/*
==================
AllocStackWinding
Return a pointer to a free fixed winding on the stack
==================
*/
winding_t *
AllocStackWinding(pstack_t *stack)
{
int i;
for (i = 0; i < STACK_WINDINGS; i++) {
if (stack->freewindings[i]) {
stack->freewindings[i] = 0;
return &stack->windings[i];
}
}
Error("%s: failed", __func__);
return NULL;
}
/*
==================
FreeStackWinding
As long as the winding passed in is local to the stack, free it. Otherwise,
do nothing (the winding either belongs to a portal or another stack
structure further up the call chain).
==================
*/
void
FreeStackWinding(winding_t *w, pstack_t *stack)
{
uintptr_t index = w - stack->windings;
if (index < (uintptr_t)STACK_WINDINGS) {
if (stack->freewindings[index])
Error("%s: winding already freed", __func__);
stack->freewindings[index] = 1;
}
}
/*
==================
ClipStackWinding
Clips the winding to the plane, returning the new winding on the positive
side. Frees the input winding (if on stack). If the resulting winding would
have too many points, the clip operation is aborted and the original winding
is returned.
==================
*/
winding_t *
ClipStackWinding(winding_t *in, pstack_t *stack, plane_t *split)
{
vec_t dists[MAX_WINDING + 1];
int sides[MAX_WINDING + 1];
int counts[3];
vec_t dot, fraction;
int i, j;
vec_t *p1, *p2;
vec3_t mid;
winding_t *neww;
/* Fast test first */
dot = DotProduct(in->origin, split->normal) - split->dist;
if (dot < -in->radius) {
FreeStackWinding(in, stack);
return NULL;
} else if (dot > in->radius) {
return in;
}
if (in->numpoints > MAX_WINDING)
Error("%s: in->numpoints > MAX_WINDING (%d > %d)",
__func__, in->numpoints, MAX_WINDING);
counts[0] = counts[1] = counts[2] = 0;
/* determine sides for each point */
for (i = 0; i < in->numpoints; i++) {
dot = DotProduct(in->points[i], split->normal);
dot -= split->dist;
dists[i] = dot;
if (dot > ON_EPSILON)
sides[i] = SIDE_FRONT;
else if (dot < -ON_EPSILON)
sides[i] = SIDE_BACK;
else {
sides[i] = SIDE_ON;
}
counts[sides[i]]++;
}
sides[i] = sides[0];
dists[i] = dists[0];
// ericw -- coplanar portals: return without clipping. Otherwise when two portals are less than ON_EPSILON apart,
// one will get fully clipped away and we can't see through it causing https://github.com/ericwa/ericw-tools/issues/261
if (counts[SIDE_ON] == in->numpoints) {
return in;
}
if (!counts[0]) {
FreeStackWinding(in, stack);
return NULL;
}
if (!counts[1])
return in;
neww = AllocStackWinding(stack);
neww->numpoints = 0;
VectorCopy(in->origin, neww->origin);
neww->radius = in->radius;
for (i = 0; i < in->numpoints; i++) {
p1 = in->points[i];
if (sides[i] == SIDE_ON) {
if (neww->numpoints == MAX_WINDING_FIXED)
goto noclip;
VectorCopy(p1, neww->points[neww->numpoints]);
neww->numpoints++;
continue;
}
if (sides[i] == SIDE_FRONT) {
if (neww->numpoints == MAX_WINDING_FIXED)
goto noclip;
VectorCopy(p1, neww->points[neww->numpoints]);
neww->numpoints++;
}
if (sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i])
continue;
/* generate a split point */
p2 = in->points[(i + 1) % in->numpoints];
fraction = dists[i] / (dists[i] - dists[i + 1]);
for (j = 0; j < 3; j++) {
/* avoid round off error when possible */
if (split->normal[j] == 1)
mid[j] = split->dist;
else if (split->normal[j] == -1)
mid[j] = -split->dist;
else
mid[j] = p1[j] + fraction * (p2[j] - p1[j]);
}
if (neww->numpoints == MAX_WINDING_FIXED)
goto noclip;
VectorCopy(mid, neww->points[neww->numpoints]);
neww->numpoints++;
}
FreeStackWinding(in, stack);
return neww;
noclip:
FreeStackWinding(neww, stack);
c_noclip++;
return in;
}
//============================================================================
/*
=============
GetNextPortal
Returns the next portal for a thread to work on
Returns the portals from the least complex, so the later ones can reuse
the earlier information.
=============
*/
portal_t *
GetNextPortal(void)
{
int i;
portal_t *p, *ret;
unsigned min;
ThreadLock();
min = INT_MAX;
ret = NULL;
for (i = 0, p = portals; i < numportals * 2; i++, p++) {
if (p->nummightsee < min && p->status == pstat_none) {
min = p->nummightsee;
ret = p;
}
}
if (ret) {
ret->status = pstat_working;
GetThreadWork_Locked__();
}
ThreadUnlock();
return ret;
}
/*
=============
UpdateMightSee
Called after completing a portal and finding that the source leaf is no
longer visible from the dest leaf. Visibility is symetrical, so the reverse
must also be true. Update mightsee for any portals on the source leaf which
haven't yet started processing.
Called with the lock held.
=============
*/
static void
UpdateMightsee(const leaf_t *source, const leaf_t *dest)
{
int i, leafnum;
portal_t *p;
leafnum = dest - leafs;
for (i = 0; i < source->numportals; i++) {
p = source->portals[i];
if (p->status != pstat_none)
continue;
if (TestLeafBit(p->mightsee, leafnum)) {
ClearLeafBit(p->mightsee, leafnum);
p->nummightsee--;
c_mightseeupdate++;
}
}
}
/*
=============
PortalCompleted
Mark the portal completed and propogate new vis information across
to the complementry portals.
Called with the lock held.
=============
*/
static void
PortalCompleted(portal_t *completed)
{
int i, j, k, bit, numblocks;
int leafnum;
const portal_t *p, *p2;
const leaf_t *myleaf;
const leafblock_t *might, *vis;
leafblock_t changed;
ThreadLock();
completed->status = pstat_done;
/*
* For each portal on the leaf, check the leafs we eliminated from
* mightsee during the full vis so far.
*/
myleaf = &leafs[completed->leaf];
for (i = 0; i < myleaf->numportals; i++) {
p = myleaf->portals[i];
if (p->status != pstat_done)
continue;
might = p->mightsee->bits;
vis = p->visbits->bits;
numblocks = (portalleafs + LEAFMASK) >> LEAFSHIFT;
for (j = 0; j < numblocks; j++) {
changed = might[j] & ~vis[j];
if (!changed)
continue;
/*
* If any of these changed bits are still visible from another
* portal, we can't update yet.
*/
for (k = 0; k < myleaf->numportals; k++) {
if (k == i)
continue;
p2 = myleaf->portals[k];
if (p2->status == pstat_done)
changed &= ~p2->visbits->bits[j];
else
changed &= ~p2->mightsee->bits[j];
if (!changed)
break;
}
/*
* Update mightsee for any of the changed bits that survived
*/
while (changed) {
bit = ffsl(changed) - 1;
changed &= ~(1UL << bit);
leafnum = (j << LEAFSHIFT) + bit;
UpdateMightsee(leafs + leafnum, myleaf);
}
}
}
ThreadUnlock();
}
double starttime, endtime, statetime;
static double stateinterval;
/*
==============
LeafThread
==============
*/
void *
LeafThread(void *arg)
{
double now;
portal_t *p;
do {
ThreadLock();
/* Save state if sufficient time has elapsed */
now = I_FloatTime();
if (now > statetime + stateinterval) {
statetime = now;
SaveVisState();
}
ThreadUnlock();
p = GetNextPortal();
if (!p)
break;
PortalFlow(p);
PortalCompleted(p);
if (verbose > 1) {
logprint("portal:%4i mightsee:%4i cansee:%4i\n",
(int)(p - portals), p->nummightsee, p->numcansee);
}
} while (1);
return NULL;
}
/*
===============
CompressRow
===============
*/
static int
CompressRow(const byte *vis, const int numbytes, byte *out)
{
int i, rep;
byte *dst;
dst = out;
for (i = 0; i < numbytes; i++) {
*dst++ = vis[i];
if (vis[i])
continue;
rep = 1;
for (i++; i < numbytes; i++)
if (vis[i] || rep == 255)
break;
else
rep++;
*dst++ = rep;
i--;
}
return dst - out;
}
/*
===============
LeafFlow
Builds the entire visibility list for a leaf
===============
*/
int64_t totalvis;
static void
LeafFlow(int leafnum, mleaf_t *dleaf)
{
leaf_t *leaf;
byte *outbuffer;
byte *compressed;
int i, j, shift, len;
int numvis;
byte *dest;
const portal_t *p;
/*
* flow through all portals, collecting visible bits
*/
outbuffer = uncompressed + leafnum * leafbytes;
leaf = &leafs[leafnum];
for (i = 0; i < leaf->numportals; i++) {
p = leaf->portals[i];
if (p->status != pstat_done)
Error("portal not done");
for (j = 0; j < leafbytes; j++) {
shift = (j << 3) & LEAFMASK;
outbuffer[j] |= (p->visbits->bits[j >> (LEAFSHIFT - 3)] >> shift) & 0xff;
}
}
if (outbuffer[leafnum >> 3] & (1 << (leafnum & 7)))
logprint("WARNING: Leaf portals saw into leaf (%i)\n", leafnum);
outbuffer[leafnum >> 3] |= (1 << (leafnum & 7));
numvis = 0;
for (i = 0; i < portalleafs; i++)
if (outbuffer[i >> 3] & (1 << (i & 3)))
numvis++;
/*
* compress the bit string
*/
if (verbose > 1)
logprint("leaf %4i : %4i visible\n", leafnum, numvis);
totalvis += numvis;
/* Allocate for worst case where RLE might grow the data (unlikely) */
compressed = static_cast<byte *>(malloc(portalleafs * 2 / 8));
len = CompressRow(outbuffer, (portalleafs + 7) >> 3, compressed);
dest = vismap_p;
vismap_p += len;
if (vismap_p > vismap_end)
Error("Vismap expansion overflow");
/* leaf 0 is a common solid */
dleaf->visofs = dest - vismap;
memcpy(dest, compressed, len);
free(compressed);
}
void
ClusterFlow(int clusternum, leafbits_t *buffer)
{
leaf_t *leaf;
byte *outbuffer;
byte *compressed;
int i, j, len;
int numvis, numblocks;
byte *dest;
const portal_t *p;
/*
* Collect visible bits from all portals into buffer
*/
leaf = &leafs[clusternum];
numblocks = (portalleafs + LEAFMASK) >> LEAFSHIFT;
for (i = 0; i < leaf->numportals; i++) {
p = leaf->portals[i];
if (p->status != pstat_done)
Error("portal not done");
for (j = 0; j < numblocks; j++)
buffer->bits[j] |= p->visbits->bits[j];
}
// ericw -- this seems harmless and the fix for https://github.com/ericwa/ericw-tools/issues/261
// causes it to happen a lot.
//if (TestLeafBit(buffer, clusternum))
// logprint("WARNING: Leaf portals saw into cluster (%i)\n", clusternum);
SetLeafBit(buffer, clusternum);
/*
* Now expand the clusters into the full leaf visibility map
*/
numvis = 0;
outbuffer = uncompressed + clusternum * leafbytes_real;
for (i = 0; i < portalleafs_real; i++) {
if (TestLeafBit(buffer, clustermap[i])) {
outbuffer[i >> 3] |= (1 << (i & 7));
numvis++;
}
}
/*
* compress the bit string
*/
if (verbose > 1)
logprint("cluster %4i : %4i visible\n", clusternum, numvis);
/*
* increment totalvis by
* (# of real leafs in this cluster) x (# of real leafs visible from this cluster)
*/
for (i = 0; i < portalleafs_real; i++) {
if (clustermap[i] == clusternum) {
totalvis += numvis;
}
}
/* Allocate for worst case where RLE might grow the data (unlikely) */
compressed = static_cast<byte *>(malloc(portalleafs_real * 2 / 8));
len = CompressRow(outbuffer, (portalleafs_real + 7) >> 3, compressed);
dest = vismap_p;
vismap_p += len;
if (vismap_p > vismap_end)
Error("Vismap expansion overflow");
/* leaf 0 is a common solid */
leaf->visofs = dest - vismap;
memcpy(dest, compressed, len);
free(compressed);
}
/*
==================
CalcPortalVis
==================
*/
void
CalcPortalVis(const mbsp_t *bsp)
{
int i, startcount;
portal_t *p;
// fastvis just uses mightsee for a very loose bound
if (fastvis) {
for (i = 0; i < numportals * 2; i++) {
portals[i].visbits = portals[i].mightsee;
portals[i].status = pstat_done;
}
return;
}
/*
* Count the already completed portals in case we loaded previous state
*/
startcount = 0;
for (i = 0, p = portals; i < numportals * 2; i++, p++) {
if (p->status == pstat_done)
startcount++;
}
RunThreadsOn(startcount, numportals * 2, LeafThread, NULL);
if (verbose) {
logprint("portalcheck: %i portaltest: %i portalpass: %i\n",
c_portalcheck, c_portaltest, c_portalpass);
logprint("c_vistest: %i c_mighttest: %i c_mightseeupdate %i\n",
c_vistest, c_mighttest, c_mightseeupdate);
}
}
/*
==================
CalcVis
==================
*/
void
CalcVis(const mbsp_t *bsp)
{
int i;
if (LoadVisState()) {
logprint("Loaded previous state. Resuming progress...\n");
} else {
logprint("Calculating Base Vis:\n");
BasePortalVis();
}
logprint("Calculating Full Vis:\n");
CalcPortalVis(bsp);
//
// assemble the leaf vis lists by oring and compressing the portal lists
//
if (portalleafs == portalleafs_real) {
for (i = 0; i < portalleafs; i++)
LeafFlow(i, &bsp->dleafs[i + 1]);
} else {
leafbits_t *buffer;
logprint("Expanding clusters...\n");
buffer = static_cast<leafbits_t *>(malloc(LeafbitsSize(portalleafs)));
for (i = 0; i < portalleafs; i++) {
memset(buffer, 0, LeafbitsSize(portalleafs));
ClusterFlow(i, buffer);
}
free(buffer);
// Set pointers
for (i = 0; i < portalleafs_real; i++) {
bsp->dleafs[i + 1].visofs = leafs[clustermap[i]].visofs;
}
}
int64_t avg = totalvis;
avg /= static_cast<int64_t>(portalleafs_real);
logprint("average leafs visible: %i\n", static_cast<int>(avg));
}
/*
============================================================================
PASSAGE CALCULATION (not used yet...)
============================================================================
*/
int count_sep;
qboolean
PlaneCompare(plane_t *p1, plane_t *p2)
{
int i;
if (fabs(p1->dist - p2->dist) > 0.01)
return false;
for (i = 0; i < 3; i++)
if (fabs(p1->normal[i] - p2->normal[i]) > 0.001)
return false;
return true;
}
sep_t *
Findpassages(winding_t * source, winding_t * pass)
{
int i, j, k, l;
plane_t plane;
vec3_t v1, v2;
float d;
double length;
int counts[3];
qboolean fliptest;
sep_t *sep, *list;
list = NULL;
// check all combinations
for (i = 0; i < source->numpoints; i++) {
l = (i + 1) % source->numpoints;
VectorSubtract(source->points[l], source->points[i], v1);
// fing a vertex of pass that makes a plane that puts all of the
// vertexes of pass on the front side and all of the vertexes of
// source on the back side
for (j = 0; j < pass->numpoints; j++) {
VectorSubtract(pass->points[j], source->points[i], v2);
plane.normal[0] = v1[1] * v2[2] - v1[2] * v2[1];
plane.normal[1] = v1[2] * v2[0] - v1[0] * v2[2];
plane.normal[2] = v1[0] * v2[1] - v1[1] * v2[0];
// if points don't make a valid plane, skip it
length = plane.normal[0] * plane.normal[0]
+ plane.normal[1] * plane.normal[1]
+ plane.normal[2] * plane.normal[2];
if (length < ON_EPSILON)
continue;
length = 1 / sqrt(length);
plane.normal[0] *= (vec_t)length;
plane.normal[1] *= (vec_t)length;
plane.normal[2] *= (vec_t)length;
plane.dist = DotProduct(pass->points[j], plane.normal);
//
// find out which side of the generated seperating plane has the
// source portal
//
fliptest = false;
for (k = 0; k < source->numpoints; k++) {
if (k == i || k == l)
continue;
d = DotProduct(source->points[k], plane.normal) - plane.dist;
if (d < -ON_EPSILON) { // source is on the negative side, so we want all
// pass and target on the positive side
fliptest = false;
break;
} else if (d > ON_EPSILON) { // source is on the positive side, so we want all
// pass and target on the negative side
fliptest = true;
break;
}
}
if (k == source->numpoints)
continue; // planar with source portal
//
// flip the normal if the source portal is backwards
//
if (fliptest) {
VectorSubtract(vec3_origin, plane.normal, plane.normal);
plane.dist = -plane.dist;
}
//
// if all of the pass portal points are now on the positive side,
// this is the seperating plane
//
counts[0] = counts[1] = counts[2] = 0;
for (k = 0; k < pass->numpoints; k++) {
if (k == j)
continue;
d = DotProduct(pass->points[k], plane.normal) - plane.dist;
if (d < -ON_EPSILON)
break;
else if (d > ON_EPSILON)
counts[0]++;
else
counts[2]++;
}
if (k != pass->numpoints)
continue; // points on negative side, not a seperating plane
if (!counts[0])
continue; // planar with pass portal
//
// save this out
//
count_sep++;
sep = static_cast<sep_t *>(malloc(sizeof(*sep)));
sep->next = list;
list = sep;
sep->plane = plane;
}
}
return list;
}
/*
============
CalcPassages
============
*/
void
CalcPassages(void)
{
int i, j, k;
int count, count2;
leaf_t *l;
portal_t *p1, *p2;
sep_t *sep;
passage_t *passages;
logprint("building passages...\n");
count = count2 = 0;
for (i = 0; i < portalleafs; i++) {
l = &leafs[i];
for (j = 0; j < l->numportals; j++) {
p1 = l->portals[j];
for (k = 0; k < l->numportals; k++) {
if (k == j)
continue;
count++;
p2 = l->portals[k];
// definately can't see into a coplanar portal
if (PlaneCompare(&p1->plane, &p2->plane))
continue;
count2++;
sep = Findpassages(p1->winding, p2->winding);
if (!sep) {
// Error ("No seperating planes found in portal pair");
count_sep++;
sep = static_cast<sep_t *>(malloc(sizeof(*sep)));
sep->next = NULL;
sep->plane = p1->plane;
}
passages = static_cast<passage_t *>(malloc(sizeof(*passages)));
passages->planes = sep;
passages->from = p1->leaf;
passages->to = p2->leaf;
passages->next = l->passages;
l->passages = passages;
}
}
}
logprint("numpassages: %i (%i)\n", count2, count);
logprint("total passages: %i\n", count_sep);
}
// ===========================================================================