This repository was archived by the owner on Nov 19, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMinimalExport.cpp
More file actions
3371 lines (2868 loc) · 120 KB
/
Copy pathMinimalExport.cpp
File metadata and controls
3371 lines (2868 loc) · 120 KB
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
#include "BasePCH.h"
#include "MinimalExport.h"
#include "../Phoenix_Tool/apxProject.h"
#include "../Phoenix/Project.h"
#include "../Phoenix/Timeline.h"
#include "ExternalTools.h"
#define MINIZ_HEADER_FILE_ONLY
#include "../../Bedrock/UtilLib/miniz.c"
CArray<CString> symbolNameStack;
CArray<CString> symbolList;
void ResetSymbolBuilder()
{
symbolNameStack.FlushFast();
symbolList.Flush();
}
void PushSymbolName( const CString& str )
{
symbolNameStack += str;
}
void PopSymbolName()
{
symbolNameStack.DeleteByIndex( symbolNameStack.NumItems() - 1 );
}
class SymbolNameScoper
{
public:
SymbolNameScoper( const CString& string )
{
PushSymbolName( string );
}
~SymbolNameScoper()
{
PopSymbolName();
}
};
CString BuildSymbolName()
{
CString separator = "::";
//separator[ 0 ] = (char)0xfe;
CString s = symbolNameStack[ 0 ];
for ( int x = 1; x < symbolNameStack.NumItems(); x++ )
s += separator + symbolNameStack[ x ];
return s;
}
unsigned short GetSymbolID()
{
CString symbolName = BuildSymbolName();
int id = symbolList.Find( symbolName );
if ( id >= 0 )
return id;
symbolList += symbolName;
return symbolList.NumItems() - 1;
}
struct MiniMesh
{
TS32 vxDataSize;
TS32 polyDataSize;
unsigned char* vxData = nullptr;
unsigned char* polyData = nullptr;
};
CArray<MiniMesh> miniMeshes;
bool hasLTC1 = false;
bool hasLTC2 = false;
namespace CrossCompiler
{
namespace Parser
{
bool MinifyMultipleShaders( CArray<CString>& input, CString& include, CArray<CString>& output );
}
}
TS32 SplineCount = 0;
TS32 SplineDataSize = 0;
TS32 SplineDataSizeScenes = 0;
TS32 TexgenOpCount = 0;
TS32 TexgenOpDataSize = 0;
TS32 MaterialParamCount = 0;
TS32 MaterialParamSize = 0;
TS32 MaterialParameterDataCount = 0;
TS32 MaterialParameterDataSize = 0;
TS32 ModelFilterCount = 0;
TS32 ModelFilterSize = 0;
TS32 MaterialDataPackCount = 0;
TS32 MaterialParamDefaultCount = 0;
TS32 ModelObjectCount = 0;
TS32 ModelObjectDataSize = 0;
TS32 ClipSplineCount = 0;
TS32 SceneObjectCount = 0;
TS32 SceneObjectDataSize = 0;
TS32 EventCount = 0;
TS32 EventDataSize = 0;
TS32 MiscDataSize = 0;
TS32 SetupDataSize = 0;
TS32 RenderTargetDataSize = 0;
TS32 RenderLayerDataSize = 0;
TS32 TextureFilterDataSize = 0;
TS32 ShaderSize = 0;
TS32 TexgenPageDataSize = 0;
TS32 MaterialTechCount = 0;
TS32 MaterialTechDataSize = 0;
TS32 MaterialDescriptionSize = 0;
TS32 TreeDataSize = 0;
TS32 ModelSetupSize = 0;
TS32 SceneSetupSize = 0;
TS32 TimelineSetupSize = 0;
TS32 MinimalDataSize = 0;
TS32 defaultSplineCount = 0;
TS32 constantSplineCount = 0;
TS32 complexConstantSplineCount = 0;
TS32 fullyConstantSplines = 0;
CphxMaterial_Tool* NoMaterial = NULL;
int UsedModelPrimitives[ 256 ];
int UsedModelFilters[ 256 ];
int UsedTexgenInputs[ 256 ];
int UsedScenePrimitives[ 256 ];
int UsedEventTypes[ 256 ];
TBOOL hasUVClip = false;
TBOOL hasMeshParticle = false;
TBOOL hasNormalParticle = false;
TBOOL hasSubSceneParticle = false;
TBOOL TexgenImageLoad = false;
TBOOL MinimalExportSuccessful = true;
TBOOL scatterVertexSupport = false;
TBOOL scatterEdgeSupport = false;
TBOOL scatterPolySupport = false;
TBOOL scatterPolyMultiSupport = false;
TBOOL splineHasZeroKeyExport = false;
TBOOL hasTechParams = false;
TBOOL hasParticleSort = false;
TBOOL hasScatterOrientationOriginal = false;
TBOOL hasScatterOrientationNormal = false;
TBOOL hasScatterOrientationNormalRotate = false;
TBOOL hasScatterOrientationFullRotate = false;
CStreamWriterMemory MinimalStreams[ StreamCount ];
CStreamWriterMemory SymbolStreams[ StreamCount ];
int MinimalStreamPositions[ StreamCount ]{};
void StoreStreamPositions()
{
for ( int x = 0; x < StreamCount; x++ )
MinimalStreamPositions[ x ] = MinimalStreams[ x ].GetLength();
}
CStringArray fontList;
extern char* EngineFontList[];
int GetStreamSizeDiff()
{
int count = 0;
for ( int x = 0; x < StreamCount; x++ )
count += MinimalStreams[ x ].GetLength() - MinimalStreamPositions[ x ];
return count;
}
static unsigned char MeshPrimitiveParameterCounts[] = //IF THESE CHANGE YOU NEED TO CHANGE THEM IN THE PROJECT.CPP AS WELL - it's a size optimization thing
{
0,//Mesh_Cube = 0,
2,//Mesh_Plane,
5,//Mesh_Sphere,
3,//Mesh_Cylinder,
4,//Mesh_Cone,
3,//Mesh_Arc,
1,//Mesh_Line,
2,//Mesh_Spline,
7,//Mesh_Loft,
0,//Mesh_Clone,
0,//Mesh_Copy,
2,//Mesh_GeoSphere,
14,//Mesh_Scatter,
0,//Mesh_Stored,
6,//Mesh_Tree,
7,//Mesh_TreeLeaves,
2,//Mesh_Text,
0,//Mesh_Marched
0,//Mesh_StoredMini
0,//Mesh_Merge
};
static unsigned char MeshFilterParameterCounts[] = //IF THESE CHANGE YOU NEED TO CHANGE THEM IN THE PROJECT.CPP AS WELL - it's a size optimization thing
{
2,//ModelFilter_UVMap = 0,
1,//ModelFilter_Bevel,
0,//ModelFilter_MapXForm,
2,//ModelFilter_MeshSmooth,
1,//ModelFilter_SmoothGroup,
2,//ModelFilter_TintMesh,
3,//ModelFilter_TintMeshShape,
1,//ModelFilter_Replicate,
2,//ModelFilter_NormalDeform
2,//ModelFilter_CSG
3,//ModelFilter_Greeble
};
#include "HLSLParser.h"
TBOOL MinifyShader_CNS( char* Data, CString& Result, TBOOL Verbose, TBOOL ResetDictionary, TBOOL RebuildDictionary )
{
CString Output;
CString Shader = Data;
if ( CrossCompiler::Parser::Parse( Shader, Output, Verbose, ResetDictionary, RebuildDictionary ) )
{
Result = Output;
return true;
}
return false;
}
CString ToMacroCompatible( CString& _name )
{
CString s = _name;
for ( int i = 0; i < (int)s.Length(); i++ ) if ( !isalnum( s[ i ] ) ) s[ i ] = '_'; else s[ i ] = toupper( s[ i ] );
s += ""; // reset hash!
return s;
}
void WriteSymbolStream( int stream, int bytes )
{
for ( int x = 0; x < bytes; x++ )
SymbolStreams[ stream ].WriteWord( GetSymbolID() );
}
#define WriteByte(a,x,y) do { MinimalStreams[a].WriteByte(x); y++; WriteSymbolStream(a,1); } while(0)
#define WriteWord(a,x,y) do { MinimalStreams[a].WriteWord(x); y+=2; WriteSymbolStream(a,2); } while(0)
#define WriteFloat16s(a,x,n,y) do { MinimalStreams[a].Write(x,n*2); y+=2*n; WriteSymbolStream(a,2*n); } while(0)
#define Write(a,x,y,z) do { MinimalStreams[a].Write(x,y); z+=y; WriteSymbolStream(a,y); } while(0)
#define WriteASCIIZ(a,x,y) do { MinimalStreams[a].WriteASCIIZ(x); y+=x.Length()+1; WriteSymbolStream(a,x.Length()+1); } while(0)
CArray<CphxSpline_Tool_float16*> exportedSplines;
int duplicateSplines = 0;
int duplicateSplineSize = 0;
bool IsSplineConstant( CphxSpline_Tool_float16* Spline, D3DXFLOAT16& constValue )
{
CphxSpline_float16* sp = (CphxSpline_float16*)Spline->Spline;
D3DXFLOAT16 value = 0;
bool isConstant = true;
/*
if ( !Spline->Keys.NumItems() )
value = sp->Value[ 0 ];
if ( Spline->Keys.NumItems() == 1 )
value = Spline->Keys[ 0 ]->Key.Value[ 0 ];
if ( Spline->Keys.NumItems() > 1 ) // go through every time position on the spline and check if the value is the same
*/
{
sp->CalculateValue( 0 );
value = sp->Value[ 0 ];
for ( int x = 0; x < 512; x++ )
{
sp->CalculateValue( x / 512.0f );
D3DXFLOAT16 v2 = sp->Value[ 0 ];
if ( value != v2 )
{
isConstant = false;
break;
}
}
}
constValue = value;
return isConstant;
}
int uniqueNonSpecialSplines = 0;
int splineTypeCounts[ 16 ];
int floatSplineWaveforms[ 16 ];
int floatSplineInterpolations[ 16 ];
int quatSplineInterpolations[ 16 ];
void ExportSpline( CphxSpline_Tool_float16* Spline )
{
SymbolNameScoper sym( "Splines" );
SplineCount++;
CphxSpline_float16* sp = (CphxSpline_float16*)Spline->Spline;
// determine if the spline is a constant spline first
if ( sp->Waveform == WAVEFORM_NONE )
{
DEFAULTSPLINETYPE defType = DEFAULTSPLINE_NOT_DEFAULT;
D3DXFLOAT16 value = 0;
bool isConstant = true;
/*
if ( !Spline->Keys.NumItems() )
value = sp->Value[ 0 ];
if ( Spline->Keys.NumItems() == 1 )
value = Spline->Keys[ 0 ]->Key.Value[ 0 ];
*/
//if ( Spline->Keys.NumItems() > 1 ) // go through every time position on the spline and check if the value is the same
{
sp->CalculateValue( 0 );
value = sp->Value[ 0 ];
for ( int x = 0; x < 512; x++ )
{
sp->CalculateValue( x / 512.0f );
D3DXFLOAT16 v2 = sp->Value[ 0 ];
if ( value != v2 )
{
isConstant = false;
break;
}
}
}
if ( isConstant )
{
defType = DEFAULTSPLINE_CONSTANTVALUE;
constantSplineCount++;
if ( value == D3DXFLOAT16( 0.0f ) )
{
defType = DEFAULTSPLINE_ZERO;
defaultSplineCount++;
}
if ( value == D3DXFLOAT16( 1.0f ) )
{
defType = DEFAULTSPLINE_ONE;
defaultSplineCount++;
}
if ( Spline->Keys.NumItems() > 1 )
complexConstantSplineCount++;
unsigned char c = defType;
WriteByte( Stream_SplineDescriptors, c, SplineDataSize );
if ( defType == DEFAULTSPLINE_CONSTANTVALUE )
Write( Stream_SplineKeyValues, &value, 2, SplineDataSize );
return;
}
}
// do this here so constant splines are ignored
floatSplineInterpolations[ sp->Interpolation ]++;
floatSplineWaveforms[ sp->Waveform ]++;
unsigned char c = sp->Interpolation << 2;
c |= sp->Loop << 4;
c |= sp->Waveform << 5;
WriteByte( Stream_SplineDescriptors, c, SplineDataSize );
//MINIMALSPLINESETUPDATA m;
//m.Loop = sp->Loop;
//m.Interpolation = sp->Interpolation;
//m.Waveform = sp->Waveform;
//
//WriteByte(*(unsigned char*)&m, SplineDataSize);
//WriteByte(Stream_SplineDescriptors, sp->Loop, SplineDataSize); //loop info
//WriteByte(Stream_SplineDescriptors, sp->Interpolation, SplineDataSize); //interpolation type - todo: merge interpolation with waveform in export
//WriteByte(Stream_SplineDescriptors, sp->Waveform, SplineDataSize); //waveform type
if ( sp->Waveform != WAVEFORM_NONE )
{
WriteByte( Stream_SplineDescriptors, sp->MultiplicativeWaveform, SplineDataSize );
Write( Stream_SplineDescriptors, &sp->WaveformAmplitude, 2, SplineDataSize );
Write( Stream_SplineDescriptors, &sp->WaveformFrequency, 2, SplineDataSize );
if ( sp->Waveform == WAVEFORM_NOISE )
WriteByte( Stream_SplineDescriptors, sp->RandSeed, SplineDataSize );
}
//special case of 1 key but linear data and no waveform
if ( Spline->Keys.NumItems() == 1 && sp->Interpolation != INTERPOLATION_BEZIER && sp->Waveform == WAVEFORM_NONE )
{
splineHasZeroKeyExport = true;
WriteByte( Stream_SplineKeyCounts, 0, SplineDataSize );
D3DXFLOAT16 v = Spline->Keys[ 0 ]->Key.Value[ 0 ];
Write( Stream_SplineKeyValues, &v, 2, SplineDataSize );
return;
}
WriteByte( Stream_SplineKeyCounts, Spline->Keys.NumItems(), SplineDataSize ); //key count
if ( !Spline->Keys.NumItems() )
{
splineHasZeroKeyExport = true;
D3DXFLOAT16 v = Spline->Spline->Value[ 0 ];
Write( Stream_SplineKeyValues, &v, 2, SplineDataSize );
}
for ( TS32 x = 0; x < Spline->Keys.NumItems(); x++ )
{
WriteByte( Stream_SplineKeyTimes, Spline->Keys[ x ]->Key.t, SplineDataSize ); //key position
Write( Stream_SplineKeyValues, &Spline->Keys[ x ]->Key.Value, 2, SplineDataSize );
if ( sp->Interpolation == INTERPOLATION_BEZIER )
{
WriteByte( Stream_SplineKeyTimes, Spline->Keys[ x ]->Key.controlpositions[ 0 ], SplineDataSize );
WriteByte( Stream_SplineKeyTimes, Spline->Keys[ x ]->Key.controlpositions[ 1 ], SplineDataSize );
Write( Stream_SplineKeyValues, &Spline->Keys[ x ]->Key.controlvalues[ 0 ], 2, SplineDataSize );
Write( Stream_SplineKeyValues, &Spline->Keys[ x ]->Key.controlvalues[ 1 ], 2, SplineDataSize );
}
}
}
void ExportSpline( CphxSpline_Tool_Quaternion16* Spline )
{
SplineCount++;
CphxSpline_Quaternion16* sp = (CphxSpline_Quaternion16*)Spline->Spline;
// determine if the spline is a constant spline first
if ( sp->Waveform == WAVEFORM_NONE )
{
DEFAULTSPLINETYPE defType = DEFAULTSPLINE_NOT_DEFAULT;
D3DXFLOAT16 value[ 4 ];
value[ 0 ] = 0;
value[ 1 ] = 0;
value[ 2 ] = 0;
value[ 3 ] = 1;
bool isConstant = true;
if ( !Spline->Keys.NumItems() )
for ( int x = 0; x < 4; x++ )
value[ x ] = sp->Value[ x ];
if ( Spline->Keys.NumItems() == 1 )
for ( int x = 0; x < 4; x++ )
value[ x ] = Spline->Keys[ 0 ]->Key.Value[ x ];
if ( Spline->Keys.NumItems() > 1 ) // go through every time position on the spline and check if the value is the same
{
sp->CalculateValue( 0 );
for ( int x = 0; x < 4; x++ )
value[ x ] = sp->Value[ x ];
for ( int x = 0; x < 512; x++ )
{
sp->CalculateValue( x / 512.0f );
for ( int y = 0; y < 4; y++ )
{
D3DXFLOAT16 v2 = sp->Value[ y ];
if ( value[ y ] != v2 )
{
isConstant = false;
break;
}
}
if ( !isConstant )
break;
}
}
if ( isConstant )
{
defType = DEFAULTSPLINE_CONSTANTVALUE;
constantSplineCount++;
if ( value[ 0 ] == D3DXFLOAT16( 0.0f ) &&
value[ 1 ] == D3DXFLOAT16( 0.0f ) &&
value[ 2 ] == D3DXFLOAT16( 0.0f ) &&
value[ 3 ] == D3DXFLOAT16( 1.0f ) )
{
defaultSplineCount++;
defType = DEFAULTSPLINE_ZERO;
}
if ( Spline->Keys.NumItems() > 1 )
complexConstantSplineCount++;
unsigned char c = defType;
WriteByte( Stream_SplineDescriptors, c, SplineDataSize );
if ( defType == DEFAULTSPLINE_CONSTANTVALUE )
for ( int x = 0; x < 4; x++ )
Write( Stream_SplineKeyValues, &value[ x ], 2, SplineDataSize );
return;
}
}
quatSplineInterpolations[ sp->Interpolation ]++;
unsigned char c = sp->Interpolation << 2;
c |= sp->Loop << 4;
//c |= sp->Waveform << 3; // quaternion splines don't have waveforms
WriteByte( Stream_SplineDescriptors, c, SplineDataSize );
//MINIMALSPLINESETUPDATA m;
//m.Loop = sp->Loop;
//m.Interpolation = sp->Interpolation;
//m.Waveform = sp->Waveform;
//WriteByte(*(unsigned char*)&m, SplineDataSize);
//WriteByte(Stream_SplineDescriptors, sp->Loop, SplineDataSize); //loop info
//WriteByte(Stream_SplineDescriptors, sp->Interpolation, SplineDataSize); //interpolation type - todo: merge interpolation with waveform in export
if ( Spline->Keys.NumItems() == 1 ) //special case for one key
{
WriteByte( Stream_SplineKeyCounts, 0, SplineDataSize ); //key count
for ( TS32 x = 0; x < 4; x++ ) //quaternion spline export
{
D3DXFLOAT16 v = Spline->Keys[ 0 ]->Key.Value[ x ];
Write( Stream_SplineKeyValues, &v, 2, SplineDataSize );
//WriteByte( Stream_SplineKeyValues, (char)( v * 127 ), SplineDataSize );
}
return;
}
WriteByte( Stream_SplineKeyCounts, Spline->Keys.NumItems(), SplineDataSize ); //key count
if ( !Spline->Keys.NumItems() )
{
for ( TS32 x = 0; x < 4; x++ ) //quaternion spline export
{
D3DXFLOAT16 v = Spline->Spline->Value[ x ];
Write( Stream_SplineKeyValues, &v, 2, SplineDataSize );
//WriteByte( Stream_SplineKeyValues, (char)( v * 127 ), SplineDataSize );
}
}
for ( TS32 x = 0; x < Spline->Keys.NumItems(); x++ )
{
WriteByte( Stream_SplineKeyTimes, Spline->Keys[ x ]->Key.t, SplineDataSize ); //key position
for ( TS32 y = 0; y < 4; y++ ) //quaternion spline export
{
D3DXFLOAT16 v = Spline->Keys[ x ]->Key.Value[ y ];
Write( Stream_SplineKeyValues, &v, 2, SplineDataSize );
//WriteByte( Stream_SplineKeyValues, (char)( v * 127 ), SplineDataSize );
}
}
}
void ExportTexgenOp( CphxTextureOperator_Tool* op )
{
SymbolNameScoper sym( op->BuildsTexture );
if ( !op->Filter )
{
LOG_ERR( "[minimalexport] Serious export issue, operator filter missing!" );
MinimalExportSuccessful = false;
return;
}
TexgenOpCount++;
unsigned char filter = op->Filter->GetResourceIndex() | ( op->ParentPage->IsHDR() << 7 );
//if (op->IsRendered()) //no need for this anymore since the first x operators are generated
// filter = filter | 0x80;
WriteByte( Stream_MaterialParam, filter, TexgenOpDataSize ); //filter ID
if ( op->Filter->Filter.DataDescriptor.ParameterCount ) //parameters
Write( Stream_Main, op->OpData.Parameters, op->Filter->Filter.DataDescriptor.ParameterCount, TexgenOpDataSize );
if ( op->Filter->Filter.DataDescriptor.NeedsRandSeed ) //randseed
WriteByte( Stream_Main, op->OpData.RandSeed, TexgenOpDataSize );
for ( TU32 x = 0; x < op->Filter->Filter.DataDescriptor.InputCount; x++ ) //inputs
{
CphxTextureOperator_Tool* p = ( (CphxTextureOperator_Tool*)op->GetParent( PHX_TEXTUREOPERATOR, x ) );
if ( p ) p = p->GetContentOp();
WriteWord( Stream_TextureOperatorReference, p->GetResourceIndex(), TexgenOpDataSize );
}
switch ( op->Filter->Filter.DataDescriptor.LookupType )
{
case 0: //no extra data needed
break;
case 1: //image load
TexgenImageLoad = true;
Write( Stream_Main, &op->ImageDataSize, 4, TexgenOpDataSize );
Write( Stream_Main, op->ImageData, op->ImageDataSize, TexgenOpDataSize );
break;
case 2: //text data
{
int oldFont = op->TextData.Font;
op->TextData.Font = fontList.Find( EngineFontList[ oldFont ] );
Write( Stream_Main, &op->TextData, 5, TexgenOpDataSize );
op->TextData.Font = oldFont;
}
//mem.WriteByte(op->TextData.XPos);
//mem.WriteByte(op->TextData.YPos);
//mem.WriteByte(op->TextData.Bold);
//mem.WriteByte(op->TextData.Italic);
//mem.WriteByte(op->TextData.CharSpace);
//mem.WriteByte(op->TextData.Font);
//mem.WriteByte(op->TextData.Size);
//mem.WriteASCIIZ(CString(op->TextData.Text));
WriteASCIIZ( Stream_ASCIIZ, CString( op->Text ), TexgenOpDataSize );
break;
case 3: //spline data
for ( TS32 x = 0; x < 4; x++ )
ExportSpline( op->Curves[ x ] );
break;
}
}
void ExportMaterialParameterValue( MATERIALPARAMTYPE& type, MATERIALVALUE& value, CphxGUID& textureGUID )
{
switch ( type )
{
case PARAM_FLOAT: //stored on one byte
WriteByte( Stream_MaterialParam, (TS32)( max( 0, min( 1, value.Float ) ) * 255 ), MaterialParameterDataSize );
break;
case PARAM_COLOR:
for ( TS32 x = 0; x < 4; x++ )
WriteByte( Stream_MaterialParam, (TS32)( max( 0, min( 1, value.Color[ x ] ) ) * 255 ), MaterialParameterDataSize );
break;
case PARAM_ZMODE:
WriteByte( Stream_MaterialParam, value.ZMode, MaterialParameterDataSize );
break;
case PARAM_ZFUNCTION:
WriteByte( Stream_MaterialParam, value.ZFunction, MaterialParameterDataSize );
break;
case PARAM_FILLMODE:
WriteByte( Stream_MaterialParam, value.Wireframe, MaterialParameterDataSize );
break;
case PARAM_CULLMODE:
WriteByte( Stream_MaterialParam, value.CullMode, MaterialParameterDataSize );
break;
case PARAM_RENDERPRIORITY:
WriteByte( Stream_MaterialParam, value.RenderPriority, MaterialParameterDataSize );
break;
case PARAM_BLENDMODE0:
case PARAM_BLENDMODE1:
case PARAM_BLENDMODE2:
case PARAM_BLENDMODE3:
case PARAM_BLENDMODE4:
case PARAM_BLENDMODE5:
case PARAM_BLENDMODE6:
case PARAM_BLENDMODE7:
WriteByte( Stream_MaterialParam, value.BlendMode + 0x11, MaterialParameterDataSize );
break;
case PARAM_TEXTURE0:
case PARAM_TEXTURE1:
case PARAM_TEXTURE2:
case PARAM_TEXTURE3:
case PARAM_TEXTURE4:
case PARAM_TEXTURE5:
case PARAM_TEXTURE6:
case PARAM_TEXTURE7:
case PARAM_RENDERTARGET:
{
CphxTextureOperator_Tool* op = Project.GetTexgenOp( textureGUID );
CphxRenderTarget_Tool* rt = Project.GetRenderTarget( textureGUID );
if ( !op && !rt )
{
//LOG_ERR("[minimalexport] Fatal error: texture/rt not found while exporting material parameter (%s)",Param->TextureGUID.GetString());
//export no texture set
WriteWord( Stream_TextureOperatorReference, 0xffff, MaterialParameterDataSize );
return;
}
if ( op )
{
op = op->GetContentOp();
op->SetRenderRequirement();
if ( !op )
{
LOG_ERR( "[minimalexport] Fatal error: texture content operator empty while exporting material parameter" );
MinimalExportSuccessful = false;
return;
}
WriteWord( Stream_TextureOperatorReference, op->GetResourceIndex(), MaterialParameterDataSize );
}
else
if ( rt )
WriteWord( Stream_TextureOperatorReference, rt->GetResourceIndex() | 0x8000, MaterialParameterDataSize );
}
break;
case PARAM_LTC1:
hasLTC1 = true;
break;
case PARAM_LTC2:
hasLTC2 = true;
break;
}
}
void ExportMaterialParameterValue( CphxMaterialParameter_Tool* Param )
{
MaterialParameterDataCount++;
ExportMaterialParameterValue( Param->Parameter.Type, Param->Parameter.Value, Param->TextureGUID );
}
void ExportMaterialParamDefaultValue( CphxMaterialParameter_Tool* Param )
{
MaterialParameterDataCount++;
ExportMaterialParameterValue( Param->Parameter.Type, Param->DefaultValue, Param->TextureGUID );
}
void ExportMaterialParams( CphxMaterialParameterBatch_Tool& Params, CString& headerPrefix, CStreamWriter& headerFile )
{
SymbolNameScoper sym( "MaterialParameters" );
MaterialParamCount += Params.Parameters.NumItems();
WriteByte( Stream_Main, Params.Parameters.NumItems(), MaterialParamSize ); //param count
for ( TS32 x = 0; x < Params.Parameters.NumItems(); x++ )
{
CphxMaterialParameter_Tool* p = Params.Parameters[ x ];
CString paramName = ToMacroCompatible( p->Name );
headerFile.WriteFormat( "#define PARAM_%s_%-30s %d\n", headerPrefix.GetPointer(), paramName.GetPointer(), x );
WriteByte( Stream_Main, p->Parameter.Scope, MaterialParamSize ); //parameter scope
WriteByte( Stream_Main, p->Parameter.Type, MaterialParamSize ); //parameter type
if ( p->Parameter.Scope == PARAM_CONSTANT )
ExportMaterialParameterValue( p );
/*
else
ExportMaterialParamDefaultValue( p );
*/
};
}
void ExportModelFilters( CphxModelObject_Tool* o, CphxModel_Tool* m )
{
SymbolNameScoper sym( "Filters" );
//filter export
TS32 filtercnt = 0;
for ( TS32 x = 0; x < o->GetFilterCount(); x++ )
if ( o->GetFilter( x )->Enabled )
{
filtercnt++;
ModelFilterCount++;
}
WriteByte( Stream_ModelFilterCount, filtercnt, ModelFilterSize ); //filter count
for ( TS32 x = 0; x < o->GetFilterCount(); x++ )
{
CphxMeshFilter_Tool* f = o->GetFilter( x );
if ( !f->Enabled ) continue;
UsedModelFilters[ f->Filter ]++;
WriteByte( Stream_ModelFilterType, f->Filter, ModelFilterSize ); //filter type
if ( MeshFilterParameterCounts[ f->Filter ] ) //filter parameters
Write( Stream_ModelFilterData, f->Parameters, MeshFilterParameterCounts[ f->Filter ], ModelFilterSize );
switch ( f->Filter )
{
case ModelFilter_UVMap:
{
if ( f->Parameters[ 1 ] & 0xf0 )
hasUVClip = true;
}
case ModelFilter_MapXForm:
Write( Stream_ModelTransformation, f->srt, 20, ModelFilterSize );
break;
case ModelFilter_TintMeshShape:
Write( Stream_ModelTransformation, f->srt, 20, ModelFilterSize );
break;
case ModelFilter_Replicate:
Write( Stream_ModelTransformation, f->srt, 24, ModelFilterSize );
break;
case ModelFilter_TintMesh:
if ( !f->Texture )
{
LOG_ERR( "[minimalexport] Fatal error: mesh tint missing texture on object %s in model %s!", o->GetName().GetPointer(), m->GetName().GetPointer() );
MinimalExportSuccessful = false;
}
else
{
CphxTextureOperator_Tool* t = f->Texture;
t = t->GetContentOp();
if ( !t )
{
LOG_ERR( "[minimalexport] Fatal error: mesh tint load op has no parent on object %s in model %s!", o->GetName().GetPointer(), m->GetName().GetPointer() );
MinimalExportSuccessful = false;
}
else
{
t->SetRenderRequirement();
WriteWord( Stream_TextureOperatorReference, t->GetResourceIndex(), ModelFilterSize );
}
}
break;
default:
break;
}
}
}
void ExportMaterialAndVariables( CphxMaterial_Tool* mat, CString Name )
{
SymbolNameScoper sym( "MaterialParameters" );
if ( !mat || mat == DefaultMaterial )
{
if ( NoMaterial )
mat = NoMaterial;
else
{
LOG_ERR( "[minimalexport] Fatal error: Material not set for '%s'!", Name.GetPointer() );
MinimalExportSuccessful = false;
return;
}
}
WriteByte( Stream_MaterialID, mat->GetResourceIndex(), MaterialDataPackCount ); //material index
for ( TS32 x = 0; x < mat->Techniques.NumItems(); x++ ) //for each tech
{
CphxMaterialTechnique_Tool* t = mat->Techniques[ x ];
for ( TS32 y = 0; y < t->TechParameters.Parameters.NumItems(); y++ ) //tech level params
{
if ( t->TechParameters.Parameters[ y ]->Parameter.Scope == PARAM_VARIABLE )
{
hasTechParams = true;
/*
bool isDefault = t->TechParameters.Parameters[ y ]->IsDefault();
WriteByte( Stream_MaterialParamDefault, isDefault, MaterialParamDefaultCount);
if ( !isDefault )
*/
ExportMaterialParameterValue( t->TechParameters.Parameters[ y ] );
}
}
for ( TS32 y = 0; y < t->Passes.NumItems(); y++ ) //for each pass
{
for ( TS32 z = 0; z < t->Passes[ y ]->PassParameters.Parameters.NumItems(); z++ ) //pass level params
{
if ( t->Passes[ y ]->PassParameters.Parameters[ z ]->Parameter.Scope == PARAM_VARIABLE )
{
/*
bool isDefault = t->Passes[ y ]->PassParameters.Parameters[ z ]->IsDefault();
WriteByte( Stream_MaterialParamDefault, isDefault, MaterialParamDefaultCount );
if ( !isDefault )
*/
ExportMaterialParameterValue( t->Passes[ y ]->PassParameters.Parameters[ z ] );
}
}
}
}
}
void ExportModelObject( CphxModelObject_Tool* o, CphxModel_Tool* m )
{
ModelObjectCount++;
TU8 modelType = o->GetPrimitive();
WriteByte( Stream_ModelPrimitives, modelType, ModelObjectDataSize ); //mesh primitive
UsedModelPrimitives[ o->GetPrimitive() ]++;
D3DXMATRIX mat = o->GetMatrix();
for ( int x = 0; x < 3; x++ )
{
mat.m[ 3 ][ x ] = 0;
mat.m[ x ][ 3 ] = 0;
}
mat.m[ 3 ][ 3 ] = 1;
D3DXMATRIX matinv;
D3DXMatrixTranspose( &matinv, &mat );
D3DXMatrixMultiply( &matinv, &mat, &matinv );
TBOOL ortho = true;
D3DXVECTOR3 v1( matinv.m[ 0 ] );
D3DXVECTOR3 v2( matinv.m[ 1 ] );
D3DXVECTOR3 v3( matinv.m[ 2 ] );
D3DXVec3Normalize( &v1, &v1 );
D3DXVec3Normalize( &v2, &v2 );
D3DXVec3Normalize( &v3, &v3 );
if ( abs( v1.y ) > 0.005 ) ortho = false;
if ( abs( v1.z ) > 0.005 ) ortho = false;
if ( abs( v2.x ) > 0.005 ) ortho = false;
if ( abs( v2.z ) > 0.005 ) ortho = false;
if ( abs( v3.x ) > 0.005 ) ortho = false;
if ( abs( v3.y ) > 0.005 ) ortho = false;
//D3DXMATRIX i;
//D3DXMatrixIdentity( &i );
//TBOOL same = true;
//for ( TS32 x = 0; x < 3; x++ )
// for ( TS32 y = 0; y < 3; y++ )
// if ( abs( matinv.m[ x ][ y ] - i.m[ x ][ y ] ) > 0.001 )
// same = false;
D3DXVECTOR3 scale;
D3DXQUATERNION rotation;
D3DXVECTOR3 translation;
mat = o->GetMatrix();
D3DXMatrixDecompose( &scale, &rotation, &translation, &mat );
TBOOL SaveTransform = true;
TBOOL SaveFullMatrix = true;
TBOOL SaveScale = false;
TBOOL SaveRotation = false;
for ( TS32 x = 9; x < 12; x++ )
if ( o->GetObject()->TransformationF16[ x ] != D3DXFLOAT16( 0 ) )
SaveTransform = true;
if ( ortho )
{
SaveFullMatrix = false;
//LOG_DBG( "MATRIX IS ORTHOGONAL: %f %f %f | %f %f %f | %f %f %f %f", translation.x, translation.y, translation.z, scale.x, scale.y, scale.z, rotation.x, rotation.y, rotation.z, rotation.w );
if ( abs( scale.x - 1 ) > 0.001 || abs( scale.y - 1 ) > 0.001 || abs( scale.z - 1 ) > 0.001 )
SaveScale = true;
if ( abs( rotation.x ) > 0.001 || abs( rotation.y ) > 0.001 || abs( rotation.z ) > 0.001 || abs( rotation.w - 1 ) > 0.001 )
SaveRotation = true;
}
else
{
//LOG_DBG( "-- MATRIX IS NOT ORTHOGONAL: %f %f %f | %f %f %f | %f %f %f %f", translation.x, translation.y, translation.z, scale.x, scale.y, scale.z, rotation.x, rotation.y, rotation.z, rotation.w );
}
//TU8 MatrixData = SaveTransform | ( SaveRotation << 1 ) | ( SaveScale << 2 ) | ( SaveFullMatrix << 3 );
//WriteByte( Stream_ModelParameters, MatrixData, ModelObjectDataSize );
//if ( SaveFullMatrix )
// Write( Stream_ModelTransformation, o->GetObject()->TransformationF16, 18, ModelObjectDataSize ); //orientation
//if ( SaveTransform )
// Write( Stream_ModelTransformation, o->GetObject()->TransformationF16 + 9, 6, ModelObjectDataSize ); //position
//if ( SaveScale )
//{
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)scale.x, 2, ModelObjectDataSize ); //orientation
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)scale.y, 2, ModelObjectDataSize ); //orientation
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)scale.z, 2, ModelObjectDataSize ); //orientation
//}
//if ( SaveRotation )
//{
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)rotation.x, 2, ModelObjectDataSize ); //orientation
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)rotation.y, 2, ModelObjectDataSize ); //orientation
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)rotation.z, 2, ModelObjectDataSize ); //orientation
// Write( Stream_ModelTransformation, &(D3DXFLOAT16)rotation.w, 2, ModelObjectDataSize ); //orientation
//}
Write( Stream_ModelTransformation, o->GetObject()->TransformationF16, 24, ModelObjectDataSize ); //transformation
unsigned char odata = o->GetParameters()[ 0 ];
// Update tree species index parameter
if ( o->GetPrimitive() == Mesh_Tree || o->GetPrimitive() == Mesh_TreeLeaves )
{
CphxTreeSpecies* spec = Project.GetTreeSpecies( o->ParentGUIDS[ 0 ] );
if ( !spec )
{
LOG_ERR( "Minimal Export error: TREE SPECIES NOT SPECIFIED." );
return;
}
o->GetParameters()[ 0 ] = spec->GetResourceIndex();
}
if ( o->GetPrimitive() == Mesh_Text )
o->GetParameters()[ 0 ] = fontList.Find( EngineFontList[ o->GetParameters()[ 0 ] ] );
if ( MeshPrimitiveParameterCounts[ o->GetPrimitive() ] ) //mesh primitive parameters
{
if ( o->GetPrimitive() != Mesh_Scatter )
Write( Stream_ModelParameters, o->GetParameters(), MeshPrimitiveParameterCounts[ o->GetPrimitive() ], ModelObjectDataSize );
else
{
if ( o->GetParameters()[ 3 ] > 0 )
scatterVertexSupport = true;
if ( o->GetParameters()[ 4 ] > 0 )
scatterEdgeSupport = true;
if ( o->GetParameters()[ 5 ] > 0 )
scatterPolySupport = true;
if ( o->GetParameters()[ 5 ] > 0 && o->GetParameters()[ 6 ] > 1 )
scatterPolyMultiSupport = true;
if ( ( (PHXSCATTERORIENTATIONTYPE)o->GetParameters()[ 8 ] ) == flareScatterOrientation_Original )
hasScatterOrientationOriginal = true;
if ( ( (PHXSCATTERORIENTATIONTYPE)o->GetParameters()[ 8 ] ) == flareScatterOrientation_Normal )
hasScatterOrientationNormal = true;