NEHE OpenGL Tutorial Delphi (4) ---- Rotate

zhaozj2021-02-16  50

The coloration of your triangle and quadrilateral coloration is taught in the previous section. This lesson will teach you how to rotate these colored objects around the coordinate axis. In fact, it is only necessary to add a few lines on the code on the previous section. The whole routine will be overwhered below. Convenient you know what has increased and modified. Increase two variables to control the rotation of these two objects. These two variables are added to the other variables behind the beginning of the program (Bool Fullscreen = True; the next two lines). They are floating point types of variables that we can rotate the object very accurately. The floating point number contains a decimal location, which means that we do not need to use the angle of 1, 2, 3 ... You will find that the floating point is the foundation of OpenGL programming. The new variable is called RTRI to rotate the triangle, and the RQUAD rotates the quadrilateral.

VAR

......

RTRI: GLFLOAT; // Used in the angle of triangles (new) RQUAD: GLFLOAT; // Used in four-axis angle (new)

......

Then modify the gldraw () as follows:

Procedure gldraw (); begin glclear (gl_color_buffer_bit or gl_depth_buffer_bit); // Clear the screen and depth cache GLLoadIdentity (); // Reset the current model observation matrix

// The next line of code is new. GLROTATEF (Angle, XVector, Yvector, Zvector) is responsible for rotating the object around a shaft. // This command has a lot of use. Angle is usually a variable represents an angle of the object. // XVector, YVector, and Zvector three parameters together determine the direction of the rotating shaft. //, such as (1, 0, 0), the vector described by the X coordinate axis and direction to the right. The vector described by / / (- 1, 0, 0) is passed through 1 unit of the x coordinate axis, but the direction is left. // d. Michael Traub: Provides the above explanation of XVector, YVector, and Zvector. // For better understanding X, Y and Z rotation, I will give some examples ...

// x axis - you are using a section saw. The axis of the saw blade is placed from left to right (just like the X axis in OpenGL). / / The sharp serrated is rotated around the X axis, it looks forward, or turn down. / / Depending on the direction of the saw blade begins. This is the same as what we rotate around the X-axis in OpenGL. // (Translator Note: This will make the face to make the face to the display, and keep the saws of the flower ^ - ^.)

// Y axis - Suppose you are in a huge tornado center, pointing from the floor from the ground to the sky (just like the Y axis in OpenGL). // garbage and fragments around the Y-axis from left to right or from right to left madness. // This is the same as what we rotate around the Y axis in OpenGL.

// Z axis - you look at a fan from the front. The center of the fan is just moving towards you (just like the Z axis in OpenGL). // The blade of the fan is turned around the Z axis clockwise or counterclockwise. This is the same as what we rotate around the Z axis in OpenGL.

GLTRANSLATEF (-1.5, 0.0, -6.0); // Left shift 1.5 unit, and move into a line of code below the screen, if RTRI is equal to 7, we rotate the triangle from left to the right from left to the left. / / You can also change the value of the parameter, let the triangle rotate about the X and Y axes.

GLROTATEF (RTRI, 0.0, 1.0, 0.0); // Rotating triangle around Y-axis (new)

// The code below has no change. Draw a color gradient triangle on the left side of the screen and rotates Glbegin (GL_TRIANGLES) from left to the left, and draws the triangle Glcolor3f (1.0, 0.0, 0.0); // Set the current color to red GlvertEX3F (0.0, 1.0, 0.0); // On the top of Glcolor3f (0.0, 1.0, 0.0); // Set the current color to green GlvertEX3F (-1.0, -1.0, 0.0); // Left Glcolor3f (0.0, 0.0, 1.0); // Set the current color to blue glvertex3f (1.0, -1.0, 0.0); // Right down (); // Triangle Drawing End / / You will pay attention to the following code we add another GLLoadIndentity () call. // The purpose is to reset the model observation matrix. // If we don't reset, call GLTRANSLATE directly, the result is unexpected. // Because the coordinate axis has been rotated, it is likely that it is not possible to face the direction you want. // So we originally want to move the object left and right, it might be able to move up and down, depending on how much angle you turn the coordinate axis. // Try out what results will appear after commenting with GLLOADIDENTITY ().

// Reset the model observation matrix, X, Y, the Z axis are reset, we call GLTranslate. / / You will notice this time we only have 1.5 units, not 3.0 units of the last class. // When we reset the scene, the focus returns to the center of the scene (0.0). // This is enough to remove 1.5 units to the right. // When we move to a new location, rotate the quadrilateral around the X-axis. Squares will rotate up and down.

GLLoadIdentity (); // Reset model observation matrix GLTRANSLATEF (1.5, 0.0, -6.0); // Right shift 1.5 unit, and move into the screen 6.0 GLROTATEF (RQUAD, 1.0, 0.0); // Rotate the quadrilateral around the X-axis (New)

Glbegin (GL_QUADS); // Draw Square Glcolor3f (0.6, 0.2, 2.0); // Set the current color to purple Glvertex3f (-1.0, 1.0, 0.0); // on the left GlvertEx3f (1.0, 1.0, 0.0); // Glvertex3f (1.0, -1.0, 0.0); // The lower left GlvertEx3f (-1.0, -1.0, 0.0); // Right glend (); // Square Drawing End // The next two lines are new. // If the RTRI and RQUAD imagination are containers, then the start we created a container (RTRI, and RQUAD) in the beginning of the program. // After the container is created, it is empty. // The first line of code below is 0.2 to the container. // So every time we run the previous code, you will increase the value in the RTRI container by 0.2 here. // The rear line will reduce the value of the RQUAD container by 0.15. // After we run the previous code, you will fall here in the RQUAD container down 0.15. // The decline will eventually lead to the opposite direction of the direction of rotation of the object. RTRI: = RTRI 0.2; // Increase triangle rotation variable (new) rquad: = rquad - 0.15; // Reduce quadrilateral rotation variable (new) end;

Run a look at the effect:

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

New Post(0)