Esempio n. 1
0
        private void DrawFlat(GraphicsDevice graphicsDevice, SectorBounds bounds, ISector rootSector)
        {
            RenderTarget2D renderTarget = renderTargets[rootSector];

            // Set the render target
            graphicsDevice.SetRenderTarget(renderTarget);
            graphicsDevice.DepthStencilState = new DepthStencilState()
            {
                DepthBufferEnable = true
            };
            graphicsDevice.Clear(Pallete.OCEAN_BLUE);

            double         relativeCameraZoom = camera.cameraZoom - Math.Log(ZCoords.GetSectorManager().GetTopmostOSMSectors().Count, 4) + (Game1.RECORDING ? 1 : 0);
            int            zoomLevel          = Math.Min(Math.Max((int)(relativeCameraZoom - 3), 0), ZCoords.GetSectorManager().GetHighestOSMZoom());
            List <ISector> containedSectors   = rootSector.GetSectorsInRange(bounds.minX, bounds.maxX, bounds.minY, bounds.maxY, zoomLevel);
            List <ISector> sorted             = containedSectors.Where(x => x.GetRoot().Equals(rootSector)).ToList();

            sorted.Sort((x, y) => x.Zoom.CompareTo(y.Zoom));
            foreach (var sector in sorted)
            {
                IGraphicsBuffer buffer = loadedMaps[sector];
                if (!(buffer is ImageTileBuffer))
                {
                    continue;
                }
                Matrixd       projection = Matrixd.CreateOrthographicOffCenter(bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.maxY * (1 << sector.Zoom) - sector.Y, bounds.minY * (1 << sector.Zoom) - sector.Y, -1, 0.01f); // TODO: why negative?
                RenderContext context    = new RenderContext(graphicsDevice, projection, bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.minY * (1 << sector.Zoom) - sector.Y, bounds.maxY * (1 << sector.Zoom) - sector.Y, camera.cameraZoom, RenderContext.LayerPass.MAIN_PASS);
                buffer.Draw(context);
            }
        }
Esempio n. 2
0
 public Texture2D GetImage(GraphicsDevice graphicsDevice)
 {
     if (RENDER_BUFFER == null)
     {
         Vector2d topLeft     = new Vector2d(0, 0);
         Vector2d bottomRight = new Vector2d(1, 1);
         Matrixd  projection  = Matrixd.CreateOrthographicOffCenter(0, 1, Math.Sqrt(0.5), 0, -2, 2); // TODO: why negative?
                                                                                                     //projection = Matrixd.CreateOrthographicOffCenter(0.1, 0.105, 0.1 + Math.Sqrt(0.5) * 0.005, 0.1, -2, 2); // TODO: why negative?
         Matrixd skew = Matrixd.CreateRotationX(Math.PI / 4);
         context              = new RenderContext(graphicsDevice, skew * projection, topLeft.X, bottomRight.X, topLeft.Y, bottomRight.Y, 0, RenderContext.LayerPass.MAIN_PASS);
         context.highQuality  = true;
         context.deferred     = false;
         context.treeExtraPH  = Math.Sqrt(0.5); // undo our stretching when measuring tree height (TODO: very hacky)
         context.treeLayer    = MakeDefaultRenderTarget(graphicsDevice);
         context.grassLayer   = MakeDefaultRenderTarget(graphicsDevice);
         RENDER_BUFFER        = MakeDefaultRenderTarget(graphicsDevice);
         TREE_DENSITY_BUFFER  = context.treeLayer;
         GRASS_DENSITY_BUFFER = context.grassLayer;
     }
     InitDraw(context);
     graphicsDevice.SetRenderTarget(TREE_DENSITY_BUFFER);
     context.layerPass = RenderContext.LayerPass.TREE_DENSITY_PASS;
     Draw(context);
     graphicsDevice.SetRenderTarget(GRASS_DENSITY_BUFFER);
     context.layerPass = RenderContext.LayerPass.GRASS_DENSITY_PASS;
     Draw(context);
     graphicsDevice.SetRenderTarget(RENDER_BUFFER);
     context.layerPass = RenderContext.LayerPass.MAIN_PASS;
     Draw(context);
     return(DownScale(graphicsDevice, RENDER_BUFFER, 512));
 }
        internal static Matrixd GetWorldProjectiond(double distance, double aspectRatio)
        {
            switch (MODE)
            {
            case 0:
                return(Matrixd.CreatePerspectiveFieldOfView(Math.PI / 2, aspectRatio, distance * 0.1f, distance * 100));

            case 1:
                distance *= M_1;
                return(Matrixd.CreatePerspectiveFieldOfView(Math.PI / 4, aspectRatio, distance * 0.5f, distance * 2));

            case 2:
                distance *= M_2;
                return(Matrixd.CreateOrthographicOffCenter(-0.2 * distance * aspectRatio, 0.2 * distance * aspectRatio, -0.2 * distance, 0.2 * distance, distance * 0.1, distance * 100));

            case 3:
                return(Matrixd.CreatePerspectiveFieldOfView(Math.PI * 80 / 180, aspectRatio, distance * 0.1f, distance * 100));
            }
            throw new NotImplementedException();
        }
