Fog issue: objects at front are fogged

(I thought I could stay away from the forum but… I couldn’t get an answer for that one after quite some time Googling. So, here I am again.)

I’ve a simple scene (a few spheres and cylinders) and I’m trying to use fog. Problem is, however much I play with the different styles and parameters, I always end up getting the same results: objects far from the camera are fogged away, but also objects too close to it.


I’ve edited the snapshot to mark 3 of the spheres with a red +; without fog, all three spheres have the same color. With fog, notice that the farthest sphere is fogged, the one in the middle is almost not fogged and the one in the front is also fogged!

The only suggestion I got from my searches on these message boards is to check that camera translation and rotations are in model view and not in projection view, but that’s already case: the only two calls that affect the projection matrix in my code are glLoadIdentity() and glOrtho().

Detailled info: this is on MacOS 10.4, though I hope it’s not OS-dependent. I suppose this must be a common error because I have found at least one released app (iMol, that has the same problem. The code is the following:

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glOrtho (-1, 1, -1, 1, -10, 10);
  glMatrixMode (GL_MODELVIEW);

  glClearColor (.9, .9, .9, 1.); 
  glShadeModel (GL_SMOOTH); 

  GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 
  GLfloat mat_shininess[] = { 50.0 }; 
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0}; 
  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  GLfloat light_position[] = { -3.0, -3.0, 10.0, 0.0 }; 
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);


  glRotatef (gTrackBallRotation[0], gTrackBallRotation[1], gTrackBallRotation[2], gTrackBallRotation[3]);
  glTranslatef (gTrackBallTranslation[0], gTrackBallTranslation[1], gTrackBallTranslation[2]);

  float fogColor[4] = { .9, .9, .9, 1. };
  glFogfv (GL_FOG_COLOR, fogColor);
  drawObjects ();

Your glOrtho is spanning the z=0 .
Try with only positive z, fog is calculated on the z space, assuming the camera is on the z=0 plane.
See formula here :