/// <summary>
        /// Handles a vertex list record
        /// </summary>
        /// <returns></returns>
        protected bool HandleVertexList()
        {
            VertexList vl = new VertexList(this);

            vl.Parse();
            return(true);
        }
Example #2
0
        /// <summary>
        /// Prepares the vertices and triangulates the faces ready for creating a mesh.
        /// </summary>
        public override void PrepareForImport()
        {
            // Do we draw this face?
            if (FlagsHidden)
            {
                return;
            }

            if (Parent is InterRecord)
            {
                InterRecord ir = Parent as InterRecord;

                // Find vertex list
                VertexList vl = Children.Find(o => o is VertexList) as VertexList;
                if (vl != null)
                {
                    int startIndex = ir.Vertices.Count;

                    int[] triangles = null;

                    //////////////////////////
                    // Triangle
                    //////////////////////////
                    if (vl.Offsets.Count == 3)
                    {
                        // DuckbearLab: FIX! Inverted X so inverted order too
                        triangles = new int[] { startIndex + 2, startIndex + 1, startIndex };

                        // Extract verts for this triangle
                        foreach (int vwcI in vl.Offsets)
                        {
                            VertexWithColor vwc = Header.VertexPalette.Vertices[vwcI];
                            ir.Vertices.Add(vwc);
                        }
                    }
                    //////////////////////////
                    // Polygon
                    //////////////////////////
                    else
                    {
                        // Extract verts and positions for triangulation of this face
                        List <Vector3> positions  = new List <Vector3>(vl.Offsets.Count);
                        Vector3        faceNormal = Vector3.zero;                  // Need the normal to convert the positions to 2d for triangulation.
                        foreach (int vwcI in vl.Offsets)
                        {
                            VertexWithColor vwc = Header.VertexPalette.Vertices[vwcI];
                            ir.Vertices.Add(vwc);
                            positions.Add(new Vector3((float)vwc.Coordinate[0], (float)vwc.Coordinate[1], (float)vwc.Coordinate[2]));

                            if (vwc is VertexWithColorNormal)
                            {
                                faceNormal += (vwc as VertexWithColorNormal).Normal;
                            }
                        }
                        faceNormal.Normalize();

                        // Triangulate the face
                        Triangulator triangulator = new Triangulator();
                        triangulator.initTriangulator(positions, faceNormal);
                        triangles = triangulator.Triangulate(0);

                        // Apply index offset
                        for (int i = 0; i < triangles.Length; ++i)
                        {
                            triangles[i] += startIndex;
                        }
                    }

                    // We now have our triangles. Lets find the correct submesh to add them to.
                    KeyValuePair <IntermediateMaterial, List <int> > submesh = ir.FindOrCreateSubMesh(this);
                    submesh.Value.AddRange(triangles);
                }
                else
                {
                    Log.WriteWarning(ID + "- Could not find vertex list for face");
                }
            }
            else
            {
                Log.WriteWarning("Face is not a child of a InterRecord, can not create face.");
            }

            base.ImportIntoScene();
        }