Esempio n. 4
0
        private void InitDraw(GraphicsDevice graphicsDevice, SectorBounds bounds, ISector rootSector)
        {
            double relativeCameraZoom = camera.cameraZoom - Math.Log(ZCoords.GetSectorManager().GetTopmostOSMSectors().Count, 4) + (Game1.RECORDING ? 1 : 0);
            // autoload stuff
            // TODO: move to update step?
            int            zoomLevel        = Math.Min(Math.Max((int)(relativeCameraZoom - 3), 0), ZCoords.GetSectorManager().GetHighestOSMZoom());
            List <ISector> containedSectors = rootSector.GetSectorsInRange(bounds.minX, bounds.maxX, bounds.minY, bounds.maxY, zoomLevel);

            foreach (var pair in loadedMaps.Where(x => AllowUnload(x.Key, rootSector, containedSectors)).ToList())
            {
                loadedMaps[pair.Key].Dispose();
                loadedMaps.Remove(pair.Key);
            }
            // end autoload stuff
            if (toLoad != null || Constants.TO_LOAD != null)
            {
                if (Constants.TO_LOAD != null)
                {
                    toLoad = ZCoords.GetSectorManager().FromString(Constants.TO_LOAD);
                }
                Stopwatch sw = new Stopwatch();
                sw.Start();
                foreach (var sector in toLoad.GetChildrenAtLevel(ZCoords.GetSectorManager().GetHighestOSMZoom()))
                {
                    osmSectorLoader.GetGraphicsBuffer(graphicsDevice, sector).Dispose();
                }
                Console.WriteLine($"Total load time for {toLoad} is {sw.Elapsed.TotalHours} h");
                toLoad = null;
                if (Constants.TO_LOAD != null)
                {
                    Constants.TERMINATE = true;
                    Constants.TO_LOAD   = null;
                }
            }
            bool loadCache = !(relativeCameraZoom - 4 > ZCoords.GetSectorManager().GetHighestOSMZoom());

            foreach (var l in containedSectors)
            {
                if (loadCache)
                {
                    if (!loadedMaps.ContainsKey(l))
                    {
                        loadedMaps[l] = osmSectorLoader.GetCacheBuffer(graphicsDevice, l);
                    }
                }
                else
                {
                    if (!loadedMaps.ContainsKey(l) || loadedMaps[l] is ImageTileBuffer)
                    {
                        if (loadedMaps.ContainsKey(l))
                        {
                            loadedMaps[l].Dispose();
                        }
                        loadedMaps[l] = osmSectorLoader.GetGraphicsBuffer(graphicsDevice, l);
                    }
                }
            }
            List <ISector> sorted = containedSectors.Where(x => x.GetRoot().Equals(rootSector)).ToList();

            sorted.Sort((x, y) => x.Zoom.CompareTo(y.Zoom));
            foreach (var sector in sorted)
            {
                IGraphicsBuffer buffer     = loadedMaps[sector];
                Matrixd         projection = Matrixd.CreateOrthographicOffCenter(bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.maxY * (1 << sector.Zoom) - sector.Y, bounds.minY * (1 << sector.Zoom) - sector.Y, -1, 0.01f); // TODO: why negative?
                RenderContext   context    = new RenderContext(graphicsDevice, projection, bounds.minX * (1 << sector.Zoom) - sector.X, bounds.maxX * (1 << sector.Zoom) - sector.X, bounds.minY * (1 << sector.Zoom) - sector.Y, bounds.maxY * (1 << sector.Zoom) - sector.Y, camera.cameraZoom, RenderContext.LayerPass.MAIN_PASS);
                buffer.InitDraw(context);
            }
        }