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

问题描述

我正在尝试在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,vPMatrix,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,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;
}

矩形类

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

    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)