For more information, see http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html.
1. Specify Coordinate
The following code draws a group of isolated points on the screen without any connections between them.
Void display ()
{Glclear (gl_color_buffer_bit); glbegin (gl_points); glvertex2f (-0.5,-0.5); // point p1glvertex2f (-0.5, 0.5); // point p2glvertex2f (0.5, 0.5); // point p3glvertex2f (0.5,-0.5); // point p4glend (); glflush ();}
Run the program after compilation. If your eyes are good enough, you will find four small dots in the window. That is, four vertices of a square. 1.
Code explanation
A series of functions whose names start with glvertex are used to specify vertex coordinates. 2 In the name indicates two dimensions (X and Y), and F indicates float. These functions include:
Glvertex2d
Glvertex2f
Glvertex3f
Glvertex3fv
And so on.
A number indicates the number of parameters, and a letter indicates the parameter type. The specific meaning is
S represents a 16-bit integer (OpenGL defines this type as glshort ),
I Represents a 32-bit integer (OpenGL defines this type as glint and glsizei ),
F indicates a 32-bit floating point number (OpenGL defines this type as glfloat and glclampf ),
D indicates a 64-bit floating point number (OpenGL defines this type as gldouble and glclampd ).
V indicates that several parameters passed will use pointers. See the example below.
These functions have the same function except for the type and number of parameters. For example, the functions of the following five code segments are equivalent:
glVertex2i(1, 3); glVertex2f(1.0f, 3.0f);glVertex3f(1.0f, 3.0f, 0.0f);glVertex4f(1.0f, 3.0f, 0.0f, 1.0f);GLfloat VertexArr3[] = {1.0f, 3.0f, 0.0f};glVertex3fv(VertexArr3);
In the future, we will use glvertex * to represent this series of functions.
Note: many functions of OpenGL adopt this form. Adding parameter description marks to the same prefix will help you learn more.
For the code in the example
glVertex2f(-0.5, 0.5)
The X and Y coordinates of a vertex are-0.5 and 0.5, respectively. In this example, you can consider that the coordinate origin is in the center of the window, the X axis is right, and the Y axis is positive. The height and width of the drawing area of the window are both 1.
2. Draw Line Segments
The parameters of the glbegin function tell the program what operations are performed on the point set given later. Common Values of this parameter are:
(1) gl_points: draw a single point. There is no line between the points. The effect is shown in figure 1 above.
92) gl_lines: a line segment between two vertices. (For the above Code, if the parameter is gl_lines, draw two line segments: P1P2 and p3p4.
Result 2. (I added the red text myself. The following is the same)
(3) gl_line_strip: Draw a continuous line with the given point, except the last point. Each vertex is the starting point of the next section. Draw three lines: P1P2, p2p3, and p3p4,
Effect 3 is shown.
(4) gl_line_loop: Draw a closed line with the given point. That is, the last vertex and the first vertex are connected Based on gl_line_strip. Draw four lines: P1P2, p2p3, p3p4, and p4p1. Result 4: