Basic cube does not render correctly

Started by HyperNerd, May 29, 2012, 09:43:30

Previous topic - Next topic

HyperNerd

Hi everyone, new user to LWJGL here with a (hopefully) simple problem which needs solving.

I have tried to copy some very basic tutorials from around the web, and have run into a problem with rendering a simple cube.
Examples online show a cube as you would expect it to be drawn in 3D, rotating, with the nearest faces drawn in front etc.
When I copy the code into my own application, the faces render in the order which I added the faces with glVertex3f(). This results in the the 'front' face of the cube rendering last (nearest the screen) even when it is furthest away.

Attached is an image of the problem.
(How do I embed images??)

My rendering code is below:

The variables wid, hei and len are floats which define the size of the box, and x, y and z are its location.

public void render(){
		glLoadIdentity();
		glTranslated(x, y, z);
		glRotatef(pitch, 0.1f, 0.0f, 0.0f);
		
		glBegin(GL_QUADS);
			glColor3f(0,1,1);
			glVertex3f(-wid/2f, hei/2f, len/2f);
			glVertex3f(-wid/2f, hei/2f,-len/2f);
			glVertex3f( wid/2f, hei/2f,-len/2f);
			glVertex3f( wid/2f, hei/2f, len/2f);
			glColor3f(1,0,0);
			glVertex3f(-wid/2f,-hei/2f,-len/2f);
			glVertex3f( wid/2f,-hei/2f,-len/2f);
			glVertex3f( wid/2f, hei/2f,-len/2f);
			glVertex3f(-wid/2f, hei/2f,-len/2f);
			glColor3f(0,1,0);
			glVertex3f(-wid/2f,-hei/2f,-len/2f);
			glVertex3f(-wid/2f,-hei/2f, len/2f);
			glVertex3f(-wid/2f, hei/2f, len/2f);
			glVertex3f(-wid/2f, hei/2f,-len/2f);
			glColor3f(0,0,1);
			glVertex3f( wid/2f,-hei/2f, len/2f);
			glVertex3f( wid/2f,-hei/2f,-len/2f);
			glVertex3f( wid/2f, hei/2f,-len/2f);
			glVertex3f( wid/2f, hei/2f, len/2f);
			glColor3f(1,1,0);
			glVertex3f(-wid/2f,-hei/2f,-len/2f);
			glVertex3f( wid/2f,-hei/2f,-len/2f);
			glVertex3f( wid/2f,-hei/2f, len/2f);
			glVertex3f(-wid/2f,-hei/2f, len/2f);
			glColor3f(1,0,1);
			glVertex3f(-wid/2f,-hei/2f, len/2f);
			glVertex3f( wid/2f,-hei/2f, len/2f);
			glVertex3f( wid/2f, hei/2f, len/2f);
			glVertex3f(-wid/2f, hei/2f, len/2f);
		glEnd();
		
	}


I'm sure that it's something simple I have missed or copied incorrectly, and any help would be greatly appreciated.

Thanks in advance.

-HyperNerd

EDIT: Added image of offending cube.

matheus23

When initializing code, call
glEnable(GL_DEPTH_BUFFER);

EDIT: sorry,
glEnable(GL_DEPTH_TEST);

and when clearing the screen, call
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Note, the GL_DEPTH_BUFFER_BIT.

The depth buffer:
When you render something, like a quad (one side of the cube, in your example), then openGL will calculate the distance from the pixel drawn to screen from the actual eye position, in other words: the depth. If something later wants to draw on top of the depth, then openGL checks the depth the other pixel has, and if the depth function (I think the standard function is glDepthFunc(GL_LEQUAL);) returns true, the pixel gets drawn, otherwise the pixel is behind the perviously drawn pixel, and does NOT get draw.

Short version: The depth buffer enables objects, appearing in front of the viewer, to overdraw objects behind them, and does not draw objects behind other objects (, which you can't see).
My github account and currently active project: https://github.com/matheus23/UniverseEngine

HyperNerd

Thank you so much - it worked immediately! :D

Thank you also for your explanation of the process.

Now off to look at texturing my cube...