Bố Của Titu
Bố Của Titu

Reputation: 23

openGL: draw multiple cube GL_CULL_FACE problems

I have 6 cubes joined as the Image below:

before rotation

But when I Rotate it. the problem is the behind cube is not covered by the front cube

after rotation

although I use:

    gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
    gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)

this is my onDrawFrame override function:

    @Override
public void onDrawFrame(GL10 gl) {
    // Clear color and depth buffers
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);        

    gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise
    gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
    gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
    gl.glEnable(GL10.GL_TEXTURE_2D);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Enable texture-coords-array (NEW)



    // ----- Render the Cube -----
    update();
    if (currentTransperent != newTransperent) {
        cube.loadTexture(gl, context, newTransperent);
        currentTransperent = newTransperent;
    }
    gl.glLoadIdentity(); // Reset the current model-view matrix
    gl.glTranslatef(xPosition, yPosition, zPosition); // Translate into the screen
    gl.glRotatef(angleCube, xAxis, yAxis, zAxis); // Rotate

    gl.glPushMatrix();
    gl.glRotatef(90.0f, 1, 0, 0);
    gl.glTranslatef(0.0f, 0.0f, 1.0f);
    cube.draw(gl);
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glTranslatef(0.0f, 0.0f, -1.0f);
    cube.draw(gl);
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glRotatef(270.0f, 0, 1, 0);
    gl.glTranslatef(0.0f, 0.0f, -1.0f);
    cube.draw(gl);
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glTranslatef(0.0f, 0.0f, 1.0f);
    cube.draw(gl);
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glRotatef(270.0f, 0, 1, 0);
    gl.glTranslatef(0.0f, 0.0f, 1.0f);
    cube.draw(gl);
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glRotatef(90.0f, 1, 0, 0);
    gl.glTranslatef(0.0f, 0.0f, -1.0f);
    cube.draw(gl);
    gl.glPopMatrix();

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Disable texture-coords-array
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);

    angleCube += rotateSpeed;
}

Upvotes: 2

Views: 824

Answers (1)

derhass
derhass

Reputation: 45322

Backeface culling does not guarantee a correct depth sort unless the whole scene consists of a single, opaque, concave object.

If you draw a single cube, backface culling will guarantee that only these (up to 3) faces are draw, which are oriented towards the viewer. However, when you draw two cubes, the GL will render them in the order you draw them, and without other means, drawing the second cube will overwrite whatever was previously in the color buffer, i.e. the first cube. The fact that it only will draw the front faces of the second cube, and omit its back faces, does not change anything about every drawn face of the second cube appearing in front of the first one.

The standard way to handle visibility for opaque objects is using the Z buffer algorithm, which GPUs have hardware support for, and is available in OpenGL as the depth test.

Upvotes: 5

Related Questions