/// <summary> /// Parses a polygon node, which is a vertex list. Each vertex's /// position is referenced by its vertex ID to the primitive's master list. /// Texture coordinates and lightmap coordinates are picked up here, /// as well as triangulation. Polys are added to a primitive grouped by /// their material ID so they can be split up later into multiple meshes. /// </summary> /// <param name="xmlNode">Xml Poly node</param> /// <param name="dxsPrim">Dxs Primitive this polygon belongs to</param> private void ParsePolygon(XmlNode xmlNode, DxsPrimitive dxsPrim) { //Parse attributes int mid = int.Parse(xmlNode.Attributes["mid"].Value); int count = xmlNode.ChildNodes.Count; DxsPoly poly = new DxsPoly(mid); //Add the material to the primitive - if its a newly encountered material if (materialList.ContainsKey(mid)) { dxsPrim.AddMaterial(materialList[mid]); } //Now go through each vertex and parse it for (int i = count - 1; i >= 0; i--) { // for(int i = 0; i < count; i++) { XmlNode vertex = xmlNode.ChildNodes[i]; ParseVertex(vertex, dxsPrim, poly); } //Either triangulate the polygon, or simply add it if its already a triangle if (count > 3) { TriangulatePolygon(dxsPrim, poly); } else { poly.ComputeFaceNormal(); dxsPrim.AddPoly(poly); } }
/// <summary> /// Uses the first vertex in a polygon as a pivot to triangulate the polygon, /// which get added to the dxs primitive. /// </summary> /// <param name="dxsPrim">Dxs Primitive the poly belongs to</param> /// <param name="dxsPoly">Dxs Poly to be triangulated</param> private void TriangulatePolygon(DxsPrimitive dxsPrim, DxsPoly dxsPoly) { DxsVertex[] vertices = dxsPoly.Vertices.ToArray(); DxsVertex v0 = vertices[0]; //Use the first vertex as a pivot and create triangles. for (int v = 1; v < vertices.Length - 1; v++) { DxsPoly poly = new DxsPoly(dxsPoly.MaterialID); poly.Vertices.Add(v0); poly.Vertices.Add(vertices[v]); poly.Vertices.Add(vertices[v + 1]); poly.ComputeFaceNormal(); dxsPrim.AddPoly(poly); } }