summaryrefslogtreecommitdiff
path: root/NW4RTools/ObjImporter.cs
blob: 81fcf81d130b7ffecc79a7a34f8af1bf75ba871c (plain)
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
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using NW4RTools.Models;

namespace NW4RTools {
	public class ObjImporter {
		public static void ImportModel(string basePath, TextReader tr, ResFile file, string modelName) {
			new ObjImporter(basePath, file, tr).ImportModel(modelName);
		}



		private const bool Lightmaps = true;

		string BasePath;
		ResFile CurrentFile;
		Models.Model CurrentModel;
		TextReader Input;

		List<float[]> Positions;
		List<float[]> Normals;
		List<float[]> TexCoords;

		Vec3 CurrentMinimum;
		Vec3 CurrentMaximum;

		private ObjImporter(string basePath, ResFile file, TextReader tr) {
			BasePath = basePath;
			CurrentFile = file;
			Input = tr;
		}

		public void ImportModel(string modelName) {
			var modelGroup = CurrentFile.CreateModelGroup();
			var texGroup = CurrentFile.CreateTextureGroup();

			// OK, so here's what I'm going to do:
			// I'll read the file into an internal array and process commands as they come.

			// Vertex position/normal/texcoord (v, vn, vt):
			// -- These will be added into a list.

			// Faces (f):
			// -- These will be added into a list for the appropriate group.

			// Set group (g):
			// -- A shape will be created for this group. It will be added to DrawOpa.
			// -- TODO: Add DrawXlu handling.

			// Load material library (mtllib):
			// -- The specified file will be loaded.
			// -- All materials in it (and their associated textures) will be added to the model.

			// Set current material (usemtl):
			// -- A different material will be assigned for the current shape.

			if (Lightmaps) {
				var lm01m = new Texture();
				
				lm01m.Images = new System.Drawing.Bitmap[1];
				lm01m.Images[0] = new System.Drawing.Bitmap(Path.Combine(BasePath, "images/lm_01m.png"));
				
				lm01m.Format = TextureFormat.I8;

				var lm02m = new Texture();

				lm02m.Images = new System.Drawing.Bitmap[1];
				lm02m.Images[0] = new System.Drawing.Bitmap(Path.Combine(BasePath, "images/lm_02m.png"));

				lm02m.Format = TextureFormat.I8;

				CurrentFile.GetTextureGroup().Add("lm_01m", lm01m);
				CurrentFile.GetTextureGroup().Add("lm_02m", lm02m);
			}



			CurrentModel = new Model();
			modelGroup.Add(modelName, CurrentModel);

			// Before we start reading the OBJ file, prepare the model
			CurrentModel.ScaleMode = Model.ScaleModeType.Standard;
			CurrentModel.TexMatrixMode = Model.TexMatrixModeType.Maya;
			CurrentModel.UsesNrmMtxArray = true;
			// why?

			// Todo: vertex count, triangle count
			// Minimum, Maximum will be calc'd later
			CurrentMinimum = new Vec3(0, 0, 0);
			CurrentMaximum = new Vec3(0, 0, 0);

			// Create one node
			// Default settings from test_lift.brres
			var newNode = new Node();
			newNode.Flags = 0x31F;
			newNode.Scale = new Vec3(1, 1, 1);
			newNode.BoxMin = new Vec3(-16.0f, -16.0f, 0.0f);
			newNode.BoxMax = new Vec3(16.0f, 16.0f, 0.0f);
			
			newNode.NodeMatrix.v00 = 1;
			newNode.NodeMatrix.v01 = 0;
			newNode.NodeMatrix.v02 = 0;
			newNode.NodeMatrix.v03 = 0;
			newNode.NodeMatrix.v10 = 0;
			newNode.NodeMatrix.v11 = 1;
			newNode.NodeMatrix.v12 = 0;
			newNode.NodeMatrix.v13 = 0;
			newNode.NodeMatrix.v20 = 0;
			newNode.NodeMatrix.v21 = 0;
			newNode.NodeMatrix.v22 = 1;
			newNode.NodeMatrix.v23 = 0;
			
			newNode.NodeInvMatrix.v00 = 1;
			newNode.NodeInvMatrix.v01 = -0;
			newNode.NodeInvMatrix.v02 = 0;
			newNode.NodeInvMatrix.v03 = 0;
			newNode.NodeInvMatrix.v10 = -0;
			newNode.NodeInvMatrix.v11 = 1;
			newNode.NodeInvMatrix.v12 = -0;
			newNode.NodeInvMatrix.v13 = 0;
			newNode.NodeInvMatrix.v20 = 0;
			newNode.NodeInvMatrix.v21 = -0;
			newNode.NodeInvMatrix.v22 = 1;
			newNode.NodeInvMatrix.v23 = 0;

			CurrentModel.Nodes.Add("RootNode", newNode);

			// Map it correctly
			CurrentModel.MatrixIDtoNodeID = new int[1];
			CurrentModel.MatrixIDtoNodeID[0] = 0;

			// Now put together the NodeTree
			var nodeTreeInsn = new ByteCode.AssignNodeToParentMtxInstruction();
			nodeTreeInsn.NodeID = 0;
			nodeTreeInsn.ParentMatrixID = 0;

			var nodeTreeEndInsn = new ByteCode.DoneInstruction();

			var nodeTree = new ByteCode();
			nodeTree.Instructions.Add(nodeTreeInsn);
			nodeTree.Instructions.Add(nodeTreeEndInsn);

			CurrentModel.Bytecode.Add("NodeTree", nodeTree);

			// Also, DrawOpa
			var drawOpa = new ByteCode();
			CurrentModel.Bytecode.Add("DrawOpa", drawOpa);

			// Initial setup is done, let's go!
			Positions = new List<float[]>();
			Normals = new List<float[]>();
			TexCoords = new List<float[]>();

			string line;

			while ((line = Input.ReadLine()) != null) {
				line = line.Trim();

				if (line.Length == 0 || line[0] == '#')
					continue;

				var parsed = line.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);

				switch (parsed[0]) {
				case "mtllib":
					LoadMaterialLibrary(string.Join(" ", parsed, 1, parsed.Length - 1));
					break;
				case "v":
					Positions.Add(ParseFloatArray(parsed, 1, 3));
					break;
				case "vn":
					Normals.Add(ParseFloatArray(parsed, 1, 3));
					break;
				case "vt":
					var vtEntry = ParseFloatArray(parsed, 1, 2);
					vtEntry[1] = 1.0f - vtEntry[1];
					TexCoords.Add(vtEntry);
					break;
				case "f":
					AddFace(parsed);
					break;
				case "g":
					Console.WriteLine("Beginning shape {0}", parsed[1]);
					BeginShape(parsed[1]);
					break;
				case "usemtl":
					Console.WriteLine("Setting material {0}", parsed[1]);
					SetMaterial(CurrentModel.Materials[parsed[1]]);
					break;
				default:
					Console.WriteLine("Unhandled OBJ command: {0}", parsed[0]);
					break;
				}
			}

