Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Triangulates the specified brush
- /// </summary>
- /// <param name="brush"></param>
- /// <returns></returns>
- private ModelInput BrushToModel(Brush brush)
- {
- // First, we need to get all brush sides and planes associated with the brush
- BrushSide[] sides = new BrushSide[brush.NumSides];
- for (int i = 0; i < brush.NumSides; i++)
- sides[i] = loader.BrushSides[i];
- Plane[] planes = new Plane[sides.Length];
- Mat4[] planemats = new Mat4[sides.Length];
- for (int i = 0; i < sides.Length; i++)
- {
- planes[i] = loader.Planes[sides[i].PlaneNum];
- planemats[i] = MathUtil.ConstructPlaneMatrix(planes[i]);
- }
- // Next, we should calculate all intersections and build a list of all possible vertices
- // At the same time, store indices for vertices found on each plane
- List<Vec3> vertices = new List<Vec3>();
- List<int>[] planeindices = new List<int>[sides.Length];
- for (int i = 0; i < sides.Length; i++)
- planeindices[i] = new List<int>();
- for (int i = 0; i < sides.Length - 2; i++)
- {
- // Get the plane
- Plane plane0 = planes[i];
- // We should find combinations of 3 planes which produce intersections
- for (int j = i + 1; j < sides.Length; j++)
- {
- Plane plane1 = planes[j];
- for (int k = j + 1; k < sides.Length; k++)
- {
- Plane plane2 = planes[k];
- // Now, test for intersection
- Vec3 intersectpt;
- if (MathUtil.IntersectPlanesPoint(plane0, plane1, plane2, out intersectpt))
- {
- // Ok we found an intersection, add the point (or locate the indice for it)
- int idx;
- bool found = false;
- for (idx = 0; idx < vertices.Count; idx++)
- if (MathUtil.VectorEquals(vertices[idx], intersectpt))
- {
- found = true;
- break;
- }
- if (!found)
- {
- idx = vertices.Count;
- vertices.Add(intersectpt);
- }
- // Add the point to the planes
- if (!planeindices[i].Contains(idx)) planeindices[i].Add(idx);
- if (!planeindices[j].Contains(idx)) planeindices[j].Add(idx);
- if (!planeindices[k].Contains(idx)) planeindices[k].Add(idx);
- }
- }
- }
- }
- // Triangulate all planes
- List<int> indices = new List<int>();
- for (int i = 0; i < sides.Length; i++)
- {
- // Get the vertices relative to the plane
- List<int> myindices = planeindices[i];
- Vec2[] planevertices = new Vec2[myindices.Count];
- Plane plane = planes[i];
- int leftmost = -1;
- Vec2 leftmostpt = default(Vec2);
- for (int j = 0; j < myindices.Count; j++)
- {
- // Get the vertex, transform and store
- Vec3 vertex = vertices[myindices[j]];
- Vec4 localtoplane = MathUtil.RoundCoordinate(planemats[i] * Vec4.FromVec3(vertex, 1.0));
- Vec2 planevertex = new Vec2() { x = localtoplane.x, y = localtoplane.y };
- planevertices[j] = planevertex;
- // Check leftmost
- if (leftmost == -1 || planevertex.x < leftmostpt.x)
- {
- leftmost = j;
- leftmostpt = planevertex;
- }
- }
- // Start at vertex a, build convex hull
- int[] hull = new int[myindices.Count];
- int pointonhull = leftmost;
- int k = 0;
- int endpoint;
- do
- {
- endpoint = 0;
- hull[k++] = pointonhull;
- Vec2 curpoint = planevertices[pointonhull];
- for (int m = 0; m < myindices.Count; m++)
- if (m != pointonhull)
- {
- Vec2 mpoint = planevertices[m];
- if (endpoint == pointonhull || MathUtil.PointLine(mpoint, curpoint, planevertices[endpoint]) == MathUtil.PointLineRelationship.Left)
- endpoint = m;
- }
- pointonhull = endpoint;
- } while (endpoint != hull[0]);
- // Triangulate hull
- for (int j = 1; j < myindices.Count - 1; j++)
- {
- indices.Add(myindices[0]);
- indices.Add(myindices[j]);
- indices.Add(myindices[j + 1]);
- }
- }
- // Return the model
- return new ModelInput(vertices.ToArray(), new int[][] { indices.ToArray() });
- }
Advertisement
Add Comment
Please, Sign In to add comment