Ejemplo n.º 1
0
        // Build a BSP tree out of `polygons`. When called on an existing tree, the
        // new polygons are filtered down to the bottom of the tree and become new
        // nodes there. Each set of polygons is partitioned using the first polygon
        // (no heuristic is used to pick a good split).
        public void Build(List <CSG_Polygon> list)
        {
            if (list == null || list.Count < 1)
            {
                return;
            }

            if (plane == null || !plane.Valid())
            {
                plane = new CSG_Plane
                {
                    normal = list[0].plane.normal,
                    w      = list[0].plane.w
                };
            }

            if (polygons == null)
            {
                polygons = new List <CSG_Polygon>();
            }

            var listFront = new List <CSG_Polygon>();
            var listBack  = new List <CSG_Polygon>();

            for (int i = 0; i < list.Count; i++)
            {
                plane.SplitPolygon(list[i], polygons, polygons, ref listFront, ref listBack);
            }

            if (listFront.Count > 0)
            {
                if (front == null)
                {
                    front = new CSG_Node();
                }

                front.Build(listFront);
            }

            if (listBack.Count > 0)
            {
                if (back == null)
                {
                    back = new CSG_Node();
                }

                back.Build(listBack);
            }
        }