Browse Source

wavefront export

Vicente Penades 6 years ago
parent
commit
8a25b3fc21

+ 18 - 0
src/SharpGLTF.Toolkit/Geometry/InterleavedMeshBuilder.cs

@@ -19,6 +19,14 @@ namespace SharpGLTF.Geometry
 
         #endregion
 
+        #region properties
+
+        public IReadOnlyList<TVertex> Vertices => _Vertices;
+
+        public IReadOnlyCollection<TMaterial> Materials => _Indices.Keys;
+
+        #endregion
+
         #region API
 
         public void AddPolygon(TMaterial material, params TVertex[] points)
@@ -51,6 +59,16 @@ namespace SharpGLTF.Geometry
             indices.Add(cc);
         }
 
+        public IEnumerable<(int, int, int)> GetTriangles(TMaterial material)
+        {
+            if (!_Indices.TryGetValue(material, out List<int> indices)) yield break;
+
+            for (int i = 2; i < indices.Count; i += 3)
+            {
+                yield return (indices[i - 2], indices[i - 1], indices[i]);
+            }
+        }
+
         public void CopyToNode(Node dstNode, Func<TMaterial, Material> materialEvaluator)
         {
             dstNode.Mesh = dstNode.LogicalParent.CreateMesh();

+ 27 - 20
tests/SharpGLTF.Tests/WavefrontWriter.cs

@@ -6,11 +6,10 @@ using System.Text;
 
 using static System.FormattableString;
 
-
-
 namespace SharpGLTF
 {
-    using Collections;
+    using VERTEX = Geometry.VertexTypes.StaticPositionNormal;
+    using MATERIAL = Vector4;
 
     /// <summary>
     /// Tiny wavefront object writer
@@ -19,27 +18,30 @@ namespace SharpGLTF
     {
         #region data
 
-        private readonly VertexColumn<Vector3> _Positions = new VertexColumn<Vector3>();
-        private readonly VertexColumn<Vector3> _Normals = new VertexColumn<Vector3>();
-
-        private readonly List<(int, int, int)> _Indices = new List<(int, int, int)>();        
-
+        private readonly Geometry.InterleavedMeshBuilder<VERTEX, MATERIAL> _Mesh = new Geometry.InterleavedMeshBuilder<VERTEX, MATERIAL>();
+        
         #endregion
 
         #region API        
 
         public void AddTriangle(Vector3 a, Vector3 b, Vector3 c)
         {
-            var aa = _Positions.Use(a);
-            var bb = _Positions.Use(b);
-            var cc = _Positions.Use(c);
+            var aa = new Geometry.VertexTypes.StaticPositionNormal
+            {
+                Position = a
+            };
 
-            // check for degenerated triangles:
-            if (aa == bb) return;
-            if (aa == cc) return;
-            if (bb == cc) return;
+            var bb = new Geometry.VertexTypes.StaticPositionNormal
+            {
+                Position = b
+            };
+
+            var cc = new Geometry.VertexTypes.StaticPositionNormal
+            {
+                Position = c
+            };
 
-            _Indices.Add((aa, bb, cc));
+            _Mesh.AddTriangle(Vector4.One, aa, bb, cc);
         }
 
         public override string ToString()
@@ -48,18 +50,23 @@ namespace SharpGLTF
 
             sb.AppendLine();
 
-            foreach (var v in _Positions)
+            foreach (var v in _Mesh.Vertices)
             {
-                sb.AppendLine(Invariant($"v {v.X} {v.Y} {v.Z}"));
+                sb.AppendLine(Invariant($"v {v.Position.X} {v.Position.Y} {v.Position.Z}"));
             }
 
             sb.AppendLine();
 
             sb.AppendLine("g default");
 
-            foreach (var p in _Indices)
+            foreach(var m in _Mesh.Materials)
             {
-                sb.AppendLine(Invariant($"f {p.Item1 + 1} {p.Item2 + 1} {p.Item3 + 1}"));
+                var triangles = _Mesh.GetTriangles(m);
+
+                foreach (var t in triangles)
+                {
+                    sb.AppendLine(Invariant($"f {t.Item1 + 1} {t.Item2 + 1} {t.Item3 + 1}"));
+                }
             }
 
             return sb.ToString();