-1

我知道如何生成平面并映射纹理。现在,我试图在我的表单上显示一个 alpha 混合的 PNG,就像它是一个精灵一样。

我从谷歌搜索和猜测中得出以下代码:

//  Get the OpenGL object.
var gl = openGLControl.OpenGL;

//  We need to load the texture from file.
var textureImage = Resources.Resource1.bg;

//  A bit of extra initialisation here, we have to enable textures.
gl.Enable(OpenGL.GL_TEXTURE_2D);

//  Get one texture id, and stick it into the textures array.
gl.GenTextures(1, textures);    

//  Bind the texture.
gl.BindTexture(OpenGL.GL_TEXTURE_2D, textures[0]);

gl.Enable(OpenGL.GL_BLEND);
gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_DST_ALPHA);

var locked = textureImage.LockBits(
    new Rectangle(0, 0, textureImage.Width, textureImage.Height),
    System.Drawing.Imaging.ImageLockMode.ReadOnly,
    System.Drawing.Imaging.PixelFormat.Format32bppArgb
);

gl.TexImage2D(
    OpenGL.GL_TEXTURE_2D,
    0,
    4,
    textureImage.Width,
    textureImage.Height,
    0,
    OpenGL.GL_RGBA,
    OpenGL.GL_UNSIGNED_BYTE,
    locked.Scan0
);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S,     OpenGL.GL_CLAMP);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T,     OpenGL.GL_CLAMP);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);

这是我正在使用的原始源图像:

这是我在表单上渲染此精灵 10 × 10 (100) 次时的输出:

输出一团糟,似乎不尊重图像的 alpha 通道。需要对我的代码进行哪些更改以确保其正确呈现?

4

1 回答 1

1

Your question is very vague. I think that what your looking for is changing the blend function to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

This would enable both semi and fully transparent texture rendering.

Please note that this will not work together with depth buffering.

于 2016-03-08T15:31:20.130 回答