Help with cube rendering

Started by bpx95, June 19, 2011, 03:43:25

Previous topic - Next topic

bpx95

So I just started learning LWJGL, and I am not having much difficulty with it, but I found that whenever I move 2 (or more) objects in front of each other, it doesn't quite preform perfectly. Regardless of depth, one of the objects (the one that was written) is rendered in front of the other. So they are rendered in order instead of by depth. I know I could fix this by writing something myself to determine in what order to render the shapes, but isn't there a way to do this with LWJGL? I haven't seen this mentioned before and I did a lot of Googling. I tried using depth testing with:

GL11.glClearDepth(1.0f);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthFunc(GL11.GL_LEQUAL);


But all that did was make the shapes flicker when they position above each other.

How do I fix this? Here's my code:

GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GLU.gluPerspective(45f, 800f/600f, 0.0f, 50.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glPushMatrix();
		
		GL11.glTranslatef(0f, 0f, -200f);
		GL11.glRotatef(angle, 1f, 0f, 0f);
		
		//cube
		GL11.glBegin(GL11.GL_QUADS);
		
		int s = 50;
		
			//bottom side
			GL11.glColor3f(1f, 0f, 0f);
			GL11.glVertex3f(-s, -s, s);
			GL11.glVertex3f(-s, -s, -s);
			GL11.glVertex3f(s, -s, -s);
			GL11.glVertex3f(s, -s, s);
			
			//top side
			GL11.glColor3f(0f, 1f, 0f);
			GL11.glVertex3f(-s, s, s);
			GL11.glVertex3f(-s, s, -s);
			GL11.glVertex3f(s, s, -s);
			GL11.glVertex3f(s, s, s);
			
		GL11.glEnd();
		
		GL11.glPopMatrix();

bpx95

http://lwjgl.org/forum/index.php?topic=2151.0

I found this, but that is 4.5 years old, and they never found exactly what was causing/fixing it. Does this help?

bpx95

Haha... The depth testing did work.
GLU.gluPerspective(45f, 800f/600f, 0.0f, 50.0f);

That's the issue. I accidentally put "0.0f" instead of "0.1f". Not that I really understand what that changes, but I'll learn someday I'm sure. The correct code is:
GLU.gluPerspective(45f, 800f/600f, 0.1f, 50.0f);

jediTofu

http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml

The very bottom says that zNear should never be set to 0, and as you can see in the equation, zNear is the denominator of a division equation, so it makes sense.  I bet if you called glError/whatever it'd show an error.
cool story, bro