15. OpenGL Fog Types

In this openg tutorial I will show you the different types of opengl fog
that you can use within your opengl program, they are GL_EXP, GL_EXP2 and GL_LINEAR. You would use a different fog type depending on how you would like the current part of your opengl application to look. It is more a trial and error thing, to choose which opengl fog type you want. But when you get to know them well, you should be able to visualise the way it will look inside your opengl application.

In this tutorial I will be expanding upon the previous fog tutorial.

Now in the last one, you will have seen that I used the fog type
GL_EXP2
Now this is not the only one. I only chose it because I think
that it looks best.

The others include:
GL_EXP
and
GL_LINEAR

So there are 3 in total.

In this project I have given you the option of viewing them all
by pressing a, s and d.

Now with our fogHint. We have 3 options once again.
They are:
GL_NICEST
GL_FASTEST

and
GL_DONT_CARE

Nicest makes it look better.
Fastest makes it look worse, but run better.
And
Don’t care sets it to either (I believe it depends on the hardware).

Now we are going to take a look at the start and ending positions for the fog.
We set the fog starting position with:
glFogf (GL_FOG_START, 1);
This sets how far into the screen the fog will start.
Then the fog ending position is set with
glFogf (GL_FOG_END, 10);
This will set how far into the screen the fog will end.

And that is all there is to it.

If you have any questions please email me at swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
    #include <GL/gl.h>
#include <GL/glut.h>

GLfloat angle = 0.0;

GLfloat density = 0.3;
GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};

void cube (void) {
    glRotatef(angle, 1.0, 0.0, 0.0);
    glRotatef(angle, 0.0, 1.0, 0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);
    glColor3f(1.0, 0.0, 0.0);
    glutSolidCube(2);
}

void init (void) {
    glEnable (GL_DEPTH_TEST);
    glEnable (GL_FOG);
    glFogi (GL_FOG_MODE, GL_LINEAR);
    glFogfv (GL_FOG_COLOR, fogColor);
    glFogf (GL_FOG_DENSITY, density);
    glHint (GL_FOG_HINT, GL_NICEST);
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();  
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glutSwapBuffers();
    angle ++;
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0)
;
    glMatrixMode (GL_MODELVIEW);
}

void keyboard (unsigned char key, int x, int y) {
    if (key==‘a’) {
    glFogi (GL_FOG_MODE, GL_EXP);
    }
    if (key==‘s’) {
    glFogi (GL_FOG_MODE, GL_EXP2);
    }
    if (key==‘d’) {
    glFogi (GL_FOG_MODE, GL_LINEAR);
    }
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH
);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (“A basic OpenGL Window);
    init ();
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutKeyboardFunc (keyboard);
    glutMainLoop ();
    return 0;
}

  • March 25, 2010
  • 4