Why doesn't PNG texture come out with transparency?

5atB6.png

The bottom right image should have a transparent background.

Loading my Notch PNG using the following 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 checked bmpwith the debugger and used bmp.GetPixel(255,0)(just above the tree tree in the black area) and it returns (0,0,0,0). The docs say that 0 is completely transparent, so ... I have to do something wrong on the OpenGL side. 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();
}
+5
source share
1 answer

Just need to enable mixing :

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

I didn’t think it was necessary in OpenGL 3 if you are writing your own shader, but I think it still exists.

S38wa.png

+7

All Articles