private void loadMD3Data(ContentReader input, MD3Object m) { //load meshes m.meshes = new List<MD3SubMeshes>(); int part = input.ReadInt32(); if (part == 0) m.part = TMD3Part.HEAD; else if (part == 1) m.part = TMD3Part.LOWER; else m.part = TMD3Part.UPPER; m.num_frames = input.ReadInt32(); int sub_meshes_count = input.ReadInt32(); for (int i = 0; i < sub_meshes_count; ++i) { MD3SubMeshes sub_mesh = new MD3SubMeshes(); sub_mesh.indices = new List<int>(); sub_mesh.vertices = new List<Vector3>(); sub_mesh.normals = new List<Vector3>(); sub_mesh.text_coord = new List<Vector2>(); sub_mesh.skins = new List<string>(); sub_mesh.meshinfo.strName = input.ReadString(); input.ReadObject<List<int>>(sub_mesh.indices); input.ReadObject<List<Vector3>>(sub_mesh.vertices); input.ReadObject<List<Vector3>>(sub_mesh.normals); input.ReadObject<List<Vector2>>(sub_mesh.text_coord); m.meshes.Add(sub_mesh); } //load tags m.tags = new List<MD3tag>(); int tags_count = input.ReadInt32(); for (int i = 0; i < tags_count; ++i) { MD3tag tag = new MD3tag(); tag.strName = input.ReadString(); tag.vPosition = input.ReadVector3(); tag.rotation = input.ReadMatrix(); m.tags.Add(tag); } //load bounding_boxes m.bounding_boxes = new List<BoundingBox>(); int bb_count = input.ReadInt32(); for (int i = 0; i < bb_count; ++i) { BoundingBox bb = new BoundingBox(); bb.Min = input.ReadVector3(); bb.Max = input.ReadVector3(); m.bounding_boxes.Add(bb); } }
public void loadMD3(MemoryStream stream) { BinaryReader reader = new BinaryReader(stream); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); //System.Diagnostics.Debugger.Launch(); header = new MD3header(); header.fileID = getText(enc.GetString(reader.ReadBytes(4))); header.version = reader.ReadInt32(); header.strFile = getText(enc.GetString(reader.ReadBytes(68))); header.numFrames = reader.ReadInt32(); header.numTags = reader.ReadInt32(); header.numMeshes = reader.ReadInt32(); header.numMaxSkins = reader.ReadInt32(); header.headerSize = reader.ReadInt32(); header.tagStart = reader.ReadInt32(); header.tagEnd = reader.ReadInt32(); header.fileSize = reader.ReadInt32(); bones = new List<MD3bone>(); for (int i = 0; i < header.numFrames; ++i) { MD3bone bone = new MD3bone(); bone.mins.X = reader.ReadSingle(); bone.mins.Y = reader.ReadSingle(); bone.mins.Z = reader.ReadSingle(); bone.maxs.X = reader.ReadSingle(); bone.maxs.Y = reader.ReadSingle(); bone.maxs.Z = reader.ReadSingle(); bone.position.X = reader.ReadSingle(); bone.position.Y = reader.ReadSingle(); bone.position.Z = reader.ReadSingle(); bone.scale = reader.ReadSingle(); bone.creator = getText(enc.GetString(reader.ReadBytes(16))); bones.Add(bone); } tag = new List<MD3tag>(); for (int i = 0; i < header.numFrames * header.numTags; ++i) { MD3tag data_tag= new MD3tag(); data_tag.strName = getText(enc.GetString(reader.ReadBytes(64))); data_tag.vPosition.X = reader.ReadSingle(); data_tag.vPosition.Y = reader.ReadSingle(); data_tag.vPosition.Z = reader.ReadSingle(); data_tag.rotation = Matrix.Identity; data_tag.rotation.M11 = reader.ReadSingle(); data_tag.rotation.M12 = reader.ReadSingle(); data_tag.rotation.M13 = reader.ReadSingle(); data_tag.rotation.M21 = reader.ReadSingle(); data_tag.rotation.M22 = reader.ReadSingle(); data_tag.rotation.M23 = reader.ReadSingle(); data_tag.rotation.M31 = reader.ReadSingle(); data_tag.rotation.M32 = reader.ReadSingle(); data_tag.rotation.M33 = reader.ReadSingle(); tag.Add(data_tag); } long mesh_offset = reader.BaseStream.Position; sub_meshes = new List<MD3SubMeshes>(); for (int i = 0; i < header.numMeshes; ++i) { reader.BaseStream.Seek(mesh_offset, SeekOrigin.Begin); //read mesh info MD3SubMeshes sub_mesh = new MD3SubMeshes(); sub_mesh.meshinfo = new MD3meshinfo(); sub_mesh.meshinfo.meshID = getText(enc.GetString(reader.ReadBytes(4))); sub_mesh.meshinfo.strName = getText(enc.GetString(reader.ReadBytes(68))); sub_mesh.meshinfo.numMeshFrames = reader.ReadInt32(); sub_mesh.meshinfo.numSkins = reader.ReadInt32(); sub_mesh.meshinfo.numVertices = reader.ReadInt32(); sub_mesh.meshinfo.numTriangles = reader.ReadInt32(); sub_mesh.meshinfo.triStart = reader.ReadInt32(); sub_mesh.meshinfo.headerSize = reader.ReadInt32(); sub_mesh.meshinfo.uvStart = reader.ReadInt32(); sub_mesh.meshinfo.vertexStart = reader.ReadInt32(); sub_mesh.meshinfo.meshSize = reader.ReadInt32(); //Skin int size = sub_mesh.meshinfo.numSkins; sub_mesh.skins = new List<string>(); for (int a = 0; a < size; ++a) { sub_mesh.skins.Add( getText(enc.GetString(reader.ReadBytes(68)))); } //Faces size = sub_mesh.meshinfo.numTriangles * 3; sub_mesh.indices = new List<int>(); reader.BaseStream.Seek(mesh_offset + sub_mesh.meshinfo.triStart, SeekOrigin.Begin); for (int a = 0; a < size; ++a) { sub_mesh.indices.Add(reader.ReadInt32()); } //Vertex Texture size = sub_mesh.meshinfo.numVertices; sub_mesh.text_coord = new List<Vector2>(); reader.BaseStream.Seek(mesh_offset + sub_mesh.meshinfo.uvStart, SeekOrigin.Begin); for (int a = 0; a < size; ++a) { Vector2 text_coord; text_coord.X = reader.ReadSingle(); text_coord.Y = reader.ReadSingle(); sub_mesh.text_coord.Add(text_coord); } //Vertex size = sub_mesh.meshinfo.numVertices * sub_mesh.meshinfo.numMeshFrames; sub_mesh.vertices = new List<Vector3>(); sub_mesh.normals = new List<Vector3>(); reader.BaseStream.Seek(mesh_offset + sub_mesh.meshinfo.vertexStart, SeekOrigin.Begin); for (int a = 0; a < size; ++a) { Vector3 vertex; vertex.X = reader.ReadInt16() / 64.0f; vertex.Y = reader.ReadInt16() / 64.0f; vertex.Z = reader.ReadInt16() / 64.0f; sub_mesh.vertices.Add(vertex); Vector3 normal; float lng = reader.ReadByte() / 255.0f * MathHelper.TwoPi; float lat = reader.ReadByte() / 255.0f * MathHelper.TwoPi; normal.X = (float)(Math.Cos(lat) * Math.Sin(lng)); normal.Y = (float)(Math.Sin(lat) * Math.Sin(lng)); normal.Z = (float)Math.Cos(lng); normal.Normalize(); sub_mesh.normals.Add(normal); } mesh_offset += sub_mesh.meshinfo.meshSize; sub_meshes.Add(sub_mesh); } reader.Close(); stream.Close(); }