PDA

View Full Version : How to use fog correctly in opengl application?



honeyhuang
03-21-2006, 02:45 AM
Hi all,
I meet a problem when i use GL_FOG in opengl application. Perhaps i misunderstand the param "GL_FOG_START" and "GL_FOG_END".

here is the related code :

...
GLfloat fogColor[4] = {1.0f,1.0f,1.0f,1.0f};

glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 1.0f);
glHint(GL_FOG_HINT, GL_DONT_CARE); glFogf(GL_FOG_START, 2000.0f); // Fog Start Depth
glFogf(GL_FOG_END, 8000.0f); // Fog End Depth
glEnable(GL_FOG);
...


gluPerspective(30.0, 1, 500.0f, 8000.0f);


gluLookAt(0, 0, -4500, 0, 0, 0, 0,1,0);


let me explain now, In my application, there are two Sphere (radius=30) in my scene, In the World coordinate system, SphereA is at point(250, 100, 0), sphereB is at point(350, 200, -3500), Why the fog can effect sphereB? the SphereB is out of the range of the fog!

Who can tell me Why? and how to set the fog param to affect all the object in the appointed range (for example Z=1000 ~ 2000) ?
Thanks a lot!

Ehsan Kamrani
03-21-2006, 03:52 AM
First of all, you don't need to specify the fog density, since you are using the linear fog.
In linear fog, fog starts from GL_FOG_START, increases linearly to the GL_FOG_END and in GL_FOG_END reaches the maximun density . So after GL_FOG_END, you can not see anything.
-Ehsan-

honeyhuang
03-21-2006, 04:20 AM
Thank you for your reply. But why the SphereB is effected by fog? it is far before GL_FOG_START!
thanks again!

Ehsan Kamrani
03-21-2006, 05:52 AM
It seems that something else is going wrong in your code. As an example before you use from the function gluLookAt(), you should select the modelview matrix and call glLoadIdentity(). Before you use from the function gluPerspective(), you should select the projection matrix and call glLoadIdentity().
-Ehsan-