在 android 上使用 opengl es 绘制矩形不起作用

Drawing rectangle with opengl es on android doesn't work

我正在尝试在 android 上使用 opengl es 3.0 绘制一个矩形,但它不起作用。我可以用颜色填充背景,但不渲染对象。这很奇怪,因为我从我的旧项目中复制了大部分代码。但是从这个时候 android 改变了示例 ConstraintLayout 定义。我所知道的是 SurfaceView 和 Renderer 工作正常。下面是我的绘图函数的代码。

    float []translateMatrix = new float[16];
    float []targetMatrix = new float[16];

    if (rectangle.getTexture().getBitmapName() == null || rectangle.getTexture().getBitmapName().isEmpty())
    {
        throw new CustomException("bitmap name cannot be empty!");
    } else if (rectangle.getTexture().getBitmap() == null)
    {
        rectangle.getTexture().setBitmap(
                BitmapUtil.getBitmap(
                        bitmaps.getHashMap().get(rectangle.getTexture().getBitmapName()
                        )
                )
        );
    }
    Matrix.multiplyMM(
            targetMatrix,
            0,
            vPMatrix,
            0,
            translateMatrix,
            0
    );


    GLES20.glUniform1f(handlers[HandlersUtil.ALPHAMOD_ID], 1);

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, rectangle.getTexture().getBitmap());

    GLES20.glEnableVertexAttribArray(handlers[HandlersUtil.VPOSITION_ID]);
    GLES20.glVertexAttribPointer(
            handlers[HandlersUtil.VPOSITION_ID],
            VERTICE_VERTEX,
            GLES20.GL_FLOAT,
            NORMALIZED,
            OBJ_STRIDE,
            rectangle.getCoordinatesBuffer()
    );

    GLES20.glEnableVertexAttribArray(handlers[HandlersUtil.ATEXCOORDINATE_ID]);
    GLES20.glVertexAttribPointer(
            handlers[HandlersUtil.ATEXCOORDINATE_ID],
            2,
            GLES20.GL_FLOAT,
            NORMALIZED,
            TEX_STRIDE,
            rectangle.getTexture().getBuffer()
    );

    GLES20.glUniformMatrix4fv(
            handlers[HandlersUtil.UMVPMATRIX_ID],
            COUNT,
            TRANSPOSE,
            targetMatrix,
            0
    );

    GLES20.glDrawElements(
            GLES20.GL_TRIANGLE_STRIP,
            rectangle.getIndices(),
            GLES20.GL_UNSIGNED_SHORT,
            rectangle.getOrderBuffer()
    );

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glUniform1i(handlers[HandlersUtil.UTEXTURE_ID], X);

    GLES20.glDisableVertexAttribArray(handlers[HandlersUtil.ATEXCOORDINATE_ID]);
    GLES20.glDisableVertexAttribArray(handlers[HandlersUtil.VPOSITION_ID]); 

我的片段着色器

precision mediump float;
uniform vec4 vColor;
uniform sampler2D uTexture;
varying vec2 vTexCoordinate;
uniform float alphaMod;

void main(){

    gl_FragColor = texture2D(uTexture, vTexCoordinate);
    gl_FragColor.a *= alphaMod;
}

顶点着色器

uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
attribute vec2 aTexCoordinate;
varying vec2 vTexCoordinate;

void main(){
    gl_Position = uMVPMatrix * vPosition;
    vTexCoordinate = aTexCoordinate;
}

矩形class

public final class Rectangle
{
    public final static int INDICES = 4;
    public final static int MATRIX_SIZE = 16;
    public final static int OFFSET = 0;
    public final static int VERTICES = 12;
    public final static int LEFT_TOP_ID = 0;
    public final static int LEFT_BOTTOM_ID = 1;
    public final static int RIGHT_TOP_ID = 2;
    public final static int RIGHT_BOTTOM_ID = 3;
    public final static int X = 0;
    public final static int Y = 1;
    public final static int Z = 2;
    public final static int VERTICE_VERTEX = 3;
    public final static float DEFAULT_ALPHA = 1.0f;

    private final int indices;
    private float alphaMod;
    private final short[] order;
    private final float[] coordinates;
    private final float[] translateMatrix;
    private final float[] targetMatrix;
    private FloatBuffer coordinatesBuffer;
    private final ShortBuffer orderBuffer;
    private final Texture texture;

    public Rectangle()
    {
        this.indices = INDICES;
        this.alphaMod = DEFAULT_ALPHA;
        this.order = new short[]{
                0, 1, 2, 3
        };
        this.coordinates = new float[VERTICES];
        this.translateMatrix = new float[MATRIX_SIZE];
        this.targetMatrix = new float[MATRIX_SIZE];
        this.coordinatesBuffer = BufferUtil.getBuffer(coordinates);
        this.orderBuffer = BufferUtil.getBuffer(order);
        this.texture = new Texture();

        Matrix.setIdentityM(translateMatrix, OFFSET);

    }
}

纹理样本uniform与纹理的绑定点是纹理单元的索引。您必须将纹理单元的索引设置为统一,并且必须将纹理绑定到相应的纹理单元。
glBindTexture bids a texture to the currently active texture unit (glActiveTexture)。例如设置值 1 并激活纹理单元 GL_TEXTURE1:

GLES20.glUniform1f(handlers[HandlersUtil.ALPHAMOD_ID], 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, rectangle.getTexture().getBitmap());