示例#1
0
        private void Load(string path)
        {
            var numberRegex = new Regex(@"-?[\d]+(?:\.[\d]*)?(e[-|+]?\d+)?", RegexOptions.IgnoreCase);
            var faceRegex = new Regex(@"(\d+)/([\d]*)/([\d]*)", RegexOptions.IgnoreCase);

            Vertices.Clear();
            Normals.Clear();
            Textures.Clear();
            Faces.Clear();

            foreach (var line in File.ReadLines(path).Where(line => numberRegex.IsMatch(line)))
            {
                var matches = numberRegex.Matches(line);

                if (line.StartsWith("v "))
                {
                    if (matches.Count != 3)
                        throw new FileLoadException("Error parsing vertices.");

                    Vertices.Add(
                        new Vector4 {
                            X = float.Parse(matches[0].Value),
                            Y = float.Parse(matches[1].Value),
                            Z = float.Parse(matches[2].Value),
                            W = 1f
                        });
                }
                else if (line.StartsWith("vt "))
                {
                    if (matches.Count != 2)
                        throw new FileLoadException("Error parsing textures.");

                    Textures.Add(
                        new Vector2 {
                            X = float.Parse(matches[0].Value),
                            Y = float.Parse(matches[1].Value)
                        });
                }
                else if (line.StartsWith("vn "))
                {
                    if (matches.Count != 3)
                        throw new FileLoadException("Error parsing normals.");

                    Normals.Add(
                        Vector3.Normalize(new Vector3 {
                            X = float.Parse(matches[0].Value),
                            Y = float.Parse(matches[1].Value),
                            Z = float.Parse(matches[2].Value)
                        }));
                }
                else if (line.StartsWith("f "))
                {
                    matches = faceRegex.Matches(line);

                    if (matches.Count != 4)
                        throw new FileLoadException("Error parsing faces.");

                    var face = new Face();
                    foreach (var i in Enumerable.Range(0, 4))
                    {
                        var point =
                            new Point {
                                PositionIndex = int.Parse(matches[i].Groups[1].Value) - 1,
                                TextureIndex = matches[i].Groups[2].Value == String.Empty ? 0 : int.Parse(matches[i].Groups[2].Value) - 1,
                                NormalIndex = int.Parse(matches[i].Groups[3].Value) - 1
                            };

                        face.Points.Add(point);
                        _pointIndexLookUp[point] = (uint) (Faces.Count * 4 + i);
                    }

                    Faces.Add(face);

                    var patch = new AccPatch {
                            Points = new List<Point>(face.Points),
                            Prefixes = new List<int>(),
                            Valences = new List<int>()
                        };

                    foreach (var point in face.Points)
                    {
                        if (_pointNeighborLookUp.ContainsKey(point.PositionIndex))
                            _pointNeighborLookUp[point.PositionIndex].Add(patch);
                        else
                            _pointNeighborLookUp[point.PositionIndex] = new List<AccPatch> { patch };
                    }

                    AccPatches.Add(patch);
                }
               }
        }
示例#2
0
        private void Load(string path)
        {
            var numberRegex = new Regex(@"-?[\d]+(?:\.[\d]*)?(e[-|+]?\d+)?", RegexOptions.IgnoreCase);
            var faceRegex   = new Regex(@"(\d+)/([\d]*)/([\d]*)", RegexOptions.IgnoreCase);

            Vertices.Clear();
            Normals.Clear();
            Textures.Clear();
            Faces.Clear();

            foreach (var line in File.ReadLines(path).Where(line => numberRegex.IsMatch(line)))
            {
                var matches = numberRegex.Matches(line);

                if (line.StartsWith("v "))
                {
                    if (matches.Count != 3)
                    {
                        throw new FileLoadException("Error parsing vertices.");
                    }

                    Vertices.Add(
                        new Vector4 {
                        X = float.Parse(matches[0].Value),
                        Y = float.Parse(matches[1].Value),
                        Z = float.Parse(matches[2].Value),
                        W = 1f
                    });
                }
                else if (line.StartsWith("vt "))
                {
                    if (matches.Count != 2)
                    {
                        throw new FileLoadException("Error parsing textures.");
                    }

                    Textures.Add(
                        new Vector2 {
                        X = float.Parse(matches[0].Value),
                        Y = float.Parse(matches[1].Value)
                    });
                }
                else if (line.StartsWith("vn "))
                {
                    if (matches.Count != 3)
                    {
                        throw new FileLoadException("Error parsing normals.");
                    }

                    Normals.Add(
                        Vector3.Normalize(new Vector3 {
                        X = float.Parse(matches[0].Value),
                        Y = float.Parse(matches[1].Value),
                        Z = float.Parse(matches[2].Value)
                    }));
                }
                else if (line.StartsWith("f "))
                {
                    matches = faceRegex.Matches(line);

                    if (matches.Count != 4)
                    {
                        throw new FileLoadException("Error parsing faces.");
                    }

                    var face = new Face();
                    foreach (var i in Enumerable.Range(0, 4))
                    {
                        var point =
                            new Point {
                            PositionIndex = int.Parse(matches[i].Groups[1].Value) - 1,
                            TextureIndex  = matches[i].Groups[2].Value == String.Empty ? 0 : int.Parse(matches[i].Groups[2].Value) - 1,
                            NormalIndex   = int.Parse(matches[i].Groups[3].Value) - 1
                        };

                        face.Points.Add(point);
                        _pointIndexLookUp[point] = (uint)(Faces.Count * 4 + i);
                    }

                    Faces.Add(face);

                    var patch = new AccPatch {
                        Points   = new List <Point>(face.Points),
                        Prefixes = new List <int>(),
                        Valences = new List <int>()
                    };

                    foreach (var point in face.Points)
                    {
                        if (_pointNeighborLookUp.ContainsKey(point.PositionIndex))
                        {
                            _pointNeighborLookUp[point.PositionIndex].Add(patch);
                        }
                        else
                        {
                            _pointNeighborLookUp[point.PositionIndex] = new List <AccPatch> {
                                patch
                            }
                        };
                    }

                    AccPatches.Add(patch);
                }
            }
        }