Get visible triangles of a mesh

In order to get the visible triangles of a mesh you need to:

1) Derive the Mesh class and override its DrawForSelection method in order to draw the triangles in false colors:

public class MyMesh : Mesh
{
            public MyMesh(Mesh other, ViewportLayout vp) : base(other)
            {
                this.vp = vp;
            }

            public bool GetAllVisibleTriangles { get; set; }

            private ViewportLayout vp;

            protected override void DrawForSelection(DrawParams data)
            {
                if (GetAllVisibleTriangles)
                {
                    // Draws the quads with the color-coding needed for visibility computation 
                    for (int i = 0; i < Triangles.Length; i++)
                    {
                        vp.SetColorDrawForSelection(i);

                        IndexTriangle tri = Triangles[i];
                        data.RenderContext.DrawTriangles(new Point3D[]
                        {
                            Vertices[tri.V1],
                            Vertices[tri.V2],
                            Vertices[tri.V3],
                        },
                            new Vector3D[]
                            {
                                Vector3D.AxisZ,
                                Vector3D.AxisZ,
                                Vector3D.AxisZ,
                            });
                    }
                }
                else
                    base.DrawForSelection(data);
            }


            public int[] SelectedTriangles;

            
            protected override void Draw(DrawParams data)
            {
                // Just for Debug
                if (SelectedTriangles != null)
                {
                    for (int i = 0; i < SelectedTriangles.Length; i++)
                    {
                        SmoothTriangle tri = (SmoothTriangle) Triangles[SelectedTriangles[i]];
                        data.RenderContext.DrawTriangles(new Point3D[]
                        {
                            Vertices[tri.V1],
                            Vertices[tri.V2],
                            Vertices[tri.V3],
                        },
                            new Vector3D[]
                            {
                               Normals[tri.N1],
                               Normals[tri.N2],
                               Normals[tri.N3],
                            });
                    }
                }
                else
                    base.Draw(data);
            }
}

 

2) Derive the ViewportLayout and expose a method that gets the visible triangles:

public class MyViewportLayout : ViewportLayout
{
        public int[] GetVisibleTriangles()
        {
            Form1.MyMesh mymesh = null;

            // Set Selectable = false to all the entities different from the MyMesh, so they don't interfere (only one MyMesh is supported)

            for (int i = 0; i &lt; Entities.Count; i++)
            {
                if (!(Entities[i] is Form1.MyMesh))
                    Entities[i].Selectable = false;

                else

                    mymesh = ((Form1.MyMesh) Entities[i]);
            }

            mymesh.GetAllVisibleTriangles = true;

            int[] triangles = GetAllVisibleEntities(new Rectangle(0, 0, Size.Width, Size.Height));

            mymesh.GetAllVisibleTriangles = false;

            for (int i = 0; i &lt; Entities.Count; i++)
            
                Entities[i].Selectable = true;

            mymesh.SelectedTriangles = triangles; // For debug

            return triangles;
        } 
       
}

 

 3) Create the MyMesh object and call the exposed method to get its visible vertices:

 

viewportLayout1.Entities.Add(new Form1.MyMesh(Mesh.CreateTorus(20, 5, 10, 20), viewportLayout1),0, Color.Red);
....
int[] visibleTriangles = viewportLayout1.GetVisibleTriangles();

 

On the left the original mesh, on the right the same mesh with just the visible triangles drawn after the view has been rotated a bit:

Capture.PNG                 Capture2.PNG

 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.