forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisp_common.cpp
1296 lines (1082 loc) · 35.6 KB
/
disp_common.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "disp_common.h"
#include "disp_powerinfo.h"
#include "builddisp.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
class CNodeVert
{
public:
CNodeVert() {}
CNodeVert( int ix, int iy ) {x=ix; y=iy;}
inline int& operator[]( int i ) {return ((int*)this)[i];}
inline int const& operator[]( int i ) const {return ((int*)this)[i];}
int x, y;
};
static CNodeVert const g_NodeChildLookup[4][2] =
{
{CNodeVert(0,0), CNodeVert(1,1)},
{CNodeVert(1,0), CNodeVert(2,1)},
{CNodeVert(0,1), CNodeVert(1,2)},
{CNodeVert(1,1), CNodeVert(2,2)}
};
static CNodeVert const g_NodeTriWinding[9] =
{
CNodeVert(0, 1),
CNodeVert(0, 0),
CNodeVert(1, 0),
CNodeVert(2, 0),
CNodeVert(2, 1),
CNodeVert(2, 2),
CNodeVert(1, 2),
CNodeVert(0, 2),
CNodeVert(0, 1)
};
// Indexed by CORNER_. These store NEIGHBOREDGE_ defines and tell which edges butt up against the corner.
static int g_CornerEdges[4][2] =
{
{ NEIGHBOREDGE_BOTTOM, NEIGHBOREDGE_LEFT }, // CORNER_LOWER_LEFT
{ NEIGHBOREDGE_TOP, NEIGHBOREDGE_LEFT }, // CORNER_UPPER_LEFT
{ NEIGHBOREDGE_TOP, NEIGHBOREDGE_RIGHT }, // CORNER_UPPER_RIGHT
{ NEIGHBOREDGE_BOTTOM, NEIGHBOREDGE_RIGHT } // CORNER_LOWER_RIGHT
};
int g_EdgeDims[4] =
{
0, // NEIGHBOREDGE_LEFT = X
1, // NEIGHBOREDGE_TOP = Y
0, // NEIGHBOREDGE_RIGHT = X
1 // NEIGHBOREDGE_BOTTOM = Y
};
CShiftInfo g_ShiftInfos[3][3] =
{
{
{0, 0, true}, // CORNER_TO_CORNER -> CORNER_TO_CORNER
{0, -1, true}, // CORNER_TO_CORNER -> CORNER_TO_MIDPOINT
{2, -1, true} // CORNER_TO_CORNER -> MIDPOINT_TO_CORNER
},
{
{0, 1, true}, // CORNER_TO_MIDPOINT -> CORNER_TO_CORNER
{0, 0, false}, // CORNER_TO_MIDPOINT -> CORNER_TO_MIDPOINT (invalid)
{0, 0, false} // CORNER_TO_MIDPOINT -> MIDPOINT_TO_CORNER (invalid)
},
{
{-1, 1, true}, // MIDPOINT_TO_CORNER -> CORNER_TO_CORNER
{0, 0, false}, // MIDPOINT_TO_CORNER -> CORNER_TO_MIDPOINT (invalid)
{0, 0, false} // MIDPOINT_TO_CORNER -> MIDPOINT_TO_CORNER (invalid)
}
};
int g_EdgeSideLenMul[4] =
{
0,
1,
1,
0
};
// --------------------------------------------------------------------------------- //
// Helper functions.
// --------------------------------------------------------------------------------- //
inline int SignedBitShift( int val, int shift )
{
if( shift > 0 )
return val << shift;
else
return val >> -shift;
}
static inline void RotateVertIndex(
NeighborOrientation neighor,
int sideLengthMinus1,
CVertIndex const &in,
CVertIndex &out )
{
if( neighor == ORIENTATION_CCW_0 )
{
out = in;
}
else if( neighor == ORIENTATION_CCW_90 )
{
out.x = in.y;
out.y = sideLengthMinus1 - in.x;
}
else if( neighor == ORIENTATION_CCW_180 )
{
out.x = sideLengthMinus1 - in.x;
out.y = sideLengthMinus1 - in.y;
}
else
{
out.x = sideLengthMinus1 - in.y;
out.y = in.x;
}
}
static inline void RotateVertIncrement(
NeighborOrientation neighor,
CVertIndex const &in,
CVertIndex &out )
{
if( neighor == ORIENTATION_CCW_0 )
{
out = in;
}
else if( neighor == ORIENTATION_CCW_90 )
{
out.x = in.y;
out.y = -in.x;
}
else if( neighor == ORIENTATION_CCW_180 )
{
out.x = -in.x;
out.y = -in.y;
}
else
{
out.x = -in.y;
out.y = in.x;
}
}
// --------------------------------------------------------------------------------- //
// CDispHelper functions.
// --------------------------------------------------------------------------------- //
int GetEdgeIndexFromPoint( CVertIndex const &index, int iMaxPower )
{
int sideLengthMinus1 = 1 << iMaxPower;
if( index.x == 0 )
return NEIGHBOREDGE_LEFT;
else if( index.y == sideLengthMinus1 )
return NEIGHBOREDGE_TOP;
else if( index.x == sideLengthMinus1 )
return NEIGHBOREDGE_RIGHT;
else if( index.y == 0 )
return NEIGHBOREDGE_BOTTOM;
else
return -1;
}
int GetCornerIndexFromPoint( CVertIndex const &index, int iPower )
{
int sideLengthMinus1 = 1 << iPower;
if( index.x == 0 && index.y == 0 )
return CORNER_LOWER_LEFT;
else if( index.x == 0 && index.y == sideLengthMinus1 )
return CORNER_UPPER_LEFT;
else if( index.x == sideLengthMinus1 && index.y == sideLengthMinus1 )
return CORNER_UPPER_RIGHT;
else if( index.x == sideLengthMinus1 && index.y == 0 )
return CORNER_LOWER_RIGHT;
else
return -1;
}
int GetNeighborEdgePower( CDispUtilsHelper *pDisp, int iEdge, int iSub )
{
CDispNeighbor *pEdge = pDisp->GetEdgeNeighbor( iEdge );
CDispSubNeighbor *pSub = &pEdge->m_SubNeighbors[iSub];
if ( !pSub->IsValid() )
return -1;
CDispUtilsHelper *pNeighbor = pDisp->GetDispUtilsByIndex( pSub->GetNeighborIndex() );
CShiftInfo *pInfo = &g_ShiftInfos[pSub->m_Span][pSub->m_NeighborSpan];
Assert( pInfo->m_bValid );
return pNeighbor->GetPower() + pInfo->m_PowerShiftAdd;
}
CDispUtilsHelper* SetupEdgeIncrements(
CDispUtilsHelper *pDisp,
int iEdge,
int iSub,
CVertIndex &myIndex,
CVertIndex &myInc,
CVertIndex &nbIndex,
CVertIndex &nbInc,
int &myEnd,
int &iFreeDim )
{
int iEdgeDim = g_EdgeDims[iEdge];
iFreeDim = !iEdgeDim;
CDispNeighbor *pSide = pDisp->GetEdgeNeighbor( iEdge );
CDispSubNeighbor *pSub = &pSide->m_SubNeighbors[iSub];
if ( !pSub->IsValid() )
return NULL;
CDispUtilsHelper *pNeighbor = pDisp->GetDispUtilsByIndex( pSub->m_iNeighbor );
CShiftInfo *pShiftInfo = &g_ShiftInfos[pSub->m_Span][pSub->m_NeighborSpan];
Assert( pShiftInfo->m_bValid );
// Setup a start point and edge increment (NOTE: just precalculate these
// and store them in the CDispSubNeighbors).
CVertIndex tempInc;
const CPowerInfo *pPowerInfo = pDisp->GetPowerInfo();
myIndex[iEdgeDim] = g_EdgeSideLenMul[iEdge] * pPowerInfo->m_SideLengthM1;
myIndex[iFreeDim] = pPowerInfo->m_MidPoint * iSub;
TransformIntoSubNeighbor( pDisp, iEdge, iSub, myIndex, nbIndex );
int myPower = pDisp->GetPowerInfo()->m_Power;
int nbPower = pNeighbor->GetPowerInfo()->m_Power + pShiftInfo->m_PowerShiftAdd;
myInc[iEdgeDim] = tempInc[iEdgeDim] = 0;
if( nbPower > myPower )
{
myInc[iFreeDim] = 1;
tempInc[iFreeDim] = 1 << (nbPower - myPower);
}
else
{
myInc[iFreeDim] = 1 << (myPower - nbPower);
tempInc[iFreeDim] = 1;
}
RotateVertIncrement( pSub->GetNeighborOrientation(), tempInc, nbInc );
// Walk along the edge.
if( pSub->m_Span == CORNER_TO_MIDPOINT )
myEnd = pDisp->GetPowerInfo()->m_SideLength >> 1;
else
myEnd = pDisp->GetPowerInfo()->m_SideLength - 1;
return pNeighbor;
}
int GetSubNeighborIndex(
CDispUtilsHelper *pDisp,
int iEdge,
CVertIndex const &nodeIndex )
{
const CPowerInfo *pPowerInfo = pDisp->GetPowerInfo();
const CDispNeighbor *pSide = pDisp->GetEdgeNeighbor( iEdge );
// Figure out if this is a vertical or horizontal edge.
int iEdgeDim = g_EdgeDims[iEdge];
int iFreeDim = !iEdgeDim;
int iFreeIndex = nodeIndex[iFreeDim];
// Figure out which of the (up to two) neighbors it lies in.
int iSub = 0;
if( iFreeIndex == pPowerInfo->m_MidPoint )
{
// If it's in the middle, we only are interested if there's one neighbor
// next to us (so we can enable its middle vert). If there are any neighbors
// that touch the midpoint, then we have no need to return them because it would
// touch their corner verts which are always active.
if( pSide->m_SubNeighbors[0].m_Span != CORNER_TO_CORNER )
return -1;
}
else if ( iFreeIndex > pPowerInfo->m_MidPoint )
{
iSub = 1;
}
// Make sure we get a valid neighbor.
if( !pSide->m_SubNeighbors[iSub].IsValid() )
{
if( iSub == 1 &&
pSide->m_SubNeighbors[0].IsValid() &&
pSide->m_SubNeighbors[0].m_Span == CORNER_TO_CORNER )
{
iSub = 0;
}
else
{
return -1;
}
}
return iSub;
}
void SetupSpan( int iPower, int iEdge, NeighborSpan span, CVertIndex &viStart, CVertIndex &viEnd )
{
int iFreeDim = !g_EdgeDims[iEdge];
const CPowerInfo *pPowerInfo = GetPowerInfo( iPower );
viStart = pPowerInfo->GetCornerPointIndex( iEdge );
viEnd = pPowerInfo->GetCornerPointIndex( (iEdge+1) & 3 );;
if ( iEdge == NEIGHBOREDGE_RIGHT || iEdge == NEIGHBOREDGE_BOTTOM )
{
// CORNER_TO_MIDPOINT and MIDPOINT_CORNER are defined where the edge moves up or right,
// but pPowerInfo->GetCornerPointIndex walks around the edges clockwise, so on the
// bottom and right edges (where GetCornerPointIndex has us moving down and left) we need to
// reverse the sense here to make sure we return the right span.
if ( span == CORNER_TO_MIDPOINT )
viStart[iFreeDim] = pPowerInfo->GetMidPoint();
else if ( span == MIDPOINT_TO_CORNER )
viEnd[iFreeDim] = pPowerInfo->GetMidPoint();
}
else
{
if ( span == CORNER_TO_MIDPOINT )
viEnd[iFreeDim] = pPowerInfo->GetMidPoint();
else if ( span == MIDPOINT_TO_CORNER )
viStart[iFreeDim] = pPowerInfo->GetMidPoint();
}
}
CDispUtilsHelper* TransformIntoSubNeighbor(
CDispUtilsHelper *pDisp,
int iEdge,
int iSub,
CVertIndex const &nodeIndex,
CVertIndex &out
)
{
const CDispSubNeighbor *pSub = &pDisp->GetEdgeNeighbor( iEdge )->m_SubNeighbors[iSub];
// Find the part of pDisp's edge that this neighbor covers.
CVertIndex viSrcStart, viSrcEnd;
SetupSpan( pDisp->GetPower(), iEdge, pSub->GetSpan(), viSrcStart, viSrcEnd );
// Find the corresponding parts on the neighbor.
CDispUtilsHelper *pNeighbor = pDisp->GetDispUtilsByIndex( pSub->GetNeighborIndex() );
int iNBEdge = (iEdge + 2 + pSub->GetNeighborOrientation()) & 3;
CVertIndex viDestStart, viDestEnd;
SetupSpan( pNeighbor->GetPower(), iNBEdge, pSub->GetNeighborSpan(), viDestEnd, viDestStart );
// Now map the one into the other.
int iFreeDim = !g_EdgeDims[iEdge];
int fixedPercent = ((nodeIndex[iFreeDim] - viSrcStart[iFreeDim]) * (1<<16)) / (viSrcEnd[iFreeDim] - viSrcStart[iFreeDim]);
Assert( fixedPercent >= 0 && fixedPercent <= (1<<16) );
int nbDim = g_EdgeDims[iNBEdge];
out[nbDim] = viDestStart[nbDim];
out[!nbDim] = viDestStart[!nbDim] + ((viDestEnd[!nbDim] - viDestStart[!nbDim]) * fixedPercent) / (1<<16);
Assert( out.x >= 0 && out.x < pNeighbor->GetSideLength() );
Assert( out.y >= 0 && out.y < pNeighbor->GetSideLength() );
return pNeighbor;
}
CDispUtilsHelper* TransformIntoNeighbor(
CDispUtilsHelper *pDisp,
int iEdge,
CVertIndex const &nodeIndex,
CVertIndex &out
)
{
if ( iEdge == -1 )
iEdge = GetEdgeIndexFromPoint( nodeIndex, pDisp->GetPower() );
int iSub = GetSubNeighborIndex( pDisp, iEdge, nodeIndex );
if ( iSub == -1 )
return NULL;
CDispUtilsHelper *pRet = TransformIntoSubNeighbor( pDisp, iEdge, iSub, nodeIndex, out );
#if 0
// Debug check.. make sure it comes back to the same point from the other side.
#if defined( _DEBUG )
static bool bTesting = false;
if ( pRet && !bTesting )
{
bTesting = true;
// We could let TransformIntoNeighbor figure out the index but if this is a corner vert, then
// it may pick the wrong edge and we'd get a benign assert.
int nbOrientation = pDisp->GetEdgeNeighbor( iEdge )->m_SubNeighbors[iSub].GetNeighborOrientation();
int iNeighborEdge = (iEdge + 2 + nbOrientation) & 3;
CVertIndex testIndex;
CDispUtilsHelper *pTest = TransformIntoNeighbor( pRet, iNeighborEdge, out, testIndex );
Assert( pTest == pDisp );
Assert( testIndex == nodeIndex );
bTesting = false;
}
#endif
#endif
return pRet;
}
bool DoesPointHaveAnyNeighbors(
CDispUtilsHelper *pDisp,
const CVertIndex &index )
{
// See if it connects to a neighbor on the edge.
CVertIndex dummy;
if ( TransformIntoNeighbor( pDisp, -1, index, dummy ) )
return true;
// See if it connects to a neighbor on a corner.
int iCorner = GetCornerIndexFromPoint( index, pDisp->GetPower() );
if ( iCorner == -1 )
return false;
// If there are any neighbors on the specified corner, then the point has neighbors.
if ( pDisp->GetCornerNeighbors( iCorner )->m_nNeighbors > 0 )
return true;
// Since points on corners touch two edges, we actually want to test two edges to see
// if the point has a neighbor on either edge.
for ( int i=0; i < 2; i++ )
{
if ( TransformIntoNeighbor( pDisp, g_CornerEdges[iCorner][i], index, dummy ) )
return true;
}
return false;
}
// ------------------------------------------------------------------------------------ //
// CDispSubEdgeIterator.
// ------------------------------------------------------------------------------------ //
CDispSubEdgeIterator::CDispSubEdgeIterator()
{
m_pNeighbor = 0;
m_FreeDim = m_Index.x = m_Inc.x = m_End = 0; // Setup so Next returns false.
}
void CDispSubEdgeIterator::Start( CDispUtilsHelper *pDisp, int iEdge, int iSub, bool bTouchCorners )
{
m_pNeighbor = SetupEdgeIncrements( pDisp, iEdge, iSub, m_Index, m_Inc, m_NBIndex, m_NBInc, m_End, m_FreeDim );
if ( m_pNeighbor )
{
if ( bTouchCorners )
{
// Back up our current position by 1 so we hit the corner first, and extend the endpoint
// so we hit the other corner too.
m_Index -= m_Inc;
m_NBIndex -= m_NBInc;
m_End += m_Inc[m_FreeDim];
}
}
else
{
m_FreeDim = m_Index.x = m_Inc.x = m_End = 0; // Setup so Next returns false.
}
}
bool CDispSubEdgeIterator::Next()
{
m_Index += m_Inc;
m_NBIndex += m_NBInc;
// Were we just at the last point on the edge?
return m_Index[m_FreeDim] < m_End;
}
bool CDispSubEdgeIterator::IsLastVert() const
{
return (m_Index[m_FreeDim] + m_Inc[m_FreeDim]) >= m_End;
}
// ------------------------------------------------------------------------------------ //
// CDispEdgeIterator.
// ------------------------------------------------------------------------------------ //
CDispEdgeIterator::CDispEdgeIterator( CDispUtilsHelper *pDisp, int iEdge )
{
m_pDisp = pDisp;
m_iEdge = iEdge;
m_iCurSub = -1;
}
bool CDispEdgeIterator::Next()
{
while ( !m_It.Next() )
{
// Ok, move up to the next sub.
if ( m_iCurSub == 1 )
return false;
++m_iCurSub;
m_It.Start( m_pDisp, m_iEdge, m_iCurSub );
}
return true;
}
// ------------------------------------------------------------------------------------ //
// CDispCircumferenceIterator.
// ------------------------------------------------------------------------------------ //
CDispCircumferenceIterator::CDispCircumferenceIterator( int sideLength )
{
m_iCurEdge = -1;
m_SideLengthM1 = sideLength - 1;
}
bool CDispCircumferenceIterator::Next()
{
switch ( m_iCurEdge )
{
case -1:
{
m_iCurEdge = NEIGHBOREDGE_LEFT;
m_VertIndex.Init( 0, 0 );
}
break;
case NEIGHBOREDGE_LEFT:
{
++m_VertIndex.y;
if ( m_VertIndex.y == m_SideLengthM1 )
m_iCurEdge = NEIGHBOREDGE_TOP;
}
break;
case NEIGHBOREDGE_TOP:
{
++m_VertIndex.x;
if ( m_VertIndex.x == m_SideLengthM1 )
m_iCurEdge = NEIGHBOREDGE_RIGHT;
}
break;
case NEIGHBOREDGE_RIGHT:
{
--m_VertIndex.y;
if ( m_VertIndex.y == 0 )
m_iCurEdge = NEIGHBOREDGE_BOTTOM;
}
break;
case NEIGHBOREDGE_BOTTOM:
{
--m_VertIndex.x;
if ( m_VertIndex.x == 0 )
return false; // Done!
}
break;
}
return true;
}
// Helper function to setup an index either on the edges or the center
// of the box defined by [bottomleft,topRight].
static inline void SetupCoordXY( CNodeVert &out, CNodeVert const &bottomLeft, CNodeVert const &topRight, CNodeVert const &info )
{
for( int i=0; i < 2; i++ )
{
if( info[i] == 0 )
out[i] = bottomLeft[i];
else if( info[i] == 1 )
out[i] = (bottomLeft[i] + topRight[i]) >> 1;
else
out[i] = topRight[i];
}
}
static unsigned short* DispCommon_GenerateTriIndices_R(
CNodeVert const &bottomLeft,
CNodeVert const &topRight,
unsigned short *indices,
int power,
int sideLength )
{
if( power == 1 )
{
// Ok, add triangles. All we do here is follow a list of verts (g_NodeTriWinding)
// around the center vert of this node and make triangles.
int iCurTri = 0;
CNodeVert verts[3];
// verts[0] is always the center vert.
SetupCoordXY( verts[0], bottomLeft, topRight, CNodeVert(1,1) );
int iCurVert = 1;
for( int i=0; i < 9; i++ )
{
SetupCoordXY( verts[iCurVert], bottomLeft, topRight, g_NodeTriWinding[i] );
++iCurVert;
if( iCurVert == 3 )
{
for( int iTriVert=2; iTriVert >= 0; iTriVert-- )
{
int index = verts[iTriVert].y * sideLength + verts[iTriVert].x;
*indices = index;
++indices;
}
// Setup for the next triangle.
verts[1] = verts[2];
iCurVert = 2;
iCurTri++;
}
}
}
else
{
// Recurse into the children.
for( int i=0; i < 4; i++ )
{
CNodeVert childBottomLeft, childTopRight;
SetupCoordXY( childBottomLeft, bottomLeft, topRight, g_NodeChildLookup[i][0] );
SetupCoordXY( childTopRight, bottomLeft, topRight, g_NodeChildLookup[i][1] );
indices = DispCommon_GenerateTriIndices_R( childBottomLeft, childTopRight, indices, power-1, sideLength );
}
}
return indices;
}
// ------------------------------------------------------------------------------------------- //
// CDispUtilsHelper functions.
// ------------------------------------------------------------------------------------------- //
int CDispUtilsHelper::GetPower() const
{
return GetPowerInfo()->GetPower();
}
int CDispUtilsHelper::GetSideLength() const
{
return GetPowerInfo()->GetSideLength();
}
const CVertIndex& CDispUtilsHelper::GetCornerPointIndex( int iCorner ) const
{
return GetPowerInfo()->GetCornerPointIndex( iCorner );
}
int CDispUtilsHelper::VertIndexToInt( const CVertIndex &i ) const
{
Assert( i.x >= 0 && i.x < GetSideLength() && i.y >= 0 && i.y < GetSideLength() );
return i.y * GetSideLength() + i.x;
}
CVertIndex CDispUtilsHelper::GetEdgeMidPoint( int iEdge ) const
{
int end = GetSideLength() - 1;
int mid = GetPowerInfo()->GetMidPoint();
if ( iEdge == NEIGHBOREDGE_LEFT )
return CVertIndex( 0, mid );
else if ( iEdge == NEIGHBOREDGE_TOP )
return CVertIndex( mid, end );
else if ( iEdge == NEIGHBOREDGE_RIGHT )
return CVertIndex( end, mid );
else if ( iEdge == NEIGHBOREDGE_BOTTOM )
return CVertIndex( mid, 0 );
Assert( false );
return CVertIndex( 0, 0 );
}
int DispCommon_GetNumTriIndices( int power )
{
return (1<<power) * (1<<power) * 2 * 3;
}
void DispCommon_GenerateTriIndices( int power, unsigned short *indices )
{
int sideLength = 1 << power;
DispCommon_GenerateTriIndices_R(
CNodeVert( 0, 0 ),
CNodeVert( sideLength, sideLength ),
indices,
power,
sideLength+1 );
}
//=============================================================================
//
// Finding neighbors.
//
// This table swaps MIDPOINT_TO_CORNER and CORNER_TO_MIDPOINT.
static NeighborSpan g_SpanFlip[3] = {CORNER_TO_CORNER, MIDPOINT_TO_CORNER, CORNER_TO_MIDPOINT};
static bool g_bEdgeNeighborFlip[4] = {false, false, true, true};
// These map CCoreDispSurface neighbor orientations (which are actually edge indices)
// into our 'degrees of rotation' representation.
static int g_CoreDispNeighborOrientationMap[4][4] =
{
{ORIENTATION_CCW_180, ORIENTATION_CCW_270, ORIENTATION_CCW_0, ORIENTATION_CCW_90},
{ORIENTATION_CCW_90, ORIENTATION_CCW_180, ORIENTATION_CCW_270, ORIENTATION_CCW_0},
{ORIENTATION_CCW_0, ORIENTATION_CCW_90, ORIENTATION_CCW_180, ORIENTATION_CCW_270},
{ORIENTATION_CCW_270, ORIENTATION_CCW_0, ORIENTATION_CCW_90, ORIENTATION_CCW_180}
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void ClearNeighborData( CCoreDispInfo *pDisp )
{
for ( int i=0; i < 4; i++ )
{
pDisp->GetEdgeNeighbor( i )->SetInvalid();
pDisp->GetCornerNeighbors( i )->SetInvalid();
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void GetDispBox( CCoreDispInfo *pDisp, CDispBox &box )
{
// Calculate the bbox for this displacement.
Vector vMin( 1e24, 1e24, 1e24 );
Vector vMax( -1e24, -1e24, -1e24 );
for ( int iVert = 0; iVert < 4; ++iVert )
{
const Vector &vTest = pDisp->GetSurface()->GetPoint( iVert );
VectorMin( vTest, vMin, vMin );
VectorMax( vTest, vMax, vMax );
}
// Puff the box out a little.
static float flPuff = 0.1f;
vMin -= Vector( flPuff, flPuff, flPuff );
vMax += Vector( flPuff, flPuff, flPuff );
box.m_Min = vMin;
box.m_Max = vMax;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SetupDispBoxes( CCoreDispInfo **ppListBase, int nListSize, CUtlVector<CDispBox> &out )
{
out.SetSize( nListSize );
for ( int iDisp = 0; iDisp < nListSize; ++iDisp )
{
CCoreDispInfo *pDisp = ppListBase[iDisp];
GetDispBox( pDisp, out[iDisp] );
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline bool DoBBoxesTouch( const CDispBox &a, const CDispBox &b )
{
for ( int i=0; i < 3; i++ )
{
if ( a.m_Max[i] < b.m_Min[i] )
return false;
if ( a.m_Min[i] > b.m_Max[i] )
return false;
}
return true;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool FindEdge( CCoreDispInfo *pInfo, Vector const &vPoint1, Vector const &vPoint2, int &iEdge )
{
CCoreDispSurface *pSurface = pInfo->GetSurface();
for( iEdge=0; iEdge < 4; iEdge++ )
{
if( VectorsAreEqual( vPoint1, pSurface->GetPoint( iEdge ), 0.01f ) &&
VectorsAreEqual( vPoint2, pSurface->GetPoint( (iEdge+1) & 3), 0.01f ) )
{
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
NeighborSpan NeighborSpanFlip( int iEdge, NeighborSpan span )
{
if ( g_bEdgeNeighborFlip[iEdge] )
return g_SpanFlip[span];
else
return span;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void AddNeighbor( CCoreDispInfo *pMain,
int iEdge, // Which of pMain's sides this is on.
int iSub, // Which sub neighbor this takes up in pSide.
NeighborSpan span, // What span this fills in pMain.
CCoreDispInfo *pOther, int iNeighborEdge, NeighborSpan nbSpan )
{
// The edge iteration before coming in here goes 0-1, 1-2, 2-3, 3-4.
// This flips the sense of CORNER_TO_MIDPOINT/MIDPOINT_TO_CORNER on the right and
// bottom edges and is undone here.
span = NeighborSpanFlip( iEdge, span );
nbSpan = NeighborSpanFlip( iNeighborEdge, nbSpan );
// Get the subspan this fills on our displacement.
CDispSubNeighbor *pSub = &pMain->GetEdgeNeighbor(iEdge)->m_SubNeighbors[iSub];
// Which subspan does this use in the neighbor?
CDispSubNeighbor *pNeighborSub;
if ( nbSpan == MIDPOINT_TO_CORNER )
{
pNeighborSub = &pOther->GetEdgeNeighbor(iNeighborEdge)->m_SubNeighbors[1];
}
else
{
pNeighborSub = &pOther->GetEdgeNeighbor(iNeighborEdge)->m_SubNeighbors[0];
}
// Make sure this slot isn't used on either displacement.
if ( pSub->IsValid() || pNeighborSub->IsValid() )
{
ExecuteOnce( Warning( "Found a displacement edge abutting multiple other edges.\n" ) );
return;
}
// Now just copy the data into each displacement.
pSub->m_iNeighbor = pOther->GetListIndex();
pSub->m_NeighborOrientation = g_CoreDispNeighborOrientationMap[iEdge][iNeighborEdge];
pSub->m_Span = span;
pSub->m_NeighborSpan = nbSpan;
pNeighborSub->m_iNeighbor = pMain->GetListIndex();
pNeighborSub->m_NeighborOrientation = g_CoreDispNeighborOrientationMap[iNeighborEdge][iEdge];
pNeighborSub->m_Span = nbSpan;
pNeighborSub->m_NeighborSpan = span;
#if defined( _DEBUG )
// Walk an iterator over the new connection to make sure it works.
CDispSubEdgeIterator it;
it.Start( pMain, iEdge, iSub );
while ( it.Next() )
{
CVertIndex nbIndex;
TransformIntoNeighbor( pMain, iEdge, it.GetVertIndex(), nbIndex );
}
#endif
}
//-----------------------------------------------------------------------------
// This function is symmetric wrt pMain and pOther. It sets up valid neighboring data for
// the relationship between both of them.
//-----------------------------------------------------------------------------
void SetupEdgeNeighbors( CCoreDispInfo *pMain, CCoreDispInfo *pOther )
{
// Initialize..
for( int iEdge=0; iEdge < 4; iEdge++ )
{
// Setup the edge points and the midpoint.
Vector pt[2], mid;
pMain->GetSurface()->GetPoint( iEdge, pt[0] );
pMain->GetSurface()->GetPoint( (iEdge + 1) & 3, pt[1] );
mid = (pt[0] + pt[1]) * 0.5f;
// Find neighbors.
int iNBEdge;
if( FindEdge( pOther, pt[1], pt[0], iNBEdge ) )
{
AddNeighbor( pMain, iEdge, 0, CORNER_TO_CORNER, pOther, iNBEdge, CORNER_TO_CORNER );
}
else
{
// Look for one that takes up our whole side.
if( FindEdge( pOther, pt[1], pt[0]*2 - pt[1], iNBEdge ) )
{
AddNeighbor( pMain, iEdge, 0, CORNER_TO_CORNER, pOther, iNBEdge, CORNER_TO_MIDPOINT );
}
else if( FindEdge( pOther, pt[1]*2 - pt[0], pt[0], iNBEdge ) )
{
AddNeighbor( pMain, iEdge, 0, CORNER_TO_CORNER, pOther, iNBEdge, MIDPOINT_TO_CORNER );
}
else
{
// Ok, look for 1 or two that abut this side.
if( FindEdge( pOther, mid, pt[0], iNBEdge ) )
{
AddNeighbor( pMain, iEdge, g_bEdgeNeighborFlip[iEdge], CORNER_TO_MIDPOINT, pOther, iNBEdge, CORNER_TO_CORNER );
}
if( FindEdge( pOther, pt[1], mid, iNBEdge ) )
{
AddNeighbor( pMain, iEdge, !g_bEdgeNeighborFlip[iEdge], MIDPOINT_TO_CORNER, pOther, iNBEdge, CORNER_TO_CORNER );
}
}
}
}
}
//-----------------------------------------------------------------------------
// Returns true if the displacement has an edge neighbor with the given index.
//-----------------------------------------------------------------------------
bool HasEdgeNeighbor( const CCoreDispInfo *pMain, int iNeighbor )
{
for ( int i=0; i < 4; i++ )
{
const CDispCornerNeighbors *pCorner = pMain->GetCornerNeighbors( i );
for ( int iNB=0; iNB < pCorner->m_nNeighbors; iNB++ )
if ( pCorner->m_Neighbors[iNB] == iNeighbor )
return true;
const CDispNeighbor *pEdge = pMain->GetEdgeNeighbor( i );
if ( pEdge->m_SubNeighbors[0].GetNeighborIndex() == iNeighbor ||
pEdge->m_SubNeighbors[1].GetNeighborIndex() == iNeighbor )
{
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SetupCornerNeighbors( CCoreDispInfo *pMain, CCoreDispInfo *pOther, int *nOverflows )
{
if ( HasEdgeNeighbor( pMain, pOther->GetListIndex() ) )
return;
// Do these two share a vertex?
int nShared = 0;
int iMainSharedCorner = -1;
int iOtherSharedCorner = -1;
for ( int iMainCorner=0; iMainCorner < 4; iMainCorner++ )
{
Vector const &vMainCorner = pMain->GetCornerPoint( iMainCorner );
for ( int iOtherCorner=0; iOtherCorner < 4; iOtherCorner++ )
{
Vector const &vOtherCorner = pOther->GetCornerPoint( iOtherCorner );
if ( VectorsAreEqual( vMainCorner, vOtherCorner, 0.001f ) )
{
iMainSharedCorner = iMainCorner;
iOtherSharedCorner = iOtherCorner;