Jeff Molofee (nehe) OpenGL Tutorial Second Lesson 2 Translated By CKER In the first lesson, I teach you how to create an OpenGL window. In this lesson, I will teach you how to create triangles and quadrangular shapes. We use to create a triangle of GL_TRIANGLES, GL_QUADS to create a quadrilateral. On the basis of the first lesson, we only need to add code during the DrawGlscene (). Let me override the entire process. If you plan to modify the code of the previous class, just override the original DrawGlscene () with the following code. INT Drawglscene (glvoid) // This process includes all draw code {glclear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth cache GLLoadIdentity (); // Reset viewport When you call GLLoadIndentity (), you actually The current point is moved to the center of the screen, the X coordinate axis from left to right, Y coordinate axis from below, Z coordinate axis from the outside. The coordinate value of the OpenGL screen is 0.0F on the X and Y axes. The coordinate value on the left of the center is a negative value, and the right side is positive. Moving to the top of the screen is a positive value, and the shift to the bottom end of the screen is negative. Transferring into the depth of the screen is a negative value, and the screen is a positive value. GLTRANSLATEF (X, Y, Z) move along the X, Y and Z axis. According to the previous order, the following code shifts 1.5 units along the X-axis left, the Y axis does not move (0.0F), and finally move into the screen 6.0f unit. Note that when you move in Gltranslatef (x, y, z), you are not moving relative to the center of the screen, but relative to the current screen location. GLTRANSLATEF (-1.5F, 0.0F, -6.0F); // Left shift 1.5 unit, and move into screen 6.0 Now we have moved to the left half of the screen, and put the view into the screen behind the screen so that we can see All scenes - create triangles. Glbegin (GL_TRIANGLES) means to start drawing triangles, glend () tells the OpenGL triangle has been created. Usually you will need to draw 3 top points, you can use GL_TRIANGLES. Draw a triangle is quite fast on the vast majority of graphics cards. If you want to draw four vertices, it will be more convenient to use GL_QUADS. But according to I know, most of the graphics cards use triangles to color. Finally, if you want to draw more vertices, GL_POLYGON can be used. In this simple example of this section, we only draw a triangle. If you want to draw a second triangle, you can add three lines of code (3 points) after these three points. All six-point code should be included between Glbegin (GL_TRIANGLES) and GLEND (). There will never have extra points between them, that is, the points between gl_triangles and glend () are in three points. This is also suitable for quadrilateral. If you know that you are drawn, you must add four points to a collection of points after the first four points. On the other hand, the polygon can be from any top point, (GL_Polygon) does not care how many lines of code between Glbegin (GL_TRIANGLES) and GLEND (). The first line of Glbegin sets the first vertex of the polygon. The first parameter of GlvertEX is X coordinate, and then the Y coordinates and Z coordinates are sequentially. The first point is the top of the top, then the top of the left vertices and the right vertices. Glend () tells OpenGL without other points. This will display a padded triangle.
{Translator: Here, it should be noted here that there are two different coordinate transformation methods, the X, Y, Z) in GLTRANSLATEF (X, Y, Z) is relative to the displacement with your current point, but GLVERTEX (x, y, Z) is the displacement of the new origin after moving with GLTRANSLATEF (X, Y, Z). Thus, it can be considered that GLTRANSLATE moves the coordinate origin, and the point in GLVERTEX is the coordinate value of the relatively upload coordinate original. } Glbegin (GL_TRIANGLES); // Draw triangles GlvertEX3F (0.0F, 1.0F, 0.0F); // On the top of Glvertex3f (-1.0F, -1.0F, 0.0F); // Left GlvertEx3f (1.0F, -1.0 f, 0.0f); // Right lower glend (); // Triangular Drawing End After drawing a triangle on the left half of the screen, we have to move to the right half. Use Gltranslate again to this. This right is shifted, so the X coordinate value is positive. Since the previous left shifts 1.5 units, this time you should move back to the screen center (1.5 units), move 1.5 units to the right. A total of 3.0 units are required to be right. GLTRANSLATEF (3.0F, 0.0F, 0.0F); // Right shift 3 units Now use GL_QUADS to draw squares. Similar to the code drawing triangles, drawing quadrangular shapes is also very simple. The only difference is to replace GL_TRIANGLES with GL_QUADS. And increase a point. We use the clockwise order to draw the square - left - upper right - upper right - lower left. The rear surface of the object is used in clockwise. This means that we see the back of the square. The square of the counterclockwise painted is that the front is facing us. It is not important for you now, but you must know later. Glbegin (GL_QUADS); // Draw square GlvertEX3F (-1.0F, 1.0F, 0.0F); // The upper left GlvertEX3F (1.0F, 1.0F, 0.0F); // On the upper right glvertex3f (1.0F, -1.0F, 0.0 f); // The lower left Glvertex3f (-1.0F, -1.0F, 0.0F); // Right lower glend (); / / Square Draw End Return True; // Continue to Run} Finally replace the title in window mode . IF (Keys [vk_f1]) // F1 button presses? {keys [vk_f1] = false; // If it makes the value in the corresponding KEY array is false killglwindow (); // Destroy the current window fullscreen = Fullscreen; / / Switch full screen / window mode // Rebuild the OpenGL window (Modify) if (! CreateGLWindow ("Nehe's First Polygon Tutorial, 640, 480, 16, Fullscreen) {return 0; // If the window is not created, program Exit}} Markus Knauer Note: ("OpenGL Programming Guide: The Official Guide to Learning OpenGL, Release 1", J. Neider, T. Davis, M. Woo, Addison-Wesley, 1993) OpenGL Programming Guide: OpenGL Learning The official guide, the first edition "clearly explains the concept of NEHE to move in OpenGL in OpenGL in the book:" Is there a difference between inches and miles in OpenGL? The answer is a sentence: No. Perspective and other The transformation is unique. If you want to crop the plane between 1.0 to 20.0 meters, inch, km, etc., you can't do it in OpenGL. The only rule is that you must use the consistent metric unit.
"In this lesson, I have tried to explain the steps related to multilateral drawing. And create an OpenGL program drawn triangles and squares. If you have any comments or suggestions, give me email. If you think there is What is wrong or can be improved, please tell me. I want to be the best OpenGL tutorial and interested in your feedback. {Translator: Nehe's document seems very simple, it seems very rushed. But if you carefully think about this master, you See a few? Or that sentence, I am not a master, I hope you are, sincere.} The following is the source code download link. I wish you good luck!