-
Notifications
You must be signed in to change notification settings - Fork 12
/
ModelAndCollisionMapEditor.cs
3045 lines (2524 loc) · 117 KB
/
ModelAndCollisionMapEditor.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SM64DSe.ImportExport;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using SM64DSe.ImportExport.Loaders.InternalLoaders;
using SM64DSe.SM64DSFormats;
using System.Text.RegularExpressions;
using SM64DSe.ImportExport.Writers.InternalWriters;
using System.Drawing.Imaging;
using System.IO;
namespace SM64DSe
{
public partial class ModelAndCollisionMapEditor : Form
{
public enum StartMode
{
ModelAndCollisionMap,
CollisionMap
};
protected enum ModelSourceType
{
External,
Internal,
None
};
protected enum CopyMode
{
Copy,
Cut,
None
};
protected enum CopySourceType
{
Geometry,
PolyList,
None
};
protected class ModelImportationSettings
{
public float m_Scale;
public float m_InGamePreviewScale;
public BMDImporter.BMDExtraImportOptions m_ExtraOptions;
public ModelImportationSettings(float scale, float gameScale, BMDImporter.BMDExtraImportOptions extraOptions)
{
m_Scale = scale;
m_InGamePreviewScale = gameScale;
m_ExtraOptions = extraOptions;
}
public ModelImportationSettings(float gameScale)
: this(1f, gameScale, BMDImporter.BMDExtraImportOptions.DEFAULT) { }
public ModelImportationSettings()
: this(1f) { }
public float GetImportationScale()
{
return m_Scale;
}
public float GetPreviewScale()
{
return m_Scale * m_InGamePreviewScale;
}
}
protected class CollisionMapImportationSettings
{
public float m_Scale;
public float m_InGameModelScale;
public float m_MinimumFaceSize;
public CollisionMapImportationSettings(float scale, float gameScale, float minimumFaceSize)
{
m_Scale = scale;
m_InGameModelScale = gameScale;
m_MinimumFaceSize = minimumFaceSize;
}
public CollisionMapImportationSettings(float gameScale)
: this(1f, gameScale, 0f) { }
public CollisionMapImportationSettings()
: this(1f) { }
public float GetImportationScale()
{
return m_Scale * m_InGameModelScale;
}
public float GetPreviewScale()
{
return GetImportationScale();
}
}
private static readonly string TITLE_MODEL_AND_COLLISION_MAP_IMPORTER = "Model and Collision Map Importer";
private static readonly string TITLE_COLLISION_MAP_EDITOR = "Collision Map Editor";
private StartMode m_StartMode;
private ModelBase m_ModelBase;
private SortedDictionary<string, ModelBase.TextureDefNitro> m_WorkingTexturesCopy;
private SortedDictionary<string, byte[]> m_WorkingPalettesCopy;
private ModelSourceType m_ModelSourceType;
private string m_ModelSourceName;
private bool m_ModelSourceLoaded;
private string m_BMDTargetName;
private string m_KCLTargetName;
private BMD m_ImportedModel;
private KCL m_ImportedCollisionMap;
private ModelImportationSettings m_ModelImportationSettings;
private CollisionMapImportationSettings m_CollisionMapImportationSettings;
private float m_ModelPreviewScale;
private float m_CollisionMapPreviewScale;
private int[] m_BMDDisplayLists;
private int[] m_KCLPickingDisplayLists;
private int[] m_KCLMeshDisplayLists;
private Dictionary<string, int> m_CollisionMapMaterialTypeMap;
private CopyMode m_CopyMode;
private CopySourceType m_CopySourceType;
private ModelBase.BoneDef m_SourceBone;
private List<ModelBase.GeometryDef> m_SourceGeometries;
private List<ModelBase.PolyListDef> m_SourcePolylists;
private ModelBase.GeometryDef m_TargetGeometry;
private ModelBase.BoneDef m_TargetBone;
private bool m_CollisionMapWireFrameView;
private List<KCL.ColFace> m_CollisionMapPlanes;
private KCLLoader.CollisionMapColours m_CollisionMapColours;
private int m_CollisionMapSelectedTriangle;
private OpenFileDialog m_OpenFileDialogue;
private ROMFileSelect m_ROMFileSelect;
private FolderBrowserDialog m_FolderBrowserDialogue;
private SaveFileDialog m_SaveFileDialogue;
private static readonly Regex ASCII_PRINTABLE_NO_SPACE = new Regex("[^!-~]+");
private static readonly byte[] DUMMY_BMD_DATA;
private static readonly NitroFile DUMMY_BMD_NITRO_FILE;
private static readonly BMD DUMMY_BMD;
private static readonly byte[] DUMMY_KCL_DATA;
private static readonly NitroFile DUMMY_KCL_NITRO_FILE;
private static readonly KCL DUMMY_KCL;
static ModelAndCollisionMapEditor()
{
DUMMY_BMD_DATA = new byte[0x30];
DUMMY_BMD_NITRO_FILE = new NitroFile();
DUMMY_BMD_NITRO_FILE.m_ID = 0xFFFF;
DUMMY_BMD_NITRO_FILE.m_Name = "DUMMY_BMD";
DUMMY_BMD_NITRO_FILE.m_Data = DUMMY_BMD_DATA;
DUMMY_BMD = new BMD(DUMMY_BMD_NITRO_FILE);
DUMMY_KCL_DATA = new byte[0x30];
DUMMY_KCL_NITRO_FILE = new NitroFile();
DUMMY_KCL_NITRO_FILE.m_ID = 0xFFFF;
DUMMY_KCL_NITRO_FILE.m_Name = "DUMMY_KCL";
DUMMY_KCL_NITRO_FILE.m_Data = DUMMY_KCL_DATA;
DUMMY_KCL = new KCL(DUMMY_KCL_NITRO_FILE);
}
public ModelAndCollisionMapEditor(
string bmdModelTargetName,
string kclTargetName,
float gameScale,
StartMode startMode
)
{
m_BMDTargetName = bmdModelTargetName;
m_KCLTargetName = kclTargetName;
m_BMDDisplayLists = new int[3]; // Standard, Geometry Highlighting, Skeleton
m_KCLMeshDisplayLists = new int[3]; // Fill, WireFrame, Highlight
m_KCLPickingDisplayLists = new int[1];
m_ModelImportationSettings = new ModelImportationSettings(gameScale);
m_CollisionMapImportationSettings = new CollisionMapImportationSettings(gameScale);
m_ModelPreviewScale = m_ModelImportationSettings.GetPreviewScale();
m_CollisionMapPreviewScale = m_CollisionMapImportationSettings.GetPreviewScale();
m_StartMode = startMode;
m_ModelSourceType = ModelSourceType.None;
m_ModelSourceLoaded = false;
InitialiseForm();
}
public ModelAndCollisionMapEditor(
string bmdModelTargetName,
string kclTargetName,
float gameScale
)
: this(bmdModelTargetName, kclTargetName, gameScale, StartMode.ModelAndCollisionMap) { }
public ModelAndCollisionMapEditor(
string bmdModelTargetName,
string kclTargetName
)
: this(bmdModelTargetName, kclTargetName, 1f) { }
public ModelAndCollisionMapEditor(
StartMode startMode
)
: this(null, null, 1f, startMode) { }
public ModelAndCollisionMapEditor()
: this(null, null) { }
private void InitialiseForm()
{
InitializeComponent();
switch (m_StartMode)
{
default:
case StartMode.ModelAndCollisionMap:
Text = TITLE_MODEL_AND_COLLISION_MAP_IMPORTER;
break;
case StartMode.CollisionMap:
Text = TITLE_COLLISION_MAP_EDITOR;
break;
}
UpdateEnabledStateMenuControls();
if (m_StartMode == StartMode.ModelAndCollisionMap)
{
// Model General Settings
txtModelGeneralTargetName.Text = m_BMDTargetName;
chkModelGeneralStripify.Checked = m_ModelImportationSettings.m_ExtraOptions.m_ConvertToTriangleStrips;
chkModelGeneralKeepVertexOrderDuringStripping.Checked = m_ModelImportationSettings.m_ExtraOptions.m_KeepVertexOrderDuringStripping;
chkModelGeneralAlwaysWriteFullVertexCmd23h.Checked = m_ModelImportationSettings.m_ExtraOptions.m_AlwaysWriteFullVertexCmd23h;
switch (m_ModelImportationSettings.m_ExtraOptions.m_TextureQualitySetting)
{
case BMDImporter.BMDExtraImportOptions.TextureQualitySetting.SmallestSize:
rbModelGeneralTextureAlwaysCompress.Checked = true;
break;
case BMDImporter.BMDExtraImportOptions.TextureQualitySetting.BetterQualityWhereSensible:
rbModelGeneralTextureBetterQualityWhereSensible.Checked = true;
break;
case BMDImporter.BMDExtraImportOptions.TextureQualitySetting.BestQuality:
rbModelGeneralTextureNeverCompress.Checked = true;
break;
}
chkModelGeneralVFlipAllTextures.Checked = m_ModelImportationSettings.m_ExtraOptions.m_VerticallyFlipAllTextures;
txtModelGeneralScale.Text = Helper.ToString4DP(m_ModelImportationSettings.m_Scale);
txtModelPreviewScale.Text = Helper.ToString4DP(m_ModelImportationSettings.m_InGamePreviewScale);
// Model Bones
SetEnabledStateModelBoneBaseControls(false);
SetEnabledStateModelBoneSpecificControls(false);
SetEnabledStateModelBoneGeometryControls(false);
SetEnabledStateModelBonePolylistControls(false);
ResetGeometryDuplicationAndMovementState();
// Model Materials
SetEnabledStateModelMaterialControls(false);
// Model Textures
SetEnabledStateModelTextureControls(false);
// Model Palettes
SetEnabledStateModelPaletteControls(false);
// Model GL
glModelView.Initialise();
glModelView.ProvideDisplayLists(m_BMDDisplayLists);
glModelView.ProvideScaleRef(ref m_ModelPreviewScale);
}
else
{
tcMain.TabPages.Remove(tpgModel);
mnitImport.DropDownItems.Remove(mnitImportModelAndCollisionMap);
mnitImport.DropDownItems.Remove(mnitImportModelOnly);
mnitExport.DropDownItems.Remove(mnitExportModel);
mnitExport.DropDownItems.Remove(mnitExportTextures);
}
// Collision Map General Settings
txtCollisionMapGeneralTargetName.Text = m_KCLTargetName;
txtCollisionMapGeneralFaceSizeThreshold.Text = Helper.ToString4DP(m_CollisionMapImportationSettings.m_MinimumFaceSize);
txtCollisionMapGeneralScale.Text = Helper.ToString4DP(m_CollisionMapImportationSettings.m_Scale);
txtCollisionMapGeneralTargetScale.Text = Helper.ToString4DP(m_CollisionMapImportationSettings.m_InGameModelScale);
// Collision Map Material Collision Types
SetEnabledStateCollisionMapMaterialCollisionTypes(false);
SetEnabledStateCollisionMapPlanes(false);
// Collision Map GL
PopulateCollisionMapPreviewFillModeOptions();
glCollisionMapView.Initialise();
glCollisionMapView.ProvideDisplayLists(m_KCLMeshDisplayLists);
glCollisionMapView.ProvidePickingDisplayLists(m_KCLPickingDisplayLists);
glCollisionMapView.ProvideCallListForDisplayLists(CallListForKCLDisplayLists);
// File selection dialogues
m_OpenFileDialogue = new OpenFileDialog();
m_ROMFileSelect = new ROMFileSelect();
m_FolderBrowserDialogue = new FolderBrowserDialog();
m_FolderBrowserDialogue.SelectedPath = System.IO.Path.GetDirectoryName(Program.m_ROMPath);
m_SaveFileDialogue = new SaveFileDialog();
}
private void ModelAndCollisionMapEditor_Load(object sender, System.EventArgs e)
{
if (m_StartMode == StartMode.CollisionMap && IsKCLTargetSet())
{
m_ModelSourceName = m_KCLTargetName;
m_ModelSourceType = ModelSourceType.Internal;
LoadCollisionMap();
}
}
private void ModelAndCollisionMapEditor_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
if (!m_ModelSourceLoaded) return;
glModelView.PrepareForClose();
glCollisionMapView.PrepareForClose();
if (m_ImportedModel != null) m_ImportedModel.Release();
}
private bool IsBMDTargetSet()
{
return (m_BMDTargetName != null && m_BMDTargetName.Trim().Length > 0);
}
private bool IsKCLTargetSet()
{
return (m_KCLTargetName != null && m_KCLTargetName.Trim().Length > 0);
}
private void LoadModel()
{
m_ModelBase = null;
if (!ModelSourceType.None.Equals(m_ModelSourceType))
{
try
{
m_ModelBase = BMDImporter.LoadModel(m_ModelSourceName);
}
catch (Exception e)
{
new ExceptionMessageBox("Error loading model", e).ShowDialog();
return;
}
}
DeleteDisplayLists();
// BMD
m_ImportedModel = DUMMY_BMD;
m_WorkingTexturesCopy = new SortedDictionary<string, ModelBase.TextureDefNitro>();
m_WorkingPalettesCopy = new SortedDictionary<string, byte[]>();
foreach (KeyValuePair<string, ModelBase.TextureDefBase> textureEntry in m_ModelBase.m_Textures)
{
NitroTexture nitroTexture = BMDWriter.ConvertTexture(0, 0, textureEntry.Value,
m_ModelImportationSettings.m_ExtraOptions.m_TextureQualitySetting, false);
ModelBase.TextureDefNitro textureDefNitroTexture = new ModelBase.TextureDefNitro(nitroTexture);
m_WorkingTexturesCopy.Add(textureDefNitroTexture.GetTexName(), textureDefNitroTexture);
if (textureDefNitroTexture.HasNitroPalette())
{
byte[] palette = textureDefNitroTexture.GetNitroPalette();
byte[] paletteCopy = new byte[palette.Length];
Array.Copy(palette, paletteCopy, palette.Length);
m_WorkingPalettesCopy.Add(textureDefNitroTexture.GetPalName(), paletteCopy);
}
foreach (ModelBase.MaterialDef material in m_ModelBase.m_Materials.Values)
{
if (textureEntry.Key.Equals(material.m_TextureDefID))
{
material.m_TextureDefID = textureDefNitroTexture.GetTexName();
}
}
}
m_ModelBase.m_Textures.Clear();
txtModelGeneralScale.BackColor = Color.White;
txtModelPreviewScale.BackColor = Color.White;
PopulateBoneTree();
PopulateMaterialsList();
PopulateTextureAndPaletteLists();
ResetGeometryDuplicationAndMovementState();
m_ModelSourceLoaded = true;
UpdateBMDModelAndPreview();
// KCL
ResetCollisionMapState();
string extension = Path.GetExtension(m_ModelSourceName);
if (extension != null && extension.ToLower().Equals(".kcl"))
{
m_ImportedCollisionMap = new KCL(Program.m_ROM.GetFileFromName(m_ModelSourceName));
UpdateKCLMapAndPreview(false);
}
else
{
UpdateKCLMapAndPreview(1f);
}
// General
UpdateEnabledStateMenuControls();
lblMainStatus.Text = "Source: " + m_ModelSourceName;
}
private void LoadCollisionMap()
{
LoadModel();
}
private void ResetCollisionMapState()
{
m_ImportedCollisionMap = DUMMY_KCL;
m_CollisionMapMaterialTypeMap = new Dictionary<string, int>();
m_CollisionMapPlanes = new List<KCL.ColFace>();
m_CollisionMapColours = new KCLLoader.CollisionMapColours();
m_CollisionMapSelectedTriangle = -1;
m_CollisionMapWireFrameView = false;
}
protected void CallListForKCLDisplayLists()
{
// Solid polygons
if (!m_CollisionMapWireFrameView)
{
GL.CallList(m_KCLMeshDisplayLists[0]);
}
// WireFrame overlay
GL.CallList(m_KCLMeshDisplayLists[1]);
// Highlighted triangles
GL.CallList(m_KCLMeshDisplayLists[2]);
}
private void DeleteDisplayLists()
{
foreach (int dl in m_BMDDisplayLists)
{
if (dl > 0) GL.DeleteLists(dl, 1);
}
foreach (int dl in m_KCLMeshDisplayLists)
{
if (dl > 0) GL.DeleteLists(dl, 1);
}
foreach (int dl in m_KCLPickingDisplayLists)
{
if (dl > 0) GL.DeleteLists(dl, 1);
}
}
private void PrerenderBMDModel()
{
if (m_BMDDisplayLists[0] == 0)
{
m_BMDDisplayLists[0] = GL.GenLists(1);
}
GL.NewList(m_BMDDisplayLists[0], ListMode.Compile);
GL.PushAttrib(AttribMask.AllAttribBits);
Vector3 previewScale = new Vector3(m_ModelPreviewScale);
if (m_ModelSourceLoaded)
{
GL.Disable(EnableCap.Lighting);
GL.PushMatrix();
GL.Scale(previewScale);
GL.FrontFace(FrontFaceDirection.Ccw);
m_ImportedModel.PrepareToRender();
m_ImportedModel.Render(RenderMode.Opaque, 1f);
m_ImportedModel.Render(RenderMode.Translucent, 1f);
GL.PopMatrix();
}
GL.PopAttrib();
GL.EndList();
}
private void UpdateBMDModelAndPreview()
{
m_ModelBase.m_Textures.Clear();
foreach (KeyValuePair<string, ModelBase.TextureDefNitro> textureEntry in m_WorkingTexturesCopy)
{
m_ModelBase.m_Textures.Add(textureEntry.Key, textureEntry.Value);
}
m_ImportedModel = CallBMDImporter(false);
glModelView.SetShowMarioReference(true);
PrerenderBMDModel();
HighlightSelectedGeometriesAndPolyLists();
DrawSkeleton();
glModelView.Refresh();
}
private BMD CallBMDImporter(bool save = false)
{
Dictionary<string, Dictionary<string, ModelBase.GeometryDef>> originalGeometries =
new Dictionary<string, Dictionary<string, ModelBase.GeometryDef>>();
foreach (ModelBase.BoneDef bone in m_ModelBase.m_BoneTree)
{
Dictionary<string, ModelBase.GeometryDef> boneGeometries = new Dictionary<string, ModelBase.GeometryDef>();
foreach (KeyValuePair<string, ModelBase.GeometryDef> geometryEntry in bone.m_Geometries)
{
boneGeometries[geometryEntry.Key] = DuplicateGeometry(geometryEntry.Value);
}
originalGeometries[bone.m_ID] = boneGeometries;
}
m_ModelBase.ScaleModel(m_ModelImportationSettings.GetImportationScale());
BMD result = BMDImporter.CallBMDWriter(ref m_ImportedModel.m_File,
m_ModelBase, m_ModelImportationSettings.m_ExtraOptions, save);
foreach (ModelBase.BoneDef bone in m_ModelBase.m_BoneTree)
{
foreach (ModelBase.GeometryDef geometry in bone.m_Geometries.Values)
{
foreach (ModelBase.PolyListDef polyList in geometry.m_PolyLists.Values)
{
polyList.m_FaceLists =
originalGeometries[bone.m_ID][geometry.m_ID].m_PolyLists[polyList.m_ID].m_FaceLists;
}
}
}
PopulateMaterialsList();
return result;
}
private KCL CallKCLImporter(float scale, bool preserveIndividualFaceCollisionTypes = true, bool save = false)
{
List<KCL.ColFace> originalPlanes = m_CollisionMapPlanes;
KCL result = KCLImporter.CallKCLWriter(m_ImportedCollisionMap.m_File, m_ModelBase,
scale, m_CollisionMapImportationSettings.m_MinimumFaceSize,
m_CollisionMapMaterialTypeMap, save);
if (preserveIndividualFaceCollisionTypes)
{
for (int i = 0; i < originalPlanes.Count; i++)
{
SetCollisionMapPlaneCollisionType(result, i, originalPlanes[i].type);
}
}
return result;
}
private KCL CallKCLImporter(bool preserveIndividualFaceCollisionTypes = true, bool save = false)
{
return CallKCLImporter(m_CollisionMapImportationSettings.GetImportationScale(), preserveIndividualFaceCollisionTypes, save);
}
private void RefreshBMDScale()
{
m_ModelPreviewScale = m_ModelImportationSettings.GetPreviewScale();
if (m_ModelSourceLoaded)
{
PrerenderBMDModel();
glModelView.Refresh();
}
}
private void RefreshKCLScale()
{
m_CollisionMapPreviewScale = m_CollisionMapImportationSettings.GetPreviewScale();
if (m_ModelSourceLoaded)
{
PrerenderCollisionMap();
glCollisionMapView.Refresh();
}
}
private void PopulateBoneTree()
{
tvModelBonesBones.Nodes.Clear();
Dictionary<string, TreeNode> parentNodes = new Dictionary<string, TreeNode>();
foreach (ModelBase.BoneDef rootBone in m_ModelBase.m_BoneTree.GetRootBones())
{
foreach (ModelBase.BoneDef bone in rootBone.GetBranch())
{
TreeNode node = new TreeNode(bone.m_ID);
if (bone.m_Parent == null)
{
tvModelBonesBones.Nodes.Add(node);
}
else
{
parentNodes[bone.m_Parent.m_ID].Nodes.Add(node);
}
parentNodes[bone.m_ID] = node;
}
}
tvModelBonesBones.ExpandAll();
SetEnabledStateModelBoneBaseControls(true, m_ModelBase.m_BoneTree.Count);
SetEnabledStateModelBoneSpecificControls(false);
SetEnabledStateModelBoneGeometryControls(false);
SetEnabledStateModelBonePolylistControls(false);
txtModelBonesName.Text = null;
txtModelBonesName.BackColor = Color.White;
chkModelBonesSettingsBillboard.Checked = false;
lbxModelBonesGeometries.Items.Clear();
lbxModelBonesPolylists.Items.Clear();
}
private void SetEnabledStateModelBoneBaseControls(bool state, int nBones = -1)
{
btnModelBonesAddBone.Enabled = (nBones > -1) ? (nBones <= ModelBase.BoneDefRoot.MAX_BONE_COUNT) : state;
}
private void SetEnabledStateModelBoneSpecificControls(bool state)
{
btnModelBonesPasteToBone.Enabled = (m_SourcePolylists != null || m_SourceGeometries != null);
btnModelBonesRenameBone.Enabled = state;
txtModelBonesName.Enabled = state;
chkModelBonesSettingsBillboard.Enabled = state;
}
private void SetEnabledStateModelBoneGeometryControls(bool state)
{
btnModelBonesCopyGeometry.Enabled = state;
btnModelBonesCutGeometry.Enabled = state;
btnModelBonesPasteToGeometry.Enabled = (m_SourcePolylists != null);
btnModelBonesRemoveGeometry.Enabled = state;
}
private void SetEnabledStateModelBonePolylistControls(bool state)
{
btnModelBonesCopyPolylist.Enabled = state;
btnModelBonesCutPolylist.Enabled = state;
btnModelBonesRemovePolylist.Enabled = state;
cmbModelBonesPolylistMaterial.Enabled = state;
}
private void PopulateMaterialsList()
{
lbxModelMaterials.Items.Clear();
cmbModelBonesPolylistMaterial.Items.Clear();
foreach (ModelBase.MaterialDef material in m_ModelBase.m_Materials.Values)
{
lbxModelMaterials.Items.Add(material.m_ID);
cmbModelBonesPolylistMaterial.Items.Add(material.m_ID);
}
txtModelMaterialName.Text = null;
chkModelMaterialLight1.Checked = false;
chkModelMaterialLight2.Checked = false;
chkModelMaterialLight3.Checked = false;
chkModelMaterialLight4.Checked = false;
cmbModelMaterialPolygonDrawingFace.SelectedIndex = -1;
cmbModelMaterialPolygonMode.SelectedIndex = -1;
chkModelMaterialWireMode.Checked = false;
chkModelMaterialDepthTestDecal.Checked = false;
chkModelMaterialFog.Checked = false;
chkModelMaterialRenderOnePixelPolygons.Checked = false;
chkModelMaterialFarClipping.Checked = false;
chkModelMaterialShiniessTable.Checked = false;
btnModelMaterialDiffuse.ResetColourButtonValue();
btnModelMaterialAmbient.ResetColourButtonValue();
btnModelMaterialSpecular.ResetColourButtonValue();
btnModelMaterialEmission.ResetColourButtonValue();
nudModelMaterialAlpha.Value = 0;
cmbModelMaterialTextureID.SelectedIndex = -1;
cmbModelMaterialTextureTilingX.SelectedIndex = -1; ;
cmbModelMaterialTextureTilingY.SelectedIndex = -1;
txtModelMaterialTextureScaleX.Text = null;
txtModelMaterialTextureScaleY.Text = null;
txtModelMaterialTextureRotation.Text = null;
txtModelMaterialTextureTranslationX.Text = null;
txtModelMaterialTextureTranslationY.Text = null;
cmbModelMaterialTexGenMode.SelectedIndex = -1;
btnModelMaterialAddMaterial.Enabled = true;
}
private void PopulateMaterialSettings(ModelBase.MaterialDef material)
{
SetEnabledStateModelMaterialControls(true);
bool referencedByBone = false;
foreach (ModelBase.BoneDef bone in m_ModelBase.m_BoneTree.GetAsList())
{
if (bone.m_MaterialsInBranch.Contains(material.m_ID))
{
referencedByBone = true;
break;
}
}
btnModelMaterialRemoveMaterial.Enabled = !referencedByBone;
txtModelMaterialName.Text = material.m_ID;
chkModelMaterialLight1.Checked = material.m_Lights[0];
chkModelMaterialLight2.Checked = material.m_Lights[1];
chkModelMaterialLight3.Checked = material.m_Lights[2];
chkModelMaterialLight4.Checked = material.m_Lights[3];
cmbModelMaterialPolygonDrawingFace.SelectedIndex = (int)material.m_PolygonDrawingFace;
cmbModelMaterialPolygonMode.SelectedIndex = (int)material.m_PolygonMode;
chkModelMaterialWireMode.Checked = material.m_WireMode;
chkModelMaterialDepthTestDecal.Checked = material.m_DepthTestDecal;
chkModelMaterialFog.Checked = material.m_FogFlag;
chkModelMaterialRenderOnePixelPolygons.Checked = material.m_RenderOnePixelPolygons;
chkModelMaterialFarClipping.Checked = material.m_FarClipping;
chkModelMaterialShiniessTable.Checked = material.m_ShininessTableEnabled;
btnModelMaterialDiffuse.Colour = material.m_Diffuse;
btnModelMaterialAmbient.Colour = material.m_Ambient;
btnModelMaterialSpecular.Colour = material.m_Specular;
btnModelMaterialEmission.Colour = material.m_Emission;
nudModelMaterialAlpha.Value = material.m_Alpha;
bool hasTexture = (material.m_TextureDefID != null);
chkModelMaterialTextureEnabled.Checked = hasTexture;
if (hasTexture)
{
cmbModelMaterialTextureID.SelectedIndex = cmbModelMaterialTextureID.Items.IndexOf(material.m_TextureDefID);
}
cmbModelMaterialTextureTilingX.SelectedIndex = (int)material.m_TexTiling[0];
cmbModelMaterialTextureTilingY.SelectedIndex = (int)material.m_TexTiling[1];
txtModelMaterialTextureScaleX.Text = Helper.ToString4DP(material.m_TextureScale.X);
txtModelMaterialTextureScaleY.Text = Helper.ToString4DP(material.m_TextureScale.Y);
txtModelMaterialTextureRotation.Text = Helper.ToString4DP(material.m_TextureRotation);
txtModelMaterialTextureTranslationX.Text = Helper.ToString4DP(material.m_TextureTranslation.X);
txtModelMaterialTextureTranslationY.Text = Helper.ToString4DP(material.m_TextureTranslation.Y);
cmbModelMaterialTexGenMode.SelectedIndex = (int)material.m_TexGenMode;
SetEnabledStateModelMaterialTextureSettings(hasTexture);
}
private void ResetMaterialTextureSettings()
{
cmbModelMaterialTextureID.SelectedIndex = -1;
cmbModelMaterialTextureTilingX.SelectedIndex = -1;
cmbModelMaterialTextureTilingY.SelectedIndex = -1;
txtModelMaterialTextureScaleX.Text = null;
txtModelMaterialTextureScaleY.Text = null;
txtModelMaterialTextureRotation.Text = null;
txtModelMaterialTextureTranslationX.Text = null;
txtModelMaterialTextureTranslationY.Text = null;
cmbModelMaterialTexGenMode.SelectedIndex = -1;
}
private void SetEnabledStateModelMaterialTextureSettings(bool state)
{
cmbModelMaterialTextureID.Enabled = state;
cmbModelMaterialTextureTilingX.Enabled = state;
cmbModelMaterialTextureTilingY.Enabled = state;
txtModelMaterialTextureScaleX.Enabled = state;
txtModelMaterialTextureScaleY.Enabled = state;
txtModelMaterialTextureRotation.Enabled = state;
txtModelMaterialTextureTranslationX.Enabled = state;
txtModelMaterialTextureTranslationY.Enabled = state;
cmbModelMaterialTexGenMode.Enabled = state;
}
private void SetEnabledStateModelMaterialControls(bool state)
{
btnModelMaterialAddMaterial.Enabled = state;
if (btnModelMaterialRemoveMaterial.Enabled)
{
btnModelMaterialRemoveMaterial.Enabled = state;
}
btnModelMaterialRenameMaterial.Enabled = state;
txtModelMaterialName.Enabled = state;
chkModelMaterialLight1.Enabled = state;
chkModelMaterialLight2.Enabled = state;
chkModelMaterialLight3.Enabled = state;
chkModelMaterialLight4.Enabled = state;
cmbModelMaterialPolygonDrawingFace.Enabled = state;
cmbModelMaterialPolygonMode.Enabled = state;
chkModelMaterialWireMode.Enabled = state;
chkModelMaterialDepthTestDecal.Enabled = state;
chkModelMaterialFog.Enabled = state;
chkModelMaterialRenderOnePixelPolygons.Enabled = state;
chkModelMaterialFarClipping.Enabled = state;
chkModelMaterialShiniessTable.Enabled = state;
btnModelMaterialDiffuse.Enabled = state;
btnModelMaterialAmbient.Enabled = state;
btnModelMaterialSpecular.Enabled = state;
btnModelMaterialEmission.Enabled = state;
nudModelMaterialAlpha.Enabled = state;
chkModelMaterialTextureEnabled.Enabled = state;
SetEnabledStateModelMaterialTextureSettings(state);
btnModelMaterialApplySettings.Enabled = state;
}
private void PopulateTextureAndPaletteLists()
{
lbxModelTextures.Items.Clear();
cmbModelMaterialTextureID.Items.Clear();
lbxModelPalettes.Items.Clear();
cmbModelTexturesPalette.Items.Clear();
txtModelTexturesName.Text = null;
txtModelTexturesName.BackColor = Color.White;
txtModelPalettesName.Text = null;
txtModelPalettesName.BackColor = Color.White;
foreach (ModelBase.TextureDefBase texture in m_WorkingTexturesCopy.Values)
{
lbxModelTextures.Items.Add(texture.m_ID);
cmbModelMaterialTextureID.Items.Add(texture.m_ID);
}
foreach (string paletteID in m_WorkingPalettesCopy.Keys)
{
lbxModelPalettes.Items.Add(paletteID);
cmbModelTexturesPalette.Items.Add(paletteID);
}
pbxModelTexturesPreview.Image = null;
pbxModelTexturesPreview.Refresh();
gridModelPalettesPaletteColours.ClearColours();
btnModelPalettesSelectedColour.ResetColourButtonValue();
SetEnabledStateModelTextureControls(false);
SetEnabledStateModelPaletteControls(false);
}
private void btnModelGeneralSelectTarget_Click(object sender, EventArgs e)
{
m_ROMFileSelect.ReInitialize("Select a model (BMD) file to replace", new String[] { ".bmd" });
DialogResult result = m_ROMFileSelect.ShowDialog();
if (result == DialogResult.OK)
{
m_BMDTargetName = m_ROMFileSelect.m_SelectedFile;
txtModelGeneralTargetName.Text = m_BMDTargetName;
UpdateEnabledStateMenuControls();
}
}
private void UpdateEnabledStateMenuControls()
{
mnitLoad.Enabled = true;
mnitLoadExternalModel.Enabled = true;
mnitLoadInternalModelCollisionMap.Enabled = true;
mnitLoadRevertChanges.Enabled = m_ModelSourceLoaded;
mnitImport.Enabled = m_ModelSourceLoaded;
mnitImportModelAndCollisionMap.Enabled = (IsBMDTargetSet() && IsKCLTargetSet());
mnitImportModelOnly.Enabled = IsBMDTargetSet();
mnitImportCollisionMapOnly.Enabled = IsKCLTargetSet();
mnitExport.Enabled = m_ModelSourceLoaded;
bool modelAvailable = m_ModelSourceLoaded && m_StartMode != StartMode.CollisionMap;
mnitExportModel.Enabled = modelAvailable;
mnitExportCollisionMap.Enabled = m_ModelSourceLoaded;
mnitExportTextures.Enabled = modelAvailable;
}
private void mnitLoadExternalModel_Click(object sender, EventArgs e)
{
m_OpenFileDialogue.Title = "Select a model";
m_OpenFileDialogue.Filter = Strings.MODEL_FORMATS_FILTER;
if (m_OpenFileDialogue.ShowDialog(this) == DialogResult.OK)
{
m_ModelSourceName = m_OpenFileDialogue.FileName;
m_ModelSourceType = ModelSourceType.External;
LoadModel();
}
}
private void mnitLoadInternalModelCollisionMap_Click(object sender, EventArgs e)
{
m_ROMFileSelect.ReInitialize("Select a model (BMD) or collision map (KCL) to load", new String[] { ".bmd",".kcl" });
DialogResult result = m_ROMFileSelect.ShowDialog();
if (result == DialogResult.OK)
{
m_ModelSourceName = m_ROMFileSelect.m_SelectedFile;
m_ModelSourceType = ModelSourceType.Internal;
LoadModel();
}
}
private void mnitLoadRevertChanges_Click(object sender, EventArgs e)
{
if (m_ModelSourceLoaded)
{
LoadModel();
}
}
private void tcModelSettings_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (m_ModelSourceLoaded)
{
UpdateBMDModelAndPreview();
}
}
private bool IsModelBonesTabSelected()
{
if (tcModelSettings.SelectedTab == null) return false;
return "Model.Bones".Equals(tcModelSettings.SelectedTab.Tag);
}
private void lbxModelMaterials_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbxModelMaterials.SelectedIndex > -1)
{
string materialID = lbxModelMaterials.SelectedItem.ToString();
ModelBase.MaterialDef material = m_ModelBase.m_Materials[materialID];
PopulateMaterialSettings(material);
}
}
private void btnModelMaterialDiffuse_Click(object sender, EventArgs e)
{
btnModelMaterialDiffuse.SelectColour();
}
private void btnModelMaterialAmbient_Click(object sender, EventArgs e)
{
btnModelMaterialAmbient.SelectColour();
}
private void btnModelMaterialSpecular_Click(object sender, EventArgs e)
{
btnModelMaterialSpecular.SelectColour();
}
private void btnModelMaterialEmission_Click(object sender, EventArgs e)
{
btnModelMaterialEmission.SelectColour();
}
private void chkModelMaterialWireMode_CheckedChanged(object sender, EventArgs e)
{
nudModelMaterialAlpha.Enabled = !chkModelMaterialWireMode.Checked;
}
private void chkModelMaterialTextureEnabled_CheckedChanged(object sender, EventArgs e)
{
SetEnabledStateModelMaterialTextureSettings(chkModelMaterialTextureEnabled.Checked);