简体   繁体   中英

Why is my PNG texture not coming out with transparency?

The bottom right image should have a transparent background.

I load my Notch's PNG via these functions:

public void Image2D(Bitmap bmp, int mipmapReductionLevel = 0)
{
    var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    var data = bmp.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    GL.TexImage2D(TextureTarget.Texture2D, mipmapReductionLevel, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
        OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

    bmp.UnlockBits(data);
}

public void Image2D(string filename, int mipmapReductionLevel = 0)
{
    Image2D(new Bitmap(filename), mipmapReductionLevel);
}

And my fragment shader looks like this:

#version 330

in vec2 TexCoord0;

uniform sampler2D TexSampler;

void main()
{
    gl_FragColor = texture2D(TexSampler, TexCoord0.xy);
}

I've inspected the bmp with the debugger, and used bmp.GetPixel(255,0) (just above that tree sapling, in the black area) and it comes back (0,0,0,0) . The docs say 0 is fully transparent, so... I must be doing something wrong on the OpenGL side of things. But what?


Render function

protected override void OnRenderFrame(FrameEventArgs e)
{
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

    _blockInstanceBuffer.Bind();
    _blockIndexBuffer.Bind();
    GL.DrawElementsInstancedBaseVertex(BeginMode.TriangleStrip, Data.FaceIndices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero, _blockCount, 0);

    SwapBuffers();
}

Just needed to enable blending :

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

I didn't think that was necessary in OpenGL 3 if you write your own shader, but I guess it still is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM