private void ExpandTextureVertices() { if (facesTextureCoords.Count != 0) { foreach (int index in facesTextureCoords) { Vector3 vector = textureIndices[index]; TextureCoords.Add(new Vector3(vector.X, vector.Y, vector.Z)); } } else { //Generate same for all for (int i = 0; i < Faces.Count; i++) { TextureCoords.Add(new Vector3(1, 1, 0)); } } }
public void ParseObj(StreamReader data) { string parse = string.Empty; WtGroup CurrentGroup = null; while ((parse = data.ReadLine()) != null) { int castword = WordsParser.WordIndex(ref parse, 0); if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref Usemtl, 0, 6)) { WtGroup gp = new WtGroup(); CurrentGroup = gp; Groups.Add(CurrentGroup); castword = WordsParser.WordNext(ref parse, castword); CurrentGroup.Material = _getMaterialFromName(WordsParser.WordStringValue(ref parse, castword)); continue; } if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref F, 0, 1)) { WtFace v; castword = WordsParser.WordNext(ref parse, castword); v.V0 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.VT0 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.VN0 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.V1 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.VT1 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.VN1 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.V2 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.VT2 = WordsParser.WordIntValue(ref parse, castword) - 1; castword = WordsParser.WordNext(ref parse, castword); v.VN2 = WordsParser.WordIntValue(ref parse, castword) - 1; //Console.WriteLine("f {0}/{1}/{2} {3}/{4}/{5} {6}/{7}/{8}", v.V0, v.VN0, v.VT0, v.V1, v.VN1,v.VT1, v.V2, v.VN2, v.VT2); CurrentGroup.Faces.Add(v); continue; } if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref Vn, 0, 2)) { WtVector3 v; castword = WordsParser.WordNext(ref parse, castword); v.X = WordsParser.WordFloatValue(ref parse, castword); castword = WordsParser.WordNext(ref parse, castword); v.Y = WordsParser.WordFloatValue(ref parse, castword); castword = WordsParser.WordNext(ref parse, castword); v.Z = WordsParser.WordFloatValue(ref parse, castword); //Console.WriteLine("VN {0}, {1}, {2}", v.X, v.Y, v.Z); Normals.Add(v); continue; } if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref Vt, 0, 2)) { WtVector2 v; castword = WordsParser.WordNext(ref parse, castword); v.X = WordsParser.WordFloatValue(ref parse, castword); castword = WordsParser.WordNext(ref parse, castword); v.Y = WordsParser.WordFloatValue(ref parse, castword); //Console.WriteLine("VT {0}, {1}", v.X, v.Y); TextureCoords.Add(v); continue; } if (WordsParser.WordCmp(ref parse, castword, WordsParser.WordLen(ref parse, castword), ref V, 0, 1)) { WtVector3 v; castword = WordsParser.WordNext(ref parse, castword); v.X = WordsParser.WordFloatValue(ref parse, castword); castword = WordsParser.WordNext(ref parse, castword); v.Y = WordsParser.WordFloatValue(ref parse, castword); castword = WordsParser.WordNext(ref parse, castword); v.Z = WordsParser.WordFloatValue(ref parse, castword); //Console.WriteLine("V {0}, {1}, {2}",v.X,v.Y, v.Z); Vertexes.Add(v); continue; } } }
/// <summary> /// /// </summary> private void LoadFromFile(string objFilename) { Filename = objFilename; // TODO read material file *.mtl var lines = File.ReadAllLines(objFilename); var timer = new HighPerformanceTimer(); timer.Start(); var min = new Vector3(float.MaxValue); var max = new Vector3(float.MinValue); for (var i = 0; i < lines.Length; i++) { var line = lines[i]; try { if (line.StartsWith("# object")) { if (string.IsNullOrEmpty(Name)) { Name = line.Substring("# object ".Length); } } // vertex else if (line.StartsWith("v ")) { var parts = line.Substring("v ".Length).Replace('.', ',').Split(' '); var vertex = new Vector3(float.Parse(parts[0], System.Globalization.NumberStyles.Float), float.Parse(parts[1]), float.Parse(parts[2])); if (vertex.X < min.X) { min.X = vertex.X; } if (vertex.Y < min.Y) { min.Y = vertex.Y; } if (vertex.Z < min.Z) { min.Z = vertex.Z; } if (vertex.X > max.X) { max.X = vertex.X; } if (vertex.Y > max.Y) { max.Y = vertex.Y; } if (vertex.Z > max.Z) { max.Z = vertex.Z; } Vertices.Add(vertex); } // vertex normal else if (line.StartsWith("vn ")) { var parts = line.Substring("vn ".Length).Replace('.', ',').Split(' '); Normals.Add(new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]))); } // vertex texture coordinates else if (line.StartsWith("vt ")) { var parts = line.Substring("vt ".Length).Replace('.', ',').Split(' '); TextureCoords.Add(new Vector2(float.Parse(parts[0]), float.Parse(parts[1]))); } // face indices else if (line.StartsWith("f ")) { var parts = line.Substring("f ".Length).Split(' '); var v1Parts = parts[0].Split('/'); var v2Parts = parts[1].Split('/'); var v3Parts = parts[2].Split('/'); Faces.Add( new Face( this, int.Parse(v1Parts[0]) - 1, int.Parse(v2Parts[0]) - 1, int.Parse(v3Parts[0]) - 1, int.Parse(v1Parts[1]) - 1, int.Parse(v2Parts[1]) - 1, int.Parse(v3Parts[1]) - 1, int.Parse(v1Parts[2]) - 1, int.Parse(v2Parts[2]) - 1, int.Parse(v3Parts[2]) - 1 ) ); } } catch (Exception ex) { throw new Exception(string.Format("ObjImporter failed at line='{0}'", line), ex); } } BoundingBox = new AACell { Min = min, Max = max }; timer.Stop(); Log.Instance.AddMsg(LogLevel.Info, string.Format("WavefrontObjMesh '{0}' [vertices: {1}; faces: {2}; size: {3}] loaded in {4}", Name, Vertices.Count, Faces.Count, BoundingBox.Size.ToString(), FormatString.GetDuration((int)timer.Duration))); }