Knowledge point: 1. the header file of the library required by openggl is as follows: # Include <windows. h> // header file for Windows # Include <GL \ Gl. h> // header file for the opengl32 Library # Include <GL \ Glu. h> // header file for the glu32 Library # Include <GL \ Glaux. h> // header file for the Glaux Library W 2. Connection Method of the library required by openggl (the following is vs2003) 3. OpenGL functions // Clear the screen to the specified color and clear the depth Cache Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and the depth buffer // Reset scenario) Glloadidentity (); // reset the current model view Matrix // The function of gltranslatef (x, y, z) is to move along the X, Y, and Z axes. The following code moves 1.5 units to the left on the X axis, the Y axis does not move (0.0), and the Z axis moves 6.0 units to the screen. Note that when you move, you do it relative to the current position, rather than the center position of the screen (scene. Gltranslatef ( -1.5f , 0.0f, -6.0f ); // Move left 1.5 units and into the screen 6.0 // The following line of code is newly added. Glrotatef (angle, xvector, yvector, zvector) is used to rotate objects around the axis. This is a very useful function. Angle is a number used to specify the rotation angle (usually stored in variables ). The xvector, yvector, and zvector parameters are used to describe a vector to specify the rotation axis of an object. If you use a value like (, 0), you will describe a vector with a length of 1 unit pointing to the right along the X axis; and (-, 0) this value describes a vector with a length of 1 unit that follows the X axis but points to the left. To better understand the rotation of X, Y, and Z, here are some examples ...... X axis-you are using a saw, and wood passes through the center of the saw knife. The saw blade quickly rotates on the X axis, and the tooth seems to be cut up or down, depending on the direction of rotation of the saw blade. This is similar to rotating an object with the X axis in OpenGL. // Y axis-imagine a tornado hangs in the wilderness. The center of the tornado is on the Y axis, and the ground it is blowing and fragments are spinning around the Y axis (the center of the tornado) from left to right or from right to left. This is similar to rotating an object with the Y axis in OpenGL. // Z axis-you are looking at a rotating fan, and the center of the fan is facing you. The fan blades rotate around the Z axis, clockwise or counterclockwise. This is similar to rotating an object in OpenGL with the Z axis. // Therefore, in the following line of code, if rtri is equal to 7, we will rotate 7 degrees on the Y axis (from left to right ). You can try to modify the code so that the triangle rotates simultaneously on the X and Y axes. Note that the rotation is based on the angle. If the rtri value is 10, we will rotate it 10 degrees on the Y axis. Glrotatef (rtri, 0.0f , 1.0f , 0.0f ); // Rotate the triangle on the Y axis (new) //Rtri is not declared here,Declare rtri firstTo achieve dynamic rotation, add rtri + = to the drawing function. 0.2f ;Or rtri-= 0.14f ;Such statements // In this simple program, we draw only one triangle. If you want to add another triangle, we need to add three lines of code (three vertices) to the end of the original three lines of code, so that in glbegin (gl_triangles) and glend () there will be six lines of code. There should be a group of three vertices between glbegin (gl_triangles) and glend (), and extra vertices will be ignored. The same principle applies to four edges. If you draw all four edges, to add another quadrilateral, you need to add a group of four lines of code after the original four lines of code. A polygon can be composed of any number of vertices, so there can be any line of code between glbegin (gl_polygon) and glend. // The first line of code after glbegin sets the first vertex for our triangle. The first parameter of glvertex specifies the X coordinate of the vertex, and the second parameter specifies the Y coordinate, the third one specifies the Z coordinate. Therefore, the three lines of code after glbegin (gl_triangles) Specify the vertex above the triangle, the vertex in the lower left corner and the vertex in the lower right corner. Then, glend () tells OpenGL that there are no vertices. In this way, a closed triangle is complete. In clockwise order, we will draw the back of the Quadrilateral. We can also say that we actually see the back of it. If an object is drawn in a counterclockwise order, it will face us on the front. Glbegin (gl_triangles); // drawing using triangles(Quadrilateral: gl_quads, Line: gl_lines, Polygon, gl_polygon) // Set the current color to red (Red brightness is full, no green or blue ). The following code is the first vertex (above the triangle). We will draw it in red as the current color. From now on, if we do not change the current color, we will draw everything in red. Glcolor 3f ( 1.0f , 0.0f , 0.0f ); // Set the color to red Glvertex 3f (0.0f, 1.0f , 0.0f); // top Glvertex 3f ( -1.0f , -1.0f , 0.0f); // bottom left Glvertex 3f ( 1.0f , -1.0f , 0.0f); // bottom right Glend (); // finished drawing the triangle |