Nehe OpenGL tutorial (16)

Source: Internet
Author: User

Lesson 1

Fog:

This course is based on the Code of Lesson 7th. You will learn three different calculation methods for fog and how to set the fog color and scope.
 
This tutorial is written by Chris aliotta.
Do you want to add fog effects to your OpenGL program? I will teach you how to do this in this tutorial. This is my first tutorial. I am also a beginner in OpenGL/C ++ programming. If you find any errors, please let me know instead of blaming me. The code of this lesson is based on the Code of Lesson 7.

Data setup:

We will start with setting the variable for saving the fog information. The fogmode variable is used to save three types of fog: gl_exp, gl_exp2, and gl_linear. I will explain the differences between the three types later. These variables are placed after the gluint texture [3] line at the beginning of the program. The variable fogfilter is used to indicate which fog type we are using. The variable fogcolor stores the fog color. At the top of the program, I added a Boolean variable GP to record whether the 'G' key is pressed.

Bool gp; // indicates whether to press
Gluint filter; // specifies the texture filter used.
Gluint fogmode [] = {gl_exp, gl_exp2, gl_linear}; // fog Mode
Gluint fogfilter = 0; // which mist is used?
Glfloat fogcolor [4] = {0.5f, 0.5f, 0.5f, 1.0f}; // set the fog color to white.

Now we have set the variable. Next let's look at the initgl function. For better results, the glclearcolor () line has been changed to clear the screen to the same color as the fog. Only a small amount of code is required to use fog. In short, you will find it easy.

Glclearcolor (0.5f, 0.5f, 0.5f, 1.0f); // set the background color to the misty color.

Glfogi (gl_fog_mode, fogmode [fogfilter]); // sets the fog mode.
Glfogfv (gl_fog_color, fogcolor); // sets the fog color.
Glfogf (gl_fog_density, 0.35f); // sets the fog density.
Glhint (gl_fog_hint, gl_dont_care); // sets how the system calculates fog.
Glfogf (gl_fog_start, 1.0f); // the start position of the fog.
Glfogf (gl_fog_end, 5.0f); // The end position of the fog.
Glable (gl_fog); // use fog

Let's take a look at the first three lines of this Code. The first line is glable (gl_fog );(? This should be the last line). You don't need to explain it again, mainly to initialize the fog effect.

Line 2 glfogi (gl_fog_mode, fogmode [fogfilter]); sets up the fog filter mode. Previously we declared the array fogmode, which saves the gl_exp, gl_exp2, and gl_linear values. It's time to use them. I will explain them (For details, refer to the Redbook. In fact, this is three ways to calculate the fog effect hybrid factor ):
Gl_exp-fog filled with the basic rendering of the entire screen. It can work on older PCs, so it is not particularly like fog.
Gl_exp2-goes further than gl_exp. It is also full of the screen, but it makes the screen look more deep.
Gl_linear-the best rendering mode. The Fade-in and fade-out effects of objects are more natural.

Line 3 glfogfv (gl_fog_color, fogcolor); set the fog color. Previously, we have set the variable fogcolor to (0.5f, 0.5f, 0.5f, 1.0f), which is a great gray.

Next let's take a look at the last four rows. Glfogf (gl_fog_density, 0.35f); this line sets the fog density. Increasing the number will make the fog more dense, and decreasing it will make the fog more thin.

Glhint (gl_fog_hint, gl_dont_care); Sets correction. I used gl_dont_care because I don't care about its value.

Eric desrosiers adds: Explanation of glhint (gl_fog_hint, hintval );

Gl_dont_care-OpenGL determines which fog effect is used (perform fog Effect Calculation on each vertex or each pixel) and an unknown formula (?)
Gl_nicest-perform fog Effect Calculation on each pixel (good effect)
Gl_fastest-perform fog Efficiency Calculation on each vertex (faster, but better)

The next line glfogf (gl_fog_start, 1.0f); Sets how close the fog effect is to the screen. You can change the value as needed. The next line is similar. glfogf (gl_fog_end, 5.0f) tells OpenGL how far the fog effect continues from the screen.

Now we have built the code for drawing fog, and we will add the Keyboard Command to loop between different fog modes. This code and other key processing code are at the end of the program.

If (Keys ['G'] &! GP) // whether the G key is pressed
{
Gp = true; // Yes
Fogfilter + = 1; // Changes the fog Mode
If (fogfilter> 2) // whether the mode is greater than 2
{
Fogfilter = 0; // set it to zero
}
Glfogi (gl_fog_mode, fogmode [fogfilter]); // sets the fog mode.
}
If (! Keys ['G']) // whether the G key is released
{
Gp = false; // Yes, set to release
}

That's it! Success! Now your OpenGL program has a fog effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.