/// <summary> /// Create the polygons from the faces /// </summary> public Poly GetPolys() { int nFaces = 0; Face face = this; while (face != null) { face = face.Next; nFaces++; } Poly polyList = null; Face lfi = null; Face lfj = null; Face lfk = null; // Create polygons face = this; for (int c = 0; c < nFaces; c++) { if (polyList == null) { polyList = new Poly(); } else { polyList.AddPoly(new Poly()); } if (c == nFaces - 3) { lfi = face.Next; } else if (c == nFaces - 2) { lfj = face.Next; } else if (c == nFaces - 1) { lfk = face.Next; } face = face.Next; } // Loop through faces and create polygons Poly pi = polyList; for (Face fi = this; fi != lfi; fi = fi.Next) { Poly pj = pi.Next; for (Face fj = fi.Next; fj != lfj; fj = fj.Next) { Poly pk = pj.Next; for (Face fk = fj.Next; fk != lfk; fk = fk.Next) { Vector3 p = Vector3.zero; if (fi.plane.GetIntersection(fj.plane, fk.plane, ref p)) { Face f = this; while (true) { if (f.plane.ClassifyPoints(p) == Plane.eCP.FRONT) { break; } if (!f.IsLast) { Vertex v = new Vertex { p = p }; pi.AddVertex(v); pj.AddVertex(v); pk.AddVertex(v); break; } f = f.Next; } } pk = pk.Next; } pj = pj.Next; } pi = pi.Next; } return(polyList); }
public void SplitPoly(Poly poly, ref Poly front, ref Poly back) { Plane.eCP[] cp = new Plane.eCP[poly.NumberOfVertices]; // classify all points for (int i = 0; i < poly.NumberOfVertices; i++) { cp[i] = plane.ClassifyPoints(poly.verts[i].p); } // builds fragments Poly newFront = new Poly(); Poly newBack = new Poly(); newFront.TextureID = poly.TextureID; newBack.TextureID = poly.TextureID; newFront.plane = poly.plane; newBack.plane = poly.plane; for (int i = 0; i < poly.NumberOfVertices; i++) { // Add point to appropriate list switch (cp[i]) { case Plane.eCP.FRONT: newFront.AddVertex(poly.verts[i]); break; case Plane.eCP.BACK: newBack.AddVertex(poly.verts[i]); break; case Plane.eCP.ONPLANE: newFront.AddVertex(poly.verts[i]); newBack.AddVertex(poly.verts[i]); break; } // Check if edges should be split int iNext = i + 1; bool ignore = false; if (i == (poly.NumberOfVertices - 1)) { iNext = 0; } if (cp[i] == Plane.eCP.ONPLANE && cp[iNext] != Plane.eCP.ONPLANE) { ignore = true; } else if (cp[iNext] == Plane.eCP.ONPLANE && cp[i] != Plane.eCP.ONPLANE) { ignore = true; } if (!ignore && (cp[i] != cp[iNext])) { Vertex v = new Vertex(); // New vertex created by splitting float p = 0f; // Percentage between the two points plane.GetIntersection(poly.verts[i].p, poly.verts[iNext].p, v.p, p); v.tex[0] = poly.verts[iNext].tex[0] - poly.verts[i].tex[0]; v.tex[1] = poly.verts[iNext].tex[1] - poly.verts[i].tex[1]; v.tex[0] = poly.verts[i].tex[0] + (p * v.tex[0]); v.tex[1] = poly.verts[i].tex[1] + (p * v.tex[1]); newFront.AddVertex(v); newBack.AddVertex(v); } } newFront.CalculatePlane(); newBack.CalculatePlane(); front = newFront; back = newBack; }