Mitchell Ingram
Mitchell Ingram

Reputation: 706

Clearing an OpenGL display in Android.

I have a class that determines if a square should be drawn or not via a boolean. The problem is, the square stays put even if the boolean is false. My question is, how do I remove the drawn square if the boolean comes up false?

public void onDrawFrame(GL10 unused) {

    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    // Draw square
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    if (drawObject == true) {
    mSquare.draw(mMVPMatrix);
    }

    // Draw Square
}

Upvotes: 2

Views: 1421

Answers (1)

Mitchell Ingram
Mitchell Ingram

Reputation: 706

Ok, I found the answer.

Create a method:

public void clearBuffers(boolean color, boolean depth, boolean stencil) {
    int bits = 0;
    if (color) {
        bits = GLES20.GL_COLOR_BUFFER_BIT;
    }
    if (depth) {
        bits |= GLES20.GL_DEPTH_BUFFER_BIT;
    }
    if (stencil) {
        bits |= GLES20.GL_STENCIL_BUFFER_BIT;
    }
    if (bits != 0) {
        GLES20.glClear(bits);
    }
}

And then call it with:

    clearBuffers(true, true, true);

to clear everything on the screen.

Upvotes: 1

Related Questions