|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | + |
| 4 | +namespace TessExample |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + // The data array contains 4 values, it's the associated data of the vertices that resulted in an intersection. |
| 9 | + private static object VertexCombine(LibTessDotNet.Vec3 position, object[] data, float[] weights) |
| 10 | + { |
| 11 | + // Fetch the vertex data. |
| 12 | + var colors = new Color[] { (Color)data[0], (Color)data[1], (Color)data[2], (Color)data[3] }; |
| 13 | + // Interpolate with the 4 weights. |
| 14 | + var rgba = new float[] { |
| 15 | + colors[0].R * weights[0] + colors[1].R * weights[1] + colors[2].R * weights[2] + colors[3].R * weights[3], |
| 16 | + colors[0].G * weights[0] + colors[1].G * weights[1] + colors[2].G * weights[2] + colors[3].G * weights[3], |
| 17 | + colors[0].B * weights[0] + colors[1].B * weights[1] + colors[2].B * weights[2] + colors[3].B * weights[3], |
| 18 | + colors[0].A * weights[0] + colors[1].A * weights[1] + colors[2].A * weights[2] + colors[3].A * weights[3] |
| 19 | + }; |
| 20 | + // Return interpolated data for the new vertex. |
| 21 | + return Color.FromArgb((int)rgba[3], (int)rgba[0], (int)rgba[1], (int)rgba[2]); |
| 22 | + } |
| 23 | + |
| 24 | + static void Main(string[] args) |
| 25 | + { |
| 26 | + // Example input data in the form of a star that intersects itself. |
| 27 | + var inputData = new float[] { 0.0f, 3.0f, -1.0f, 0.0f, 1.6f, 1.9f, -1.6f, 1.9f, 1.0f, 0.0f }; |
| 28 | + |
| 29 | + // Create an instance of the tessellator. Can be reused. |
| 30 | + var tess = new LibTessDotNet.Tess(); |
| 31 | + |
| 32 | + // Construct the contour from inputData. |
| 33 | + // A polygon can be composed of multiple contours which are all tessellated at the same time. |
| 34 | + int numPoints = inputData.Length / 2; |
| 35 | + var contour = new LibTessDotNet.ContourVertex[numPoints]; |
| 36 | + for (int i = 0; i < numPoints; i++) |
| 37 | + { |
| 38 | + // NOTE : Z is here for convenience if you want to keep a 3D vertex position throughout the tessellation process but only X and Y are important. |
| 39 | + contour[i].Position = new LibTessDotNet.Vec3 { X = inputData[i * 2], Y = inputData[i * 2 + 1], Z = 0.0f }; |
| 40 | + // Data can contain any per-vertex data, here a constant color. |
| 41 | + contour[i].Data = Color.Azure; |
| 42 | + } |
| 43 | + // Add the contour with a specific orientation, use "Original" if you want to keep the input orientation. |
| 44 | + tess.AddContour(contour, LibTessDotNet.ContourOrientation.Clockwise); |
| 45 | + |
| 46 | + // Tessellate! |
| 47 | + // The winding rule determines how the different contours are combined together. |
| 48 | + // See http://www.glprogramming.com/red/chapter11.html (section "Winding Numbers and Winding Rules") for more information. |
| 49 | + // If you want triangles as output, you need to use "Polygons" type as output and 3 vertices per polygon. |
| 50 | + tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3, VertexCombine); |
| 51 | + |
| 52 | + // Same call but the last callback is optional. Data will be null because no interpolated data would have been generated. |
| 53 | + //tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3); // Some vertices will have null Data in this case. |
| 54 | + |
| 55 | + Console.WriteLine("Output triangles:"); |
| 56 | + int numTriangles = tess.ElementCount; |
| 57 | + for (int i = 0; i < numTriangles; i++) |
| 58 | + { |
| 59 | + var v0 = tess.Vertices[tess.Elements[i * 3]].Position; |
| 60 | + var v1 = tess.Vertices[tess.Elements[i * 3 + 1]].Position; |
| 61 | + var v2 = tess.Vertices[tess.Elements[i * 3 + 2]].Position; |
| 62 | + Console.WriteLine("#{0} ({1:F1},{2:F1}) ({3:F1},{4:F1}) ({5:F1},{6:F1})", i, v0.X, v0.Y, v1.X, v1.Y, v2.X, v2.Y); |
| 63 | + } |
| 64 | + Console.ReadLine(); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments