0

I'm trying to load a PNG with OpenGL (via OpenTK) and the Image is extremely pixelated and has the wrong color.

The code:

private void Create(SurfaceFormat format)
        {
            textureHandle = (uint)GL.GenTexture();
            //bind texture
            GL.BindTexture(TextureTarget.Texture2D, textureHandle);
            Log.Error("Bound Texture: " + GL.GetError());
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)format.WrapMode);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)format.WrapMode);
            Log.Error("Created Texture Parameters: " + GL.GetError());
            GL.TexImage2D(TextureTarget.Texture2D, 0, format.InternalFormat, Width, Height, 0, format.PixelFormat, format.SourceType, format.Pixels);
            Log.Error("Created Image: " + GL.GetError());
            //unbind texture
            GL.BindTexture(TextureTarget.Texture2D, 0);

            //create fbo
            fboHandle = (uint)GL.GenFramebuffer();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboHandle);
            GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureHandle, 0);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            Log.Error("Created Framebuffer: " + GL.GetError());
        }
        public void CreateFromPNG(string filePath)
        {
            //check if the file exists
            if (System.IO.File.Exists(filePath))
            {
                //make a bitmap out of the file on the disk
                System.Drawing.Bitmap textureBitmap = new System.Drawing.Bitmap(filePath);
                //get the data out of the bitmap
                System.Drawing.Imaging.BitmapData textureData =
                textureBitmap.LockBits(
                        new System.Drawing.Rectangle(0, 0, textureBitmap.Width, textureBitmap.Height),
                        System.Drawing.Imaging.ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppArgb
                    );
                if(textureBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                {
                    Log.Error("PNG Pixel format not supported ("+ filePath +") -> " + textureBitmap.PixelFormat.ToString());
                    return;
                }
                SurfaceFormat format = new SurfaceFormat();
                format.Pixels = textureData.Scan0;
                format.SourceType = PixelType.Byte;
                Create(format);
                //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
                textureBitmap.UnlockBits(textureData);

            }
        }

Result when rendering

What am I doing wrong here?

EDIT: I fixed the colors via

format.SourceType = PixelType.UnsignedByte;
format.PixelFormat = PixelFormat.Bgra;

EDIT2: Fixed it. Apparently pngs with transparency get distorted when blending is not correctly enabled!

GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

But the distortion is still going on

pixartist
  • 1,137
  • 2
  • 18
  • 40
  • if you need a quick fix, use jpg instead of png, gdi format argb, opengl format bgra...working well... – j-p Apr 02 '14 at 19:59

1 Answers1

0

The distortion might be related to what is described in this topic.

Try using System.Drawing.Imaging.PixelFormat.Format32bppPArgb to see if it makes a difference.

Community
  • 1
  • 1
The Fiddler
  • 2,726
  • 22
  • 28