Opengl ES 2.0 颜色阵列半透明被忽略

Opengl ES 2.0 color array translucency ignored

我制作了一个为元素使用颜色数组的着色器

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

启用混合

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

绘制如下

    @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

输出:

预期输出:相同但在线条上应用了 alpha

您上传的颜色值似乎配置有误。着色器颜色值通常是 0.0 到 1.0 之间的浮点值,但您上传的是 0 到 255 之间的非标准化字节值。任何高于 1 的值都会在片段写出时被限制为 1.0 - 即在您的场景中所有顶点是完全不透明的白色。

尝试将 normalized 参数设置为颜色属性 glVertexAttribPointer() 调用 true