Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Clipping multiple triangles and vertex ordering

This topic is 3048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently clipping triangles from a mesh to 6 planes in order create a new mesh for a decal. Unfortunately I cannot figure out how to combine the clipped vertices correctly in order create the decal's mesh. The following code: - Detects the triangles that intersect the decal's OBB - Each triangle that the OBB intersects is clipped to the 6 planes of the OBB - The clipped vertices are then added to a list that makes the final decal's mesh vertices
            // Test each triangle that make up the intersecting sufraces
            for (int i = 0; i < triangles.Count; ++i)
            {
                CD_Triangle tri = triangles;

                if (Vector3.Dot(normal, tri.Normal) < 0.001f)
                {
                    // Triangle is facing away from decal's normal direction and can be ignored
                    continue;
                }

                // Triangle points must be wound CCW
                Vector3[] v_ToClip = new Vector3[3];

                if (tri.IsWoundCCW())
                {
                    v_ToClip[0] = tri.GetVertex(0);
                    v_ToClip[1] = tri.GetVertex(1);
                    v_ToClip[2] = tri.GetVertex(2);
                }
                else
                {
                    v_ToClip[0] = tri.GetVertex(1);
                    v_ToClip[1] = tri.GetVertex(0);
                    v_ToClip[2] = tri.GetVertex(2);
                }

                // Following array holds all vertices that have been clipped behind each plane tested
                Vector3[] v_Clipped = new Vector3[0];

                // Clip current triangle vertices to each of the 6 OBB planes
                for (int ii = 0; ii < 6; ++ii)
                {
                    v_Clipped = ClipVerticesToPlane(planes[ii], v_ToClip);

                    // If all vertices lie on the positive side of the plane
                    if (v_ToClip.Length == 0)
                    {            
                        // Discard this polygon
                        break;
                    }
                    else
                    {
                        // Otherwise set the new vertices to clip using the clipped vertices
                        v_ToClip = v_Clipped;
                    }
                }

                // Add clipped vertices for this triangle to the final decal vertices
                decalVertices.AddRange(v_Clipped);
            }


The list of clipped vertices should represent the final mesh however, some vertices might be duplicated. Also, because each triangle is clipped separately I have no idea how to index the vertices so that the mesh decal is drawn correctly. Can anyone please help? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!