[n00b] Back painted on top of front!?

Hi - after following this tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05
(more or less), the rear of my cube consitently gets drawn in front of the front of my cube (or maybe the front is somehow transparent). Any advice would be greatly appreciated. Here is the code I am using:

import java.awt.;
import java.awt.event.
;
import net.java.games.jogl.*;

class JOGLTest extends Frame implements GLEventListener {

  public JOGLTest () {
        super.setUndecorated(true);                                                            //Undecorated for Fullscreen mode
        super.setIgnoreRepaint(true);                                                      //Ignore Repaint for Fullscreen mode
        super.addWindowListener(new WindowAdapter() {                              //Exit on window close
              public void windowClosing (WindowEvent we) {
                    System.exit(0);
              }
        });

        GLCapabilities capabilities = new GLCapabilities();                        //Get GLCapabilities for GLCanvas
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(capabilities);      //Create GLCanvas
        canvas.addGLEventListener(this);                                                //Listen for GL Events
        super.add(canvas);                                                                        //Add canvas to frame

        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);      //Set Fullscreen mode
  }

  public static void main (String args[]) {
        new JOGLTest();
  }

  public void init (GLDrawable gld) {
        GL gl = gld.getGL();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        gl.glClearDepth(8.0f);
        gl.glEnable(GL.GL_DEPTH_TEST);
  }

  public void reshape (GLDrawable gld, int x, int y, int width, int height) {
        GL gl = gld.getGL();
        GLU glu = gld.getGLU();
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0,(double)width/height, 0.0, 10.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
  }

  public void display (GLDrawable gld) {
        GL gl = gld.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -5.0f);
        //gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f); //Reenable when cube is drawn correctly
        gl.glBegin(GL.GL_QUADS);
              gl.glColor3f(1.0f, 1.0f, 1.0f);
              gl.glVertex3f(1.0f, 1.0f, -1.0f);
              gl.glVertex3f(-1.0f, 1.0f, -1.0f);
              gl.glVertex3f(-1.0f, 1.0f, 1.0f);
              gl.glVertex3f(1.0f, 1.0f, 1.0f);

              gl.glColor3f(0.0f, 0.0f, 0.0f);
              gl.glVertex3f(1.0f, -1.0f, 1.0f);
              gl.glVertex3f(-1.0f, -1.0f, 1.0f);
              gl.glVertex3f(-1.0f, -1.0f, -1.0f);
              gl.glVertex3f(1.0f, -1.0f, -1.0f);

              gl.glColor3f(0.0f, 0.0f, 1.0f);
              gl.glVertex3f(1.0f, 1.0f, 1.0f);
              gl.glVertex3f(-1.0f, 1.0f, 1.0f);
              gl.glVertex3f(-1.0f, -1.0f, 1.0f);
              gl.glVertex3f(1.0f, -1.0f, 1.0f);

              gl.glColor3f(1.0f, 0.0f, 0.0f);
              gl.glVertex3f(1.0f, -1.0f, -1.0f);
              gl.glVertex3f(-1.0f, -1.0f, -1.0f);
              gl.glVertex3f(-1.0f, 1.0f, -1.0f);
              gl.glVertex3f(1.0f, 1.0f, -1.0f);

              gl.glColor3f(0.0f, 1.0f, 0.0f);
              gl.glVertex3f(-1.0f, 1.0f, 1.0f);
              gl.glVertex3f(-1.0f, 1.0f, -1.0f);
              gl.glVertex3f(-1.0f, -1.0f, -1.0f);
              gl.glVertex3f(-1.0f, -1.0f, 1.0f);

              gl.glColor3f(0.5f, 0.5f, 0.5f);
              gl.glVertex3f(1.0f, 1.0f, -1.0f);
              gl.glVertex3f(1.0f, 1.0f, 1.0f);
              gl.glVertex3f(1.0f, -1.0f, 1.0f);
              gl.glVertex3f(1.0f, -1.0f, -1.0f);

        gl.glEnd();

  }

  public void displayChanged (GLDrawable gld, boolean modeChanged, boolean deviceChanged) {
  }

}

I think the problem is face winding you use. So, I can suggest for testing the following steps:

  1. Try to disable face culling be call
    gl.glDisable(GL.GL_CULL_FACE)
    just before your glBegin(…)

  2. Revert changes back and try to change face culling mode to GL_BACK:
    gl.glEnable(GL.GL_CULL_FACE)
    gl.glCullFace(GL.GL_FRONT);

  3. Revert changes back and try to change the winding mode from counterclockwise (default) to clockwise:
    gl.glFrontFace(GL.GL_CW);

  4. Revert changes back and try to reverse the order of vertexes in every quad you feed to OpenGL.

Yuri

Thanks for the advice! Unfortunately, none of it worked :-/.

Interestingly, when I make the blue front quad the first one and change the glTransfrom -6 on the z axis, it draws it correctly (can’t see throught the blue front).

However, when the above-mentioned changes are made, other sides are not drawn once the cube is rotated. I don’t get it. ???

My inexperience becomes more obvious with each post :). Turns out this program works fine if I just change one line.

glu.gluPerspective(45.0,(double)width/height, 0.0, 10.0);

should be

glu.gluPerspective(45.0,(double)width/height, 1.0, 10.0);

Could someone please give me an explanation of what this actually does?

um, i’m somewhat new at this but I think the number you changed was the z-coordinate of the closest points that will be rendered (10 being the z-coordinate of the furthest points, outside that range things just won’t be drawn). I could be confusing this with something else but I think that’s it.

btw, u know, u don’t have u type “super” in front of your methods unless you’ve overwritten them with methods of the same name in your class.

let’s see if 50 posts makes me a “junior member”…
(edit) guess not.

Thanks a lot, NexusOne.
(BTW, the super is there for clarification for myself - I think its more readable that way)

n/p

turns out i am a junior member now… sweeeet