PDA

View Full Version : Bottom Face of Cube Shows Through



kingcu
09-26-2005, 06:26 AM
I am using JOGL (the Java binding for OpenGL) to create a cube with 6 surfaces; each of the surface is given a different color. When I display the cube, however, the bottom color always shows through the surfaces above it. Can someone help me? Thank you.

Following is the Java code that displays the cube:

public void display(GLDrawable drawable) {
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glRotated(45.0, 1.0, 0.0, 0.0);
gl.glRotated(135.0, 0.0, 1.0, 0.0);
gl.glScaled(1.0, 1.0, 2.0);
colorCube(gl);
gl.glPopMatrix();
gl.glFlush();
}

void quad(GL gl, int a, int b, int c, int d) {
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3dv(vertices[a]);
gl.glVertex3dv(vertices[B]);
gl.glVertex3dv(vertices[c]);
gl.glVertex3dv(vertices[d]);
gl.glEnd();
}

void colorCube(GL gl) {
gl.glColor3d(0.0, 1.0, 1.0);
quad(gl, 4, 5, 6, 7);
gl.glColor3d(0.0, 1.0, 0.0);
quad(gl, 2, 3, 7, 6);
gl.glColor3d(0.0, 0.0, 1.0);
quad(gl, 1, 2, 6, 5);
gl.glColor3d(1.0, 1.0, 0.0);
quad(gl, 0, 4, 7, 3);
gl.glColor3d(1.0, 0.0, 0.0);
quad(gl, 0, 3, 2, 1);
gl.glColor3d(1.0, 0.0, 1.0);
quad(gl, 0, 1, 5, 4);
}

double[][] vertices = {{-1.0, -1.0, 1.0},
{-1.0, 1.0, 1.0}, {1.0, 1.0, 1.0},
{1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0},
{-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0},
{1.0, -1.0, -1.0}};

memfr0b
09-27-2005, 12:49 AM
The depth buffer takes care of that. Make sure you request a depth buffer with your window and enable depth testing with glEnable(GL_DEPTH_TEST). You also have to clear it once per frame. Simplest thing is to use glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).