右下方的图片应该具有透明背景。
我通过以下函数加载Notch的PNG:
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);
}
我的片段着色器看起来像这样:
#version 330
in vec2 TexCoord0;
uniform sampler2D TexSampler;
void main()
{
gl_FragColor = texture2D(TexSampler, TexCoord0.xy);
}
我用调试器检查了bmp
,并使用bmp.GetPixel(255,0)
(就在那棵树苗上方的黑色区域),结果是(0,0,0,0)
。文档说0表示完全透明,所以...我在OpenGL方面一定做错了什么。但是是什么呢?
渲染函数
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();
}