Как мне один раз отрендерить мульти текстуру с помощью openGL ES? - PullRequest
0 голосов
/ 07 мая 2020

В настоящее время я пишу приложение для видеоплеера для android. Идея заключалась в том, что я мог рисовать несколько текстур в одном и том же виде. Теперь, когда у меня есть четыре текстуры, мне нужно соединить четыре текстуры. Каждая текстура отображается в разных областях в одном и том же виде.
Мой вершинный шейдер отображается следующим образом:

private static final String VERTEX_SHADER =
        "uniform mat4 uMVPMatrix;\n" +
        "uniform mat4 uTexMatrix;\n" +
        "attribute vec4 aPosition;\n" +
        "attribute vec4 aTextureCoord;\n" +
        "varying vec2 vTextureCoord;\n" +
        "void main() {\n" +
        "    gl_Position = uMVPMatrix * aPosition;\n" +
        "    vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
        "}\n";

private static final String FRAGMENT_SHADER_2D =
        "precision highp float;\n" +
        "varying vec2 vTextureCoord;\n" +
        "uniform sampler2D sTexture;\n" +
        "void main() {\n" +
        "    gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
        "}\n";

Мой код отрисовки выглядит следующим образом:

public void draw(float[] mvpMatrix, FloatBuffer vertexBuffer, int firstVertex,
                 int vertexCount, int coordsPerVertex, int vertexStride,
                 float[] texMatrix, FloatBuffer texBuffer, int textureId,
                 int texStride, int waterMarkTextureId) {
    GlUtil.checkGlError("draw start");

    // Select the program.
    GLES20.glUseProgram(mProgramHandle);
    GlUtil.checkGlError("glUseProgram");

    // Set the texture.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(mTextureTarget, textureId);

    if (mWaterEnabledLoc >= 0) {
        if (waterMarkTextureId != -1) {
            GLES20.glUniform1i(mWaterEnabledLoc, 1);
            GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, waterMarkTextureId);
            GLES20.glUniform1i(mWaterTextureLoc, 2);
        } else {
            GLES20.glUniform1i(mWaterEnabledLoc, 0);
            GLES20.glUniform1i(mWaterTextureLoc, 0);
        }
    }

    // Copy the model / view / projection matrix over.
    GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mvpMatrix, 0);
    GlUtil.checkGlError("glUniformMatrix4fv");

    // Copy the texture transformation matrix over.
    GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, texMatrix, 0);
    GlUtil.checkGlError("glUniformMatrix4fv");

    // Enable the "aPosition" vertex attribute.
    GLES20.glEnableVertexAttribArray(maPositionLoc);
    GlUtil.checkGlError("glEnableVertexAttribArray");

    // Connect vertexBuffer to "aPosition".
    GLES20.glVertexAttribPointer(maPositionLoc, coordsPerVertex,
            GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
    GlUtil.checkGlError("glVertexAttribPointer");

    // Enable the "aTextureCoord" vertex attribute.
    GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
    GlUtil.checkGlError("glEnableVertexAttribArray");

    // Connect texBuffer to "aTextureCoord".
    GLES20.glVertexAttribPointer(maTextureCoordLoc, 2,
            GLES20.GL_FLOAT, false, texStride, texBuffer);
    GlUtil.checkGlError("glVertexAttribPointer");

    // Draw the rect.
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, firstVertex, vertexCount);
    GlUtil.checkGlError("glDrawArrays");

    // Done -- disable vertex array, texture, and program.
    GLES20.glDisableVertexAttribArray(maPositionLoc);
    GLES20.glDisableVertexAttribArray(maTextureCoordLoc);
    GLES20.glBindTexture(mTextureTarget, 0);
    GLES20.glUseProgram(0);
}

В идеале он выглядит так: следует:

___________________
|Texture1|Texture2|
|Texture3|Texture4|
___________________

Я четыре раза пытался нарисовать четыре текстуры, но это не сработало. Я использую другой texMatrix и тот же mvpMatrix, что и GlUtil.IDENTITY_MATRIX для рисования каждой текстуры. Но результат показывает, что в представлении отображается только последняя текстура. Получается следующее:

___________________
|********|********|
|********|Texture4|
___________________

* выражается как GLES20.GL_CLAMP_TO_EDGE текстуры 4.

Я также пытался преобразовать mvpMatrix, и будут представлены четыре текстуры, но порядок неверен следующим образом:

___________________
|Texture4|Texture3|
|Texture2|Texture1|
___________________

Что касается остальных областей - это GLES20.GL_CLAMP_TO_EDGE каждой текстуры, это не сработало для преобразования mvpMatrix снова.

Есть лучший способ для мне рисовать несколько текстур в одном окне? Искренне жду вашего ответа.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...