예제 #1
0
 public void Load(IMesh mesh, string filePath)
 {
   var dxfLoad = DxfDocument.Load(filePath);
   var vertices = new List<IVertex>();
   var faces = new List<IFace>();
   var verticesIndexMap = new Dictionary<IVertex, int>();
   int currentVertexIndex = 0;
   foreach (var face3D in dxfLoad.Faces3d)
   {
     var v1 = face3D.FirstVertex;
     var v2 = face3D.SecondVertex;
     var v3 = face3D.ThirdVertex;
     var v4 = face3D.FourthVertex;
     currentVertexIndex = CreateTriangleFace(v1, v2, v3, verticesIndexMap, currentVertexIndex, vertices, faces);
     currentVertexIndex = CreateTriangleFace(v3, v4, v1, verticesIndexMap, currentVertexIndex, vertices, faces);
   }
   foreach (var vertex in vertices)
   {
     mesh.AddVertex(vertex);
   }
   foreach (var face in faces)
   {
     mesh.AddFace(face);
   }
 }
        public void Load(IMesh mesh, string filePath)
        {
            var dxfLoad            = DxfDocument.Load(filePath);
            var vertices           = new List <IVertex>();
            var faces              = new List <IFace>();
            var verticesIndexMap   = new Dictionary <IVertex, int>();
            int currentVertexIndex = 0;

            foreach (var face3D in dxfLoad.Faces3d)
            {
                var v1 = face3D.FirstVertex;
                var v2 = face3D.SecondVertex;
                var v3 = face3D.ThirdVertex;
                var v4 = face3D.FourthVertex;
                currentVertexIndex = CreateTriangleFace(v1, v2, v3, verticesIndexMap, currentVertexIndex, vertices, faces);
                currentVertexIndex = CreateTriangleFace(v3, v4, v1, verticesIndexMap, currentVertexIndex, vertices, faces);
            }
            foreach (var vertex in vertices)
            {
                mesh.AddVertex(vertex);
            }
            foreach (var face in faces)
            {
                mesh.AddFace(face);
            }
        }
예제 #3
0
    private void Load(TextReader textReader, IMesh mesh)
    {
      var vertices = new List<IVertex>();
      var faces = new List<IFace>();
      var verticesIndexMap = new Dictionary<IVertex, int>();
      int currentVertexIndex = 0;

      var positions = new List<Vector3>();
      var normals = new List<Vector3>();
      var texCoords = new List<Vector3>();
      
      string line;
      while ((line = textReader.ReadLine()) != null)
      {
        line = line.Trim(_splitCharacters);
        line = line.Replace("  ", " ");

        var parameters = line.Split(_splitCharacters);

        switch (parameters[0])
        {
          case "p": // Point
            break;

          case "v": // Vertex
            var x = float.Parse(parameters[1], CultureInfo.InvariantCulture);
            var y = float.Parse(parameters[2], CultureInfo.InvariantCulture);
            var z = float.Parse(parameters[3], CultureInfo.InvariantCulture);
            positions.Add(new Vector3(x, y, z));
            break;

          case "vt": // TexCoord
            var u = float.Parse(parameters[1], CultureInfo.InvariantCulture);
            var v = float.Parse(parameters[2], CultureInfo.InvariantCulture);
            texCoords.Add(new Vector3(u, v, 0));
            break;

          case "vn": // Normal
            var nx = float.Parse(parameters[1], CultureInfo.InvariantCulture);
            var ny = float.Parse(parameters[2], CultureInfo.InvariantCulture);
            var nz = float.Parse(parameters[3], CultureInfo.InvariantCulture);
            normals.Add(new Vector3(nx, ny, nz).Normalized());
            break;

          case "f":
            var faceVertexIndices = new List<int>();
            for (int i = 0; i < parameters.Length - 1; i++)
            {
              //The definition of what is a vertex in an OBJ file only exists appears when the vertex of each face is being defined
              //We only want to add distinctive vertices to our list of vertices, so we test here if we already have a vertex with the same
              //parameters, in which case we will use the existing vertex, if not we will add the new vertex to our list and increase the vertex
              //index counter
              var candidateVertex = GetVertex(parameters[i + 1], positions, normals, texCoords);
              if (!verticesIndexMap.ContainsKey(candidateVertex))
              {
                verticesIndexMap.Add(candidateVertex, currentVertexIndex);
                vertices.Add(candidateVertex);
                currentVertexIndex++;
              }
              var index = verticesIndexMap[candidateVertex];
              faceVertexIndices.Add(index);
            }
            var face = _faceFactory.CreateFace(faceVertexIndices);
            faces.Add(face);
            break;
        }
      }
      foreach (var vertex in vertices)
      {
        mesh.AddVertex(vertex);
      }
      foreach (var face in faces)
      {
        mesh.AddFace(face);
      }
    }
예제 #4
0
        private void Load(TextReader textReader, IMesh mesh)
        {
            var vertices           = new List <IVertex>();
            var faces              = new List <IFace>();
            var verticesIndexMap   = new Dictionary <IVertex, int>();
            int currentVertexIndex = 0;

            var positions = new List <Vector3>();
            var normals   = new List <Vector3>();
            var texCoords = new List <Vector3>();

            string line;

            while ((line = textReader.ReadLine()) != null)
            {
                line = line.Trim(_splitCharacters);
                line = line.Replace("  ", " ");

                var parameters = line.Split(_splitCharacters);

                switch (parameters[0])
                {
                case "p": // Point
                    break;

                case "v": // Vertex
                    var x = float.Parse(parameters[1], CultureInfo.InvariantCulture);
                    var y = float.Parse(parameters[2], CultureInfo.InvariantCulture);
                    var z = float.Parse(parameters[3], CultureInfo.InvariantCulture);
                    positions.Add(new Vector3(x, y, z));
                    break;

                case "vt": // TexCoord
                    var u = float.Parse(parameters[1], CultureInfo.InvariantCulture);
                    var v = float.Parse(parameters[2], CultureInfo.InvariantCulture);
                    texCoords.Add(new Vector3(u, v, 0));
                    break;

                case "vn": // Normal
                    var nx = float.Parse(parameters[1], CultureInfo.InvariantCulture);
                    var ny = float.Parse(parameters[2], CultureInfo.InvariantCulture);
                    var nz = float.Parse(parameters[3], CultureInfo.InvariantCulture);
                    normals.Add(new Vector3(nx, ny, nz).Normalized());
                    break;

                case "f":
                    var faceVertexIndices = new List <int>();
                    for (int i = 0; i < parameters.Length - 1; i++)
                    {
                        //The definition of what is a vertex in an OBJ file only exists appears when the vertex of each face is being defined
                        //We only want to add distinctive vertices to our list of vertices, so we test here if we already have a vertex with the same
                        //parameters, in which case we will use the existing vertex, if not we will add the new vertex to our list and increase the vertex
                        //index counter
                        var candidateVertex = GetVertex(parameters[i + 1], positions, normals, texCoords);
                        if (!verticesIndexMap.ContainsKey(candidateVertex))
                        {
                            verticesIndexMap.Add(candidateVertex, currentVertexIndex);
                            vertices.Add(candidateVertex);
                            currentVertexIndex++;
                        }
                        var index = verticesIndexMap[candidateVertex];
                        faceVertexIndices.Add(index);
                    }
                    var face = _faceFactory.CreateFace(faceVertexIndices);
                    faces.Add(face);
                    break;
                }
            }
            foreach (var vertex in vertices)
            {
                mesh.AddVertex(vertex);
            }
            foreach (var face in faces)
            {
                mesh.AddFace(face);
            }
        }