-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmetestmesh.cpp
1757 lines (1507 loc) · 52.5 KB
/
dmetestmesh.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 © 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "movieobjects/dmetestmesh.h"
#include "movieobjects/dmetransform.h"
#include "movieobjects_interfaces.h"
#include "tier0/dbg.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "mathlib/vector.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/imesh.h"
#include "datacache/imdlcache.h"
#include "istudiorender.h"
#include "studio.h"
#include "bone_setup.h"
#include "materialsystem/ivertextexture.h"
#include "morphdata.h"
#include "tier3/tier3.h"
#include <strstream>
#include <fstream>
#include <algorithm>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeTestMesh, CDmeTestMesh );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeTestMesh::OnConstruction()
{
m_MDLHandle = MDLHANDLE_INVALID;
m_pMaterial = NULL;
m_pMesh = NULL;
m_pMorph = NULL;
m_pControlCage = NULL;
SetValue( "transform", g_pDataModel->IsUnserializing() ? NULL : CreateElement< CDmeTransform >( "transform", GetFileId() ) );
SetValue( "mdlfilename", "models/alyx.mdl" );
SetValue( "morphfilename", "models/alyx.morph" );
SetValue( "skin", 0 );
SetValue( "body", 0 );
SetValue( "sequence", 0 );
SetValue( "lod", 0 );
SetValue( "playbackrate", 1.0f );
SetValue( "time", 0.0f );
SetValue( "subdivlevel", 1 );
}
void CDmeTestMesh::OnDestruction()
{
UnloadMorphData();
UnreferenceMDL();
DestroyControlCage();
DestroyMesh();
}
//-----------------------------------------------------------------------------
// Addref/Release the MDL handle
//-----------------------------------------------------------------------------
void CDmeTestMesh::ReferenceMDL( const char *pMDLName )
{
if ( !g_pMDLCache )
return;
if ( pMDLName && pMDLName[0] )
{
Assert( m_MDLHandle == MDLHANDLE_INVALID );
m_MDLHandle = g_pMDLCache->FindMDL( pMDLName );
}
}
void CDmeTestMesh::UnreferenceMDL()
{
if ( !g_pMDLCache )
return;
if ( m_MDLHandle != MDLHANDLE_INVALID )
{
g_pMDLCache->Release( m_MDLHandle );
m_MDLHandle = MDLHANDLE_INVALID;
}
}
//-----------------------------------------------------------------------------
// Creates the mesh to draw
//-----------------------------------------------------------------------------
void CDmeTestMesh::CreateMesh()
{
DestroyMesh();
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
m_pMaterial = g_pMaterialSystem->FindMaterial( "shadertest/vertextexturetest", NULL, false );
m_pMesh = pRenderContext->CreateStaticMesh( m_pMaterial, 0, "dmemesh" );
CMeshBuilder meshBuilder;
meshBuilder.Begin( m_pMesh, MATERIAL_TRIANGLES, 8, 36 );
// Draw a simple cube
static Vector s_pPositions[8] =
{
Vector( -10, -10, -10 ),
Vector( 10, -10, -10 ),
Vector( -10, 10, -10 ),
Vector( 10, 10, -10 ),
Vector( -10, -10, 10 ),
Vector( 10, -10, 10 ),
Vector( -10, 10, 10 ),
Vector( 10, 10, 10 ),
};
static Vector2D s_pTexCoords[8] =
{
Vector2D( 0, 0 ),
Vector2D( 0.5, 0 ),
Vector2D( 0, 0.5 ),
Vector2D( 0.5, 0.5 ),
Vector2D( 0.5, 0.5 ),
Vector2D( 1, 0.5 ),
Vector2D( 0.5, 1 ),
Vector2D( 1, 1 ),
};
static unsigned char s_pColor[8][3] =
{
{ 255, 255, 255 },
{ 0, 255, 255 },
{ 255, 0, 255 },
{ 255, 255, 0 },
{ 255, 0, 0 },
{ 0, 255, 0 },
{ 0, 0, 255 },
{ 0, 0, 0 },
};
static int s_pIndices[12][3] =
{
{ 0, 1, 5 }, { 0, 5, 4 },
{ 4, 5, 7 }, { 4, 7, 6 },
{ 0, 4, 6 }, { 0, 6, 2 },
{ 0, 2, 3 }, { 0, 3, 1 },
{ 1, 3, 7 }, { 1, 7, 5 },
{ 2, 6, 7 }, { 2, 7, 3 },
};
for ( int i = 0; i < 8; ++i )
{
meshBuilder.Position3fv( s_pPositions[ i ].Base() );
meshBuilder.TexCoord2fv( 0, s_pTexCoords[ i ].Base() );
// meshBuilder.TexCoord2f( 1, i, 0.0f );
meshBuilder.Color3ubv( s_pColor[ i ] );
meshBuilder.AdvanceVertex();
}
for ( int i = 0; i < 12; ++i )
{
meshBuilder.FastIndex( s_pIndices[i][0] );
meshBuilder.FastIndex( s_pIndices[i][1] );
meshBuilder.FastIndex( s_pIndices[i][2] );
}
meshBuilder.End();
}
void CDmeTestMesh::DestroyMesh()
{
if ( m_pMesh )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->DestroyStaticMesh( m_pMesh );
m_pMesh = NULL;
}
}
//-----------------------------------------------------------------------------
// Morph data
//-----------------------------------------------------------------------------
void CDmeTestMesh::LoadMorphData( const char *pMorphFile, int nVertexCount )
{
UnloadMorphData();
IMorphData *pMorphData = CreateMorphData();
m_pMorph = pMorphData->Compile( pMorphFile, m_pMaterial, nVertexCount );
DestroyMorphData( pMorphData );
}
void CDmeTestMesh::UnloadMorphData()
{
if ( m_pMorph )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->DestroyMorph( m_pMorph );
m_pMorph = NULL;
}
}
//-----------------------------------------------------------------------------
// This function gets called whenever an attribute changes
//-----------------------------------------------------------------------------
void CDmeTestMesh::Resolve()
{
CDmAttribute *pMDLFilename = GetAttribute( "mdlfilename" );
if ( pMDLFilename && pMDLFilename->IsFlagSet( FATTRIB_DIRTY ) )
{
UnreferenceMDL();
ReferenceMDL( GetValueString( "mdlfilename" ) );
return;
}
CDmAttribute *pMorphFilename = GetAttribute( "morphfilename" );
if ( pMorphFilename && pMorphFilename->IsFlagSet( FATTRIB_DIRTY ) )
{
CreateMesh();
UnloadMorphData();
LoadMorphData( GetValueString( "morphfilename" ), 8 );
return;
}
}
//-----------------------------------------------------------------------------
// Loads the model matrix based on the transform
//-----------------------------------------------------------------------------
void CDmeTestMesh::LoadModelMatrix( CDmeTransform *pTransform )
{
// FIXME: Should this go into the DmeTransform node?
matrix3x4_t transform;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pTransform->GetTransform( transform );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadMatrix( transform );
}
//-----------------------------------------------------------------------------
// A subvision mesh
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// NOTES:
// The subdivision mesh is fast because it assumes a very particular ordering
// and definition of the data so that it can determine all subdivided data by
// inspection without any searching. Here's the layout:
//
// First, a face stores a list of edge indices which reference the edges
// that make up the face. A face is assumed to traverse its vertices in CCW order.
// We define the "relative edge index" for an edge within a face as the
// order in which that edge is visited while traversing the edges in CCW order,
// so 0 is the first visited edge, and 1 is the next, etc.
//
// First, edges are defined in a specific way. The edge is assumed to be
// *directed*, starting at vertex 0 and leading toward vertex 1. Now imagine the
// two faces that shared this edge and that they both traverse their edges in
// a right-handed, or CCW direction. Face 0 associated with the edge, to maintain
// a CCW ordering, must traverse the edge in a *reverse* direction, heading from
// vertex 1 to vertex 0. Face 1 associated with the edge traverses the edge
// in a forward direction, from vertex 0 to vertex 1.
//
// When subdivision happens, it occurs in a very specific way also. First, when
// creating the new vertices, for uniform subdivision, we create a new vertex
// per face, a new vertex per edge, and adjust all existing vertices. When creating
// these vertices in the subdivided mesh, we first add the face midpoint vertices,
// then the edge midpoint vertices, then the vertices from the un-subdivided mesh, to
// the m_Vertices array of the subdivided mesh.
//
// Edge subdivision always works in a uniform way: For each edge in the unsubdivided
// mesh, 4 edges are created from the edge midpoint, connecting to the two
// face midpoint vertices and the two edge endpoints. In order to maintain the
// specific ordering of the edges described above, we define the edges in the
// following manner:
// * Subdivided edge 0 : Starts at face 0 midpoint, ends at edge midpoint
// * Subdivided edge 1 : Starts at edge midpoint, ends at face 1 midpoint
// * Subdivided edge 2 : Starts at original edge's vertex 0, ends at edge midpoint
// * Subdivided edge 3 : Starts at edge midpoint, ends at original edge's vertex 1
//
// Face subdivision *also* always works in a uniform way: For each face in the
// unsubdivided mesh, N new faces are created, one for each edge in the unsubdivided
// face. The faces are ordered in a very specific way:
// * Subdivided face 0 : Starts at the face midpoint, goes to unsubdivided edge 0's midpoint,
// winds around the edge until it hits unsubdivided edge 1's midpoint,
// then heads back to the face midpoint.
// * Subdivided face 1 : Starts at the face midpoint, goes to unsubdivided edge 1's midpoint,
// winds around the edge until it hits unsubdivided edge 2's midpoint,
// then heads back to the face midpoint.
// etc.
//-----------------------------------------------------------------------------
struct SubdivVertex_t
{
Vector m_vecPosition;
Vector m_vecNormal;
Vector m_vecTexCoord;
int m_nValence;
};
// NOTE: The edge is always defined such that the edge going from vertex[0] to vertex[1]
// is counter-clockwise when seen from face[1] and and clockwise when seen from face[0].
struct Edge_t
{
int m_pFace[2];
int m_pRelativeEdgeIndex[2]; // Goes from 0-N always, specifies the Nth edge of the polygon it's part of for each of the two faces
int m_pVertex[2];
};
struct Face_t
{
int m_nFirstEdgeIndex;
int m_nEdgeCount;
// Stores the index of the first face in the subdivided mesh
// isn't actually a part of the mesh data, but I'm storing it here to reduce number of allocations to make
mutable int m_nFirstSubdividedFace;
};
struct SubdivMesh_t
{
CUtlVector<SubdivVertex_t> m_Vertices;
CUtlVector<Edge_t> m_Edges;
// Positive values mean read from m_Edges[x], use m_pVertex[0] for leading vertex
// Negative values mean read from m_Edges[-1-x], use m_pVertex[1] for leading vertex
CUtlVector<int> m_EdgeIndices;
CUtlVector<Face_t> m_Faces;
int m_nTotalIndexCount;
int m_nTotalLineCount;
};
//-----------------------------------------------------------------------------
// Clears a mesh
//-----------------------------------------------------------------------------
static void ClearMesh( SubdivMesh_t &dest )
{
dest.m_Vertices.RemoveAll();
dest.m_Edges.RemoveAll();
dest.m_EdgeIndices.RemoveAll();
dest.m_Faces.RemoveAll();
dest.m_nTotalIndexCount = 0;
dest.m_nTotalLineCount = 0;
}
//-----------------------------------------------------------------------------
// Gets the leading vertex of an edge
//-----------------------------------------------------------------------------
static inline int GetLeadingEdgeVertexIndex( const SubdivMesh_t &src, int nEdge )
{
if ( nEdge >= 0 )
{
const Edge_t &edge = src.m_Edges[nEdge];
return edge.m_pVertex[0];
}
const Edge_t &edge = src.m_Edges[ -1 - nEdge ];
return edge.m_pVertex[1];
}
static inline const SubdivVertex_t &GetLeadingEdgeVertex( const SubdivMesh_t &src, int nEdge )
{
return src.m_Vertices[ GetLeadingEdgeVertexIndex( src, nEdge ) ];
}
//-----------------------------------------------------------------------------
// Adds face midpoints to a mesh
//-----------------------------------------------------------------------------
static void AddFaceMidpointsToMesh( const SubdivMesh_t &src, SubdivMesh_t &dest )
{
int nCurrSubdividedFace = 0;
int nSrcFaceCount = src.m_Faces.Count();
for ( int i = 0; i < nSrcFaceCount; ++i )
{
int nEdgeCount = src.m_Faces[i].m_nEdgeCount;
int nEdgeIndex = src.m_Faces[i].m_nFirstEdgeIndex;
Assert( nEdgeCount != 0 );
int v = dest.m_Vertices.AddToTail( );
SubdivVertex_t &vert = dest.m_Vertices[v];
vert.m_vecPosition.Init();
vert.m_vecTexCoord.Init();
vert.m_nValence = nEdgeCount;
for ( int j = 0; j < nEdgeCount; ++j, ++nEdgeIndex )
{
// NOTE: Instead of calling GetLeadingEdgeVertex,
// I could add both vertices for each edge + multiply by 0.5
int nEdge = src.m_EdgeIndices[nEdgeIndex];
const SubdivVertex_t &srcVert = GetLeadingEdgeVertex( src, nEdge );
vert.m_vecPosition += srcVert.m_vecPosition;
vert.m_vecTexCoord += srcVert.m_vecTexCoord;
}
vert.m_vecPosition /= nEdgeCount;
vert.m_vecTexCoord /= nEdgeCount;
// Store off the face index in the dest mesh of the first subdivided face for this guy.
src.m_Faces[i].m_nFirstSubdividedFace = nCurrSubdividedFace;
nCurrSubdividedFace += nEdgeCount;
}
}
//-----------------------------------------------------------------------------
// Adds edge midpoints to a mesh
//-----------------------------------------------------------------------------
static void AddEdgeMidpointsToMesh( const SubdivMesh_t &src, SubdivMesh_t &dest )
{
int nSrcEdgeCount = src.m_Edges.Count();
for ( int i = 0; i < nSrcEdgeCount; ++i )
{
const Edge_t &edge = src.m_Edges[i];
int v = dest.m_Vertices.AddToTail( );
SubdivVertex_t &vert = dest.m_Vertices[v];
vert.m_nValence = 4;
const SubdivVertex_t *pSrcVert = &src.m_Vertices[ edge.m_pVertex[0] ];
vert.m_vecPosition = pSrcVert->m_vecPosition;
vert.m_vecTexCoord = pSrcVert->m_vecTexCoord;
pSrcVert = &src.m_Vertices[ edge.m_pVertex[1] ];
vert.m_vecPosition += pSrcVert->m_vecPosition;
vert.m_vecTexCoord += pSrcVert->m_vecTexCoord;
// NOTE: We know that the first n vertices added to dest correspond to the src face midpoints
pSrcVert = &dest.m_Vertices[ edge.m_pFace[0] ];
vert.m_vecPosition += pSrcVert->m_vecPosition;
vert.m_vecTexCoord += pSrcVert->m_vecTexCoord;
pSrcVert = &dest.m_Vertices[ edge.m_pFace[1] ];
vert.m_vecPosition += pSrcVert->m_vecPosition;
vert.m_vecTexCoord += pSrcVert->m_vecTexCoord;
vert.m_vecPosition /= 4.0f;
vert.m_vecTexCoord /= 4.0f;
}
}
//-----------------------------------------------------------------------------
// Adds edge midpoints to a mesh
//-----------------------------------------------------------------------------
static void AddModifiedVerticesToMesh( const SubdivMesh_t &src, SubdivMesh_t &dest )
{
int nSrcVertexCount = src.m_Vertices.Count();
// This computes the equation v(i+1) = ((N-2)/N) * v(i) + (1/N^2) * sum( ei + fi )
int nFirstDestVertex = dest.m_Vertices.Count();
for ( int i = 0; i < nSrcVertexCount; ++i )
{
int v = dest.m_Vertices.AddToTail( );
SubdivVertex_t &vert = dest.m_Vertices[v];
int nValence = src.m_Vertices[i].m_nValence;
vert.m_nValence = nValence;
float flScale = (float)(nValence - 2) / nValence;
VectorScale( src.m_Vertices[i].m_vecPosition, flScale, vert.m_vecPosition );
VectorScale( src.m_Vertices[i].m_vecTexCoord, flScale, vert.m_vecTexCoord );
}
int nSrcEdgeCount = src.m_Edges.Count();
for ( int i = 0; i < nSrcEdgeCount; ++i )
{
const Edge_t &edge = src.m_Edges[i];
for ( int j = 0; j < 2; ++j )
{
int nDestVertIndex = nFirstDestVertex + edge.m_pVertex[j];
SubdivVertex_t &destVertex = dest.m_Vertices[nDestVertIndex];
float ooValenceSq = 1.0f / destVertex.m_nValence;
ooValenceSq *= ooValenceSq;
// This adds in the contribution from the source vertex at the opposite edge
const SubdivVertex_t &srcOtherVert = src.m_Vertices[ edge.m_pVertex[ 1 - j ] ];
VectorMA( destVertex.m_vecPosition, ooValenceSq, srcOtherVert.m_vecPosition, destVertex.m_vecPosition );
VectorMA( destVertex.m_vecTexCoord, ooValenceSq, srcOtherVert.m_vecTexCoord, destVertex.m_vecTexCoord );
// This adds in the contribution from the two faces it's part of
// NOTE: Usage of dest here is correct; this grabs the vertex that
// was created that was in the middle of the source mesh's face
const SubdivVertex_t *pSrcFace = &dest.m_Vertices[ edge.m_pFace[ 0 ] ];
VectorMA( destVertex.m_vecPosition, 0.5f * ooValenceSq, pSrcFace->m_vecPosition, destVertex.m_vecPosition );
VectorMA( destVertex.m_vecTexCoord, 0.5f * ooValenceSq, pSrcFace->m_vecTexCoord, destVertex.m_vecTexCoord );
pSrcFace = &dest.m_Vertices[ edge.m_pFace[ 1 ] ];
VectorMA( destVertex.m_vecPosition, 0.5f * ooValenceSq, pSrcFace->m_vecPosition, destVertex.m_vecPosition );
VectorMA( destVertex.m_vecTexCoord, 0.5f * ooValenceSq, pSrcFace->m_vecTexCoord, destVertex.m_vecTexCoord );
}
}
}
//-----------------------------------------------------------------------------
// Adds unique subdivided edges so they aren't repeated.
//-----------------------------------------------------------------------------
static void AddSubdividedEdges( const SubdivMesh_t &src, SubdivMesh_t &dest )
{
// NOTE: We iterate over each edge in sequence and add edges
// between face 0, then face 1, then vertex 0, then vertex 1.
// The vertex index for the vert at the center of original face N is N.
// The vertex index for the vert at the center of original edge N is nSrcFaceCount + N;
// The vertex index for the vert at original vertex N is nSrcFaceCount + nSrcEdgeCount + N;
int nSrcFaceCount = src.m_Faces.Count();
int nSrcEdgeCount = src.m_Edges.Count();
for ( int i = 0; i < nSrcEdgeCount; ++i )
{
const Edge_t &srcEdge = src.m_Edges[i];
int e = dest.m_Edges.AddMultipleToTail( 4 );
Edge_t *pDstEdge = &dest.m_Edges[e];
// Grab the two source faces
const Face_t *pFaces[2];
pFaces[0] = &src.m_Faces[ srcEdge.m_pFace[0] ];
pFaces[1] = &src.m_Faces[ srcEdge.m_pFace[1] ];
// Get the first subdivided face index + relative edge index
int pSubdividedFaceIndex[2];
pSubdividedFaceIndex[0] = pFaces[0]->m_nFirstSubdividedFace;
pSubdividedFaceIndex[1] = pFaces[1]->m_nFirstSubdividedFace;
// Get the relative edge index
int pRelativeEdgeIndex[2];
pRelativeEdgeIndex[0] = srcEdge.m_pRelativeEdgeIndex[0];
pRelativeEdgeIndex[1] = srcEdge.m_pRelativeEdgeIndex[1];
int pPrevRelativeEdgeIndex[2];
pPrevRelativeEdgeIndex[0] = (srcEdge.m_pRelativeEdgeIndex[0] - 1);
if ( pPrevRelativeEdgeIndex[0] < 0 )
{
pPrevRelativeEdgeIndex[0] = pFaces[0]->m_nEdgeCount - 1;
}
pPrevRelativeEdgeIndex[1] = (srcEdge.m_pRelativeEdgeIndex[1] - 1);
if ( pPrevRelativeEdgeIndex[1] < 0 )
{
pPrevRelativeEdgeIndex[1] = pFaces[1]->m_nEdgeCount - 1;
}
// This ordering maintains clockwise order
pDstEdge[0].m_pVertex[0] = srcEdge.m_pFace[0];
pDstEdge[0].m_pVertex[1] = nSrcFaceCount + i;
pDstEdge[0].m_pFace[0] = pSubdividedFaceIndex[0] + pPrevRelativeEdgeIndex[0];
pDstEdge[0].m_pFace[1] = pSubdividedFaceIndex[0] + pRelativeEdgeIndex[0];
pDstEdge[0].m_pRelativeEdgeIndex[0] = 3;
pDstEdge[0].m_pRelativeEdgeIndex[1] = 0;
pDstEdge[1].m_pVertex[0] = nSrcFaceCount + i;
pDstEdge[1].m_pVertex[1] = srcEdge.m_pFace[1];
pDstEdge[1].m_pFace[0] = pSubdividedFaceIndex[1] + pRelativeEdgeIndex[1];
pDstEdge[1].m_pFace[1] = pSubdividedFaceIndex[1] + pPrevRelativeEdgeIndex[1];
pDstEdge[1].m_pRelativeEdgeIndex[0] = 0;
pDstEdge[1].m_pRelativeEdgeIndex[1] = 3;
pDstEdge[2].m_pVertex[0] = nSrcFaceCount + nSrcEdgeCount + srcEdge.m_pVertex[0];
pDstEdge[2].m_pVertex[1] = nSrcFaceCount + i;
pDstEdge[2].m_pFace[0] = pSubdividedFaceIndex[0] + pRelativeEdgeIndex[0];
pDstEdge[2].m_pFace[1] = pSubdividedFaceIndex[1] + pPrevRelativeEdgeIndex[1];
pDstEdge[2].m_pRelativeEdgeIndex[0] = 1;
pDstEdge[2].m_pRelativeEdgeIndex[1] = 2;
pDstEdge[3].m_pVertex[0] = nSrcFaceCount + i;
pDstEdge[3].m_pVertex[1] = nSrcFaceCount + nSrcEdgeCount + srcEdge.m_pVertex[1];
pDstEdge[3].m_pFace[0] = pSubdividedFaceIndex[0] + pPrevRelativeEdgeIndex[0];
pDstEdge[3].m_pFace[1] = pSubdividedFaceIndex[1] + pRelativeEdgeIndex[1];
pDstEdge[3].m_pRelativeEdgeIndex[0] = 2;
pDstEdge[3].m_pRelativeEdgeIndex[1] = 1;
}
}
//-----------------------------------------------------------------------------
// Adds unique subdivided faces
//-----------------------------------------------------------------------------
static void AddSubdividedFaces( const SubdivMesh_t &src, SubdivMesh_t &dest )
{
dest.m_nTotalIndexCount = 0;
dest.m_nTotalLineCount = 0;
int nSrcFaceCount = src.m_Faces.Count();
for ( int i = 0; i < nSrcFaceCount; ++i )
{
int nEdgeCount = src.m_Faces[i].m_nEdgeCount;
const int *pSrcEdgeIndex = &src.m_EdgeIndices[ src.m_Faces[i].m_nFirstEdgeIndex ];
int ei = dest.m_EdgeIndices.AddMultipleToTail( nEdgeCount * 4 );
int *pDestEdgeIndex = &dest.m_EdgeIndices[ ei ];
int *pPrevDestEdgeIndex = &pDestEdgeIndex[(nEdgeCount - 1) * 4];
for ( int j = 0; j < nEdgeCount; ++j )
{
// Add another quad.
dest.m_nTotalIndexCount += 6;
dest.m_nTotalLineCount += 4;
// Add a face for every edge. Note that subdivided face N
// is the face whose goes through edge N.
int f = dest.m_Faces.AddToTail();
Face_t *pDestFace = &dest.m_Faces[f];
pDestFace->m_nEdgeCount = 4;
pDestFace->m_nFirstEdgeIndex = ei + (j * 4);
// Fill it with bogus data
pDestFace->m_nFirstSubdividedFace = -1;
// Now add in the edge indices to refer to the edges created in AddSubdividedEdges.
// Note that the new edge index == the old edge index * 4, since we always
// create 4 edges for every edge in the source list.
int *pCurrDestEdgeIndex = &pDestEdgeIndex[j*4];
int nSrcEdgeIndex = pSrcEdgeIndex[j];
if ( nSrcEdgeIndex >= 0 )
{
// This means this polygon is the '1' index in the edge; it's following this edge CCW.
int nDestEdgeIndex = nSrcEdgeIndex * 4;
pCurrDestEdgeIndex[0] = -1 - (nDestEdgeIndex + 1); // We're following this edge backwards
pCurrDestEdgeIndex[1] = nDestEdgeIndex + 3;
pPrevDestEdgeIndex[2] = nDestEdgeIndex + 2;
pPrevDestEdgeIndex[3] = nDestEdgeIndex + 1;
}
else
{
// This means this polygon is the '0' index in the edge; it's following this edge CW.
int nDestEdgeIndex = (-1 - nSrcEdgeIndex) * 4;
pCurrDestEdgeIndex[0] = nDestEdgeIndex;
pCurrDestEdgeIndex[1] = -1 - (nDestEdgeIndex + 2); // We're following this edge backwards
pPrevDestEdgeIndex[2] = -1 - (nDestEdgeIndex + 3); // We're following this edge backwards
pPrevDestEdgeIndex[3] = -1 - (nDestEdgeIndex); // We're following this edge backwards
}
pPrevDestEdgeIndex = pCurrDestEdgeIndex;
}
}
}
//-----------------------------------------------------------------------------
// Subdivides a mesh
//-----------------------------------------------------------------------------
static void SubdivideMesh( const SubdivMesh_t &src, SubdivMesh_t &dest )
{
// Preallocate space for dest data
int nSrcFaceCount = src.m_Faces.Count();
int nSrcEdgeCount = src.m_Edges.Count();
dest.m_Vertices.EnsureCapacity( nSrcFaceCount + nSrcEdgeCount + src.m_Vertices.Count() );
dest.m_Edges.EnsureCapacity( nSrcEdgeCount * 4 );
dest.m_EdgeIndices.EnsureCapacity( nSrcFaceCount * 16 );
dest.m_Faces.EnsureCapacity( nSrcFaceCount * 4 ); // This is only true if we have valence 4 everywhere.
// First, compute midpoints of each face, add them to the mesh
AddFaceMidpointsToMesh( src, dest );
// Next, for each edge, compute a new point which is the average of the edge points and the face midpoints
AddEdgeMidpointsToMesh( src, dest );
// Add modified versions of the vertices in the src mesh based on the new computed points and add them to the dest mesh
AddModifiedVerticesToMesh( src, dest );
// Add subdivided edges based on the previous edges
AddSubdividedEdges( src, dest );
// Add subdivided faces referencing the subdivided edges
AddSubdividedFaces( src, dest );
}
//-----------------------------------------------------------------------------
// Creates/destroys the subdiv control cage
//-----------------------------------------------------------------------------
void CDmeTestMesh::CreateControlCage( )
{
DestroyControlCage();
m_pControlCage = new SubdivMesh_t;
// Draw a simple cube
static Vector s_pPositions[8] =
{
Vector( -30, -30, -30 ),
Vector( 30, -30, -30 ),
Vector( -30, 30, -30 ),
Vector( 30, 30, -30 ),
Vector( -30, -30, 30 ),
Vector( 30, -30, 30 ),
Vector( -30, 30, 30 ),
Vector( 30, 30, 30 ),
};
static Vector2D s_pTexCoords[8] =
{
Vector2D( 0, 0 ),
Vector2D( 0.5, 0 ),
Vector2D( 0, 0.5 ),
Vector2D( 0.5, 0.5 ),
Vector2D( 0.5, 0.5 ),
Vector2D( 1, 0.5 ),
Vector2D( 0.5, 1 ),
Vector2D( 1, 1 ),
};
// Indices into the vertex array
static int s_pEdges[12][2] =
{
{ 0, 4 }, { 4, 6 }, { 6, 2 }, { 2, 0 }, // 0 -> -x
{ 1, 3 }, { 3, 7 }, { 7, 5 }, { 5, 1 }, // 1 -> +x
{ 0, 1 }, { 5, 4 }, // 2 -> -y
{ 6, 7 }, { 3, 2 }, // 3 -> +y
// 4 -> -z
// 5 -> +z
};
// Indices into the face array associated w/ the edges above
static int s_pEdgeFaces[12][2] =
{
{ 2, 0 }, { 5, 0 }, { 3, 0 }, { 4, 0 }, // 0 -> -x
{ 4, 1 }, { 3, 1 }, { 5, 1 }, { 2, 1 }, // 1 -> +x
{ 4, 2 }, { 5, 2 }, // 2 -> -y
{ 5, 3 }, { 4, 3 }, // 3 -> +y
// 4 -> -z
// 5 -> +z
};
// In what order does edge s_pEdges[i] appear on faces s_pEdgeFaces[i][0] and s_pEdgeFaces[i][1]
// in the list s_pIndices[s_pEdgeFaces[i][j]] below? Note the #s 0, 1, 2, and 3 should appear 6 times each in this array
// representing the fact that each face has a 0th,1st,2nd, and 3rd edge.
static int s_pRelativeEdgeIndex[12][2] =
{
{ 3, 0 }, { 3, 1 }, { 0, 2 }, { 0, 3 }, // 0 -> -x
{ 2, 0 }, { 2, 1 }, { 1, 2 }, { 1, 3 }, // 1 -> +x
{ 3, 0 }, { 0, 2 }, // 2 -> -y
{ 2, 1 }, { 1, 3 }, // 3 -> +y
// 4 -> -z
// 5 -> +z
};
static int s_pIndices[6][5] =
{
{ 0, 4, 6, 2, 0 }, // 0 -> -x
{ 1, 3, 7, 5, 1 }, // 1 -> +x
{ 0, 1, 5, 4, 0 }, // 2 -> -y
{ 2, 6, 7, 3, 2 }, // 3 -> +y
{ 0, 2, 3, 1, 0 }, // 4 -> -z
{ 4, 5, 7, 6, 4 }, // 5 -> +z
};
// Add vertices
int i;
for ( i = 0; i < 8; ++i )
{
int v = m_pControlCage->m_Vertices.AddToTail();
SubdivVertex_t &vert = m_pControlCage->m_Vertices[v];
vert.m_vecPosition = s_pPositions[i];
vert.m_vecNormal = vec3_origin;
vert.m_vecTexCoord.AsVector2D() = s_pTexCoords[i];
vert.m_nValence = 3;
}
// Add unique edges
for ( i = 0; i < 12; ++i )
{
int e = m_pControlCage->m_Edges.AddToTail();
Edge_t &edge = m_pControlCage->m_Edges[e];
edge.m_pVertex[0] = s_pEdges[i][0];
edge.m_pVertex[1] = s_pEdges[i][1];
edge.m_pFace[0] = s_pEdgeFaces[i][0];
edge.m_pFace[1] = s_pEdgeFaces[i][1];
edge.m_pRelativeEdgeIndex[0] = s_pRelativeEdgeIndex[i][0];
edge.m_pRelativeEdgeIndex[1] = s_pRelativeEdgeIndex[i][1];
}
m_pControlCage->m_nTotalIndexCount = 0;
m_pControlCage->m_nTotalLineCount = 0;
for ( i = 0; i < 6; ++i )
{
int f = m_pControlCage->m_Faces.AddToTail();
Face_t &face = m_pControlCage->m_Faces[f];
face.m_nFirstEdgeIndex = m_pControlCage->m_EdgeIndices.Count();
face.m_nEdgeCount = 4;
// Place an invalid value here
face.m_nFirstSubdividedFace = -1;
// Two triangles per quad
m_pControlCage->m_nTotalIndexCount += 6;
m_pControlCage->m_nTotalLineCount += 4;
for ( int j = 0; j < 4; ++j )
{
int k;
for ( k = 0; k < 12; ++k )
{
if ( (s_pIndices[i][j] == s_pEdges[k][0]) && (s_pIndices[i][j+1] == s_pEdges[k][1]) )
{
m_pControlCage->m_EdgeIndices.AddToTail( k );
break;
}
if ( (s_pIndices[i][j] == s_pEdges[k][1]) && (s_pIndices[i][j+1] == s_pEdges[k][0]) )
{
m_pControlCage->m_EdgeIndices.AddToTail( -1-k );
break;
}
}
Assert( k != 12 );
}
}
}
void CDmeTestMesh::DestroyControlCage( )
{
if ( m_pControlCage )
{
delete m_pControlCage;
m_pControlCage = NULL;
}
}
//-----------------------------------------------------------------------------
// Draws a subdiv mesh
//-----------------------------------------------------------------------------
void CDmeTestMesh::DrawSubdivMesh( const SubdivMesh_t &mesh )
{
if ( !g_pMaterialSystem )
return;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
IMaterial *pMaterial = g_pMaterialSystem->FindMaterial( "debug/debugwireframe", NULL, false );
pRenderContext->Bind( pMaterial );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
CMeshBuilder meshBuilder;
int nVertexCount = mesh.m_Vertices.Count();
// meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, nVertexCount, mesh.m_nTotalIndexCount );
meshBuilder.Begin( pMesh, MATERIAL_LINES, nVertexCount, mesh.m_nTotalLineCount * 2 );
for ( int i = 0; i < nVertexCount; ++i )
{
meshBuilder.Position3fv( mesh.m_Vertices[ i ].m_vecPosition.Base() );
meshBuilder.TexCoord2fv( 0, mesh.m_Vertices[ i ].m_vecTexCoord.Base() );
meshBuilder.TexCoord2f( 1, i, 0.0f );
meshBuilder.Color3ub( 255, 255, 255 );
meshBuilder.AdvanceVertex();
}
int nFaceCount = mesh.m_Faces.Count();
for ( int i = 0; i < nFaceCount; ++i )
{
int nEdgeCount = mesh.m_Faces[i].m_nEdgeCount;
const int *pEdgeIndex = &mesh.m_EdgeIndices[ mesh.m_Faces[i].m_nFirstEdgeIndex ];
int nPrevIndex = GetLeadingEdgeVertexIndex( mesh, pEdgeIndex[nEdgeCount-1] );
for ( int j = 0; j < nEdgeCount; ++j )
{
int nCurrIndex = GetLeadingEdgeVertexIndex( mesh, pEdgeIndex[j] );
meshBuilder.FastIndex( nPrevIndex );
meshBuilder.FastIndex( nCurrIndex );
nPrevIndex = nCurrIndex;
}
}
/*
int nFaceCount = mesh.m_Faces.Count();
for ( int i = 0; i < nFaceCount; ++i )
{
int nEdgeCount = mesh.m_Faces[i].m_nEdgeCount;
const int *pEdgeIndex = &mesh.m_EdgeIndices[ mesh.m_Faces[i].m_nFirstEdgeIndex ];
int nRootIndex = GetLeadingEdgeVertexIndex( mesh, pEdgeIndex[0] );
int nPrevIndex = GetLeadingEdgeVertexIndex( mesh, pEdgeIndex[1] );
for ( int j = 0; j < nEdgeCount - 2; ++j )
{
int nCurrIndex = GetLeadingEdgeVertexIndex( mesh, pEdgeIndex[j+2] );
meshBuilder.FastIndex( nRootIndex );
meshBuilder.FastIndex( nPrevIndex );
meshBuilder.FastIndex( nCurrIndex );
nPrevIndex = nCurrIndex;
}
}
*/
meshBuilder.End();
pMesh->Draw();
}
//-----------------------------------------------------------------------------
// Draws a subdivided box
//-----------------------------------------------------------------------------
void CDmeTestMesh::DrawSubdividedBox()
{
if ( !g_pMaterialSystem )
return;
if ( !m_pControlCage )
{
CreateControlCage( );
}
int nSubdivLevel = GetValue<int>( "subdivlevel" );
if ( nSubdivLevel == 0 )
{
DrawSubdivMesh( *m_pControlCage );
return;
}
// Construct the initial mesh
SubdivMesh_t subdivMesh[2];
SubdivideMesh( *m_pControlCage, subdivMesh[0] );
// Compute the subdivided vertices
int nCurrMesh = 0;
while ( --nSubdivLevel > 0 )
{
ClearMesh( subdivMesh[1 - nCurrMesh] );
SubdivideMesh( subdivMesh[nCurrMesh], subdivMesh[1 - nCurrMesh] );
if (( subdivMesh[1 - nCurrMesh].m_nTotalLineCount * 2 >= 32768 ) || ( subdivMesh[1 - nCurrMesh].m_Vertices.Count() >= 32768 ))
break;
nCurrMesh = 1 - nCurrMesh;
}
// Draw the subdivided mesh
DrawSubdivMesh( subdivMesh[nCurrMesh] );
}
//-----------------------------------------------------------------------------
// Draws the mesh
//-----------------------------------------------------------------------------
void CDmeTestMesh::DrawBox( CDmeTransform *pTransform )
{
if ( !g_pMaterialSystem )
return;
// FIXME: Hack!
if ( !m_pMorph || !m_pMesh )
return;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
// Set up morph factors
float pMorphFactors[32];
for ( int i = 0; i < 32; ++i )
{
pMorphFactors[i] = 0.5f + 0.5f * sin( 2 * 3.14 * ( Plat_FloatTime() / 5.0f + (float)i / 32.0f ) );
}
pMorphFactors[1] = 1.0f - pMorphFactors[0];
pRenderContext->SetMorphTargetFactors( 0, pMorphFactors, 32 );
// FIXME: Should this call be made from the application rendering the mesh?
LoadModelMatrix( pTransform );
pRenderContext->BindMorph( m_pMorph );
pRenderContext->Bind( m_pMaterial );
m_pMesh->Draw();
pRenderContext->BindMorph( NULL );
}
//-----------------------------------------------------------------------------
// Draws the mesh
//-----------------------------------------------------------------------------
void CDmeTestMesh::Draw( const matrix3x4_t& shapeToWorld, CDmeDrawSettings *pDrawSettings )
{
if ( !g_pMaterialSystem || !g_pMDLCache || !g_pStudioRender )
return;
#if 0
// DrawSubdividedBox( pTransform );
DrawBox( pTransform );
return;
#elif 0
if ( m_MDLHandle == MDLHANDLE_INVALID )
return;
// Color + alpha modulation
Vector white(1.0f, 1.0f, 1.0f);
g_pStudioRender->SetColorModulation( white.Base() );
g_pStudioRender->SetAlphaModulation( 1.0f );
DrawModelInfo_t info;
info.m_pStudioHdr = g_pMDLCache->GetStudioHdr( m_MDLHandle );
info.m_pHardwareData = g_pMDLCache->GetHardwareData( m_MDLHandle );
info.m_Decals = STUDIORENDER_DECAL_INVALID;
info.m_Skin = GetAttributeValueInt( "skin" );
info.m_Body = GetAttributeValueInt( "body" );
info.m_HitboxSet = 0;