Example #1
0
        static public void OptimizeFaces(Faces faces)
        {
            int   removalCount   = 0;
            Faces facesOptimized = new Faces();

            facesOptimized.AddRange(faces);
            for (int i = 0; i < faces.Count; i++)
            {
                bool bNotRemoved = true;
                Face face        = faces[i];
                for (int j = 0; j < facesOptimized.Count && bNotRemoved == true; j++)
                {
                    Face facePossibleContainer = facesOptimized[j];
                    if (facePossibleContainer == face)
                    {
                        continue;
                    }
                    if (Vector3D.Dot(facePossibleContainer.GetNormal(), face.GetNormal()) < -0.9999)
                    {
                        Vector3D midPoint = face.GetMidPoint();
                        if (facePossibleContainer.IsContained(midPoint) == true &&
                            facePossibleContainer.GetPlane().GetSign(midPoint) == 0)
                        {
                            int containedPoints = 0;
                            for (int p = 0; p < face.Points.Count; p++)
                            {
                                if (facePossibleContainer.IsContained(face.Points[p]))
                                {
                                    containedPoints++;
                                }
                            }
                            if (containedPoints == face.Points.Count)
                            {
                                facesOptimized.Remove(face);
                                //Debug.Write( "." );
                                bNotRemoved = false;
                                removalCount++;
                            }
                        }
                    }
                }
            }
            faces.Clear();
            faces.AddRange(facesOptimized);
        }
Example #2
0
        //-----------------------------------------------------------------------------

        static public World  FromWorldcraftMap(WorldcraftMap wm)
        {
            // create new world
            World world = new World();

            world.Textures = wm.Textures;
            world.SkyBox   = new SkyBox("redSky");
            world.FileName = Path.Combine(ExoEngine.sWorldPath, Path.ChangeExtension(Path.GetFileName(wm.FileName), ExoEngine.sWorldExtension));
            world.Dirty    = true;


            // find the starting location
            WorldcraftObject woStartPosition = wm.Objects.FindByClassName("StartPosition");

            if (woStartPosition == null)
            {
                return(null);
            }

            Vector3D ptStartPosition;

            FaceUtils.GetMidPoint(woStartPosition.Faces, out ptStartPosition);
            world.StartPosition    = ptStartPosition;
            world.StartOrientation = woStartPosition.GetPropertyInteger("orientation", 0);

            // find the main light
            WorldcraftObject woLight = wm.Objects.FindByClassName("Light");

            if (woLight == null)
            {
                return(null);
            }

            Vector3D ptLight;

            FaceUtils.GetMidPoint(woLight.Faces, out ptLight);
            world.Light = ptLight;

            // find "Default" group... it is the static world
            WorldcraftObject woDefault = wm.Objects.FindByClassName("Default");

            if (woDefault == null)
            {
                return(null);
            }

            // setup BSP tree
            Faces faces = new Faces();

            // handle the other objects
            foreach (WorldcraftObject wo in wm.Objects)
            {
                if (wo.ClassName == "Water")
                {
                    Vector3D ptMin, ptMax;
                    FaceUtils.GetExtents(wo.Faces, out ptMin, out ptMax);
                    Water water = new Water(ptMin, ptMax, wo.GetPropertyFloat("waveHeight", 100));
                    water.Color = wo.GetPropertyColor("color", Color.Azure);
                    world.Entities.Add(water);
                }
                else if (wo.ClassName == "Duck")
                {
                    Vector3D ptMin, ptMax;
                    FaceUtils.GetExtents(wo.Faces, out ptMin, out ptMax);
                    Duck duck = new Duck(ptMin, ptMax);
                    duck.LoadDataSet(Path.Combine(ExoEngine.sSpritePath, "duck.odf"), wo.GetPropertyColor("color", Color.Yellow));
                    world.Entities.Add(duck);

                    foreach (Face face in wo.Faces)
                    {
                        face.Visible = false;
                    }
                    faces.AddRange(wo.Faces);
                }
            }

            world.Entities.SortByPriority();

            faces.AddRange(woDefault.Faces);
            FaceUtils.OptimizeFaces(faces);
            world.BSPTreeRoot = BSPTreeNode.FromFaces(faces);

            return(world);
        }