Seeing Fog in Action

Rather than write an entire script to test fog, let's modify the spinning tank from the previous chapter (Listing 11-8). We will start by adding a few lines to the init function to enable fog and set the fog parameters. The following lines create a simple linear fog that fades to white:

glEnable(GL_FOG)

glFogfv(GL_FOG_COLOR, (1.0, 1.0, 1.0)) glFogi(GL_FOG_MODE, GL_LINEAR) glFogf(GL_FOG_START, 1.5) glFogf(GL_FOG_END, 3.5)

If you run the modified Listing 11-8 now, you will see something like Figure 12-2. The fog starts at 1.5 units from the camera and ends at 3.5 units from the camera, which makes part of the tank completely obscured by fog. It is not a very useful fog since it would obscure anything that isn't very close to the camera, but it does demonstrate the effect well.

Figure 12-2. A heavily fogged tank

Another useful modification to the spinning tank demo is the ability to move the tank relative to the camera so that we can see how the fog changes over distance. Replace the call to glTranslate in the run function with the following lines, to move the tank with the mouse:

tank_distance = pygame.mouse.get_pos()[l] / 50.0 glTranslatef(0.0, -1.5, -tank_distance)

Now when you run the tank demo, you can control the tank's distance from the camera by moving the mouse up and down. Try replacing the calls to glFogi that we added to the init function with the settings for Martian haze, or experiment with your own values to come up with new effects. You might also want to change the clear color to be similar to the fog color so that a heavily fogged tank disappears completely into the background.

Was this article helpful?

0 0

Post a comment