opengl-esopengl-es-2.0translucency

Opengl ES 2.0 color array translucency ignored


I made a shader which uses a color array for the elements

private static final String VERTEX_SHADER_CODE =
        "uniform mat4 uMVPMatrix;" +
        "attribute vec4 vPosition;" +
        "attribute vec4 aColor;" +
        "varying vec4 vColor;" +
        "void main() {" +
        "    vColor = aColor;" +
        "    gl_Position = uMVPMatrix * vPosition;" +
        "}";

private static final String FRAGMENT_SHADER_CODE =
        "precision mediump float;" +
        "varying vec4 vColor;" +
        "void main() {" +
        "    gl_FragColor = vColor;" +
        "}";

Blend enabled

GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

Drawing as follows

    @Override
    public void onDrawFrame(GL10 gl) {
        lineCoordinatesBuffer.position(0);
        lineColorBuffer.position(0);

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        GLES20.glUseProgram(program);

        final int positionHandle = GLES20.glGetAttribLocation(program, "vPosition");
        GLES20.glEnableVertexAttribArray(positionHandle);

        GLES20.glVertexAttribPointer(
                positionHandle,
                COORDINATES_PER_VERTEX,
                GLES20.GL_SHORT,
                false,
                0,
                lineCoordinatesBuffer);

        final int colorHandle = GLES20.glGetAttribLocation(program, "aColor");
        GLES20.glEnableVertexAttribArray(colorHandle);

        GLES20.glVertexAttribPointer(
                colorHandle,
                COLOR_BYTES_PER_VERTEX,
                GLES20.GL_UNSIGNED_BYTE,
                false,
                0,
                lineColorBuffer);

        final int mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
        GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 24);
    }

lineCoordinatesBuffer

493, 906, 533, 790, 483, 902,
533, 790, 524, 787, 483, 902,
76, 1513, 177, 1636, 83, 1507,
177, 1636, 184, 1630, 83, 1507,
988, 439, 928, 531, 996, 444,
928, 531, 936, 536, 996, 444,
950, 413, 905, 591, 960, 415,
905, 591, 915, 594, 960, 415

lineColorBuffer

-1, -1, -1, -122,
-1, -1, -1, -122,
-1, -1, -1, -122,
-1, -1, -1, -122,
-1, -1, -1, -122,
-1, -1, -1, -122,
-1, -1, -1, 97,
-1, -1, -1, 97,
-1, -1, -1, 97,
-1, -1, -1, 97,
-1, -1, -1, 97,
-1, -1, -1, 97,
-1, -1, -1, -110,
-1, -1, -1, -110,
-1, -1, -1, -110,
-1, -1, -1, -110,
-1, -1, -1, -110,
-1, -1, -1, -110,
-1, -1, -1, 72,
-1, -1, -1, 72,
-1, -1, -1, 72,
-1, -1, -1, 72,
-1, -1, -1, 72,
-1, -1, -1, 72

Output:

enter image description here

Expected output: the same but with alpha applied on the lines


Solution

  • It looks like your upload of the color values is misconfigured. Shader color values are generally float values between 0.0 and 1.0, but you're uploading non-normalized byte values between 0 and 255. Any value higher that 1 is just going to get clamped to 1.0 on fragment writeout - i.e. in your scenario all vertices are fully opaque white.

    Try setting the normalized parameter to the color attribute glVertexAttribPointer() call to true.