Example #1
0
        private void createTexture(bool generateMipMaps, int mipMapCount, IntPtr inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
        {
            Width   = width;
            Height  = height;
            Format  = outputFormat;
            MipMaps = new List <TextureContentMipMap>();
            bool hwCompressedInput  = inputFormat == TextureContentFormat.DXT1 || inputFormat == TextureContentFormat.DXT3 || inputFormat == TextureContentFormat.DXT5;
            bool hwCompressedOutput = outputFormat == TextureContentFormat.DXT1 || outputFormat == TextureContentFormat.DXT3 || outputFormat == TextureContentFormat.DXT5;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                texture = GL.GenTexture();

                GL.BindTexture(TextureTarget.Texture2D, texture);
                setDefaultTextureParameters();

                //GL.TexStorage2D(TextureTarget2d.Texture2D,(GenerateMipMaps ? 1 : MipMapCount),SizedInternalFormat.Rgba8,width,height);
                //GL.TexSubImage2D(TextureTarget.Texture2D,0,0,0,width,height,
                GL.TexImage2D(TextureTarget.Texture2D, 0, (hwCompressedOutput ? (OpenTK.Graphics.OpenGL4.PixelInternalFormat)outputFormat : OpenTK.Graphics.OpenGL4.PixelInternalFormat.Rgba), width, height, 0, (hwCompressedInput ? (OpenTK.Graphics.OpenGL4.PixelFormat)inputFormat : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra), PixelType.UnsignedByte, inputData);
                if (!generateMipMaps)
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, mipMapCount);
                    GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest);
                    GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                }
            });

            PreprocessMipMaps();

            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.DeleteTexture(texture);
            });
        }
Example #2
0
        private void createTexture(bool generateMipMaps, int mipMapCount, IntPtr inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
        {
            Width   = width;
            Height  = height;
            Format  = outputFormat;
            MipMaps = new List <TextureContentMipMap>();
            bool hwCompressedInput  = inputFormat == TextureContentFormat.DXT1 || inputFormat == TextureContentFormat.DXT3 || inputFormat == TextureContentFormat.DXT5;
            bool hwCompressedOutput = outputFormat == TextureContentFormat.DXT1 || outputFormat == TextureContentFormat.DXT3 || outputFormat == TextureContentFormat.DXT5;

            ThreadingHelper.BlockOnUIThread(() =>
            {
                texture = GL.GenTexture();

                GL.BindTexture(TextureTarget.Texture2D, texture);
                bool doGenerate = generateMipMaps && mipMapCount > 1;

                setDefaultTextureParameters();
                //GL.TexStorage2D(TextureTarget2d.Texture2D,(GenerateMipMaps ? 1 : MipMapCount),SizedInternalFormat.Rgba8,width,height);
                //GL.TexSubImage2D(TextureTarget.Texture2D,0,0,0,width,height,
                if (doGenerate)
                {
                    if (graphicsDevice.majorVersion < 3 &&
                        ((graphicsDevice.majorVersion == 1 && graphicsDevice.minorVersion >= 4) ||
                         graphicsDevice.majorVersion > 1))
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
                    }
                    else if (graphicsDevice.majorVersion < 3)
                    {
                        throw new NotSupportedException("Can't generate MipMaps on this Hardware");
                    }
                }
                GL.TexImage2D(TextureTarget.Texture2D, 0, (hwCompressedOutput ? (OpenTK.Graphics.OpenGL4.PixelInternalFormat)outputFormat : OpenTK.Graphics.OpenGL4.PixelInternalFormat.Rgba), width, height, 0, (hwCompressedInput ? (OpenTK.Graphics.OpenGL4.PixelFormat)inputFormat : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra), PixelType.UnsignedByte, inputData);
                if (doGenerate)
                {
                    //TOODO non power of 2 Textures?
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, mipMapCount);
                    GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest);
                    if (graphicsDevice.majorVersion >= 3)
                    {
                        GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                    }
                }
            });

            PreprocessMipMaps();

            ThreadingHelper.BlockOnUIThread(() =>
            {
                GL.DeleteTexture(texture);
            });
        }
Example #3
0
        public override Texture2D Read(ContentManager manager, ContentReader reader)
        {
            bool genMipMaps = reader.ReadBoolean();
            int  mipCount   = reader.ReadInt32();

            int width = reader.ReadInt32(), height = reader.ReadInt32();
            TextureContentFormat format = (TextureContentFormat)reader.ReadInt32();
            bool      hwCompressed      = format == TextureContentFormat.DXT1 || format == TextureContentFormat.DXT3 || format == TextureContentFormat.DXT5;
            Texture2D text;
            int       size              = reader.ReadInt32();

            byte[] buffer = reader.ReadBytes(size);
            if (hwCompressed)
            {
                text = new Texture2D(manager.graphicsDevice, width, height, mipCount, (PixelInternalFormat)format);

                text.SetData <byte>(buffer, 0, (OpenTK.Graphics.OpenGL4.PixelFormat)format);
                //TODO:...
            }
            else
            {
                text = new Texture2D(manager.graphicsDevice, width, height, mipCount);
                using (var stream = new System.IO.MemoryStream(buffer))
                    text = Texture2D.FromBitmap(manager.graphicsDevice, new System.Drawing.Bitmap(stream), mipCount);
            }

            if (genMipMaps)
            {
                return(text);
            }
            for (int i = 1; i < mipCount; i++)
            {
                size         = reader.ReadInt32();
                buffer       = reader.ReadBytes(size);
                hwCompressed = format == TextureContentFormat.DXT1 || format == TextureContentFormat.DXT3 || format == TextureContentFormat.DXT5;
                if (hwCompressed)
                {
                    text.SetData <byte>(buffer, i, (OpenTK.Graphics.OpenGL4.PixelFormat)format);
                }
                else
                {
                    using (var stream = new System.IO.MemoryStream(buffer))
                        text.SetData <byte>(buffer, i);
                }
            }
            return(text);
        }
Example #4
0
 public TextureContent(GraphicsDevice graphicsDevice, bool generateMipMaps, int mipMapCount, IntPtr inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
 {
     this.graphicsDevice = graphicsDevice;
     createTexture(generateMipMaps, mipMapCount, inputData, width, height, inputFormat, outputFormat);
 }
Example #5
0
 protected TextureContentMipMap(int width, int height, TextureContentFormat format)
 {
     Width  = width;
     Height = height;
     Format = format;
 }
Example #6
0
 public TextureContentMipMap(int width, int height, TextureContentFormat format, System.Drawing.Bitmap data)
     : this(width, height, format)
 {
     this.bitmap = data;
 }
Example #7
0
 public TextureContentMipMap(int width, int height, TextureContentFormat format, byte[] data)
     : this(width, height, format)
 {
     this.data = data;
 }
Example #8
0
 public TextureContent(GraphicsDevice graphicsDevice, bool generateMipMaps, int mipMapCount, byte[] inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
 {
     this.graphicsDevice = graphicsDevice;
     System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(inputData, System.Runtime.InteropServices.GCHandleType.Pinned);
     createTexture(generateMipMaps, mipMapCount, handle.AddrOfPinnedObject(), width, height, inputFormat, outputFormat);
     handle.Free();
 }
Example #9
0
 public TextureContent(bool generateMipMaps, int mipMapCount, IntPtr inputData, int width, int height, TextureContentFormat inputFormat, TextureContentFormat outputFormat)
 {
     createTexture(generateMipMaps, mipMapCount, inputData, width, height, inputFormat, outputFormat);
 }