Problem with cube

I’m very new to OpenGL (to be honest, 48hours ago I donloaded the first java opengl toolkit) but very familiar with java.
I followed the Nehe tutorials but there’s one problem that I’m chewing my teeth out on.
I have stripped down the code to just display the front and the backside of the cube.
I spin it around it’s y-axis, but the back is displayed above the front (although at the correct size). But there is no problem when the back is spinned to the front (after 180 degrees), it then covers everything.
I’ve been playing around with the code for quite some time, creating the quads clockwise, coun terclockwise, mixed …
Could anybody please give me a hint what I’m doing wrong?

	private static void drawScene(GL gl){
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
		gl.glLoadIdentity();						// Reset The View
		gl.glTranslatef(-1.5f,0.0f,-6.0f);
		gl.glRotatef(rtri,0.0f,1.0f,0.0f);
		gl.glBegin(GL.GL_QUADS);

		gl.glColor3f(0.9f, 0.9f, 0.9f);
		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.9f, 0.9f, 0.1f);
		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(); 
		
		rtri=rtri-0.003f;
	}

enable and properly setup the depth-buffer.

currently the last drawn poly is rendered on top of everything

Hm, I checked that.
I got these lines in my source:


glu.gluPerspective(45.0f,(float)parent.getSize().x/(float)parent.getSize().y,0.1f,100.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
//now we init gl
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);							// Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST);						// Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL);							// The Type Of Depth Test To Do

I just switched over to this example:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet209.java?view=markup&content-type=text%2Fvnd.viewcvs-markup&revision=HEAD
I got the same problem here.
Sorry for bothering you but I just need to get a little rotating box working for a demo, I’ll get some real OpenGL education by next weekend and so far I only need a simple rotating box.

Here the full source of the example (I had to modify the original example to make it run inside an eclipse viewpart)



package jogltest;

import javax.media.opengl.GL;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.glu.GLU;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.part.ViewPart;

public class ViewPart2 extends ViewPart {
	private static float rtri=3.0f;
	
	private static void drawScene(GL gl){
		
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
		gl.glLoadIdentity();						// Reset The View
//		textu.bind(gl);
		gl.glTranslatef(-1.5f,0.0f,-6.0f);
		gl.glRotatef(rtri,0.0f,1.0f,0.0f);
		gl.glBegin(GL.GL_QUADS);

		gl.glColor3f(0.9f, 0.9f, 0.9f);
//		gl.glTexCoord2f(0.0f, 0.0f); 
		gl.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
//		gl.glTexCoord2f(1.0f, 0.0f); 
		gl.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
//		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f(-1.0f, 1.0f,  1.0f);	// Top Right Of The Texture and Quad
//		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f( 1.0f, 1.0f,  1.0f);	// Top Left Of The Texture and Quad
		
		gl.glColor3f(0.9f, 0.9f, 0.1f);
//		gl.glTexCoord2f(0.0f, 0.0f); 
		gl.glVertex3f( -1.0f, -1.0f,  -1.0f);	// Bottom Left Of The Texture and Quad
//		gl.glTexCoord2f(1.0f, 0.0f); 
		gl.glVertex3f(1.0f, -1.0f,  -1.0f);	// Bottom Right Of The Texture and Quad
//		gl.glTexCoord2f(1.0f, 1.0f);
		gl.glVertex3f(1.0f, 1.0f,  -1.0f);	// Top Right Of The Texture and Quad
//		gl.glTexCoord2f(0.0f, 1.0f);
		gl.glVertex3f( -1.0f, 1.0f,  -1.0f);	// Top Left Of The Texture and Quad

        gl.glEnd(); 
		
		rtri=rtri-0.003f;
	}
	/**
	 * 
	 */
	public ViewPart2() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(final Composite parent) {
		final Display display = parent.getDisplay();
		parent.setLayout(new FillLayout());
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new FillLayout());
		GLData data = new GLData ();
		data.doubleBuffer = true;
		final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

		canvas.setCurrent();
		final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext();

		canvas.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				Rectangle bounds = canvas.getBounds();
				float fAspect = (float) bounds.width / (float) bounds.height;
				canvas.setCurrent();
				context.makeCurrent();
				GL gl = context.getGL ();
				gl.glViewport(0, 0, bounds.width, bounds.height);
				gl.glMatrixMode(GL.GL_PROJECTION);
				gl.glLoadIdentity();
				GLU glu = new GLU();
				glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
				gl.glMatrixMode(GL.GL_MODELVIEW);
				gl.glLoadIdentity();
				context.release();
			}
		});

		context.makeCurrent();
		GL gl = context.getGL ();
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		gl.glColor3f(1.0f, 0.0f, 0.0f);
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		gl.glClearDepth(1.0);
		gl.glLineWidth(2);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);	
		context.release();

		display.asyncExec(new Runnable() {
			int rot = 0;
			public void run() {
				if (!canvas.isDisposed()) {
					canvas.setCurrent();
					context.makeCurrent();
					GL gl = context.getGL ();
					gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
					gl.glClearColor(.3f, .5f, .8f, 1.0f);
					gl.glLoadIdentity();
					gl.glTranslatef(0.0f, 0.0f, -10.0f);
					gl.glPolygonMode(GL.GL_FRONT, GL.GL_LINE);
					gl.glColor3f(0.9f, 0.9f, 0.9f);
					drawScene(gl);
					canvas.swapBuffers();
					context.release();
					display.asyncExec(this);
				}
			}
		});


	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}

To answer my own question:
The snipplets (actually all the opengl examples for swt) are missing one thing:
data.depthSize = 24;
That is done on the GLData object, it doesn’t matter to them as they are only using monocolor frames.
Sorry, for the bothering. I’ll submit it as a bug to eclipse.