Bit map wave: jeff molofee (nehe) OpenGL tutorial - the eleventh lesson

zhaozj2021-02-08  226

OpenGL tutorial of Jeff Molofee (Nehe)

Eleventh lesson

Translated by cker

Hello everyone! For those friends who want to know what I have made here, you can first follow the links listed at the end of the article, download my meaningful demo to see first! I am BOSCO, I will do my best to teach you to achieve an image that moves in a sine wave. This class is based on Nehe's tutorial sixth lesson, of course, you should also learn the knowledge of one to six lessons. You need to download the source compression package and release the Data directory of the compressed package evenly to your code directory. Or use your own bitmap, of course, its size must be suitable for OpenGL texture.

Before we start, open the Visual C "Translator: I use C Builder ... J" and after the other #inlude, add the following code. This will introduce our complicated "complicated" in our program to use? Mathematical functions SINE and COSINE.

#include // Introduced Sin in the mathematical library ()

We will use the Points array to store the independent X, Y, Z coordinates of the grid. The grid here is formed by 45 × 45 points. In other words, it is composed of 44 grid 44. Wiggle_count is used to specify the motion speed of the texture wave. Every 3 frames look very good, the variable hold will store a floating point number for smooth flag-shaped waves. These lines are added to the program head, and after the last line #include, the location of Gluint Texture [1].

FLOAT POINTS [45] [45] [3]; // Points grid vertex array

INT wiggle_count = 0; // Specify the speed of motion of the flag-shaped wave

GLFLOAT HOLD; // Temporary variable

Then move to the loadGlTextures () process. The texture file name used in this lesson is Tim.BMP. Find loadBMP ("Data / Nehe.bmp") and replace it with LoadBMP ("Data / Tim.Bmp").

IF (TextureImage [0] = loadBMP ("DATA / TIM.BMP")) // Load bitmap

The following code is added prior to the tail RETURN TRUE of the initGl () function.

GLPOLYGONMODE (GL_BACK, GL_FILL); // The back surface is completely filled

GLPOLYGONMODE (GL_FRONT, GL_LINE); // front surface uses line drawing

The above code specifies that the back surface of the polygon area is stored in full fill mode "or the rear surface." Instead, the front surface "of the polygon is filled with the outline. These methods are entirely on your personal preference. And related to the orientation of the polygon or the direction of the vertex. Please refer to the Red book for details. Here I am in the way, promote the "Programmer's Guide to OpenGL" published by Iddison-Wesley published in OpenGL. Personally think this is a priceless treasure that learns OpenGL.

The above code is followed, and the following line is added before the Return True.

/ / Ring along the X plane

FOR (int x = 0; x <45; x )

