pixartist's picture

PNG distorted after loading

I load a PNG like this:

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);
 
            }
        }
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());
        }

The PNG is loaded, but it is distorted and extremely pixelated, as well as missing colors (It's plain blue instead of light blue). What am I doing wrong?

Inline Images

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Invoke's picture

Don't know if you've found your answer already since it's been a while but i'll answer so others with similar problems might be helped out,

- change "format.SourceType = PixelType.Byte;" to "PixelType.UnsignedByte"
- on Windows make sure that the "format" parameter in TexImage2D is "Bgra" not "Rgba"
- make sure blending is configured correctly
- try switching min and mag filter to Linear