11th class OpenGL floating flag

Source: Internet
Author: User
Tags mathematical functions

Floating flag:

This lesson starts with the code of Lesson 6 and creates a floating flag. I believe that at the end of this lesson, you can master texture ing and hybrid operations.

Hello everyone! For those who want to know what I have made here, You can first download my meaningless demo by following the link listed at the end of the article! I am Bosco, and I will do my best to teach you how to implement an image that is moving in a sine wave. This course is based on the nehe tutorial. Of course, you should learn at least one to six lessons. You need to download the source code compressed package and release the Bitmap Under the data directory in the compressed package to your code directory. Or use your own bitmap. Of course, its size must be suitable for OpenGL texture requirements.
Before we start, open visual c ++ ...) Add the following code after # inlude. This will introduce the complexity we will use in the program ?) Mathematical functions sine and cosine.

# Include <math. h> // introduce sin in the mathematical function library

We will use the points array to store the X, Y, and zcoordinates of each vertex in the grid. The grid is formed by 45x45 points. In other words, the grid is composed of small square grids with 44x44 grids. Wigggle_count is used to specify the movement speed of texture waves. Every three frames looks good, and the variable hold will store a floating point that is used to smooth the flag wave. These lines are added to the program header, located after the last line # include and before gluint texture [1.

Float points [45] [45] [3]; // The points mesh vertex array int wigle_count = 0; // specify the glfloat hold of the flat wave. // Temporary Variable

