【问题标题】:Opengl ES 2.0 color array translucency ignoredOpengl ES 2.0 颜色数组半透明被忽略
【发布时间】:2018-06-14 19:30:01
【问题描述】:

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

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

【问题讨论】:

    标签: opengl-es opengl-es-2.0 translucency


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      相关资源
      最近更新 更多