Sign in to follow this  
OpenGL_Guru

Z-Fighting Problem -- Please Help....

Recommended Posts

OpenGL_Guru    104
i have 2 surfaces that am trying to render at the same time. the surfaces cross each other many times giving me horrible Z-fighting/artifacts. i dont do any translating, i use gluLookAt, so x,y and z change when you hit certain keys on the keyboard. all of that can be seen below. how would i get rid of this z-fighting or at least improve it to where i can get it to looking better. Thanks all a lot for your help and info in advance.. //global stuff float x = 0.0; float y = 0.0; float z = -3000.0;
void myReshape(GLsizei width, GLsizei height)
{
   if (height==0)	// Prevent A Divide By Zero By                         
    { height=1;}       // Making Height Equal One
	
     glViewport(0,0,width,height); // Reset The Current Viewport
     glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
     glLoadIdentity();	         // Reset The Projection Matrix
     setWidthHeight(width, height);
     gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,2000000.0f);
     glMatrixMode(GL_MODELVIEW);  // Select The Modelview Matrix
     glLoadIdentity();	         // Reset The Modelview Matrix
     glutPostRedisplay();
}


void draw()
{
  .........
  .........
 
  gluLookAt(x, y, z, 0.0, 0.0, 500000.0, 0.0, 1.0, 0.0);  //cam pos, eye, up

  //draw_surfaces

  .........
  .........

} 

void specialKey(int key, int i, int j)
{
  
 switch(key)
 {
   case GLUT_KEY_HOME:
   break;
  
   case GLUT_KEY_UP:
    z += speed;
    break;
   case GLUT_KEY_DOWN:
    z -= speed;
    break;
   case GLUT_KEY_LEFT:
    x -= speed;
    break;
   case GLUT_KEY_RIGHT:
    x += speed;
    break;	 
   case GLUT_KEY_PAGE_UP:
    y += speed;
    break;
   case GLUT_KEY_PAGE_DOWN: 
    y -= speed;
    break;  
 } //end switch

  GLUI_Master.sync_live_all();
  glutPostRedisplay();
}

int main(int argc, char **argv)
{

 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
 glutInitWindowSize(1280, 1024);
 glutInitWindowPosition(100, 50);
 window = glutCreateWindow("test");

 InitGL(1280, 1024, file);
 glutDisplayFunc(draw);
 .....
 ..... 
 .....
 GLUI_Master.set_glutSpecialFunc(specialKey) 
 .....
 .....
 

Share this post


Link to post
Share on other sites
OrangyTang    1298
Methinks your name is badly chosen.

As with 90% of z-fighting, the problem is the near plane. Increase it (from your rather close 0.1) to something more realistic like 1f and you'll likely solve your problems. If not, look into a 32bit z-buffer or pulling your far plane in as well.

Share this post


Link to post
Share on other sites
Kalidor    1087
From your code...
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,2000000.0f);

You should definitely increase the near clip plane value. Set it to as far out as possible. Also consider decreasing the far clip plane value if you don't need it that far out. Doing these two things (especially increasing the near plane) will increase your depth precision a decent amount.
There's a good link hiding around here somewhere that explains how it all works very well, let me look for it.

EDIT: Argh, beaten. Anyway, here is that link.

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
i changed my near plane to 1.0 and my far plane to 20000.0. its still pretty bad, havent noticed any difference. i am drawing terrain of the earth with 3D values of around -6000 to 6000. the earth is about 6,000,000 though so i divided by 1000. with using gluLookAt my near and far plane will be the same at the same time the distance from the camera can change each frame...

thanks for that link.. i just read it but i am going to go read it again right now...

Share this post


Link to post
Share on other sites
OrangyTang    1298
Whoa, +/-6000 is some big values, you're obviously going to have to push your near plane out much further than just 1. Just keep increasing it until it fixes your z-fighting. It's highly unlikely that you'll find it intersecting near geometry before then.

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
what about my 2D vertex stuff? they will get clipped if i move it beyond 1.0. i have some 2D vertex legends on the screen ranging from 0 to 2 in x and 0 to 1 in the y direction. should i increase these numbers to compensate for the increasing near clip plane?

Edit: i increased my near clip to 1000 and my far clip to 20,000. this seems to work perfect, no z-fighting at all. but now my 2D stuff is not showing up..like i thought my happened when i first made this post.

Share this post


Link to post
Share on other sites
OrangyTang    1298
Depends what you're using them for. If its 2D HUD stuff then you should be setting an ortho projection before you render them, which will have its own near and far planes. If they actually exist in 3d it should be easy to push them back as well.

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
this is my code to draw the legend. its simply a QUAD used to give an idea of the min and max height and i switch to use to rasterize the values..


void draw_legend(float min, float max)
{

char min_string[15];
char max_string[15];

glPushMatrix();
//glClear(GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // to make sure that wireframe doesnt effect the legend
glTranslatef(-0.75, -1.1, -3.0);

glEnable(GL_TEXTURE_1D);

glBegin(GL_QUADS);
glTexCoord1f(1);
glVertex2f(0.0, 0.0);
glTexCoord1f(1);
glVertex2f(0.0, -0.1);
glTexCoord1f(0);
glVertex2f(2.0, -0.1);
glTexCoord1f(0);
glVertex2f(2.0, 0);
glEnd();

glDisable(GL_TEXTURE_1D);

glPopMatrix();

glPushMatrix();
setOrthoProj();
sprintf(min_string, "%4.2f", -min);
sprintf(max_string, "%4.2f", -max);
glColor3f(1, 1, 1);
glRasterPos2f(w/1.10, h/1.031);
renderBitmapString(min_string);
glRasterPos2f(w/5.75, h/1.031);
renderBitmapString(max_string);
resetPerspProj();
glPopMatrix();
}




if i increased the near plane from 0.1 to 1000, couldnt i just increase the z from -3.0 to maybe -3000.0?? i dunno though..

you can look at what i am doing here

Share this post


Link to post
Share on other sites
Optus    422
Hey if you haven't had any luck with the z-fighting, I had a wacky idea. On the second pass of rendering the terrain:

glEnable(GL_POLYGON_OFFSET);
glPolygonOffset(2,2); // try some different values here

// render

glDisable(GL_POLYGON_OFFSET);

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
hey Optus -- thanks for that tip.. i got the Z-fighting to work, i changed the near plane from 0.1 to 1000.0 and the far plane back down to 20000.0.

now my legend wont show up b/c the near plane has moved beyond where it is being drawn.. see the code above. i understand how to reset the near and far clip plane.. i wonder if i will have to reset the near and far clip plane back in the draw function so i wont get any z-fighting whenthe surfaces are drawn???

i am going to try that out and see how it works....

thanks all for your input..

Share this post


Link to post
Share on other sites
zedzeek    529
see
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
also check out www.opengl.org the developer far, it suggests how to do a spacesim frustum

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this