Beispiel #1
0
        public void Build(ref InteriorResource ir)
        {
            Console.WriteLine("Building BSP");
            var bspnodes = new List <BSPBuilder.BSPNode>();

            foreach (var poly in polygons)
            {
                var leafnode = new BSPBuilder.BSPNode();
                leafnode.IsLeaf  = true;
                leafnode.Polygon = poly;
                var n = new BSPBuilder.BSPNode();
                n.Front = leafnode;
                n.Plane = new Plane(poly.Vertices[0], poly.Normal);
                bspnodes.Add(n);
            }

            var BSPBuilder = new BSPBuilder();
            var root       = BSPBuilder.BuildBSPRecursive(bspnodes);

            polygons.Clear();
            Console.WriteLine("Gathering Polygons");
            root.GatherPolygons(ref polygons);

            interior = new Interior();

            Console.WriteLine("Exporting BSP");
            var orderedpolys = new List <Polygon>();

            ExportBSP(root, ref orderedpolys);

            var groupedPolys = new List <List <Polygon> >();

            int fullpolycount = orderedpolys.Count / 8;
            int rem           = orderedpolys.Count % 8;

            for (int i = 0; i < orderedpolys.Count - rem; i += 8)
            {
                var polysList = new List <Polygon>();
                for (int j = 0; j < 8; j++)
                {
                    polysList.Add(orderedpolys[i + j]);
                }

                groupedPolys.Add(polysList);
            }
            var lastPolys = new List <Polygon>();

            for (int i = orderedpolys.Count - rem; i < orderedpolys.Count; i++)
            {
                lastPolys.Add(orderedpolys[i]);
            }
            if (lastPolys.Count != 0)
            {
                groupedPolys.Add(lastPolys);
            }

            Console.WriteLine("Exporting Convex Hulls");
            ExportConvexHulls(groupedPolys);

            Console.WriteLine("Exporting Zones");
            if (interior.zones == null)
            {
                interior.zones = new List <Zone>();
            }
            var z = new Zone();

            z.portalStart  = 0;
            z.portalCount  = 0;
            z.surfaceStart = 0;
            z.surfaceCount = interior.surfaces.Count;

            interior.zones.Add(z);

            Console.WriteLine("Exporting ZoneSurfaces");
            if (interior.zoneSurfaces == null)
            {
                interior.zoneSurfaces = new MultiSizeIntList <short, short>();
            }

            if (interior.surfaces.Count > ushort.MaxValue)
            {
                throw new Exception("Max surfaces for zone reached");
            }
            for (int i = 0; i < interior.surfaces.Count; i++)
            {
                interior.zoneSurfaces.Add((short)i);
            }

            interior.boundingBox    = GetBoundingBox();
            interior.boundingSphere = GetBoundingSphere();

            Console.WriteLine("Exporting CoordBins");
            ExportCoordBins();

            Console.WriteLine("Finalizing");
            //Initialize all the null values to default values
            interior.interiorFileVersion = 0;
            interior.materialListVersion = 1;
            interior.coordBinMode        = 0;
            interior.baseAmbientColor    = new ColorF()
            {
                red = 1, green = 1, blue = 1, alpha = 1
            };
            interior.alarmAmbientColor = new ColorF()
            {
                red = 1, green = 1, blue = 1, alpha = 1
            };
            interior.detailLevel          = 0;
            interior.minPixels            = 250;
            interior.hasAlarmState        = false;
            interior.numLightStateEntries = 0;
            interior.animatedLights       = new List <AnimatedLight>();
            interior.lightMaps            = new List <Lightmap>();
            interior.lightStates          = new List <LightState>();
            interior.nameBuffer           = new List <byte>();
            interior.nullSurfaces         = new List <NullSurface>();
            interior.portals              = new List <Portal>();
            interior.stateDataBuffer      = new StateDataBuffer();
            interior.stateDatas           = new List <StateData>();
            interior.texMatIndices        = new List <int>();
            interior.texMatrices          = new List <TexMatrix>();
            interior.texNormals           = new List <Vector3>();
            interior.zonePortalList       = new MultiSizeIntList <short, short>();
            interior.extendedLightMapData = new ExtendedLightmapData()
            {
                extendedData = 0
            };

            ir.detailLevels.Add(interior);
        }
Beispiel #2
0
        short ExportBSP(BSPBuilder.BSPNode n, ref List <Polygon> orderedPolys)
        {
            if (interior.bspNodes == null)
            {
                interior.bspNodes = new List <BSPNode>();
            }

            if (interior.bspSolidLeaves == null)
            {
                interior.bspSolidLeaves = new List <BSPSolidLeaf>();
            }

            if (interior.solidLeafSurfaces == null)
            {
                interior.solidLeafSurfaces = new MultiSizeIntList <int, short>();
            }

            if (n.IsLeaf)
            {
                var leafindex = interior.bspSolidLeaves.Count;

                if (leafindex > ushort.MaxValue)
                {
                    throw new Exception("Max BSPSolidLeaves Reached");
                }

                var leaf = new BSPSolidLeaf();
                leaf.surfaceIndex = interior.solidLeafSurfaces.Count;
                leaf.surfaceCount = 0;

                var leafPolyIndices = new List <int>();


                leafPolyIndices.Add(ExportSurface(n.Polygon));
                orderedPolys.Add(n.Polygon);

                for (int i = 0; i < leafPolyIndices.Count; i++)
                {
                    interior.solidLeafSurfaces.Add(leafPolyIndices[i]);
                    leaf.surfaceCount++;
                }

                interior.bspSolidLeaves.Add(leaf);
                return(CreateLeafIndex((ushort)leafindex, true));
            }
            else
            {
                var bspnode   = new BSPNode();
                int nodeindex = interior.bspNodes.Count;
                if (nodeindex > ushort.MaxValue)
                {
                    throw new Exception("Max BSPNodes reached");
                }
                interior.bspNodes.Add(bspnode);
                bspnode.planeIndex = ExportPlane(n.Plane);
                if (n.Front != null)
                {
                    bspnode.frontIndex = ExportBSP(n.Front, ref orderedPolys);
                }
                else
                {
                    bspnode.frontIndex = CreateLeafIndex(0, false);
                }
                if (n.Back != null)
                {
                    bspnode.backIndex = ExportBSP(n.Back, ref orderedPolys);
                }
                else
                {
                    bspnode.backIndex = CreateLeafIndex(0, false);
                }
                return((short)nodeindex);
            }
        }