仅显示Opengl ES 1.0 2D最后绘制的纹理

时间:2018-06-02 18:32:36

标签: android opengl-es

设置:OpenGL ES 1.0,Android

术语背景图片这里表示全屏纹理,它绘制在两个三角形上,形成全屏矩形。

如果我绘制完整场景,包括背景图像,我会得到黑色背景颜色而不是清晰的彩色背景,我看不到我的背景显示纹理

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
drawParticles(scene.getNumDots());

渲染图像:

Particles

但是,如果我只绘制 背景图片,我会看到它。

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
// drawParticles(scene.getNumDots()); commented out

渲染图像:

Render background only

实现:

// how it was setup
public void setupGl(@NonNull final GL10 gl) {
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}

// This was called
public void setDimensions(@NonNull final GL10 gl, final int width, final int height) {

    gl.glViewport(0, 0, width, height);
    gl.glOrthof(0, width, 0, height, 1, -1);
}

// textures were loaded like this
private void loadTexture(
        @NonNull final GL10 gl,
        @NonNull final Bitmap texture,
        final int handleOffset) {

    gl.glGenTextures(1, textureHandle, handleOffset);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[handleOffset]);

    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
}

private void drawBackground() {
    backgroundTextureCoordinates.position(0);
    backgroundCoordinates.position(0);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_BACKGROUND]);

    gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, backgroundTextureCoordinates);
    gl.glVertexPointer(2, GL10.GL_SHORT, 0, backgroundCoordinates);
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisable(GL10.GL_TEXTURE_2D);
}

private void drawParticles(final int count) {
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    particlesTexturesCoordinates.position(0);
    particlesTrianglesCoordinates.position(0);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_PARTICLE]);

    gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, particlesTexturesCoordinates);
    gl.glVertexPointer(2, GL10.GL_SHORT, 0, particlesTrianglesCoordinates);
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, count * VERTICES_PER_PARTICLE);

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisable(GL10.GL_TEXTURE_2D);
}

public void setClearColor(
        @NonNull final GL10 gl,
        @ColorInt int color) {
    gl.glClearColor(
            Color.red(color) / 255f,
            Color.green(color) / 255f,
            Color.blue(color) / 255f, 0f);
}

1 个答案:

答案 0 :(得分:0)

问题出在我用来绘制背景的方式

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);

在那里,我指定了三角形的数量:2

我必须为两个三角形指定顶点数6

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);