在opentk中加载png不能按预期工作

时间:2014-04-01 15:05:34

标签: opengl png opentk

我正在尝试使用OpenGL加载PNG(通过OpenTK)并且图像非常像素化并且颜色错误。

代码:

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

我在这里做错了什么?

编辑:我通过

修正了颜色
format.SourceType = PixelType.UnsignedByte;
format.PixelFormat = PixelFormat.Bgra;

EDIT2:修正了。显然,如果没有正确启用混合,透明度的png会变形!

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

但失真仍在继续

1 个答案:

答案 0 :(得分:0)

失真可能与所描述的内容有关in this topic

尝试使用System.Drawing.Imaging.PixelFormat.Format32bppPArgb查看是否有所作为。