0
我想加載一個PNG與OpenGL(通過OpenTK)和圖像是非常像素化,並具有錯誤的顏色。在opentk中加載PNG不能正常工作
代碼:
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);
}
}
我在做什麼錯在這裏?
編輯:固定它:我經由
format.SourceType = PixelType.UnsignedByte;
format.PixelFormat = PixelFormat.Bgra;
EDIT2固定的顏色。顯然,當透明度未正確啓用時,透明度的png會發生扭曲!
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
但是失真在
如果您需要快速修復,請使用jpg代替png,gdi格式argb,opengl格式bgra ...運行良好... –