Пример #1
0
        private void PlatformInitialize()
        {
            _viewport = new Viewport(0, 0, PresentationParameters.BackBufferWidth, PresentationParameters.BackBufferHeight);

            // Ensure the vertex attributes are reset
            _enabledVertexAttributes.Clear();

            // Free all the cached shader programs.
            _programCache.Clear();
            _shaderProgram = null;

            if (GraphicsCapabilities.SupportsFramebufferObjectARB)
            {
                Framebuffer  = new FramebufferObject();
                Renderbuffer = new RenderbufferObject();
            }
            #if !(GLES || MONOMAC)
            else if (GraphicsCapabilities.SupportsFramebufferObjectEXT)
            {
                Framebuffer  = new FramebufferObjectEXT();
                Renderbuffer = new RenderbufferObjectEXT();
            }
            #endif
            else
            {
                throw new PlatformNotSupportedException(
                          "MonoGame requires either ARB_framebuffer_object or EXT_framebuffer_object." +
                          "Try updating your graphics drivers.");
            }
        }
        /// <summary>
        /// Saves this <see cref="FramebufferObject"/>'s image to a stream. You can't save multisampled framebuffers.
        /// </summary>
        /// <param name="framebuffer">The <see cref="FramebufferObject"/> whose image to save.</param>
        /// <param name="stream">The stream to save the framebuffer image to.</param>
        /// <param name="imageFormat">The format the image will be saved as.</param>
        /// <param name="flip">Whether to flip the image after the pixels are read.</param>
        public static void SaveAsImage(this FramebufferObject framebuffer, Stream stream, SaveImageFormat imageFormat, bool flip = false)
        {
            if (framebuffer == null)
            {
                throw new ArgumentNullException(nameof(framebuffer));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            IImageFormat format = ImageUtils.GetFormatFor(imageFormat);

            using Image <Rgba32> image = new Image <Rgba32>((int)framebuffer.Width, (int)framebuffer.Height);
            framebuffer.ReadPixels(image, flip);
            image.Save(stream, format);
        }
        /// <summary>
        /// Reads pixels from this <see cref="FramebufferObject"/>.
        /// </summary>
        /// <param name="framebuffer">The <see cref="FramebufferObject"/> to read pixels from.</param>
        /// <param name="x">The x position of the first pixel to read.</param>
        /// <param name="y">The y position of the first pixel to read.</param>
        /// <param name="image">The image in which to write the pixel data.</param>
        /// <param name="flip">Whether to flip the image after the pixels are read.</param>
        public static void ReadPixels(this FramebufferObject framebuffer, int x, int y, Image <Rgba32> image, bool flip = false)
        {
            if (framebuffer == null)
            {
                throw new ArgumentNullException(nameof(framebuffer));
            }

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            if (!image.TryGetSinglePixelSpan(out Span <Rgba32> pixels))
            {
                throw new InvalidDataException(ImageUtils.ImageNotContiguousError);
            }

            framebuffer.ReadPixels(pixels, x, y, (uint)image.Width, (uint)image.Height);

            if (flip)
            {
                image.Mutate(x => x.Flip(FlipMode.Vertical));
            }
        }
 /// <summary>
 /// Saves this <see cref="FramebufferObject"/>'s image to a file. You can't save multisampled framebuffers.<para/>
 /// If the file already exists, it will be replaced.
 /// </summary>
 /// <param name="framebuffer">The <see cref="FramebufferObject"/> whose image to save.</param>
 /// <param name="file">The name of the file where the image will be saved.</param>
 /// <param name="imageFormat">The format the image will be saved as.</param>
 /// <param name="flip">Whether to flip the image after the pixels are read.</param>
 public static void SaveAsImage(this FramebufferObject framebuffer, string file, SaveImageFormat imageFormat, bool flip = false)
 {
     using FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
     SaveAsImage(framebuffer, fileStream, imageFormat, flip);
 }
 /// <summary>
 /// Reads pixels from this <see cref="FramebufferObject"/>.
 /// </summary>
 /// <param name="framebuffer">The <see cref="FramebufferObject"/> to read pixels from.</param>
 /// <param name="image">The image in which to write the pixel data.</param>
 /// <param name="flip">Whether to flip the image after the pixels are read.</param>
 public static void ReadPixels(this FramebufferObject framebuffer, Image <Rgba32> image, bool flip = false)
 {
     ReadPixels(framebuffer, 0, 0, image, flip);
 }