2016-03-08 105 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