{

/ / Circulation along the Y plane

For (int y = 0; y <45; y )

{

/ / Add wave effect to the surface Points [x] [y] = float ((x / 5.0f) -4.5f);

Points [x] [y] [1] = float ((Y / 5.0F) -4.5F);

Points [x] [y] [2] = float (((((((x / 5.0f) * 40.0F) / 360.0F) * 3.141592654 * 2.0F)

}

}

Thanks to Graham Gibbons to eliminate pulse sawtooth between waves using integer cycle variables.

The above two cycles initialize the point on the grid. Using an integer cycle can eliminate the appearance of pulse sawtooth due to floating point calculations. We divide the X and Y variables by 5, then minus 4.5. This makes our waves can "hit" "" This calculation result will fall between the interval [-4.5, 4.5]. .

Point [X] [Y] [2] The last value is the result of a SINE function calculation. The SIN () function requires an arc parameter. Map float_x by 40.0F to get angle values. Then divide by PI by 360.0 F, multiplying 2, it is converted to an arc.

Then I will completely rewrite the DrawGlscene function.

INT Drawglscene (GLVOID) // Draw Our GL Scene

{

INT x, y; // cycle variable

FLOAT FLOAT_X, FLOAT_Y, FLOAT_XB, FLOAT_YB; // Used to divide the flag-shaped waves into a small quadrangular shape

We use different variables to control cycles. Most variables in the following code have nothing elsewhere in addition to controlling cyclic and storage temporary variables.

GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / / Clear Screen and Depth Buffer

GLLoadIdentity (); // Reset the current model observation matrix

GltranslateF (0.0F, 0.0F, -12.0F); // Move into the screen 12 units

GLROTATEF (XROT, 1.0F, 0.0F, 0.0F); // Rotate around X-axis

GLROTATEF (YROT, 0.0F, 1.0F, 0.0F); // Rotate the y-axis

GLROTATEF (Zrot, 0.0F, 0.0F, 1.0F); // Rotate around Z axis

GlbindTexture (GL_Texture_2D, Texture [0]); // Select Texture

As you can see, the above code and the sixth lesson are very similar, the only difference is that I will be more far away from the scene.

Glbegin (GL_QUADS); // Quadrilateral Drawing Start

For (x = 0; x <44; x ) // RC along the X plane 0-44 (45 points)

{

For (y = 0; y <44; y ) // along the Y plane 0-44 cycle (45 points)

{

Then start using a loop to perform polygonal drawing. The use of integers here can avoid INT () forced type conversion 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

Above we use 4 variables to store texture coordinates. Each polygon (four sides between the grid) maps the 1/44 x 1/44 portion of the texture, respectively. The loop first determines the value of the lower left vertex, then we get the other three points.

GLTEXCOORD2F (FLOAT_X, FLOAT_Y); // First Texture Coordinate (Left Level)

Glvertex3f (Points [x] [Y], Points [x] [Y] [1], Points [x] [Y];

Gltexcoord2f (float_x, float_yb); // Second texture coordinate (left upper corner)

Glvertex3f (Points [x] [Y 1] [0], Points [x] [y 1] [1], Points [x] [y 1] [2])

Gltexcoord2f (float_xb, float_yb); // Third texture coordinate (upper right corner)

GLVERTEX3F (Points [x 1] [Y 1] [0], Points [x 1] [Y 1] [1], Points [x 1] [Y 1

GLTEXCOORD2F (FLOAT_XB, FLOAT_Y); // Fourth texture coordinate (lower right corner)

Glvertex3f (Points [x 1] [Y] [0], Points [x 1] [Y] [1], Points [x 1] [Y] [2])

}

}

Glend (); // quadrilateral drawback

The above lines use GLTEXCOORD2F () and GlvertEX3F () to load data. Remind a little: quadrilateral is a counterclockwise. That is to say, the surface you will start is on the back. The rear surface is completely filled, and the front surface is composed of lines.

If you draw in a clockwise sequence, you may be the front surface when you see it. That is to say, you will see the grid type texture effect rather than completely filled.

IF (wiggle_count == 2) // to reduce wave speed (once every 2 frames)

{

Draw two scenarios, loop the SINE value to produce motion.

For (y = 0; y <45; y ) // along the Y plane cycle

{

HOLD = POINTS [0] [Y] [Y]; // Store the current left wave value

For (x = 0; x <44; x ) // along the X plane cycle

{

// Current wave value is equal to the wave value of the right side

Points [x] [y] [2] = Points [x 1] [Y] [2];

}

Points [44] [Y] [2] = HOLD; // Just now the value is the leftmost wave value

}

WIGGLE_COUNT = 0; // counter Clear

}

WIGGLE_COUNT ; // counter add one

What you do is to store the first value of each row, then move the waves left, it is an image generated wave. The stored value moves the end to generate an endless wave texture effect. The counter wiggle_count is then reset to keep the animation.

The above code was modified by Nehe (February 2000) to eliminate the fine sawtooth in the wavy. XROT = 0.3F; // X axis rotation

YROT = 0.2F; // Y axis rotation

Zrot = 0.4F; // Z axis rotation

Return true; // Return

}

Standard NEHE Rotate Increase :). Now compile and run the program, you will see a beautiful bitmap wave. In addition to the snoring, I dare not believe everyone's reaction. But I hope that everyone can learn from this lesson. If you have any questions or clarify, please contact me casually. Thank you everyone!

Bosco (bosco4@home.com) * DOWNLOAD Visual C Code For This Lesson. * DOWNLOAD Visual C / OpenIL Code For This Lesson. (Conversion by Denton Woods) * DOWNLOAD Visual Basic Code For This Lesson. (Conversion by Ross Dawson) * DOWNLOAD Cygwin (FREE Language) Code For This Lesson. (Conversion by Stephan Ferraro) * DOWNLOAD Irix Code For This Lesson. (Conversion by Rob Fletcher) * DOWNLOAD MacOS X / GLUT Code For This Lesson. (Conversion by Raal Goff) * DOWNLOAD MASM Code For This Lesson. (Conversion by Nico (Scalp)) * DOWNLOAD Linux Code For This Lesson. (Conversion by Richard Campbell) * DOWNLOAD Linux / GLX Code For This Lesson. (Conversion by Mihael Vrbanec) * DOWNLOAD Visual Fortran Code For This Lesson. (Conversion by Jean-Philippe Perois) * DOWNLOAD Delphi Code For This Lesson. (Conversion by Marc Aarts) * DOWNLOAD Mac OS Code For This Lesson. (Conversion by Anthony Parker) * DOWNLOAD Power Basic Code For This Lesson. ( Conversion by angssolus law) * Download Java Code for this L Conversion by Darren Hodges * Download MingW32 & Allegro Code for this Lesson (Conversion By Peter Puck) * Download Borland C Builder 4.0 Code for this Lesson. (Conversion by Patrick Salmons)

转载请注明原文地址:https://www.9cbs.com/read-2232.html

New Post(0)