Пример #1
0
        /// <summary>
        /// Creates a screenshot of the game and saves it to a file.
        /// </summary>
        /// <param name="filename">The file to save the screenshot to</param>
        /// <param name="resolution">The width/height of the image</param>
        /// <returns>True if the screenshot could be taken, false otherwise</returns>
        public bool TakeScreenshot(string filename, Point resolution)
        {
            try
            {
                using (
                    RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, resolution.X, resolution.Y, false,
                                                                     SurfaceFormat.Color, DepthFormat.Depth24))
                {
                    var frustum     = Camera.GetDrawFrustum();
                    var renderables = EnumerateIntersectingObjects(frustum)
                                      .Where(r => r.IsVisible && !ChunkManager.IsAboveCullPlane(r.GetBoundingBox()));

                    var    oldProjection    = Camera.ProjectionMatrix;
                    Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView(Camera.FOV, ((float)resolution.X) / resolution.Y, Camera.NearPlane, Camera.FarPlane);
                    Camera.ProjectionMatrix = projectionMatrix;
                    GraphicsDevice.SetRenderTarget(renderTarget);
                    DrawSky(new DwarfTime(), Camera.ViewMatrix, 1.0f, Color.CornflowerBlue);
                    Draw3DThings(new DwarfTime(), DefaultShader, Camera.ViewMatrix);

                    DefaultShader.View       = Camera.ViewMatrix;
                    DefaultShader.Projection = Camera.ProjectionMatrix;

                    ComponentRenderer.Render(renderables, new DwarfTime(), ChunkManager, Camera,
                                             DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader,
                                             ComponentRenderer.WaterRenderType.None, 0);
                    InstanceRenderer.Flush(GraphicsDevice, DefaultShader, Camera,
                                           InstanceRenderMode.Normal);


                    GraphicsDevice.SetRenderTarget(null);
                    renderTarget.SaveAsPng(new FileStream(filename, FileMode.Create), resolution.X, resolution.Y);
                    GraphicsDevice.Textures[0] = null;
                    GraphicsDevice.Indices     = null;
                    GraphicsDevice.SetVertexBuffer(null);
                    Camera.ProjectionMatrix = oldProjection;
                }
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
Пример #2
0
        public static IEnumerable <IRenderableComponent> EnumerateVisibleRenderables(
            IEnumerable <IRenderableComponent> Renderables,
            ChunkManager chunks,
            Camera Camera)
        {
            var frustrum = Camera.GetFrustrum();

            // Todo: Use an octtree for this.
            var visibleComponents = Renderables.Where(r =>
            {
                if (!r.IsVisible)
                {
                    return(false);
                }
                if (chunks.IsAboveCullPlane(r.GetBoundingBox()))
                {
                    return(false);
                }
                if (r.FrustumCull)
                {
                    if ((r.GlobalTransform.Translation - Camera.Position).LengthSquared() >=
                        chunks.DrawDistanceSquared)
                    {
                        return(false);
                    }
                    if (!r.GetBoundingBox().Intersects(frustrum))
                    {
                        return(false);
                    }
                }

                return(true);
            }).ToList();

            return(visibleComponents);
        }