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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "particles_simple.h"
#include "iviewrender.h"
#include "proxyentity.h"
#include "materialsystem/imaterialvar.h"
#include "model_types.h"
#include "engine/ivmodelinfo.h"
#include "clienteffectprecachesystem.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define MAX_NUM_PANELS 16
extern IVDebugOverlay *debugoverlay;
enum WinSide_t
{
WIN_SIDE_BOTTOM,
WIN_SIDE_RIGHT,
WIN_SIDE_TOP,
WIN_SIDE_LEFT,
};
enum WinEdge_t
{
EDGE_NOT = -1, // No edge
EDGE_NONE, /* No edge on both sides /##\ */
EDGE_FULL, // Edge on both sides |##|
EDGE_LEFT, /* Edge is on left only |##\ */
EDGE_RIGHT, // Edge is on right only /##|
};
#define STYLE_HIGHLIGHT = -1;
#define NUM_EDGE_TYPES 4
#define NUM_EDGE_STYLES 3
//==================================================
// C_BreakableSurface
//==================================================
//-----------------------------------------------------------------------------
// All the information associated with a particular handle
//-----------------------------------------------------------------------------
struct Panel_t
{
char m_nWidth;
char m_nHeight;
char m_nSide;
char m_nEdgeType;
char m_nStyle;
};
struct EdgeTexture_t
{
int m_nRenderIndex;
int m_nStyle;
CMaterialReference m_pMaterialEdge;
CTextureReference m_pMaterialEdgeTexture;
};
// Bits for m_nPanelBits
#define BITS_PANEL_IS_SOLID (1<<0)
#define BITS_PANEL_IS_STALE (1<<1)
class C_BreakableSurface : public C_BaseEntity, public IBrushRenderer
{
public:
DECLARE_CLIENTCLASS();
DECLARE_CLASS( C_BreakableSurface, C_BaseEntity );
DECLARE_DATADESC();
int m_nNumWide;
int m_nNumHigh;
float m_flPanelWidth;
float m_flPanelHeight;
Vector m_vNormal;
Vector m_vCorner;
bool m_bIsBroken;
int m_nSurfaceType;
// This is the texture we're going to use to multiply by the cracked base texture
ITexture* m_pCurrentDetailTexture;
// Stores linked list of edges to render
CUtlLinkedList< Panel_t, unsigned short > m_RenderList;
C_BreakableSurface();
~C_BreakableSurface();
public:
void InitMaterial(WinEdge_t nEdgeType, int nEdgeStyle, char const* pMaterialName);
virtual void OnDataChanged( DataUpdateType_t updateType );
virtual void OnPreDataChanged( DataUpdateType_t updateType );
bool IsTransparent( void );
bool HavePanel(int nWidth, int nHeight);
bool RenderBrushModelSurface( IClientEntity* pBaseEntity, IBrushSurface* pBrushSurface );
int DrawModel( int flags );
void DrawSolidBlocks( IBrushSurface* pBrushSurface );
virtual void OnRestore();
virtual bool ShouldReceiveProjectedTextures( int flags );
private:
// One bit per pane
CNetworkArray( bool, m_RawPanelBitVec, MAX_NUM_PANELS * MAX_NUM_PANELS );
bool m_PrevRawPanelBitVec[ MAX_NUM_PANELS * MAX_NUM_PANELS ];
// 2 bits of flags and 2 bits of edge type
byte m_nPanelBits[MAX_NUM_PANELS][MAX_NUM_PANELS]; //UNDONE: allocate this dynamically?
CMaterialReference m_pMaterialBox;
EdgeTexture_t m_pSolid;
EdgeTexture_t m_pEdge[NUM_EDGE_TYPES][NUM_EDGE_STYLES];
inline bool InLegalRange(int nWidth, int nHeight);
inline bool IsPanelSolid(int nWidth, int nHeight);
inline bool IsPanelStale(int nWidth, int nHeight);
inline void SetPanelSolid(int nWidth, int nHeight, bool value);
inline void SetPanelStale(int nWidth, int nHeight, bool value);
void DrawOneEdge( IBrushSurface* pBrushSurface, IMesh* pMesh,
CMeshBuilder *pMeshBuilder, const Vector &vStartPos,
const Vector &vWStep, const Vector &vHstep, WinSide_t nEdge);
void DrawOneHighlight( IBrushSurface* pBrushSurface, IMesh* pMesh,
CMeshBuilder *pMeshBuilder, const Vector &vStartPos,
const Vector &vWStep, const Vector &vHstep, WinSide_t nEdge);
void DrawOneBlock(IBrushSurface* pBrushSurface, IMesh* pMesh,
CMeshBuilder *pMeshBuilder, const Vector &vPosition,
const Vector &vWidth, const Vector &vHeight);
void DrawRenderList( IBrushSurface* pBrushSurface);
void DrawRenderListHighlights( IBrushSurface* pBrushSurface );
int FindRenderPanel(int nWidth, int nHeight, WinSide_t nSide);
void AddToRenderList(int nWidth, int nHeight, WinSide_t nSide, WinEdge_t nEdgeType, int forceStyle);
int FindFirstRenderTexture(WinEdge_t nEdgeType, int nStyle);
inline void SetStyleType( int w, int h, int type )
{
Assert( type < NUM_EDGE_STYLES );
Assert( type >= 0 );
// Clear old value
m_nPanelBits[ w ][ h ] &= 0xF0; // ( ~0x03 << 2 ); Left shifting a negative value has undefined behavior. Use the constant 0xF0 instead.
// Insert new value
m_nPanelBits[ w ][ h ] |= ( type << 2 );
}
inline int GetStyleType( int w, int h )
{
int value = m_nPanelBits[ w ][ h ];
value = ( value >> 2 ) & 0x03;
Assert( value < NUM_EDGE_STYLES );
return value;
}
// Gets at the cracked version of the material
void FindCrackedMaterial();
CMaterialReference m_pCrackedMaterial;
CTextureReference m_pMaterialBoxTexture;
void UpdateEdgeType(int nWidth, int nHeight, int forceStyle = -1 );
};
BEGIN_DATADESC( C_BreakableSurface )
DEFINE_ARRAY( m_nPanelBits, FIELD_CHARACTER, MAX_NUM_PANELS * MAX_NUM_PANELS ),
// DEFINE_FIELD( m_nNumWide, FIELD_INTEGER ),
// DEFINE_FIELD( m_nNumHigh, FIELD_INTEGER ),
// DEFINE_FIELD( m_flPanelWidth, FIELD_FLOAT ),
// DEFINE_FIELD( m_flPanelHeight, FIELD_FLOAT ),
// DEFINE_FIELD( m_vNormal, FIELD_VECTOR ),
// DEFINE_FIELD( m_vCorner, FIELD_VECTOR ),
// DEFINE_FIELD( m_bIsBroken, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_nSurfaceType, FIELD_INTEGER ),
// DEFINE_FIELD( m_pCurrentDetailTexture, ITexture* ),
// DEFINE_FIELD( m_RenderList, CUtlLinkedList < Panel_t , unsigned short > ),
// DEFINE_FIELD( m_pMaterialBox, CMaterialReference ),
// DEFINE_FIELD( m_pSolid, EdgeTexture_t ),
// DEFINE_ARRAY( m_pEdge, EdgeTexture_t, NUM_EDGE_TYPES][NUM_EDGE_STYLES ),
// DEFINE_FIELD( m_pCrackedMaterial, CMaterialReference ),
// DEFINE_FIELD( m_pMaterialBoxTexture, CTextureReference ),
END_DATADESC()
bool C_BreakableSurface::InLegalRange(int nWidth, int nHeight)
{
return (nWidth < m_nNumWide && nHeight < m_nNumHigh &&
nWidth >=0 && nHeight >= 0 );
}
bool C_BreakableSurface::IsPanelSolid(int nWidth, int nHeight)
{
return ( BITS_PANEL_IS_SOLID & m_nPanelBits[nWidth][nHeight] )!=0 ;
}
bool C_BreakableSurface::IsPanelStale(int nWidth, int nHeight)
{
return ( BITS_PANEL_IS_STALE & m_nPanelBits[nWidth][nHeight] )!=0 ;
}
void C_BreakableSurface::SetPanelSolid(int nWidth, int nHeight, bool value)
{
if ( !InLegalRange( nWidth, nHeight ) )
return;
if ( value )
{
m_nPanelBits[nWidth][nHeight] |= BITS_PANEL_IS_SOLID;
}
else
{
m_nPanelBits[nWidth][nHeight] &= ~BITS_PANEL_IS_SOLID;
}
}
void C_BreakableSurface::SetPanelStale(int nWidth, int nHeight, bool value)
{
if ( !InLegalRange( nWidth, nHeight) )
return;
if ( value )
{
m_nPanelBits[nWidth][nHeight] |= BITS_PANEL_IS_STALE;
}
else
{
m_nPanelBits[nWidth][nHeight] &= ~BITS_PANEL_IS_STALE;
}
}
void C_BreakableSurface::OnRestore()
{
BaseClass::OnRestore();
// FIXME: This restores the general state, but not the random edge bits
// those would need to be serialized separately...
// traverse everthing and restore bits
// Initialize panels
for (int w=0;w<m_nNumWide;w++)
{
for (int h=0;h<m_nNumHigh;h++)
{
// Force recomputation
SetPanelSolid(w,h,IsPanelSolid(w,h));
SetPanelStale(w,h,true);
UpdateEdgeType( w, h, GetStyleType(w,h ) );
}
}
}
//Receive datatable
IMPLEMENT_CLIENTCLASS_DT( C_BreakableSurface, DT_BreakableSurface, CBreakableSurface )
RecvPropInt( RECVINFO( m_nNumWide ) ),
RecvPropInt( RECVINFO( m_nNumHigh ) ),
RecvPropFloat( RECVINFO( m_flPanelWidth) ),
RecvPropFloat( RECVINFO( m_flPanelHeight) ),
RecvPropVector( RECVINFO( m_vNormal ) ),
RecvPropVector( RECVINFO( m_vCorner ) ),
RecvPropInt( RECVINFO( m_bIsBroken )),
RecvPropInt( RECVINFO( m_nSurfaceType )),
RecvPropArray3( RECVINFO_ARRAY(m_RawPanelBitVec), RecvPropInt( RECVINFO( m_RawPanelBitVec[ 0 ] ))),
END_RECV_TABLE()
//-----------------------------------------------------------------------------
// Gets at the cracked version of the material
//-----------------------------------------------------------------------------
void C_BreakableSurface::FindCrackedMaterial()
{
m_pCrackedMaterial = 0;
// First time we've seen it, get the material on the brush model
int materialCount = modelinfo->GetModelMaterialCount( const_cast<model_t*>(GetModel()) );
if( materialCount != 1 )
{
Warning( "Encountered func_breakablesurf that has a material applied to more than one surface!\n" );
m_pCrackedMaterial.Init( "debug/debugempty", TEXTURE_GROUP_OTHER );
return;
}
// Get at the first material; even if there are more than one.
IMaterial* pMaterial;
modelinfo->GetModelMaterials( const_cast<model_t*>(GetModel()), 1, &pMaterial );
// The material should point to a cracked version of itself
bool foundVar;
IMaterialVar* pCrackName = pMaterial->FindVar( "$crackmaterial", &foundVar, false );
if (foundVar)
{
m_pCrackedMaterial.Init( pCrackName->GetStringValue(), TEXTURE_GROUP_CLIENT_EFFECTS );
}
else
{
m_pCrackedMaterial.Init( pMaterial );
}
}
//-----------------------------------------------------------------------------
// Gets at the base texture
//-----------------------------------------------------------------------------
static ITexture* GetBaseTexture( IMaterial* pMaterial )
{
bool foundVar;
IMaterialVar* pTextureVar = pMaterial->FindVar( "$basetexture", &foundVar, false );
if (!foundVar)
return 0;
return pTextureVar->GetTextureValue();
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::InitMaterial(WinEdge_t nEdgeType, int nEdgeStyle, char const* pMaterialName)
{
m_pEdge[nEdgeType][nEdgeStyle].m_nRenderIndex = m_RenderList.InvalidIndex();
m_pEdge[nEdgeType][nEdgeStyle].m_nStyle = nEdgeStyle;
m_pEdge[nEdgeType][nEdgeStyle].m_pMaterialEdge.Init(pMaterialName, TEXTURE_GROUP_CLIENT_EFFECTS);
m_pEdge[nEdgeType][nEdgeStyle].m_pMaterialEdgeTexture.Init( GetBaseTexture( m_pEdge[nEdgeType][nEdgeStyle].m_pMaterialEdge ) );
}
//-----------------------------------------------------------------------------
// constructor, destructor
//-----------------------------------------------------------------------------
C_BreakableSurface::C_BreakableSurface()
{
m_vNormal.Init();
m_vCorner.Init();
m_bIsBroken = false;
m_pCurrentDetailTexture = NULL;
Q_memset( m_PrevRawPanelBitVec, 0xff, sizeof( m_PrevRawPanelBitVec ) );
}
C_BreakableSurface::~C_BreakableSurface()
{
}
void C_BreakableSurface::OnPreDataChanged( DataUpdateType_t updateType )
{
BaseClass::OnPreDataChanged( updateType );
if ( updateType == DATA_UPDATE_CREATED )
{
// Initialize panels
m_nNumWide = MAX_NUM_PANELS;
m_nNumHigh = MAX_NUM_PANELS;
for (int w=0;w<MAX_NUM_PANELS;w++)
{
for (int h=0;h<MAX_NUM_PANELS;h++)
{
SetPanelSolid(w,h,true);
SetPanelStale(w,h,false);
m_RawPanelBitVec.Set( w + h * MAX_NUM_PANELS, true );
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : bnewentity -
//-----------------------------------------------------------------------------
void C_BreakableSurface::OnDataChanged( DataUpdateType_t updateType )
{
C_BaseEntity::OnDataChanged( updateType );
if ( updateType == DATA_UPDATE_CREATED )
{
// Get at the cracked material
FindCrackedMaterial();
// Use same solid box for all breakable surfaces
m_pMaterialBox.Init( "models/brokenglass/glassbroken_solid", TEXTURE_GROUP_MODEL );
m_pMaterialBoxTexture.Init( GetBaseTexture( m_pMaterialBox ) );
// NOTE: If you add or change this list of materials, change
// the precache list on func_breakablesurf.cpp on the server.
// Load the edge types and styles for the specific surface type
if (m_nSurfaceType == SHATTERSURFACE_TILE)
{
InitMaterial(EDGE_NONE, 0,"models/brokentile/tilebroken_03a");
InitMaterial(EDGE_FULL, 0,"models/brokentile/tilebroken_03b");
InitMaterial(EDGE_LEFT, 0,"models/brokentile/tilebroken_03c");
InitMaterial(EDGE_RIGHT,0,"models/brokentile/tilebroken_03d");
InitMaterial(EDGE_NONE, 1,"models/brokentile/tilebroken_02a");
InitMaterial(EDGE_FULL, 1,"models/brokentile/tilebroken_02b");
InitMaterial(EDGE_LEFT, 1,"models/brokentile/tilebroken_02c");
InitMaterial(EDGE_RIGHT,1,"models/brokentile/tilebroken_02d");
InitMaterial(EDGE_NONE, 2,"models/brokentile/tilebroken_01a");
InitMaterial(EDGE_FULL, 2,"models/brokentile/tilebroken_01b");
InitMaterial(EDGE_LEFT, 2,"models/brokentile/tilebroken_01c");
InitMaterial(EDGE_RIGHT,2,"models/brokentile/tilebroken_01d");
}
else
{
InitMaterial(EDGE_NONE, 0,"models/brokenglass/glassbroken_03a");
InitMaterial(EDGE_FULL, 0,"models/brokenglass/glassbroken_03b");
InitMaterial(EDGE_LEFT, 0,"models/brokenglass/glassbroken_03c");
InitMaterial(EDGE_RIGHT,0,"models/brokenglass/glassbroken_03d");
InitMaterial(EDGE_NONE, 1,"models/brokenglass/glassbroken_02a");
InitMaterial(EDGE_FULL, 1,"models/brokenglass/glassbroken_02b");
InitMaterial(EDGE_LEFT, 1,"models/brokenglass/glassbroken_02c");
InitMaterial(EDGE_RIGHT,1,"models/brokenglass/glassbroken_02d");
InitMaterial(EDGE_NONE, 2,"models/brokenglass/glassbroken_01a");
InitMaterial(EDGE_FULL, 2,"models/brokenglass/glassbroken_01b");
InitMaterial(EDGE_LEFT, 2,"models/brokenglass/glassbroken_01c");
InitMaterial(EDGE_RIGHT,2,"models/brokenglass/glassbroken_01d");
}
}
bool changed = false;
for ( int j = 0; j < m_nNumHigh; j++ )
{
for ( int i = 0; i < m_nNumWide; i++ )
{
int offset = i + j * m_nNumWide;
bool newVal = m_RawPanelBitVec[ offset ];
bool oldVal = m_PrevRawPanelBitVec[ offset ];
if ( newVal != oldVal )
{
changed = true;
}
SetPanelSolid(i,j,newVal);
if ( !newVal && changed )
{
// Mark these panels and being stale (need edge type updated)
// We update them in one fell swoop rather than as each panel
// is updated, so we don't have to do duplicate operations
SetPanelStale(i, j ,true);
SetPanelStale(i, j+1,true);
SetPanelStale(i, j-1,true);
SetPanelStale(i-1, j ,true);
SetPanelStale(i+1, j ,true);
SetPanelStale(i+1, j+1,true);
SetPanelStale(i-1, j+1,true);
SetPanelStale(i+1, j-1,true);
SetPanelStale(i-1, j-1,true);
}
}
}
if ( changed )
{
for (int width=0;width<m_nNumWide;width++)
{
for (int height=0;height<m_nNumHigh;height++)
{
if ( IsPanelStale(width,height) )
{
UpdateEdgeType( width, height );
}
}
}
}
Q_memcpy( m_PrevRawPanelBitVec, m_RawPanelBitVec.Base(), sizeof( m_PrevRawPanelBitVec ) );
}
bool C_BreakableSurface::IsTransparent( void )
{
// Not an identity brush if it's broken
if (m_bIsBroken)
return true;
return C_BaseEntity::IsTransparent();
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
int C_BreakableSurface::DrawModel( int flags )
{
if ( !m_bReadyToDraw )
return 0;
// If I'm not broken draw normally
if (m_bIsBroken)
render->InstallBrushSurfaceRenderer( this );
// If it's broken, always draw it translucent
BaseClass::DrawModel( m_bIsBroken ? flags | STUDIO_TRANSPARENCY : flags );
// Remove our nonstandard brush surface renderer...
render->InstallBrushSurfaceRenderer( 0 );
return 0;
}
bool C_BreakableSurface::RenderBrushModelSurface( IClientEntity* pBaseEntity, IBrushSurface* pBrushSurface )
{
// If tile draw highlight for grout
if (m_nSurfaceType == SHATTERSURFACE_TILE)
{
DrawRenderListHighlights(pBrushSurface);
}
DrawSolidBlocks(pBrushSurface);
DrawRenderList(pBrushSurface);
// Don't draw decals
return false;
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::DrawRenderList(IBrushSurface* pBrushSurface)
{
// Get width and height steps
QAngle vAngles;
VectorAngles(-1*m_vNormal,vAngles);
Vector vWidthStep,vHeightStep;
AngleVectors(vAngles,NULL,&vWidthStep,&vHeightStep);
vWidthStep *= m_flPanelWidth;
vHeightStep *= m_flPanelHeight;
CMeshBuilder pMeshBuilder;
IMesh* pMesh = NULL;
int nCurStyle = -1;
int nCurEdgeType = -1;
CMatRenderContextPtr pRenderContext( materials );
for( unsigned short i = m_RenderList.Head(); i != m_RenderList.InvalidIndex(); i = m_RenderList.Next(i) )
{
if (nCurStyle != m_RenderList[i].m_nStyle ||
nCurEdgeType != m_RenderList[i].m_nEdgeType )
{
nCurStyle = m_RenderList[i].m_nStyle;
nCurEdgeType = m_RenderList[i].m_nEdgeType;
m_pCurrentDetailTexture = m_pEdge[nCurEdgeType][nCurStyle].m_pMaterialEdgeTexture;
pRenderContext->Flush(false);
pRenderContext->Bind(m_pCrackedMaterial, (IClientRenderable*)this);
pMesh = pRenderContext->GetDynamicMesh( );
}
Vector vRenderPos = m_vCorner +
(m_RenderList[i].m_nWidth*vWidthStep) +
(m_RenderList[i].m_nHeight*vHeightStep);
DrawOneEdge(pBrushSurface, pMesh,&pMeshBuilder,vRenderPos,vWidthStep,vHeightStep,(WinSide_t)m_RenderList[i].m_nSide);
}
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::DrawRenderListHighlights(IBrushSurface* pBrushSurface)
{
// Get width and height steps
QAngle vAngles;
VectorAngles(-1*m_vNormal,vAngles);
Vector vWidthStep,vHeightStep;
AngleVectors(vAngles,NULL,&vWidthStep,&vHeightStep);
vWidthStep *= m_flPanelWidth;
vHeightStep *= m_flPanelHeight;
CMeshBuilder pMeshBuilder;
IMesh* pMesh = NULL;
int nCurStyle = -1;
int nCurEdgeType = -1;
CMatRenderContextPtr pRenderContext( materials );
for( unsigned short i = m_RenderList.Head(); i != m_RenderList.InvalidIndex(); i = m_RenderList.Next(i) )
{
if (nCurStyle != m_RenderList[i].m_nStyle ||
nCurEdgeType != m_RenderList[i].m_nEdgeType )
{
nCurStyle = m_RenderList[i].m_nStyle;
nCurEdgeType = m_RenderList[i].m_nEdgeType;
IMaterial *pMat = m_pEdge[nCurEdgeType][nCurStyle].m_pMaterialEdge;
pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, pMat );
}
Vector vRenderPos = m_vCorner +
(m_RenderList[i].m_nWidth*vWidthStep) +
(m_RenderList[i].m_nHeight*vHeightStep) +
(0.30*m_vNormal);
DrawOneHighlight(pBrushSurface, pMesh,&pMeshBuilder,vRenderPos,vWidthStep,vHeightStep,(WinSide_t)m_RenderList[i].m_nSide);
}
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
bool C_BreakableSurface::HavePanel(int nWidth, int nHeight)
{
// If I'm off the edge, always give support
if (!InLegalRange(nWidth,nHeight))
{
return true;
}
return (IsPanelSolid(nWidth,nHeight));
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::UpdateEdgeType(int nWidth, int nHeight, int forceStyle /*=-1*/ )
{
Assert( forceStyle < NUM_EDGE_STYLES );
// -----------------------------------
// Check edge conditions
// -----------------------------------
if (!InLegalRange(nWidth,nHeight))
{
return;
}
// ----------------------------------
// If solid has no edges
// ----------------------------------
if (IsPanelSolid(nWidth,nHeight))
{
return;
}
// Panel is no longer stale
SetPanelStale(nWidth, nHeight,false);
// ----------------------------------
// Set edge type base on neighbors
// ----------------------------------
bool bUp = HavePanel(nWidth, nHeight+1);
bool bDown = HavePanel(nWidth, nHeight-1);
bool bLeft = HavePanel(nWidth-1, nHeight );
bool bRight = HavePanel(nWidth+1, nHeight );
bool bUpLeft = HavePanel(nWidth-1, nHeight+1);
bool bUpRight = HavePanel(nWidth+1, nHeight+1);
bool bDownLeft = HavePanel(nWidth-1, nHeight-1);
bool bDownRight = HavePanel(nWidth+1, nHeight-1);
//-------------
// Top
//-------------
if (bUp)
{
bool bLeftEdge = !bLeft && bUpLeft;
bool bRightEdge = !bRight && bUpRight;
if (bLeftEdge && bRightEdge)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_TOP, EDGE_FULL, forceStyle );
}
else if (bLeftEdge)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_TOP, EDGE_LEFT, forceStyle );
}
else if (bRightEdge)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_TOP, EDGE_RIGHT, forceStyle );
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_TOP, EDGE_NONE, forceStyle );
}
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_TOP, EDGE_NOT, forceStyle );
}
//-------------
// Bottom
//-------------
if (bDown)
{
bool bLeftEdge = !bLeft && bDownLeft;
bool bRightEdge = !bRight && bDownRight;
if (bLeftEdge && bRightEdge)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_BOTTOM, EDGE_FULL, forceStyle );
}
else if (bLeftEdge)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_BOTTOM, EDGE_RIGHT, forceStyle );
}
else if (bRightEdge)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_BOTTOM, EDGE_LEFT, forceStyle );
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_BOTTOM, EDGE_NONE, forceStyle );
}
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_BOTTOM, EDGE_NOT, forceStyle );
}
//-------------
// Left
//-------------
if (bLeft)
{
bool bTop = !bUp && bUpLeft;
bool bBottom = !bDown && bDownLeft;
if (bTop && bBottom)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_LEFT, EDGE_FULL, forceStyle );
}
else if (bTop)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_LEFT, EDGE_RIGHT, forceStyle );
}
else if (bBottom)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_LEFT, EDGE_LEFT, forceStyle );
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_LEFT, EDGE_NONE, forceStyle );
}
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_LEFT, EDGE_NOT, forceStyle );
}
//-------------
// Right
//-------------
if (bRight)
{
bool bTop = !bUp && bUpRight;
bool bBottom = !bDown && bDownRight;
if (bTop && bBottom)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_RIGHT, EDGE_FULL, forceStyle );
}
else if (bTop)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_RIGHT, EDGE_LEFT, forceStyle );
}
else if (bBottom)
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_RIGHT, EDGE_RIGHT, forceStyle );
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_RIGHT, EDGE_NONE, forceStyle );
}
}
else
{
AddToRenderList(nWidth, nHeight, WIN_SIDE_RIGHT, EDGE_NOT, forceStyle );
}
}
//--------------------------------------------------------------------------------
// Purpose : Return index to panel in render list that meets these qualifications
// Input :
// Output :
//--------------------------------------------------------------------------------
int C_BreakableSurface::FindRenderPanel(int nWidth, int nHeight, WinSide_t nWinSide)
{
for( unsigned short i = m_RenderList.Head(); i != m_RenderList.InvalidIndex(); i = m_RenderList.Next(i) )
{
if (m_RenderList[i].m_nSide == nWinSide &&
m_RenderList[i].m_nWidth == nWidth &&
m_RenderList[i].m_nHeight == nHeight)
{
return i;
}
}
return m_RenderList.InvalidIndex();
}
//----------------------------------------------------------------------------------
// Purpose : Returns first element in render list with the same edge type and style
// Input :
// Output :
//----------------------------------------------------------------------------------
int C_BreakableSurface::FindFirstRenderTexture(WinEdge_t nEdgeType, int nStyle)
{
for( unsigned short i = m_RenderList.Head(); i != m_RenderList.InvalidIndex(); i = m_RenderList.Next(i) )
{
if (m_RenderList[i].m_nStyle == nStyle &&
m_RenderList[i].m_nEdgeType == nEdgeType )
{
return i;
}
}
return m_RenderList.InvalidIndex();
}
//------------------------------------------------------------------------------
// Purpose : Add a edge to be rendered to the render list
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::AddToRenderList(int nWidth, int nHeight, WinSide_t nSide, WinEdge_t nEdgeType, int forceStyle )
{
// -----------------------------------------------------
// Try to find old panel
int nOldPanelIndex = FindRenderPanel(nWidth,nHeight,nSide);
// -----------------------------------------------------
// If I have an old panel, get it's style and remove it
// otherwise randomly chose a style
int nStyle;
if (m_RenderList.IsValidIndex(nOldPanelIndex) )
{
nStyle = m_RenderList[nOldPanelIndex].m_nStyle;
m_RenderList.Remove(nOldPanelIndex);
}
else
{
nStyle = random->RandomInt(0,NUM_EDGE_STYLES-1);
}
if ( forceStyle != -1 )
{
nStyle = forceStyle;
}
// -----------------------------------------------------
// If my new panel has an edge, add it to render list
if (nEdgeType != EDGE_NOT)
{
// Renderlist is sorted by texture type. Find first element
// that shares the same texture as the new panel
unsigned short nTexIndex = FindFirstRenderTexture(nEdgeType, nStyle);
// If texture was already in list, add after last use
unsigned short nNewIndex;
if (m_RenderList.IsValidIndex(nTexIndex))
{
nNewIndex = m_RenderList.InsertAfter(nTexIndex);
}
// Otherwise add to send of render list
else
{
nNewIndex = m_RenderList.AddToTail();
}
// Now fill out my data
m_RenderList[nNewIndex].m_nHeight = nHeight;
m_RenderList[nNewIndex].m_nWidth = nWidth;
m_RenderList[nNewIndex].m_nEdgeType = nEdgeType;
m_RenderList[nNewIndex].m_nSide = nSide;
m_RenderList[nNewIndex].m_nStyle = nStyle;
Assert( nStyle < NUM_EDGE_STYLES );
SetStyleType( nWidth, nHeight, nStyle );
}
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::DrawSolidBlocks(IBrushSurface* pBrushSurface)
{
CMatRenderContextPtr pRenderContext( materials );
m_pCurrentDetailTexture = m_pMaterialBoxTexture;
// Gotta flush (in a non-stalling way) because we effectively
// have a new material due to the new base texture
pRenderContext->Flush(false);
pRenderContext->Bind(m_pCrackedMaterial, (IClientRenderable*)this);
IMesh* pMesh = pRenderContext->GetDynamicMesh( );
CMeshBuilder pMeshBuilder;
// ---------------
// Create panels
// ---------------
QAngle vAngles;
VectorAngles(-1*m_vNormal,vAngles);
Vector vWidthStep,vHeightStep;
AngleVectors(vAngles,NULL,&vWidthStep,&vHeightStep);
vWidthStep *= m_flPanelWidth;
vHeightStep *= m_flPanelHeight;
Vector vCurPos = m_vCorner;
for (int width=0;width<m_nNumWide;width++)
{
int height;
int nHCount = 0;
for (height=0;height<m_nNumHigh;height++)
{
// Keep count of how many panes there are in a row
if (IsPanelSolid(width,height))
{
nHCount++;
}
// Drow the strip and start counting again
else if (nHCount > 0)
{
vCurPos = m_vCorner + vWidthStep*width + vHeightStep*(height-nHCount);
DrawOneBlock(pBrushSurface, pMesh, &pMeshBuilder, vCurPos,vWidthStep,vHeightStep*nHCount);
nHCount = 0;
}
}
if (nHCount)
{
vCurPos = m_vCorner + vWidthStep*width + vHeightStep*(height-nHCount);
DrawOneBlock(pBrushSurface, pMesh, &pMeshBuilder, vCurPos,vWidthStep,vHeightStep*nHCount);
}
}
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::DrawOneBlock(IBrushSurface* pBrushSurface, IMesh* pMesh,
CMeshBuilder *pMeshBuilder, const Vector &vCurPos, const Vector &vWidthStep,
const Vector &vHeightStep)
{
pMeshBuilder->Begin( pMesh, MATERIAL_QUADS, 1 );
Vector2D texCoord, lightCoord;
pBrushSurface->ComputeTextureCoordinate( vCurPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vCurPos, lightCoord );
pMeshBuilder->Position3f( vCurPos.x, vCurPos.y, vCurPos.z );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2f( 0, 0, 1 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->AdvanceVertex();
Vector vNextPos = vCurPos + vWidthStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x, vNextPos.y, vNextPos.z );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2f( 0, 0, 0 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->AdvanceVertex();
vNextPos = vNextPos + vHeightStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x , vNextPos.y, vNextPos.z );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2f( 0, 1, 0 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->AdvanceVertex();
vNextPos = vNextPos - vWidthStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x, vNextPos.y , vNextPos.z);
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2f( 0, 1, 1 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->AdvanceVertex();
pMeshBuilder->End();
pMesh->Draw();
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::DrawOneEdge( IBrushSurface* pBrushSurface, IMesh* pMesh,
CMeshBuilder *pMeshBuilder, const Vector &vStartPos, const Vector &vWStep,
const Vector &vHStep, WinSide_t nEdge )
{
pMeshBuilder->Begin( pMesh, MATERIAL_QUADS, 1 );
Vector2D texCoord, lightCoord;
pBrushSurface->ComputeTextureCoordinate( vStartPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vStartPos, lightCoord );
pMeshBuilder->Position3f( vStartPos.x, vStartPos.y, vStartPos.z);
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
}
pMeshBuilder->AdvanceVertex();
Vector vNextPos = vStartPos + vWStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x , vNextPos.y , vNextPos.z);
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
}
pMeshBuilder->AdvanceVertex();
vNextPos = vNextPos + vHStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x, vNextPos.y, vNextPos.z );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
}
pMeshBuilder->AdvanceVertex();
vNextPos = vNextPos - vWStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x, vNextPos.y, vNextPos.z );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4ub( 255, 255, 255, 255 );
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
pMeshBuilder->TexCoord2fv( 2, texCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
}
pMeshBuilder->AdvanceVertex();
pMeshBuilder->End();
pMesh->Draw();
}
//------------------------------------------------------------------------------
// Purpose :
// Input :
// Output :
//------------------------------------------------------------------------------
void C_BreakableSurface::DrawOneHighlight( IBrushSurface* pBrushSurface, IMesh* pMesh,
CMeshBuilder *pMeshBuilder, const Vector &vStartPos, const Vector &vWStep,
const Vector &vHStep, WinSide_t nEdge )
{
Vector vColor = Vector(0.41,0.35,0.24);
pMeshBuilder->Begin( pMesh, MATERIAL_QUADS, 1 );
Vector2D texCoord, lightCoord;
pBrushSurface->ComputeTextureCoordinate( vStartPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vStartPos, lightCoord );
pMeshBuilder->Position3f( vStartPos.x, vStartPos.y, vStartPos.z);
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4f( vColor[0], vColor[1], vColor[2], 1.0);
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
}
pMeshBuilder->AdvanceVertex();
Vector vNextPos = vStartPos + vWStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x , vNextPos.y , vNextPos.z);
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4f( vColor[0], vColor[1], vColor[2], 1.0);
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
}
pMeshBuilder->AdvanceVertex();
vNextPos = vNextPos + vHStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x, vNextPos.y, vNextPos.z );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4f( vColor[0], vColor[1], vColor[2], 1.0);
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
}
pMeshBuilder->AdvanceVertex();
vNextPos = vNextPos - vWStep;
pBrushSurface->ComputeTextureCoordinate( vNextPos, texCoord );
pBrushSurface->ComputeLightmapCoordinate( vNextPos, lightCoord );
pMeshBuilder->Position3f( vNextPos.x, vNextPos.y, vNextPos.z );
pMeshBuilder->Normal3fv( m_vNormal.Base() );
pMeshBuilder->Color4f( vColor[0], vColor[1], vColor[2], 1.0);
pMeshBuilder->TexCoord2fv( 1, lightCoord.Base() );
switch (nEdge)
{
case WIN_SIDE_RIGHT:
pMeshBuilder->TexCoord2f( 0, 0.0f, 0.0f );
break;
case WIN_SIDE_BOTTOM:
pMeshBuilder->TexCoord2f( 0, 1.0f, 0.0f );
break;
case WIN_SIDE_LEFT:
pMeshBuilder->TexCoord2f( 0, 1.0f, 1.0f );
break;
case WIN_SIDE_TOP:
pMeshBuilder->TexCoord2f( 0, 0.0f, 1.0f );
break;
}
pMeshBuilder->AdvanceVertex();
pMeshBuilder->End();
pMesh->Draw();
}
bool C_BreakableSurface::ShouldReceiveProjectedTextures( int flags )
{
return false;
}
//------------------------------------------------------------------------------
// A material proxy that resets the texture to use the original surface texture
//------------------------------------------------------------------------------
class CBreakableSurfaceProxy : public CEntityMaterialProxy
{
public:
CBreakableSurfaceProxy();
virtual ~CBreakableSurfaceProxy();
virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
virtual void OnBind( C_BaseEntity *pC_BaseEntity );
virtual IMaterial *GetMaterial();
private:
// get at the material whose texture we're going to steal
void AcquireSourceMaterial( C_BaseEntity* pEnt );
IMaterialVar* m_BaseTextureVar;
};
CBreakableSurfaceProxy::CBreakableSurfaceProxy()
{
m_BaseTextureVar = NULL;
}
CBreakableSurfaceProxy::~CBreakableSurfaceProxy()
{
}
bool CBreakableSurfaceProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
bool foundVar;
m_BaseTextureVar = pMaterial->FindVar( "$basetexture", &foundVar, false );
return foundVar;
}
void CBreakableSurfaceProxy::OnBind( C_BaseEntity *pC_BaseEntity )
{
C_BreakableSurface *pEnt = dynamic_cast< C_BreakableSurface * >(pC_BaseEntity);
if( !pEnt )
{
return;
}
// Use the current base texture specified by the suface
m_BaseTextureVar->SetTextureValue( pEnt->m_pCurrentDetailTexture );
}
IMaterial *CBreakableSurfaceProxy::GetMaterial()
{
if ( !m_BaseTextureVar )
return NULL;
return m_BaseTextureVar->GetOwningMaterial();
}
EXPOSE_INTERFACE( CBreakableSurfaceProxy, IMaterialProxy, "BreakableSurface" IMATERIAL_PROXY_INTERFACE_VERSION );
|