7

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();
}
Zenexer
  • 18,788
  • 9
  • 71
  • 77
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    Ahhh Minecraft, I cannot escape you! – Zenexer Feb 10 '12 at 02:50
  • This looks like a game I saw once somewhere. – Ry- Feb 10 '12 at 02:51
  • Yeah, I think you're definitely loading it properly. That's not the issue. – Zenexer Feb 10 '12 at 02:52
  • Can we see your sprite drawing code? – Zenexer Feb 10 '12 at 02:54
  • @Zenexer: You want to see the render function? I'll add it to the function. It's pretty simple too. – mpen Feb 10 '12 at 02:56
  • What're you up to, anyway? Taking over Minecraft, one block at a time? Suspicious! And why not Java, seeing how Minecraft itself is in Java? You'd likely have more already done for you. (On a side note, I made a Minecraft client in C# a while back. Fun project.) – Zenexer Feb 10 '12 at 02:59
  • @Zenexer: Just building my own voxel-based game. Using sprites from MC for now until I build my own. I prefer C#. Part of the project is learning, not just taking ready-made stuff and slapping it together :) – mpen Feb 10 '12 at 03:01
  • Ah, good luck! Voxel engines are fun. – Zenexer Feb 10 '12 at 03:02
  • Hmm, I just don't see it. I'm coming from DirectX, so I'm probably missing something really simple. (Added the C# tag so it would get more attention.) – Zenexer Feb 10 '12 at 03:06
  • @Zenexer: Yup...it was really simple. Just needed to enable blending :D – mpen Feb 10 '12 at 03:09
  • 1
    ...that is actually what I was looking for. It's BlendState.AlphaBlend in XNA. – Zenexer Feb 10 '12 at 03:10

1 Answers1

11

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.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 2
    Blending is still not controlled by shaders. Even in GL4 core. – Mārtiņš Možeiko Feb 10 '12 at 03:28
  • 1
    Like Mārtiņš Možeiko already told you blending is not controlled by shaders. The simple reason for this is, that blending happens only after the fragment has been processed, i.e. when all shader stages finished execution. The blending then happens in some hardwired part of the GPU. Maybe, in a few years we'll get to see some new fragment shader input variable 'gl_DestColor' or such, which allows us to blend in the fragment shader. But to this day the problems this would cause (this would have a huge impact memory access patterns) inhibt this. – datenwolf Feb 10 '12 at 11:08
  • @datenwolf: Not a problem for me; I only need really simple blending. Are there a lot of use cases where people would actually want to do their own blending? – mpen Feb 10 '12 at 16:31
  • 1
    @Mark: Indeed there are. Just look at the various layer blending modes available in Photoshop or The Gimp. Most of them cen be implemented in OpenGL only by using shaders or texture combiners. – datenwolf Feb 10 '12 at 20:15