2

I'm trying to draw a rectangle with opengl es 3.0 on android but it doesn't work. I can fill background with color but objects are not rendered. It's strange becouse i copied most of code from my old project which works. But from this time android was changed fro example ConstraintLayout definition. All what i know is that SurfaceView and Renderer working correctlly. Code of my drawing function below.

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

My fragment shader

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

vertex shader

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

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

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

    }
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Bogus
  • 283
  • 1
  • 2
  • 13

1 Answers1

0

The binding point between a texture sample uniform and the texture is the index of the texture unit. You have to set the index of the texture unit to the uniform and you have to bind the texture to the corresponding texture unit.
glBindTexture bids a texture to the currently active texture unit (glActiveTexture). For instance set the value 1 and activate the texture unit GL_TEXTURE1:

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

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, rectangle.getTexture().getBitmap());
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • i've moved glActiveTexture like in youre code but this doesn't help. – Bogus Sep 22 '20 at 15:05
  • @Bogus Is `translateMatrix` and `vPMatrix` set? – Rabbid76 Sep 22 '20 at 15:26
  • @Bogus That is not enough. If all the field of a matrix are 0, then the transformed vertex will be (0 ,0, 0). Each matrix has to be at least the identity matrix (e.g. `Matrix.setIdentityM(translateMatrix, OFFSET);`). – Rabbid76 Sep 22 '20 at 17:41
  • i did that too. – Bogus Sep 22 '20 at 18:05
  • @Bogus Can you "see" something if you remove `gl_FragColor.a *= alphaMod;` and add a constant to the final color (`gl_FragColor = texture2D(uTexture, vTexCoordinate) + 0.5;`)? – Rabbid76 Sep 22 '20 at 18:09
  • no, the result is the same, do yo know how to set gl_FragColor in shader to red or something else color? then we will know is that rendering or texturing problem. – Bogus Sep 22 '20 at 18:18
  • @Bogus !? `gl_FragColor = texture2D(uTexture, vTexCoordinate) + 0.5;` will draw a gray color if there is no texture. `gl_FragColor = texture2D(uTexture, vTexCoordinate) + vec4(1.0, 0.0, 0.0, 0.0);` will make it red. – Rabbid76 Sep 22 '20 at 18:20
  • You'v helped me to resolve part of problem. It is something with matrix. – Bogus Sep 23 '20 at 18:51