Beispiel #1
0
        /// <summary>
        /// Generates a full set of mipmaps for the texture.
        /// </summary>
        /// <param name="overwriteExistingMipmaps">true if the existing mipmap set is replaced with the new set; false otherwise.</param>
        public virtual void GenerateMipmaps(bool overwriteExistingMipmaps)
        {
            // If we already have mipmaps and we're not supposed to overwrite
            // them then return without any generation.
            if (!overwriteExistingMipmaps && faces.Any(f => f.Count > 1))
            {
                return;
            }

            // Generate the mips for each face.
            foreach (var face in faces)
            {
                // Remove any existing mipmaps.
                var faceBitmap = face[0];
                face.Clear();
                face.Add(faceBitmap);
                var faceType = faceBitmap.GetType();
                int width    = faceBitmap.Width;
                int height   = faceBitmap.Height;
                while (width > 1 && height > 1)
                {
                    width  /= 2;
                    height /= 2;

                    var mip = (BitmapContent)Activator.CreateInstance(faceType, new object[] { width, height });
                    BitmapContent.Copy(faceBitmap, mip);
                    face.Add(mip);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generates a full set of mipmaps for the texture.
        /// </summary>
        /// <param name="overwriteExistingMipmaps">true if the existing mipmap set is replaced with the new set; false otherwise.</param>
        public virtual void GenerateMipmaps(bool overwriteExistingMipmaps)
        {
            var imageAttr = new ImageAttributes();

            imageAttr.SetWrapMode(WrapMode.TileFlipXY);

            // If we already have mipmaps and we're not supposed to overwrite
            // them then return without any generation.
            if (!overwriteExistingMipmaps && faces.Any(f => f.Count > 1))
            {
                return;
            }

            // Generate the mips for each face.
            foreach (var face in faces)
            {
                // Remove any existing mipmaps.
                var faceBitmap = face[0];
                face.Clear();
                face.Add(faceBitmap);

                int width = faceBitmap.Width, height = faceBitmap.Height;
                while (width > 1 && height > 1)
                {
                    var systemBitmap = face[face.Count - 1].ToSystemBitmap();
                    width  /= 2;
                    height /= 2;

                    var bitmap = new Bitmap(width, height);
                    using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
                    {
                        var destRect = new System.Drawing.Rectangle(0, 0, width, height);
                        graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
                        graphics.DrawImage(systemBitmap, destRect, 0, 0, width * 2, height * 2, GraphicsUnit.Pixel, imageAttr);
                    }

                    face.Add(bitmap.ToXnaBitmap(false)); //we dont want to flip textures twice
                    systemBitmap.Dispose();
                }
            }
        }