Reputation: 287
I am trying to write a simple Doom style game, and for that matter i've decided to write a block-type engine (much simmilar to Minecraft), however i have ran into a problem: The blocks render really oddly, with parts of blocks passing through like nothing is there.
This is what happens when you look at quad from left: However, when you look at it from right, everything looks perfectly fine:
Let's get to the code, shall we? This is how i initialise OpenGL:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), 0, -2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
I render block's faces using display lists. I tried using VBO's, but changed to display lists, as i thought they caused the problem. Every display list looks generally the same:
glNewList(listTop, GL_COMPILE);
glBegin(GL_QUADS);
tex.bind();
glTexCoord2f(0, 0);
glVertex3f(x+0.0f, y+1.0f, z-1.0f);
glTexCoord2f(0, 1);
glVertex3f(x+0.0f, y+1.0f, z+0.0f);
glTexCoord2f(1, 1);
glVertex3f(x+1.0f, y+1.0f, z+0.0f);
glTexCoord2f(1, 0);
glVertex3f(x+1.0f, y+1.0f, z-1.0f);
glEnd();
glEndList();
And then there's rendering:
glClearColor(0, .5f, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
if(rTo)glCallList(listTop);
if(rBt)glCallList(listBot);
if(rFr)glCallList(listFront);
if(rBk)glCallList(listBack);
if(rLe)glCallList(listLeft);
if(rRi)glCallList(listRight);
glPopMatrix();
glLoadIdentity();
//Display.update() etc here
So, what might be causing this bug? Anyone had simillar problems?
Upvotes: 1
Views: 66
Reputation: 287
Ok, so it turns out i didn't enable GL_DEPTH_TEST. While that was the problem, it was not the only one. The biggest problem was in gluPerspective method, the zNear was set to 0, i learnt that it needs to be above 0. So,changing this line:
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), 0, -2);
To this:
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), 0.1f, -2);
AND enabling GL_DEPTH_TEST fixed everything :)
Upvotes: 1
Reputation: 110
I don't see that you are enabling the DEPTH TEST,
glEnable(GL_DEPTH_TEST);
Also, you have to clear the depth buffer on every frame.
Try to do that before rendering your scene, since it looks that objects are being incorrectly overlayed. Also, I think that you shouldn't set your near value in the projection matrix to Zero, since it may cause you some troubles later.
Upvotes: 5