diff options
author | Treeki <treeki@gmail.com> | 2011-02-11 04:33:24 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-02-11 04:33:24 +0100 |
commit | f6f657812c1973172d25d7895aabb37f900071a7 (patch) | |
tree | 29797b41f60113373fc7e83493bcbee72dfbfc88 /NW4RTools/ObjWriter.cs | |
parent | 0d017deb24e7f6f8e049616a71691f1401c50b6a (diff) | |
download | nw4rtools-f6f657812c1973172d25d7895aabb37f900071a7.tar.gz nw4rtools-f6f657812c1973172d25d7895aabb37f900071a7.zip |
lots of progress. semi-working Wavefront OBJ exporter
Diffstat (limited to 'NW4RTools/ObjWriter.cs')
-rw-r--r-- | NW4RTools/ObjWriter.cs | 65 |
1 files changed, 59 insertions, 6 deletions
diff --git a/NW4RTools/ObjWriter.cs b/NW4RTools/ObjWriter.cs index 72a134c..297ae58 100644 --- a/NW4RTools/ObjWriter.cs +++ b/NW4RTools/ObjWriter.cs @@ -93,7 +93,26 @@ namespace NW4RTools { private void WriteShape(Models.Shape shape) { - // parse the DisplayList + // first, parse the first DisplayList to get the attr info + // for now we'll hardcode the offsets. must be fixed later + + var firstDL = new InputStream(shape.DisplayList1); + firstDL.Seek(0x0C); + UInt32 vtxDesc1 = firstDL.ReadUInt32(); + firstDL.Seek(0x12); + UInt32 vtxDesc2 = firstDL.ReadUInt32(); + firstDL.Seek(0x22); + UInt32 vtxAttr1 = firstDL.ReadUInt32(); + firstDL.Seek(0x28); + UInt32 vtxAttr2 = firstDL.ReadUInt32(); + firstDL.Seek(0x2E); + UInt32 vtxAttr3 = firstDL.ReadUInt32(); + + var vs = new VertexSettings(); + vs.SetDesc(vtxDesc1, vtxDesc2); + vs.SetAttrFmt(vtxAttr1, vtxAttr2, vtxAttr3); + + // now parse the second DisplayList int posOffset = VtxPosOffsets[shape.PosData]; int nrmOffset = shape.NrmData == null ? -1 : VtxNrmOffsets[shape.NrmData]; @@ -106,22 +125,56 @@ namespace NW4RTools { break; byte cmd = dl.ReadByte(); - PrimitiveType prim = (PrimitiveType)cmd; + if (cmd == 0) + break; + + PrimitiveType prim = (PrimitiveType)((cmd >> 3) & 7); int vtxCount = dl.ReadUInt16(); Output.WriteLine("# Primitive: {0} ({1} vertices)", prim, vtxCount); - // I wonder if INDEX8 is ever used here? - // Going with INDEX16 for now. + // first, parse it into a list of vertices + GXIndexedVertex[] vtxs = new GXIndexedVertex[vtxCount]; + string[] pVtxs = new string[vtxCount]; + + for (int i = 0; i < vtxCount; i++) { + vtxs[i].LoadFrom(dl, vs); + + string tc = (vtxs[i].TexCoords[0] == -1) ? "" : (tcOffset + vtxs[i].TexCoords[0]).ToString(); + string n = (vtxs[i].Normal == -1) ? "" : (nrmOffset + vtxs[i].Normal).ToString(); + pVtxs[i] = String.Format(" {0}/{1}/{2}", posOffset + vtxs[i].Position, tc, n); + } switch (prim) { + case PrimitiveType.Triangles: + for (int i = 0; i < vtxCount; i += 3) { + Output.WriteLine("f {0} {1} {2}", pVtxs[i], pVtxs[i + 1], pVtxs[i + 2]); + } + + break; + case PrimitiveType.TriangleStrip: // De-stripify it! - + for (int i = 2; i < vtxCount; i++) { + Output.Write("f"); + if ((i & 1) == 0) { + // Even number + Output.Write(pVtxs[i - 2]); + Output.Write(pVtxs[i - 1]); + Output.Write(pVtxs[i]); + } else { + // Odd number + Output.Write(pVtxs[i - 1]); + Output.Write(pVtxs[i - 2]); + Output.Write(pVtxs[i]); + } + Output.WriteLine(); + } break; default: - throw new NotImplementedException(); + Output.WriteLine("# UNIMPLEMENTED"); + return; } } } |