diff options
author | Treeki <treeki@gmail.com> | 2011-03-05 05:25:14 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-03-05 05:25:14 +0100 |
commit | e962fd89af865d6e01522e9752f5fbd855ce128a (patch) | |
tree | 0fb19e74a2d87b2ece48152fee2b2f4ed963828c /NW4RTools/ObjImporter.cs | |
parent | a5b6dc1789f2e06a26fe6a0510aee04aeccdc70b (diff) | |
download | nw4rtools-e962fd89af865d6e01522e9752f5fbd855ce128a.tar.gz nw4rtools-e962fd89af865d6e01522e9752f5fbd855ce128a.zip |
partially working obj importer. still untested in-game
Diffstat (limited to '')
-rwxr-xr-x | NW4RTools/ObjImporter.cs | 759 |
1 files changed, 759 insertions, 0 deletions
diff --git a/NW4RTools/ObjImporter.cs b/NW4RTools/ObjImporter.cs new file mode 100755 index 0000000..41b9ea4 --- /dev/null +++ b/NW4RTools/ObjImporter.cs @@ -0,0 +1,759 @@ +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); + } + + + + 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. + + 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(' '); + + switch (parsed[0]) { + case "mtllib": + LoadMaterialLibrary(string.Join(" ", parsed, 1, parsed.Length - 1)); + break; + case "v": + Positions.Add(ParseFloatArray(parsed, 1)); + break; + case "vn": + Normals.Add(ParseFloatArray(parsed, 1)); + break; + case "vt": + TexCoords.Add(ParseFloatArray(parsed, 1)); + 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) { + var output = new float[src.Length - index]; + + for (int i = index; i < src.Length; i++) { + output[i - index] = float.Parse(src[i]); + } + + 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(' ');
+
+ 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;
+
+// Default display lists, taken from test_lift
+ // The current version of MonoDevelop likes to make an awful mess + // of the indentation. There's nothing I can do about it. :/ + currentMaterial.PixDL = new byte[] { + 0x61, 0xF3, 0x1E, 0xFF, 0x80, + 0x61, 0x40, 0x00, 0x00, 0x17, + 0x61, 0xFE, 0x00, 0xFF, 0xE3, + 0x61, 0x41, 0x00, 0x34, 0xA0, + 0x61, 0x42, 0x00, 0x00, 0x00, +/* Padding */ + 0, 0, 0, 0, 0, + 0, 0 + }; + + currentMaterial.TevColorDL = new byte[] { + 0x61, 0xE2, 0x00, 0x00, 0xFF, + 0x61, 0xE3, 0x0F, 0xF0, 0xFF, + 0x61, 0xE3, 0x0F, 0xF0, 0xFF, + 0x61, 0xE3, 0x0F, 0xF0, 0xFF, + 0x61, 0xE4, 0x00, 0x00, 0x00, + 0x61, 0xE5, 0x00, 0x00, 0x00, + 0x61, 0xE5, 0x00, 0x00, 0x00, + 0x61, 0xE5, 0x00, 0x00, 0x00, + 0x61, 0xE6, 0x00, 0x00, 0x00, + 0x61, 0xE7, 0x00, 0x00, 0x00, + 0x61, 0xE7, 0x00, 0x00, 0x00, + 0x61, 0xE7, 0x00, 0x00, 0x00, + /* Padding */ + 0, 0, 0, 0, + 0x61, 0xE0, 0x80, 0x00, 0x00, + 0x61, + 0xE1, 0x80, 0x00, 0x00, + 0x61, 0xE2, 0x80, 0x00, 0x00, + 0x61, + 0xE3, 0x80, 0x00, 0x00, + 0x61, 0xE4, 0x80, 0x00, 0x00, + 0x61, + 0xE5, 0x80, 0x00, 0x00, + 0x61, 0xE6, 0x80, 0x00, 0x00, + 0x61, + 0xE7, 0x80, 0x00, 0x00, +/* More padding */ + 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + }; + + currentMaterial.IndMtxAndScaleDL = new byte[] { + 0x61, 0x25, 0x00, 0x00, 0x00, + 0x61, 0x26, 0x00, 0x00, 0x00, + /* Padding */ + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 + }; + + currentMaterial.TexCoordGenDL = new byte[] { + 0x10, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x52, + 0x80, 0x10, + 0x00, 0x00, 0x10, 0x50, 0x00, 0x00, +/* Padding? */ + 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +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; + shader.DisplayList = new byte[] { + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xF6, 0x00, 0x00, 0x04, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xF7, 0x00, 0x00, 0x0E, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xF8, 0x00, 0x00, 0x00, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xF9, 0x00, 0x00, 0x0C, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xFA, 0x00, 0x00, 0x05, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xFB, 0x00, 0x00, 0x0D, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xFC, 0x00, 0x00, 0x0A, + 0x61, 0xFE, 0x00, 0x00, 0x0F, + 0x61, 0xFD, 0x00, 0x00, 0x0E, + 0x61, 0x27, 0xFF, 0xFF, 0xFF, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0x61, 0xFE, 0xFF, 0xFF, + 0xF0, + 0x61, 0xF6, 0xE3, 0x38, 0xC0, + 0x61, 0x28, 0x03, 0xF0, + 0x40, + 0x61, 0xC0, 0x08, 0xF8, 0xAF, + 0x61, 0xC2, 0x08, 0xF2, + 0x0F, + 0x61, 0xC1, 0x08, 0xF2, 0xF0, + 0x61, 0xC3, 0x08, 0x1F, + 0xF0, +0x61, 0x10, 0x00, 0x00, 0x00, +0x61, 0x11, 0x00, 0x00, + 0x00 +}; + + Array.Resize<byte>(ref shader.DisplayList, 0x1E0); + + 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); + + + // Todo: a Display List generator class + + CurrentShape.DLBufferSize1 = 0xE0; + CurrentShape.DisplayList1 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0x08, 0x50, + (byte)((vd1 & 0xFF000000) >> 24), + (byte)((vd1 & 0x00FF0000) >> 16), + (byte)((vd1 & 0x0000FF00) >> 8), + (byte)((vd1 & 0x000000FF)), + 0x08, 0x60, + (byte)((vd2 & 0xFF000000) >> 24), + (byte)((vd2 & 0x00FF0000) >> 16), + (byte)((vd2 & 0x0000FF00) >> 8), + (byte)((vd2 & 0x000000FF)), + 0x10, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x00, + 0x14, 0, + 0x08, 0x70, + (byte)((vat1 & 0xFF000000) >> 24), + (byte)((vat1 & 0x00FF0000) >> 16), + (byte)((vat1 & 0x0000FF00) >> 8), + (byte)((vat1 & 0x000000FF)), + 0x08, 0x80, + (byte)((vat2 & 0xFF000000) >> 24), + (byte)((vat2 & 0x00FF0000) >> 16), + (byte)((vat2 & 0x0000FF00) >> 8), + (byte)((vat2 & 0x000000FF)), + 0x08, 0x90, + (byte)((vat3 & 0xFF000000) >> 24), + (byte)((vat3 & 0x00FF0000) >> 16), + (byte)((vat3 & 0x0000FF00) >> 8), + (byte)((vat3 & 0x000000FF)) + }; + + + // 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); + CurrentShape.DisplayList1[0x1E] = vtxSpecs; + + // extend it + // should it be bigger if more texcoords are used? maybe... + Array.Resize<byte>(ref CurrentShape.DisplayList1, 0x80); + + +// 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("Pos" + 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("TexCoord" + CurrentModel.VtxTexCoordData.Count.ToString(), tcData); + + // TODO: Flip texcoords + + 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("Nrm" + CurrentModel.VtxNrmData.Count.ToString(), nrmData); + + CurrentShape.PosData = posData; + CurrentShape.TexCoordData[0] = tcData; + CurrentShape.NrmData = nrmData; + + var dl = new OutputStream(); + + if (Triangles.Count > 0) { + dl.WriteByte((byte)GXCommand.DrawPrimitiveMask | ((byte)PrimitiveType.Triangles << (byte)GXCommand.PrimitiveShiftAmount)); + dl.WriteUInt16((ushort)(Triangles.Count * 3)); + + foreach (var tri in Triangles) { + dl.WriteUInt16(posIndexes[tri[0]]); + dl.WriteUInt16(normalIndexes[tri[1]]); + dl.WriteUInt16(texCoordIndexes[tri[2]]); + dl.WriteUInt16(posIndexes[tri[3]]); + dl.WriteUInt16(normalIndexes[tri[4]]); + dl.WriteUInt16(texCoordIndexes[tri[5]]); + dl.WriteUInt16(posIndexes[tri[6]]); + dl.WriteUInt16(normalIndexes[tri[7]]); + dl.WriteUInt16(texCoordIndexes[tri[8]]); + } + } + + if (Quads.Count > 0) { + dl.WriteByte((byte)GXCommand.DrawPrimitiveMask | ((byte)PrimitiveType.Quads << (byte)GXCommand.PrimitiveShiftAmount)); + dl.WriteUInt16((ushort)(Quads.Count * 3)); + + foreach (var quad in Quads) { + dl.WriteUInt16(posIndexes[quad[0]]); + dl.WriteUInt16(normalIndexes[quad[1]]); + dl.WriteUInt16(texCoordIndexes[quad[2]]); + dl.WriteUInt16(posIndexes[quad[3]]); + dl.WriteUInt16(normalIndexes[quad[4]]); + dl.WriteUInt16(texCoordIndexes[quad[5]]); + dl.WriteUInt16(posIndexes[quad[6]]); + dl.WriteUInt16(normalIndexes[quad[7]]); + dl.WriteUInt16(texCoordIndexes[quad[8]]); + dl.WriteUInt16(posIndexes[quad[9]]); + dl.WriteUInt16(normalIndexes[quad[10]]); + dl.WriteUInt16(texCoordIndexes[quad[11]]); + } + } + + dl.AlignTo(0x20); + + 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 + } +} + |