What is the reason for the strange behavior of meshbuilder AddPolygon() method?

208 views Asked by At

For a counter-clockwise ordered list of points like this:

        var points = new[]
        {
            new Point3D(-2, -4, 8), //a
            new Point3D(6,  -1, 8), //b
            new Point3D(6,  5,  8), //c
            new Point3D(0,  8,  8), //d
            new Point3D(1,  3,  8), //e
        }.ToList();

I want to add a polygon to my HelixViewPort3D view:

        var meshBuilder = new MeshBuilder(false, false, false);

        meshBuilder.AddPolygon(points);

        var geometryModel = new GeometryModel3D
        {
            Material = Materials.Red,
            BackMaterial = Materials.Blue,
            Geometry = meshBuilder.ToMesh()
        };
        var modelVisual = new ModelVisual3D { Content = geometryModel };
        view.Children.Add(modelVisual);

        int index = 0;
        foreach (var point in points)
        {
            view.Children.Add(new BillboardTextVisual3D
            {
                DepthOffset = 1e-3,
                Position = point,
                Text = string.Format("[{0}] : {1}, {2}", index++, point.X, point.Y)
            });
        }

        view.ZoomExtents();

showing each point's x and y coordinate along side with them using BillboardTextVisual3D, I'm getting this: default list

which is not right, somehow the d:(0, 8) is being connected to a:(-2, -4). and the back material (blue) is showing on top of face material (red).

changing the order of the list though fixes the problem: ordered list

var points = new[]
    {
        new Point3D(1,  3,  8), //e
        new Point3D(-2, -4, 8), //a
        new Point3D(6,  -1, 8), //b
        new Point3D(6,  5,  8), //c
        new Point3D(0,  8,  8), //d
    }.ToList();

the second list is still counter-clockwise ordered the only difference is the starting point (which is now e:(1, 3)). is there any reason why should this effect the outcome?

P.S. I think it should have something to do with the points distance to the origin (0,0,0), starting with the nearest point to the origin somehow do the job. is it a bug or I'm missing something here?

P.S.#2 This problem seems to occur only in the case of concave polygons and has no effect on the convex polygons.

1

There are 1 answers

2
Maxence On BEST ANSWER

AddPolygon() use a triangle fan when positions count is greater than 4, so your last triangle is:

-2, -4, 8
0, 8, 8
1, 3, 8

It turns in clockwise direction, the back material is then in front of the camera. It's the expected behavior.