Attempt on a single mesh file:
here is the c# class:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Globalization;
namespace ModelToObj
{
public static class KungFuStrike
{
public static void Convert(string fileIn, string fileOut)
{
string decimalChar = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
if (decimalChar == ",")
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-us");
decimalChar = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
}
List<Vector3> vertices = new List<Vector3>();
List<Indices> faces = new List<Indices>();
uint nVerts = 0;
uint chunkFaces = 0;
using (BinaryReader br = new BinaryReader(new FileStream(fileIn, FileMode.Open, FileAccess.Read)))
{
char[] header = br.ReadChars(4); // A3D.
br.ReadUInt32();
int nbrMeshes = (int)br.ReadUInt32();
if (nbrMeshes == 1)
br.BaseStream.Seek(72, SeekOrigin.Begin);
if (nbrMeshes == 2)
br.BaseStream.Seek(132, SeekOrigin.Begin);
nbrMeshes = (int)br.ReadUInt32(); // again ?
for (int i = 0; i < nbrMeshes; i++)
{
int len = (int)br.ReadUInt32();
string meshName = Helper.CharToString(br.ReadChars(len));
Debug.WriteLine(meshName);
}
br.ReadUInt32(); // 2 or 1
br.ReadUInt32(); // 00 00 00 00
nVerts = br.ReadUInt32();
chunkFaces = br.ReadUInt32() * 2;
uint nFaces = chunkFaces / 6;
for (int i = 0; i < nFaces; i++)
{
faces.Add(new Indices()
{
a = (int)br.ReadUInt16(),
b = (int)br.ReadUInt16(),
c = (int)br.ReadUInt16()
});
}
br.ReadUInt32();
br.ReadUInt32();
br.ReadUInt32();
br.ReadUInt32();
for (int i = 0; i < nVerts; i++)
{
vertices.Add(new Vector3()
{
X = br.ReadSingle(),
Y = br.ReadSingle(),
Z = br.ReadSingle()
});
}
// TEST
StringBuilder vertexLis = new StringBuilder();
StringBuilder faceList = new StringBuilder();
Debug.Write(nVerts);
Debug.Write(",");
Debug.WriteLine(nFaces);
foreach (Vector3 v in vertices)
{
vertexLis.Append(String.Format("[{0},{1},{2}],", v.X, v.Y, v.Z));
}
Debug.WriteLine(vertexLis.ToString());
foreach (Indices f in faces)
{
faceList.Append(String.Format("[{0},{1},{2}],", f.a + 1, f.b + 1, f.c + 1));
}
Debug.WriteLine(faceList.ToString());
br.Close();
}
}
}
}
no uvw mapping yet.
T.