0
votes

Setup: OpenGL ES 1.0, Android

Term background image here means a full screen texture which is drawn over two triangles which make the full screen rectangle.

If I draw full scene, including background image, I get black background color instead of clear color background, and I do not see my texture for background displayed

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

Render image attached:

Particles

However, if I draw only the background image, I see it.

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

Render image attached:

Render background only

Implementations:

// 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
You screwed up your texture states just badly. It is totally unclear why you even need different texture units, but you basically enable texturing for unit 0 in drawParticles and for unit 1 in drawBackground (in the next frame).derhass
@derhass, please elaborate more. I thought that I need units if I have multiple textures per handle. But if I remove both glActiveTexture method calls, nothing changes in the behavior.Yaroslav Mytkalyk
I have no idea what you even mean by "multiple textures per handle".derhass
@derhass I mean that the texture index array has two textures int[] textureHandle = new int[2]; index 0 for particle and index 1 for background. Be advised that I am a noob in OpenGL, this is my first OpenGL app.Yaroslav Mytkalyk
You need multiple texture units for multitexturing, not for having multiple textures in a program.derhass

1 Answers

0
votes

The problem was in the way I used to draw a background

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

There, I specified the number of triangles: 2.

Whereas I had to specify the number of vertices for the two triangles: 6.

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