繁体   English   中英

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

[英]Drawing rectangle with opengl es on android doesn't work

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

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

    }
}

纹理样本统一和纹理之间的绑定点是纹理单元的索引。 您必须将纹理单元的索引设置为统一,并且必须将纹理绑定到相应的纹理单元。
glBindTexture为当前活动的纹理单元 ( 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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM