void Draw_BSP_Tree(ref BSPTree tree, Vector4 eye) { if (tree == null) { return; } float result = tree.partition.ClassifyPoint(eye); //float result = 1; if (result > 0) { Draw_BSP_Tree(ref tree.back, eye); Draw_Polygon_List(tree.polygons); Draw_BSP_Tree(ref tree.front, eye); } else if (result < 0) { Draw_BSP_Tree(ref tree.front, eye); Draw_Polygon_List(tree.polygons); Draw_BSP_Tree(ref tree.back, eye); } else // result is 0 { // the eye point is on the partition plane... Draw_BSP_Tree(ref tree.back, eye); Draw_BSP_Tree(ref tree.front, eye); } }
private void setLines() { Polygons = new List <Polygon3>(); VP = camera.ProjectionMatrix * camera.View; myCanvas.Children.Clear(); //row 1 CreateCube(DummyCube, VP, new Vector4(-3, 0, 10, 0)); CreateCube(DummyCube, VP, new Vector4(0, 0, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-6, 0, 10, 0)); CreateCube(DummyCube, VP, new Vector4(0, 0, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-3, 0, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-6, 0, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-3, 0, 16, 0)); CreateCube(DummyCube, VP, new Vector4(0, 0, 16, 0)); CreateCube(DummyCube, VP, new Vector4(-6, 0, 16, 0)); //row 2 CreateCube(DummyCube, VP, new Vector4(0, -3, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-3, -3, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-6, -3, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-3, -3, 13, 0)); CreateCube(DummyCube, VP, new Vector4(0, -3, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-6, -3, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-3, -3, 16, 0)); CreateCube(DummyCube, VP, new Vector4(0, -3, 16, 0)); CreateCube(DummyCube, VP, new Vector4(-6, -3, 16, 0)); //row 3 CreateCube(DummyCube, VP, new Vector4(0, 3, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-3, 3, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-6, 3, 10, 0)); CreateCube(DummyCube, VP, new Vector4(-3, 3, 13, 0)); CreateCube(DummyCube, VP, new Vector4(0, 3, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-6, 3, 13, 0)); CreateCube(DummyCube, VP, new Vector4(-3, 3, 16, 0)); CreateCube(DummyCube, VP, new Vector4(0, 3, 16, 0)); CreateCube(DummyCube, VP, new Vector4(-6, 3, 16, 0)); //DrawLineFromLocal(DummyLine, new Vector4(-2, 0, 40, 0), VP); //DrawLineFromLocal(DummyLine, new Vector4(4, 0, 40, 0), VP); BSPTree tree = new BSPTree(); tree.BuildBSPTree(ref tree, Polygons); Draw_BSP_Tree(ref tree, new Vector4(camera.Position, 0)); }
public void BuildBSPTree(ref BSPTree tree, List <Polygon3> polygons) { Polygon3 root = polygons.First(); polygons.RemoveAt(0); tree.partition = new Plane(root.Points[0], root.Points[1], root.Points[2]); tree.polygons.Add(root); List <Polygon3> frontlist = new List <Polygon3>(); List <Polygon3> backlist = new List <Polygon3>(); while (polygons.Count > 0) { var poly = polygons.First(); polygons.RemoveAt(0); var result = tree.partition.ClassifyPolygon(poly); switch (result) { case Result.COINCIDENT: tree.polygons.Add(poly); break; case Result.IN_BACK_OF: backlist.Add(poly); break; case Result.IN_FRONT_OF: frontlist.Add(poly); break; case Result.SPANNING: Polygon3 front_piece = new Polygon3(), back_piece = new Polygon3(); Split_Polygon(poly, tree.partition, ref front_piece, ref back_piece); backlist.Add(back_piece); frontlist.Add(front_piece); break; } } if (!(frontlist.Count() == 0)) { tree.front = new BSPTree(); BuildBSPTree(ref tree.front, frontlist); } if (!(backlist.Count() == 0)) { tree.back = new BSPTree(); BuildBSPTree(ref tree.back, backlist); } }
public WorldData(string filePath) { using (StreamReader sr = new StreamReader(filePath)) { int id = 0; string line; var vertices = new List <Vector4>(); while ((line = sr.ReadLine()) != null) { if (line.Length > 0) { string[] points = line.Split(' '); switch (line[0]) { case 'v': vertices.Add(new Vector4(float.Parse(points[1]), float.Parse(points[2]), float.Parse(points[3]), 1.0F)); break; case 'f': var poly = new Polygon { ID = id++, vertices = new Vector4[points.Length - 1] }; for (int i = 0; i < points.Length - 1; i++) { poly.vertices[i] = vertices[int.Parse(points[i + 1]) - 1]; } poly.normal = Transformations.CalculateSurfaceNormal(poly); Polygons.Add(poly); break; } } } } BSPTree = new BSPTree(Polygons); Polygons = BSPTree.Polygons; }
private void Split_Polygon(Polygon3 poly, Plane part, ref Polygon3 front, ref Polygon3 back) { int count = poly.NumVertices(), out_c = 0, in_c = 0; Vector4 ptA, ptB; double sideA, sideB; Vector4[] outpts = new Vector4[MAXPTS], inpts = new Vector4[MAXPTS]; List <Vector4> outptsl = new List <Vector4>(), inptsl = new List <Vector4>(); ptA = poly.Points.Last(); sideA = part.ClassifyPoint(ptA); for (short i = -1; ++i < count;) { ptB = poly.Points.ElementAt(i); sideB = part.ClassifyPoint(ptB); if (sideB > 0) { if (sideA < 0) { // compute the intersection point of the line // from point A to point B with the partition // plane. This is a simple ray-plane intersection. Vector4 v = ptB - ptA; float sect = -part.ClassifyPoint(ptA) / Vector4.Distance(part.Normal, v); outptsl.Add(ptA + (v * sect)); inptsl.Add(ptA + (v * sect)); in_c++; out_c++; } //outpts[out_c++] = ptB; outptsl.Add(ptB); out_c++; } else if (sideB < 0) { if (sideA > 0) { // compute the intersection point of the line // from point A to point B with the partition // plane. This is a simple ray-plane intersection. Vector4 v = ptB - ptA; float sect = -part.ClassifyPoint(ptA) / Vector4.Distance(part.Normal, v); //outpts[out_c++] = inpts[in_c++] = ptA + (v * sect); outptsl.Add(ptA + (v * sect)); inptsl.Add(ptA + (v * sect)); in_c++; out_c++; } inptsl.Add(ptB); in_c++; } else { // outpts[out_c++] = inpts[in_c++] = v; outptsl.Add(ptB); inptsl.Add(ptB); in_c++; out_c++; } ptA = ptB; sideA = sideB; } front = new Polygon3(); front.Points = outptsl.ToList(); back = new Polygon3(); back.Points = inptsl.ToList(); }