0

I have a background picture,shown by using openGL 2.0. Now I want to set the picture's alpha value.but I failed.

@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);
}

Here is my fragment shader

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

It seems that the color.a * myAlpha does not work.I don't know where my code wrong! Is there any limit to set alpha value?

shaotine
  • 249
  • 3
  • 15

1 Answers1

0

There's no call to glClear anywhere and I think it needs to be called each frame to get the intended behaviour.

If not, then it's possible that the alpha blending is working exactly as intended, but because you keep drawing the background at half opacity every frame, the background image is accumulating until it's pretty much indistinguishable from an opaque image after just a few frames.

Columbo
  • 6,648
  • 4
  • 19
  • 30
  • I added GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); in onDrawFrame.It did no work.and I also set GLES20.glUniform1f(mTextureAlpha, 0.0f); there was no change .I don't know why? – shaotine Aug 21 '15 at 01:24