carry on
With the above frame, we can easily carry out our OpenGL programming (Nehe's tutorial is also spreading around this frame, and I read the source code of foreigners, many of which use this framework)
Here we draw a triangle and a square on the form.
Just add the following code to procedure gldraw ();
/ / =========================================================================================================================================================================================== ====================================== // All draw code. Any Dongdong that you want to display on the screen will appear in this code. // Each program will add new code here. / / =========================================================================================================================================================================================== ===============================
Procedure gldraw (); begin glclear (gl_color_buffer_bit or gl_depth_buffer_bit); // Clear screen and depth cache
// When you call GLLoadIdentity (), you actually speak to the screen center, the X coordinate axis from left to right, // Y coordinate axis from below, the 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 is negative, 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. // Mushing into the depth of the screen is a negative value, and the screen is positive. GLLoadIdentity (); // Reset the current model observation matrix
// GLTRANSLATEF (X, Y, Z) move along the X, Y and Z axis. / / According to the previous order, the following code shifts 1.5 units left left left, the Y axis does not move (0.0F), and finally move into the screen 6.0f unit. / (Note You is not moving relative to the screen center when you move, // is relative to the current screen position when you move. Gltranslatef (-1.5, 0.0, -6.0); // Left shift 1.5 unit, and move into the 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 a triangle. // Glbegin meaning 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, you can use GL_POLYGON. // In this brief example, 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 an excess point between them, and //, in the point, the points between GL_TRIANGLES and GLEND () are in three points. // This is equally applicable to the quadrangular shape. 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 ().
// Geometry element type and description // Type Description // GL_Points single vertex set // GL_Lines multi-set double vertices // GL_POLYGON single simple filling converged polygon // GL_TRAINGLES Multi-group independent padding triangle // GL_QUADS multi-set independent padding quad // GL_LINE_STRIP does not close fold line // GL_LINE_LOOP closed fold line // GL_TRAINGLE_STRIP line type continuous filling triangular string // GL_TRAINGLE_FAN sector continuous fill triangular string // GL_QUAD_STRIP Continuously filled quadrilateral string
The first line after Glbegin sets the first vertex of the polygon, // GlvertEX's first parameter is the X coordinate, and then the Y coordinates and Z coordinates. // The first point is the top of the top, then the top of the left vertices and the lower right vertices. // glend () tells OpenGL without other points. // This will display a fill triangle.
// Translator: This is to pay attention to the existence of two different coordinate transformation methods, // gltranslatef (x, y, z) x, y, z is relative to the displacement, // 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 new coordinate original.
Glbegin (GL_TRIANGLES); // Draw triangle Glvertex3f (0.0, 1.0, 0.0); // On the top of Glvertex3f (-1.0, -1.0, 0.0); // Left GlvertEx3f (1.0, -1.0, 0.0); // Right Glend (); // Triangular Drawing End / / After the top half of the screen draws the triangle, we have to move to the right half to draw the square. / / Use GLTranslate again to this. // This time, the X coordinate value is positive. // Because the front left shifts 1.5 units, then move back to the screen center (1.5 units) this time, then move 1.5 units to the right. / / The total of 3.0 units is to be shifted to the right.
GLTranslatef (3.0, 0.0, 0.0); // Right shift 3 units
// Now use GL_QUADS to draw squares. // Similar to the code to draw triangles, it is also very simple to draw quadrilates. // 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 that is reversed counterclockwise is the front towards us. // is not important now, but you must know later. Glbegin (GL_QUADS); // Draw Square Glvertex3f (-1.0, 1.0, 0.0); // Left GlvertEX3F (1.0, 1.0, 0.0); // On the upper right glvertex3f (1.0, -1.0, 0.0); // Left GlvertEx3f (- 1.0, -1.0, 0.0); // Right lower glend (); / / Square Drawing End
// 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 Official Guide, First Edition "// A clear explanation of NEHE's mobile phone in OpenGL in OpenGL: //" Is there a difference between inches and miles in OpenGL? / / Answer is a sentence: no. Perspective and other transformations are unnecessary. // If you want to crop the plane between 1.0 to 20.0 meters, inch, km, etc., // You can't do it in OpenGL To. The only rule is that you must use the consistent metric unit. "End;
Run a look at the effect