2016-03-08 48 views
-2

我知道如何生成平面并映射纹理。现在,我试图在我的表单上显示一个alpha混合的PNG,就像它是一个精灵。无法在SharpGL中渲染简单透明的精灵

我想出了下面的代码从谷歌搜索和猜测:

// 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通道。需要对我的代码进行哪些更改才能确保它正确呈现?

回答

1

您的问题非常模糊。我认为你想要的是将混合功能改变为glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

这将启用半透明和完全透明的纹理渲染。

请注意,这不会与深度缓冲一起工作。

+0

我真的很抱歉,因为模糊不清,我是OGL世界的新手,我的术语不太流利。无论如何,我正在将GDI +的2D游戏移植到OpenGL。当我说精灵我简单的意思只不过是在屏幕上的图片。我明白这是通过绘制具有纹理和UV贴图的四元组来实现的。这有效,但看起来......很奇怪。阿尔法是正确的,但颜色已成为apeshit,也许一些渠道被忽略或笏我知道。明天当我回到办公室时,我会上传一张照片。 – CyberFox

+0

OpenGL预期格式为RGBA。确保PNG数据是以32位颜色指定的,并且png解码器不会以任何其他格式(例如ARGB)(我知道java的ImageIO这样做)返回数据。当您有时间发布照片时,我会很乐意看到照片。 – SporreKing

+0

另外,如果我记得正确的话,ogl希望纹理数据在little endian中指定。 – SporreKing