public void CalculateAABB() { Min = Faces[0].Polys[0].Verts[0].P; Max = Faces[0].Polys[0].Verts[0].P; for (int i = 0; i < NumberOfFaces; i++) { Face face = Faces[i]; for (int j = 0; j < face.Polys.Length; j++) { Poly poly = face.Polys[j]; for (int k = 0; k < poly.NumberOfVertices; k++) { Vertex vert = poly.Verts[k]; // calculate minimum Vec3 min = Min; if (vert.P.X < Min.X) { min.X = vert.P.X; } if (vert.P.Y < Min.Y) { min.Y = vert.P.Y; } if (vert.P.Z < Min.Z) { min.Z = vert.P.Z; } Min = min; // calculate maximum Vec3 max = Max; if (vert.P.X > Max.X) { max.X = vert.P.X; } if (vert.P.Y > Max.Y) { max.Y = vert.P.Y; } if (vert.P.Z > Max.Z) { max.Z = vert.P.Z; } Max = max; } } } }
public static PolyClassification ClassifyPoly(Poly poly) { bool front = false; bool back = false; float distance; for (int i = 0; i < poly.NumberOfVertices; i++) { distance = Vec3.Dot(poly.P.Normal, poly.Verts[i].P) + (float)poly.P.Distance; if (distance > 0.001) { if (back) { return(PolyClassification.SPLIT); } front = true; } else if (distance < -0.001) { if (front) { return(PolyClassification.SPLIT); } back = true; } } if (front) { return(PolyClassification.FRONT); } else if (back) { return(PolyClassification.BACK); } return(PolyClassification.ON_PLANE); }
public Poly(Poly p) { Verts = p.Verts; P = p.P; }
public void SplitPoly(Poly poly, ref Poly front, ref Poly back) { PointClassification[] pointClassification = new PointClassification[poly.NumberOfVertices]; // classify all points for (int i = 0; i < poly.NumberOfVertices; i++) { pointClassification[i] = P.ClassifyPoint(poly.Verts[i].P); } // build fragments Poly _front = new Poly(); Poly _back = new Poly(); _front.P = poly.P; _back.P = poly.P; for (int i = 0; i < poly.NumberOfVertices; i++) { // add point to appropriate list switch (pointClassification[i]) { case PointClassification.FRONT: _front.Verts.Add(poly.Verts[i]); break; case PointClassification.BACK: _back.Verts.Add(poly.Verts[i]); break; case PointClassification.ON_PLANE: _front.Verts.Add(poly.Verts[i]); _back.Verts.Add(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 (pointClassification[i] == PointClassification.ON_PLANE && pointClassification[iNext] != PointClassification.ON_PLANE) { ignore = true; } else if (pointClassification[iNext] == PointClassification.ON_PLANE && pointClassification[i] != PointClassification.ON_PLANE) { ignore = true; } if (!ignore && pointClassification[i] != pointClassification[iNext]) { Vertex v = null; // new vertex made by splitting float p = 0; // percentage between the 2 points poly.P.GetIntersection(poly.Verts[i].P, poly.Verts[iNext].P, ref v, ref 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]); _front.Verts.Add(v); _back.Verts.Add(v); } } _front.CalculatePlane(); _back.CalculatePlane(); }