void ParseTexIndex(XmlReader xml, TexIndex[] texIdx) { int index = 0; int subDepth = xml.Depth; while (xml.Read() && xml.Depth > subDepth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { //int index = int.Parse(xml.GetAttribute("Index")); string[] val = xml.ReadString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); val[0] = val[0].Substring(1); val[2] = val[2].Substring(0, val[2].Length - 1); texIdx[index++] = new TexIndex(int.Parse(val[0]), int.Parse(val[1]), int.Parse(val[2])); } } } }
ModelObject ParseMeshData(XmlReader xml) { Vector3[] positions = null; Vector3[] normals = null; MeshFace[] faces = null; //Vector2[] tex1 = null; Vector2[] texVtx = null; TexIndex[] texIdx = null; ModelObject result; result.ParentId = -1; result.Mesh = null; result.LocalTransform = Matrix.Identity; result.ParentName = string.Empty; result.Index = -1; int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { switch (xml.Name) { case "Parent": result.ParentId = int.Parse(xml.GetAttribute("ID")); result.ParentName = xml.GetAttribute("Name"); break; case "WorldTM": result.LocalTransform = ParseMatrix(xml); break; case "Vertex": positions = ParseVector3Array(xml); break; case "VertexNormal": normals = ParseVector3Array(xml); for (int i = 0; i < normals.Length; i++) { normals[i].Normalize(); } break; case "TriIndex": faces = ParseMeshFaces(xml); break; case "TexVertex": texVtx = ParseVector2Array(xml); break; case "TexIndex": texIdx = new TexIndex[faces.Length]; ParseTexIndex(xml, texIdx); break; } } } Dictionary<string, int> table = new Dictionary<string, int>(faces.Length * 3); FastList<VertexPNT1> vertices = new FastList<VertexPNT1>(faces.Length * 3); for (int i = 0; i < faces.Length; i++) { int index; VertexPNT1 vtx; vtx.pos = positions[faces[i].IndexA]; vtx.n = normals[faces[i].IndexA]; vtx.u = texVtx[texIdx[i].a].X; vtx.v = texVtx[texIdx[i].a].Y; string desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexA = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexA = index; } // ========================================= vtx.pos = positions[faces[i].IndexB]; vtx.n = normals[faces[i].IndexB]; vtx.u = texVtx[texIdx[i].b].X; vtx.v = texVtx[texIdx[i].b].Y; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexB = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexB = index; } // ========================================= vtx.pos = positions[faces[i].IndexC]; vtx.n = normals[faces[i].IndexC]; vtx.u = texVtx[texIdx[i].c].X; vtx.v = texVtx[texIdx[i].c].Y; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexC = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexC = index; } } MeshData data = new MeshData((RenderSystem)null); data.Faces = faces; data.VertexElements = VertexPNT1.Elements; data.VertexSize = VertexPNT1.Size; data.VertexCount = vertices.Count; fixed (VertexPNT1* src = &vertices.Elements[0]) { data.SetData(src, VertexPNT1.Size * vertices.Count); } result.Mesh = data; return result; }
ModelObject ParseMeshData(XmlReader xml) { Vector3[] positions = null; Vector3[] normals = null; MeshFace[] faces = null; //Vector2[] tex1 = null; Vector2[] texVtx = null; TexIndex[] texIdx = null; ModelObject result; result.ParentId = -1; result.Mesh = null; result.LocalTransform = Matrix.Identity; result.ParentName = string.Empty; result.Index = -1; int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { switch (xml.Name) { case "Parent": result.ParentId = int.Parse(xml.GetAttribute("ID")); result.ParentName = xml.GetAttribute("Name"); break; case "WorldTM": result.LocalTransform = ParseMatrix(xml); break; case "Vertex": positions = ParseVector3Array(xml); break; case "VertexNormal": normals = ParseVector3Array(xml); for (int i = 0; i < normals.Length; i++) { normals[i].Normalize(); } break; case "TriIndex": faces = ParseMeshFaces(xml); break; case "TexVertex": texVtx = ParseVector2Array(xml); break; case "TexIndex": texIdx = new TexIndex[faces.Length]; ParseTexIndex(xml, texIdx); break; } } } Dictionary <string, int> table = new Dictionary <string, int>(faces.Length * 3); FastList <VertexPNT1> vertices = new FastList <VertexPNT1>(faces.Length * 3); for (int i = 0; i < faces.Length; i++) { int index; VertexPNT1 vtx; vtx.pos = positions[faces[i].IndexA]; vtx.n = normals[faces[i].IndexA]; vtx.u = texVtx[texIdx[i].a].X; vtx.v = texVtx[texIdx[i].a].Y; string desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexA = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexA = index; } // ========================================= vtx.pos = positions[faces[i].IndexB]; vtx.n = normals[faces[i].IndexB]; vtx.u = texVtx[texIdx[i].b].X; vtx.v = texVtx[texIdx[i].b].Y; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexB = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexB = index; } // ========================================= vtx.pos = positions[faces[i].IndexC]; vtx.n = normals[faces[i].IndexC]; vtx.u = texVtx[texIdx[i].c].X; vtx.v = texVtx[texIdx[i].c].Y; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexC = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexC = index; } } MeshData data = new MeshData((RenderSystem)null); data.Faces = faces; data.VertexElements = VertexPNT1.Elements; data.VertexSize = VertexPNT1.Size; data.VertexCount = vertices.Count; fixed(VertexPNT1 *src = &vertices.Elements[0]) { data.SetData(src, VertexPNT1.Size * vertices.Count); } result.Mesh = data; return(result); }