简体   繁体   English

无法在 SharpGL 中渲染简单的透明精灵

[英]Can't render simple transparent sprites in SharpGL

I know how to generate a plane and map a texture.我知道如何生成平面并映射纹理。 Now, I am trying to display an alpha-blended PNG on my form, like it was a sprite.现在,我试图在我的表单上显示一个 alpha 混合的 PNG,就像它是一个精灵。

I have come up with the following code from Googling and guessing:我从谷歌搜索和猜测中提出了以下代码:

//  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);

Here is the original source image that I am using:这是我正在使用的原始源图像:

Here is the output when I render this sprite 10 × 10 (100) times on my form:这是我在表单上渲染这个精灵 10 × 10 (100) 次时的输出:

The output is all messed up, and it doesn't seem to be respecting the image's alpha channel.输出全部搞砸了,似乎没有尊重图像的 alpha 通道。 What changes need to be made to my code to ensure that it renders correctly?需要对我的代码进行哪些更改以确保其正确呈现?

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);我认为您要寻找的是将混合函数更改为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.请注意,这不会与深度缓冲一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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