OpenGL ES 2.0。颜色混合和不正确的渲染

时间:2015-03-25 15:04:36

标签: android opengl-es-2.0 glsurfaceview blending

有时屏幕的一部分不会显示在下面的屏幕截图中(错误): real device wrong, emulator wrong, emulator right, 有时这个渲染是错误的像在Genymotion模拟器的屏幕截图(错误)。 它很少发生,我找不到问题的根源。 如果我禁用混合,我无法重现错误。也许,这种麻烦与更深层次的东西有关,混合颜色只是#34;最后一根稻草"。

我使用setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY),当然,这在渲染暂停时会发生。

一个EGLConfig:

public MyGLSurfaceView(Context context) {
    super(context);
    setEGLContextClientVersion(2);
    setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    setPreserveEGLContextOnPause(true);
}

GLSurfaceView.Renderer:

@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    GLES20.glClearColor(0.08f, 0.12f, 0.16f, 1.0f);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthRangef(0.f, 1.f);
    GLES20.glClearDepthf(1.f);
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glFrontFace(GLES20.GL_CCW);
}

@Override
public void onDrawFrame(GL10 unused) {
    GLES20.glDepthMask(true);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);

    int i, j;
    GLES20.glUseProgram(prg_shaderCube);

    //  GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ZERO);
    GLES20.glCullFace(GLES20.GL_BACK);
    GLES20.glDisable(GLES20.GL_BLEND);
    for (i = 3; i >= 0; i--) {
        for (j = 0; j < 6; j++) {
            flat = pattern[i][j];
            Matrix.multiplyMM(mMirrorFlankWithClippingMVP, 0, mMirrorFlankDownVPWithClippingMatrix, 0, mModelMatrix, 0);
            GLES20.glUniformMatrix4fv(u_changematrixCube, 1, false, mMirrorFlankWithClippingMVP, 0);
            GLES20.glUniformMatrix4fv(u_modelmatrixCube, 1, false, mModelMatrix, 0);
            drawFlat(flat);
        }
    }
    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glDepthFunc(GLES20.GL_ALWAYS);

    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    GLES20.glUseProgram(prg_shaderChess);
    GLES20.glUniformMatrix4fv(u_modelmatrixBoard, 1, false, mMirrorChessMatrix, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[4]);
    GLES20.glEnableVertexAttribArray(attr_position_chess);
    GLES20.glEnableVertexAttribArray(attr_color_chess);
    GLES20.glVertexAttribPointer(attr_position_chess, 3, GLES20.GL_FLOAT, false, STRIDE_IN_CHESSBOARD, 0);
    GLES20.glVertexAttribPointer(attr_color_chess, 4, GLES20.GL_FLOAT, false, STRIDE_IN_CHESSBOARD, 12);
    GLES20.glCullFace(GLES20.GL_FRONT);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[4]);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, capacityChessInd, GLES20.GL_UNSIGNED_SHORT, 0);
    GLES20.glDisableVertexAttribArray(attr_position_chess);
    GLES20.glDisableVertexAttribArray(attr_color_chess);
    //   GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ZERO);
    GLES20.glUseProgram(prg_shaderCube);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);

    GLES20.glDisable(GLES20.GL_BLEND);
    for (i = 3; i >= 0; i--) {
        for (j = 0; j < 6; j++) {
            flat = pattern[i][j];
            Matrix.multiplyMM(mMirrorFlankMVP, 0, mMirrorFlankViewProjectionMatrix, 0, mModelMatrix, 0);
            GLES20.glUniformMatrix4fv(u_changematrixCube, 1, false, mMirrorFlankMVP, 0);
            GLES20.glUniformMatrix4fv(u_modelmatrixCube, 1, false, mModelMatrix, 0);
            drawFlat(flat);
        }
    }
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 6; j++) {
            flat = pattern[i][j];
            Matrix.multiplyMM(mMirrorDownMVP, 0, mMirrorDownViewProjectionMatrix, 0, mModelMatrix, 0);
            GLES20.glUniformMatrix4fv(u_modelmatrixCube, 1, false, mModelMatrix, 0);
            GLES20.glUniformMatrix4fv(u_changematrixCube, 1, false, mMirrorDownMVP, 0);
            drawFlat(flat);
        }
    }

    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT);
    GLES20.glCullFace(GLES20.GL_BACK);
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 6; j++) {
            Matrix.multiplyMM(mMVP, 0, mViewMatrix, 0, mModelMatrix, 0);
            GLES20.glUniformMatrix4fv(u_modelmatrixCube, 1, false, mModelMatrix, 0);
            GLES20.glUniformMatrix4fv(u_changematrixCube, 1, false, mMVP, 0);
            drawFlat(flat);
        }
    }

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

    GLES20.glUseProgram(prg_shaderChess);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[4]);
    GLES20.glEnableVertexAttribArray(attr_position_chess);
    GLES20.glEnableVertexAttribArray(attr_color_chess);
    GLES20.glVertexAttribPointer(attr_position_chess, 3, GLES20.GL_FLOAT, false, STRIDE_IN_CHESSBOARD, 0);
    GLES20.glVertexAttribPointer(attr_color_chess, 4, GLES20.GL_FLOAT, false, STRIDE_IN_CHESSBOARD, 12);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[4]);
    GLES20.glUniformMatrix4fv(u_modelmatrixBoard, 1, false, mViewMatrix, 0);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, capacityChessInd, GLES20.GL_UNSIGNED_SHORT, 0);

    GLES20.glDisableVertexAttribArray(attr_position_chess);
    GLES20.glDisableVertexAttribArray(attr_color_chess);

}

 private void drawFlat(int flat) {
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[flat]);
    GLES20.glEnableVertexAttribArray(attr_position_cube);
    GLES20.glEnableVertexAttribArray(attr_color_cube);
    GLES20.glEnableVertexAttribArray(attr_normal_cube);
    GLES20.glVertexAttribPointer(attr_position_cube, 3, GLES20.GL_FLOAT, false, STRIDE_IN_FLAT, 0);
    GLES20.glVertexAttribPointer(attr_color_cube, 3, GLES20.GL_FLOAT, false, STRIDE_IN_FLAT, 12);
    GLES20.glVertexAttribPointer(attr_normal_cube, 3, GLES20.GL_FLOAT, false, STRIDE_IN_FLAT, 24);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[flat]);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, capacityFlatInd[flat], GLES20.GL_UNSIGNED_SHORT, 0);
    GLES20.glDisableVertexAttribArray(attr_position_cube);
    GLES20.glDisableVertexAttribArray(attr_color_cube);
    GLES20.glDisableVertexAttribArray(attr_normal_cube);
}

如果您有任何想法,请告诉我!

0 个答案:

没有答案