			EndShape();

			// Parsing is finished. Let's finish up DrawOpa
			drawOpa.Instructions.Add(new ByteCode.DoneInstruction());
		}


		private float[] ParseFloatArray(string[] src, int index) {
			return ParseFloatArray(src, index, src.Length - index);
		}

		private float[] ParseFloatArray(string[] src, int index, int count) {
			var output = new float[count];

			for (int i = 0; i < count; i++) {
				output[i] = float.Parse(src[i + index]);
			}

			return output;
		}


		#region Materials
		private void LoadMaterialLibrary(string libPath) {
			string realPath = Path.Combine(BasePath, libPath);
			Console.WriteLine("Loading material library from {0}", realPath);

			var reader = File.OpenText(realPath);
			string line;
			Material currentMaterial = null;

			while ((line = reader.ReadLine()) != null) {
				line = line.Trim();

				if (line.Length == 0 || line[0] == '#')
					continue;

				var parsed = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

				switch (parsed[0]) {
				case "newmtl":
					// Create a new material and initialise everything
					// Default settings taken from Test_Lift material
					currentMaterial = new Material();

					currentMaterial.TexCoordGenCount = 1;
					currentMaterial.ChanCount = 1;
					currentMaterial.TevStageCount = 2;
					currentMaterial.IndStageCount = 0;

					// This might need changing
					currentMaterial.CullMode = 2;

					currentMaterial.SRTSettings[0] = new SRTSettingInfo();

					currentMaterial.ChanCtrls[0] = new ChanCtrl();
					currentMaterial.ChanCtrls[0].Flags = 0x3F;
					currentMaterial.ChanCtrls[0].MatColor.Rgba = 0xFFFFFFFF;
					currentMaterial.ChanCtrls[0].AmbColor.Rgba = 0xFFFFFFFF;
					currentMaterial.ChanCtrls[0].FlagC = 0x702;
					currentMaterial.ChanCtrls[0].FlagA = 0x700;

					currentMaterial.ChanCtrls[1] = new ChanCtrl();
					currentMaterial.ChanCtrls[1].Flags = 0xF;
					currentMaterial.ChanCtrls[1].MatColor.Rgba = 0x000000FF;
					currentMaterial.ChanCtrls[1].AmbColor.Rgba = 0x00000000;
					currentMaterial.ChanCtrls[1].FlagC = 0;
					currentMaterial.ChanCtrls[1].FlagA = 0;

					currentMaterial.LightSetID = 1;

					// Default display lists, taken from test_lift
					var pixDL = new DisplayListWriter();
					pixDL.LoadBPReg(0xF31EFF80);
					pixDL.LoadBPReg(0x40000017);
					pixDL.LoadBPReg(0xFE00FFE3);
					pixDL.LoadBPReg(0x410034A0);
					pixDL.LoadBPReg(0x42000000);
					pixDL.End();

					currentMaterial.PixDL = pixDL.GetBuffer();

					var tevColorDL = new DisplayListWriter();
					tevColorDL.LoadBPReg(0xE20000FF);
					tevColorDL.LoadBPReg(0xE30FF0FF);
					tevColorDL.LoadBPReg(0xE30FF0FF);
					tevColorDL.LoadBPReg(0xE30FF0FF);
					tevColorDL.LoadBPReg(0xE4000000);
					tevColorDL.LoadBPReg(0xE5000000);
					tevColorDL.LoadBPReg(0xE5000000);
					tevColorDL.LoadBPReg(0xE5000000);
					tevColorDL.LoadBPReg(0xE6000000);
					tevColorDL.LoadBPReg(0xE7000000);
					tevColorDL.LoadBPReg(0xE7000000);
					tevColorDL.LoadBPReg(0xE7000000);
					tevColorDL.AddPadding(4);
					tevColorDL.LoadBPReg(0xE0800000);
					tevColorDL.LoadBPReg(0xE1800000);
					tevColorDL.LoadBPReg(0xE2800000);
					tevColorDL.LoadBPReg(0xE3800000);
					tevColorDL.LoadBPReg(0xE4800000);
					tevColorDL.LoadBPReg(0xE5800000);
					tevColorDL.LoadBPReg(0xE6800000);
					tevColorDL.LoadBPReg(0xE7800000);
					tevColorDL.AddPadding(24);
					tevColorDL.End();

					currentMaterial.TevColorDL = tevColorDL.GetBuffer();

					var indMtxAndScaleDL = new DisplayListWriter();
					indMtxAndScaleDL.LoadBPReg(0x25000000);
					indMtxAndScaleDL.LoadBPReg(0x26000000);
					indMtxAndScaleDL.AddPadding(54);
					indMtxAndScaleDL.End();

					currentMaterial.IndMtxAndScaleDL = indMtxAndScaleDL.GetBuffer();

					var texCoordGenDL = new DisplayListWriter();
					texCoordGenDL.LoadXFReg(0x1040, new byte[] { 0x00, 0x00, 0x52, 0x80 });
					texCoordGenDL.LoadXFReg(0x1050, new byte[] { 0x00, 0x00, 0x00, 0x00 });
					texCoordGenDL.AddPadding(110);
					texCoordGenDL.End();

					currentMaterial.TexCoordGenDL = texCoordGenDL.GetBuffer();

					CurrentModel.Materials.Add(parsed[1], currentMaterial);


					Shader thisShader = CreateShader();
					CurrentModel.Shaders.Add(parsed[1], thisShader);
					currentMaterial.ShaderRef = thisShader;

					break;

				case "map_Kd":
					var rawTexName = string.Join(" ", parsed, 1, parsed.Length - 1);
					// TODO: fix this to use the mtllib path
					var texPath = Path.Combine(BasePath, rawTexName);
					var texName = Path.GetFileNameWithoutExtension(texPath);

					if (!CurrentFile.GetTextureGroup().ContainsKey(texName))
						AddTexture(texName, texPath);

					var texInfo = new TextureInfo();
					texInfo.TextureName = texName;

					texInfo.WrapS = TextureWrapType.REPEAT;
					texInfo.WrapT = TextureWrapType.REPEAT;

					texInfo.MinFilt = 1;
					texInfo.MagFilt = 1;

					currentMaterial.TextureInfos.Add(texInfo);

					break;


				}
			}
		}


