2015-08-20 56 views
0

我有一張使用openGL 2.0顯示的背景圖片。現在我想設置圖片的alpha值,但我失敗了。如何在android中使用openGL 2.0設置圖片的alpha值

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) 
{ 

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1); 
    GLES20.glBlendFunc(GLES20.GL_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 
    GLES20.glEnable(GLES20.GL_BLEND); 
    ....... 
} 

@Override 
public void onDrawFrame(GL10 gl) 
{ 

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 
    GLES20.glUniform1i(mTextureUniformHandle0, 0); 
    // Bind the texture to this unit. 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle0); 
    // Set the sampler texture unit to 0, where we have saved the texture. 
    GLES20.glUniform1f(mTextureAlpha, 0.5f); 
    // Draw the triangle 
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT, 
      drawListBuffer); 
} 

這裏是我的片段着色器

precision mediump float; 
uniform sampler2D u_Texture; 
varying vec2 v_TexCoordinate; 

uniform float myAlpha; 
vec4 color; 

void main() 
{ 
color = texture2D(u_Texture, v_TexCoordinate); 
    gl_FragColor = vec4(color.rgb, color.a * myAlpha); 
} 

看來,color.a * myAlpha不幹活不知道我的代碼錯了!設置alpha值有沒有限制?

回答

0

有沒有調用glClear任何地方,我認爲它需要調用每幀來獲得預期的行爲。

如果沒有,那麼Alpha混合可能完全按照預期工作,但由於您每幀保持半透明背景繪製背景,因此背景圖像正在積累,直到它與一個不透明圖像幾乎無法區分幾幀。

+0

我加了GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);在onDrawFrame.It沒有work.and我也設置GLES20.glUniform1f(mTextureAlpha,0.0f);沒有變化,我不知道爲什麼? – shaotine

相關問題