Android openGLES2.0纹理背景透明

时间:2017-12-05 06:41:00

标签: android opengl-es

我从位图加载纹理,但当我希望背景透明时,背景为黑色。

创建纹理代码:

public static int loadTexture(Bitmap bitmap,boolean recycle) {
    if (bitmap == null || bitmap.isRecycled()){
        if (Logger.ON){
            Logger.w(TAG,"Could not generate a new OpenGL texture object.bitMap is null !");
        }
        return 0;
    }
    final int[] textureObjectIds = new int[1];
    GLES20.glGenTextures(1, textureObjectIds, 0);
    if (textureObjectIds[0] == 0) {
        if (Logger.ON){
            Logger.w(TAG,"Could not generate a new OpenGL texture object.");
        }
        return 0;
    }
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureObjectIds[0]);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR_MIPMAP_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,bitmap,0);
    if (recycle){
        bitmap.recycle();
    }
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
    return textureObjectIds[0];
}

顶点着色器:

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;

attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;

varying vec2 v_TextureCoordinates;

void main()
{
    v_TextureCoordinates = a_TextureCoordinates;
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * a_Position;
}

片段着色器:

precision mediump float;

uniform sampler2D u_TextureUnit;
varying vec2 v_TextureCoordinates;

void main()
{
    gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);
}

0 个答案:

没有答案