		private void AddTexture(string texName, string texPath) {
			var newTexture = new Texture();

			newTexture.Images = new System.Drawing.Bitmap[1];
			newTexture.Images[0] = new System.Drawing.Bitmap(texPath);

			newTexture.Format = TextureFormat.RGB5A3;

			CurrentFile.GetTextureGroup().Add(texName, newTexture);

			// TODO: Texture/Material pairing lookups
		}


		private Shader CreateShader() {
			var shader = new Shader();

			shader.TevStageCount = 2;
			shader.Unk1 = 0x00FFFFFF;
			shader.Unk2 = 0xFFFFFFFF;

			var dl = new DisplayListWriter();
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xF6000004);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xF700000E);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xF8000000);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xF900000C);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xFA000005);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xFB00000D);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xFC00000A);
			dl.LoadBPReg(0xFE00000F);
			dl.LoadBPReg(0xFD00000E);
			dl.LoadBPReg(0x27FFFFFF);
			dl.AddPadding(11);
			dl.LoadBPReg(0xFEFFFFF0);
			dl.LoadBPReg(0xF6E338C0);
			dl.LoadBPReg(0x2803F040);
			dl.LoadBPReg(0xC008F8AF);
			dl.LoadBPReg(0xC208F20F);
			dl.LoadBPReg(0xC108F2F0);
			dl.LoadBPReg(0xC3081FF0);
			dl.LoadBPReg(0x10000000);
			dl.LoadBPReg(0x11000000);
			dl.PadToSize(0x1E0);
			dl.End();

			shader.DisplayList = dl.GetBuffer();

			return shader;
		}
		#endregion



		#region Shapes
		Shape CurrentShape;
		Material CurrentShapeMaterial;

		List<ushort[]> Quads;
		List<ushort[]> Triangles;

		BitArray UsedVertices;
		BitArray UsedNormals;
		BitArray UsedTexCoords;

		private void BeginShape(string name) {
			if (CurrentShape != null)
				EndShape();

			CurrentShape = new Shape();
			CurrentShape.Unk = new byte[] { 0, 0, 0x14, 0, 0, 0, 0, 0x02, 0,
			0, 0, 0x14 };
			CurrentShape.DataFlags = 0x2600;
			CurrentShape.ExtraData = new ushort[0];

			CurrentModel.Shapes.Add(name, CurrentShape);

			Quads = new List<ushort[]>();
			Triangles = new List<ushort[]>();

			UsedVertices = new BitArray(Positions.Count);
			UsedTexCoords = new BitArray(TexCoords.Count);
			UsedNormals = new BitArray(Normals.Count);
		}


		private void SetMaterial(Material mat) {
			CurrentShapeMaterial = mat;
		}


		private void AddFace(string[] cmd) {
			int vtxCount = cmd.Length - 1;
			if (vtxCount != 3 && vtxCount != 4) {
				throw new NotSupportedException(string.Format("cannot deal with a {0} vertices primitive", vtxCount));
			}

			var output = new ushort[vtxCount * 3];

			for (int i = 0; i < vtxCount; i++) {
				var s = cmd[i + 1];

				int pos1 = s.IndexOf('/');
				int pos2 = s.IndexOf('/', pos1 + 1);

				int vnum = int.Parse(s.Substring(0, pos1)) - 1;
				int tcnum = int.Parse(s.Substring(pos1 + 1, pos2 - pos1 - 1)) - 1;
				int nnum = int.Parse(s.Substring(pos2 + 1)) - 1;

				UsedVertices[vnum] = true;
				UsedTexCoords[tcnum] = true;
				UsedNormals[nnum] = true;

				output[i * 3] = (ushort)vnum;
				output[i * 3 + 1] = (ushort)nnum;
				output[i * 3 + 2] = (ushort)tcnum;
			}

			if (vtxCount == 3)
				Triangles.Add(output);
			else
				Quads.Add(output);
		}


		private void EndShape() {
			// Let's assemble the display lists.

			// ** Reverse Engineering DL 1 **
			// 0x0A : CP command: Vertex Descriptor part 1
			// 0x10 : CP command: Vertex Descriptor part 2
			// 0x16 : XF command: Address 0x1008 ["XFMEM_VTXSPECS"], Transfer: 0
			// 0x1F : Padding: NOP
			// 0x20 : CP command: Vertex Attribute Format part 1
			// 0x26 : CP command: Vertex Attribute Format part 2
			// 0x2C : CP command: Vertex Attribute Format part 3
			// 0x32 : Dynamic CP command: Position Array Pointer
			// 0x38 : Dynamic CP command: Position Array Stride
			// 0x3E : Dynamic CP command: Normal Array Pointer
			// 0x44 : Dynamic CP command: Normal Array Pointer
			// 0x4A : Dynamic CP command: Colour 0 Array Pointer
			// 0x50 : Dynamic CP command: Colour 0 Array Stride
			// 0x56 : Dynamic CP command: Colour 1 Array Pointer
			// 0x5C : Dynamic CP command: Colour 1 Array Stride
			// 0x62 : Dynamic CP command: TexCoord 0 Array Pointer
			// 0x68 : Dynamic CP command: TexCoord 0 Array Stride
			// 0x6E : Dynamic CP command: TexCoord 1 Array Pointer
			// 0x74 : Dynamic CP command: TexCoord 1 Array Stride
			// 0x7A : Dynamic CP command: TexCoord 2 Array Pointer
			// 0x80 : Dynamic CP command: TexCoord 2 Array Stride
			// 0x86 : Dynamic CP command: TexCoord 3 Array Pointer
			// 0x8C : Dynamic CP command: TexCoord 3 Array Stride
			// 0x92 : Dynamic CP command: TexCoord 4 Array Pointer
			// 0x98 : Dynamic CP command: TexCoord 4 Array Stride
			// 0x9E : Dynamic CP command: TexCoord 5 Array Pointer
			// 0xA4 : Dynamic CP command: TexCoord 5 Array Stride
			// 0xAA : Dynamic CP command: TexCoord 6 Array Pointer
			// 0xB0 : Dynamic CP command: TexCoord 6 Array Stride
			// 0xB6 : Dynamic CP command: TexCoord 7 Array Pointer
			// 0xBC : Dynamic CP command: TexCoord 7 Array Stride

			// Now create vertex settings
			var vs = new VertexSettings();
			vs.PositionDesc = VertexSettings.DescType.Index16;
			vs.PositionFormat = VertexSettings.CompType.Float32;
			vs.PositionCount = VertexSettings.CompCount.Position3;

			vs.TexCoordDesc[0] = VertexSettings.DescType.Index16;
			vs.TexCoordFormat[0] = VertexSettings.CompType.Float32;
			vs.TexCoordCount[0] = VertexSettings.CompCount.TexCoord2;

			vs.NormalDesc = VertexSettings.DescType.Index16;
			vs.NormalFormat = VertexSettings.CompType.Float32;
			vs.NormalCount = VertexSettings.CompCount.Normal3;

			uint vd1, vd2, vat1, vat2, vat3;
			vs.GetDesc(out vd1, out vd2);
			vs.GetAttrFmt(out vat1, out vat2, out vat3);

			// I might need to create XFMEM_VTXSPECS...
			// test_lift uses 0x14. According to Dolphin's src, this means:
			// numcolors = 0, numnormals = 1 (just normal), numtextures = 1. Makes sense.
			byte vtxSpecs = 0 | (1 << 2) | (1 << 4);

			var dl1 = new DisplayListWriter();
			dl1.AddPadding(10);
			dl1.LoadCPReg(0x50, vd1);
			dl1.LoadCPReg(0x60, vd2);
			dl1.LoadXFReg(0x1008, new byte[] { 0x00, 0x00, 0x00, vtxSpecs });
			dl1.Nop();
			dl1.LoadCPReg(0x70, vat1);
			dl1.LoadCPReg(0x80, vat2);
			dl1.LoadCPReg(0x90, vat3);

			// extend it
			// should it be bigger if more texcoords are used? maybe...
			dl1.PadToSize(0x80);
			dl1.End();

			CurrentShape.DLBufferSize1 = 0xE0;
			CurrentShape.DisplayList1 = dl1.GetBuffer();


			// Display List 2 is where all the primitive-related fun happens
			// However, before we do that, let's compute the vertex data arrays
			ushort[] posIndexes, texCoordIndexes, normalIndexes;

			var posDataArray = ComputeVertexDataArray(Positions, UsedVertices, out posIndexes);
			var tcDataArray = ComputeVertexDataArray(TexCoords, UsedTexCoords, out texCoordIndexes);
			var nrmDataArray = ComputeVertexDataArray(Normals, UsedNormals, out normalIndexes);

			// todo: better names
			var posData = new VertexPosData();
			posData.ComponentCount = VertexSettings.CompCount.Position3;
			posData.ComponentType = VertexSettings.CompType.Float32;
			posData.EntryCount = (ushort)posDataArray.Length;
			posData.EntrySize = 12;
			posData.Fraction = 0;
			posData.Data = posDataArray;
			posData.Save();

			CurrentModel.VtxPosData.Add("P_" + CurrentModel.VtxPosData.Count.ToString(), posData);

			var tcData = new VertexTexCoordData();
			tcData.ComponentCount = VertexSettings.CompCount.TexCoord2;
			tcData.ComponentType = VertexSettings.CompType.Float32;
			tcData.EntryCount = (ushort)tcDataArray.Length;
			tcData.EntrySize = 8;
			tcData.Fraction = 0;
			tcData.Data = tcDataArray;
			tcData.Save();

			CurrentModel.VtxTexCoordData.Add("T_" + CurrentModel.VtxTexCoordData.Count.ToString(), tcData);

			var nrmData = new VertexNrmData();
			nrmData.ComponentCount = VertexSettings.CompCount.Normal3;
			nrmData.ComponentType = VertexSettings.CompType.Float32;
			nrmData.EntryCount = (ushort)nrmDataArray.Length;
			nrmData.EntrySize = 12;
			nrmData.Fraction = 0;
			nrmData.Data = nrmDataArray;
			nrmData.Save();

			CurrentModel.VtxNrmData.Add("N_" + CurrentModel.VtxNrmData.Count.ToString(), nrmData);

			CurrentShape.PosData = posData;
			CurrentShape.TexCoordData[0] = tcData;
			CurrentShape.NrmData = nrmData;

			var dl = new DisplayListWriter();

			// face writing here is reversed because the wind order seems to be wrong
			// dunno if it applies to models exported from everything, or just Maya
			// or maybe it's a setting specified in one of the structs

			if (Triangles.Count > 0) {
				dl.BeginPrimitives(PrimitiveType.Triangles, 0, (ushort)(Triangles.Count * 3));

				foreach (var tri in Triangles) {
					dl.WriteUInt16(posIndexes[tri[6]]);
					dl.WriteUInt16(normalIndexes[tri[7]]);
					dl.WriteUInt16(texCoordIndexes[tri[8]]);
					dl.WriteUInt16(posIndexes[tri[3]]);
					dl.WriteUInt16(normalIndexes[tri[4]]);
					dl.WriteUInt16(texCoordIndexes[tri[5]]);
					dl.WriteUInt16(posIndexes[tri[0]]);
					dl.WriteUInt16(normalIndexes[tri[1]]);
					dl.WriteUInt16(texCoordIndexes[tri[2]]);
				}
			}

			if (Quads.Count > 0) {
				dl.BeginPrimitives(PrimitiveType.Quads, 0, (ushort)(Quads.Count * 4));

				foreach (var quad in Quads) {
					dl.WriteUInt16(posIndexes[quad[9]]);
					dl.WriteUInt16(normalIndexes[quad[10]]);
					dl.WriteUInt16(texCoordIndexes[quad[11]]);
					dl.WriteUInt16(posIndexes[quad[6]]);
					dl.WriteUInt16(normalIndexes[quad[7]]);
					dl.WriteUInt16(texCoordIndexes[quad[8]]);
					dl.WriteUInt16(posIndexes[quad[3]]);
					dl.WriteUInt16(normalIndexes[quad[4]]);
					dl.WriteUInt16(texCoordIndexes[quad[5]]);
					dl.WriteUInt16(posIndexes[quad[0]]);
					dl.WriteUInt16(normalIndexes[quad[1]]);
					dl.WriteUInt16(texCoordIndexes[quad[2]]);
				}
			}

			dl.End();

			CurrentShape.DisplayList2 = dl.GetBuffer();
			CurrentShape.DLBufferSize2 = (uint)CurrentShape.DisplayList2.Length;

			// now add it to DrawOpa
			var newInsn = new ByteCode.DrawShapeInstruction();
			newInsn.NodeID = 0;
			newInsn.MaterialID = (ushort)CurrentModel.Materials.GetIndexForValue(CurrentShapeMaterial);
			newInsn.ShapeID = (ushort)CurrentModel.Shapes.GetIndexForValue(CurrentShape);

			CurrentModel.Bytecode["DrawOpa"].Instructions.Add(newInsn);
		}



		private float[][] ComputeVertexDataArray(List<float[]> objData, BitArray usedFields, out ushort[] indexes) {
			indexes = new ushort[usedFields.Count];

			var output = new List<float[]>();

			// How this will work:
			// I'll loop through every used vertex index in the BitArray.
			// If it's used, I will compare it to the ones already in the output list.

			// If the vertex is already in the output list, then I'll take the index of it
			// and write it to the "indexes" array. If not, then I'll add it to the output list and do that.

			// The "indexes" array matches input vertex indexes to the indexes in the optimised array.

			int vertexCount = usedFields.Count;
			int elementCount = objData[0].Length;

			for (int i = 0; i < usedFields.Count; i++) {
				if (usedFields[i]) {
					// this one is used, let's try to find it
					var thisVtx = objData[i];
					bool foundIt = false;
					int j;

					for (j = 0; j < output.Count; j++) {
						var check = output[j];
						bool isEqual = true;
						for (int k = 0; k < elementCount; k++)
							isEqual &= (thisVtx[k] == check[k]);

						if (isEqual) {
							foundIt = true;
							break;
						}
					}

					if (foundIt) {
						// it already exists, just add it to the indexes list
						indexes[i] = (ushort)j;
					} else {
						// nope, add it to the computed list
						indexes[i] = (ushort)output.Count;
						output.Add(thisVtx);
					}
				}
			}

			return output.ToArray();
		}
		#endregion
	}
}