1

我在要绘制的对象之前激活纹理。但是纹理显示在两个对象上。为什么呢?我应该在第一个要绘制的对象之前取消绑定纹理吗?- 我尝试使用 glDisable 和 glBindTexture 但没有帮助。这是我的代码:

@Override
public void onDrawFrame(GL10 gl) {
    GLES20.glClear(GL10.GL_COLOR_BUFFER_BIT);

    synchronized (camerObject) {
        surfaceTextureCamera.updateTexImage();
        cameraUpdate = false;
    }

    vertexBuffer.position(0);
    GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDE, vertexBuffer);
    GLES20.glEnableVertexAttribArray(aPositionLocation);

    vertexBuffer.position(POSITION_COMPONENT_COUNT);
    GLES20.glVertexAttribPointer(aColorLocation, COLOR_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDE, vertexBuffer);
    GLES20.glEnableVertexAttribArray(aColorLocation);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);


    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[0]);

    GLES20.glUniform1i(textureUnitLocation, 0);

    vertexBuffer2.position(0);
    GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDETEXTURE, vertexBuffer2);
    GLES20.glEnableVertexAttribArray(aPositionLocation);

    vertexBuffer2.position(TEXTURE_COMPONENT_COUNT);
    GLES20.glVertexAttribPointer(aTextureCoordinatesLocation, TEXTURE_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDETEXTURE, vertexBuffer2);
    GLES20.glEnableVertexAttribArray(aTextureCoordinatesLocation);


    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

更新 这是我的着色器程序:

    #extension GL_OES_EGL_image_external : require
precision mediump float;
uniform samplerExternalOES u_Texture;
varying vec2 v_TextureCoordinates;
varying vec4 v_Color;

void main() {

gl_FragColor = v_Color;
gl_FragColor = texture2D(u_Texture, v_TextureCoordinates);
}

attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec2 a_TextureCoordinates;
varying vec2 v_TextureCoordinates;
varying vec4 v_Color;

void main() {

    v_Color = a_Color;
    v_TextureCoordinates = a_TextureCoordinates;
     gl_Position = a_Position;

}
4

1 回答 1

1

OpenGL 是一个状态引擎。一旦设置了一个状态,它就会一直保持到它再次改变,甚至超出帧。

在第二个对象之前绑定命名纹理对象,在绘制第二个对象之后绑定默认纹理对象(0)。
此外,在绘制调用之前启用顶点属性并在之后禁用它:

对象 1

vertexBuffer.position(0);
GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDE, vertexBuffer);
vertexBuffer.position(POSITION_COMPONENT_COUNT);
GLES20.glVertexAttribPointer(aColorLocation, COLOR_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDE, vertexBuffer);    

GLES20.glEnableVertexAttribArray(aPositionLocation);
GLES20.glEnableVertexAttribArray(aColorLocation);

GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

GLES20.glDisableVertexAttribArray(aPositionLocation);
GLES20.glDisableVertexAttribArray(aColorLocation);

对象 2:

vertexBuffer2.position(0);
GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDETEXTURE, vertexBuffer2);
vertexBuffer2.position(TEXTURE_COMPONENT_COUNT);
GLES20.glVertexAttribPointer(aTextureCoordinatesLocation, TEXTURE_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDETEXTURE, vertexBuffer2);

GLES20.glEnableVertexAttribArray(aPositionLocation);
GLES20.glEnableVertexAttribArray(aTextureCoordinatesLocation);

GLES20.glUniform1i(textureUnitLocation, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[0]);

GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);

GLES20.glDisableVertexAttribArray(aPositionLocation);
GLES20.glDisableVertexAttribArray(aTextureCoordinatesLocation);

sahder 程序总是从纹理中读取颜色:

void main() {

    gl_FragColor = v_Color;
    gl_FragColor = texture2D(u_Texture, v_TextureCoordinates);
}

注意,设置的第一行gl_FragColor是无用的,因为gl_FragColor在下面的行中被覆盖了。

由于未设置颜色属性(黑色)或从纹理读取的颜色为黑色,因此可以将颜色相加:

void main() {

    gl_FragColor = v_Color + texture2D(u_Texture, v_TextureCoordinates);
}
于 2020-01-13T06:09:29.860 回答