PDA

View Full Version : Fog issue: objects at front are fogged



fx_beginner
01-15-2008, 04:35 PM
(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.

Example: http://quatramaran.ens.fr/~coudert/tmp/opengl_fog.png

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, http://www.pirx.com/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);
glLoadIdentity();

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);

glEnable(GL_MULTISAMPLE);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

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

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_FOG);
float fogColor[4] = { .9, .9, .9, 1. };
glFogfv (GL_FOG_COLOR, fogColor);
glHint (GL_FOG_HINT, GL_NICEST);
drawObjects ();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_FOG);
glFlush();

ZbuffeR
01-15-2008, 04:45 PM
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 :
http://www.opengl.org/sdk/docs/man/xhtml/glFog.xml