Next, move it down to the loadgltextures () sub-process. The texture file used in this course is named tim.bmp. Find loadbmp ("Data/nehe.bmp") and replace it with loadbmp ("Data/tim.bmp.

If (textureimage [0] = loadbmp ("Data/tim.bmp") // load the bitmap

Add the following code before return true at the end of the initgl () function.

Glpolygonmode (gl_back, gl_fill); // the rear surface is fully filled with glpolygonmode (gl_front, gl_line); // draw the front surface with lines

The code above specifies that the full fill mode is used to fill the back of the Polygon Area (TRANSLATOR: Or the back surface ). On the contrary, the front of a polygon is filled with a contour line. These methods depend entirely on your personal preferences. It is also related to the polygon orientation or the vertex direction. For more information, see red book ). Here I sell programmer's Guide to OpenGL, a good book that promotes me to learn OpenGL, published by Addison-Wesley. I personally think this is the invaluable asset of learning OpenGL.
Then add the following lines before the above Code and return true.

// Loops along the X plane for (INT x = 0; x <45; X ++) {// loops along the Y plane for (INT y = 0; y <45; Y ++) {// Add the wave effect points [x] [y] [0] = float (x/5.0f)-4.5f) to the surface ); points [x] [y] [1] = float (y/5.0f)-4.5f ); points [x] [y] [2] = float (sin (x/5.0f) * 40366f)/360.0f) * 3.141592654 * 2.0f ));}}

Thanks to Graham Gibbons for their suggestions on using integer cyclic variables to eliminate the pulse sawtooth between waves.

The above two loops initialize the points on the grid. The integer loop can be used to eliminate the appearance of pulse sawtooth caused by floating point operation rounding. Divide the X and Y variables by 5, and then subtract 4.5. In this way, our waves can be "centered" (TRANSLATOR: in this way, the calculation result falls between [-4.5, 4.5 ).

Click [x] [y] [2]. The final value is the result of an sine function compute. The sin () function requires a radian variable. Multiply float_x by 40366f to obtain the angle value. Divide it by 360.0f, multiply it by PI, and multiply it by 2 to convert it to radian.

Then I will completely rewrite the drawglscene function.

Int drawglscene (glvoid) // plot our GL scenario {int X, Y; // The loop variable float float_x, float_y, float_xb, float_yb; // It is used to split the fl-shaped wave into a very small quadrilateral.

We use different variables to control loops. In the code below, most of the variables are useless except for controlling loops and storing temporary variables.

Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and depth buffer glloadidentity (); // reset the current model observation matrix gltranslatef (0.0f, 0.0f,-12.0f ); // 12 units of glrotatef (xrot, 1.0f, 0.0f, 0.0f) are moved into the screen; // rotate glrotatef around the X axis (yrot, 0.0f, 1.0f, 0.0f ); // rotate glrotatef around Y axis (Zrt, 0.0f, 0.0f, 1.0f); // rotate glbindtexture around Z axis (gl_texture_2d, texture [0]); // select texture

As you can see, the above code is very similar to the sixth lesson. The only difference is that I moved the scene farther away from the camera.

Glbegin (gl_quads); // The quadrilateral plot starts for (x = 0; x <44; X ++) // loops between 0 and 44 along the X plane (45 points) {for (y = 0; y <44; y ++) // loops between 0 and 44 along the Y plane (45 points ){

Next, draw a polygon using loops. Using Integer here can avoid forced type conversion of INT () I used previously.

Float_x = float (X)/44.0f; // generate X floating point value float_y = float (y)/44.0f; // generate y floating point value float_xb = float (x + 1) /44.0f; // X floating point value + 0.0227f float_yb = float (Y + 1)/44.0f; // y floating point value + 0.0227f

The preceding four variables are used to store texture coordinates. Each polygon (quadrilateral between grids) maps the texture 1/44x1/44. Loop first determines the value of the lower left vertex, and then we get the value of the other three points accordingly.

Gltexcoord2f (float_x, float_y); // The first texture coordinate (lower left) glvertex3f (points [x] [y] [0], points [x] [y] [1], points [x] [y] [2]); gltexcoord2f (float_x, float_yb); // second texture coordinate (upper left corner) glvertex3f (points [x] [Y + 1] [0], points [x] [Y + 1] [1], points [x] [Y + 1] [2]); gltexcoord2f (float_xb, float_yb); // The third texture coordinate (upper right corner) glvertex3f (points [x + 1] [Y + 1] [0], points [x + 1] [Y + 1] [1], points [x + 1] [Y + 1] [2]); gltexcoord2f (float_xb, float_y); // The fourth texture coordinate (bottom right corner) glvertex3f (points [x + 1] [y] [0], points [x + 1] [y] [1], points [x + 1] [y] [2]) ;}} glend (); // The End Of The quadrilateral painting

The preceding rows use gltexcoord2f () and glvertex3f () to load data. Note: The quadrilateral is drawn counterclockwise. That is to say, the surface you first see is the back. The rear surface is completely filled, and the front surface is composed of lines.

If you plot it clockwise, you may first see the front surface. That is to say, you will see the texture effect of the grid type instead of full filling.

If (wigle_count = 2) // used to reduce the wave speed (once every 2 frames ){

Every two scenes are drawn, and the sine value is cyclically used to produce the motion effect.

For (y = 0; y <45; y ++) // loops along the Y plane {hold = points [0] [y] [2]; // store the current left wave value for (x = 0; x <44; X ++) // loops along the X plane {// The current wave value is equal to the wave value on the right of it. Points [x] [y] [2] = points [x + 1] [y] [2 ];} points [44] [y] [2] = hold; // The current value is the leftmost wave value} wigger_count = 0; // the counter is cleared.} wigger_count ++; // Add one counter

What we did above is to store the first value of each row first, and then move the wave to the left, which means the image generates waves. The stored values are moved to the end to produce an endless wave texture effect. Then reset the counter wigle_count to keep the animation running.

The above Code was modified by nehe (February 2000) to eliminate the tiny serrations between waves.

Xrot + = 0.3f; // X axis rotation yrot + = 0.2f; // y axis rotation Rojo T + = 0.4f; // Z axis rotation return true; // return}

Standard nehe rotation increment. Now compile and run the program, and you will see a beautiful bitmap wave. I am not sure of your response except for the buzz. But I hope you can learn something from this lesson. If you have any questions or need clarification, please feel free to contact me. Thank you.

11th class OpenGL floating flag

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.