Lesson 2: drawing geometric figures

Source: Internet
Author: User
Hello everyone, this is the OpenGL entry-level classroom. Before the official start, I would like to say a few words.

My courses are uploaded after one chapter, so I cannot guarantee the stability of the upload time. I have worked hard over the past few days and strive to write the previous courses faster. However, you cannot be too impatient. After all, OpenGL is a big topic and there will be a lot of content in the future. For me, as I study, the difficulty of course creation increases gradually, and the production time increases accordingly.
If you cannot wait, you can refer to other teaching materials during study.

Start Lesson 2 now.
This course is about drawing simple ry. before actually drawing, let's get familiar with some concepts.

I. Points, straight lines, and polygon
We know the concepts of straight lines and polygon in Mathematics (specifically, ry), but these concepts are different in computers.
A mathematical point has only a position and no size. However, in a computer, no matter how accurate the computation is, it cannot represent an infinitely small point. On the other hand, no matter how accurate a graphic output device (such as a display) is, it cannot output an infinitely small point. In general, the points in OpenGL will be drawn into a single pixel (you can search for the pixel concept ~), Although it may be small enough, it is not infinitely small. On the same pixel, OpenGL can draw many coordinates with only slightly different points, but the specific color of the pixel will depend on OpenGL implementation. Of course, excessive attention to the details is the tip of the horn. We don't have to spend too much time studying "how to draw multiple points to the same pixel ".
Similarly, a mathematical straight line has no width, but an OpenGL straight line has width. At the same time, OpenGL's straight lines must be limited in length, rather than infinite in mathematics. It can be considered that OpenGL's "Straight Line" concept is close to the mathematical "line segment", which can be determined by two endpoints.
A polygon is a closed area formed by the combination of multiple line segments at the beginning and end. OpenGL specifies that a polygon must be a "Convex Polygon" (defined as: The line segments determined by any two points in the polygon are within the polygon, and thus can be derived, convex Polygon cannot be hollow ). A polygon can be determined by its edge endpoint (vertex. (Note: If the polygon used is not a convex polygon, the final output result is undefined-OpenGL relaxed the check for efficiency, which may lead to display errors. To avoid this error, use a triangle as much as possible, because all triangles are convex polygon)

As you can imagine, points, straight lines, and polygon can be combined into various geometric shapes. Even you can regard an arc as a link between many short straight line segments, which are short enough to have a length smaller than the width of one pixel. In this way, the arc and circle can also be expressed. Through a small polygon located on different planes, we can also form a "surface ".
Ii. Specify vertex in OpenGL
From the above discussion, we can know that "points" are the foundation of everything.
How to specify a vertex? OpenGL provides a series of functions. They all start with glvertex, followed by a number and 1 ~ 2 letters. For example:
Glvertex2d
Glvertex2f
Glvertex3f
Glvertex3fv
And so on.
A number indicates the number of parameters. 2 indicates two parameters. 3 indicates three parameters. 4 indicates four parameters ~).
Letters indicate the parameter type, and s indicates a 16-digit 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:
(1) glvertex2i (1, 3 );
(2) glvertex2f (1.0f, 3.0f );
(3) glvertex3f (1.0f, 3.0f, 0.0f );
(4) glvertex4f (1.0f, 3.0f, 0.0f, 1.0f );
(5) 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.
3. start plotting
Assuming that I have already specified several vertices, how does OpenGL know what I want to do with these vertices? Is it drawn one by one or connected to a line? Or a polygon? Or what else?
To solve this problem, OpenGL requires that the command to specify the vertex must be included after the glbegin function and before the glend function (otherwise, the specified vertex will be ignored ). Glbegin is used to specify how to use these points.
For example, I write:
Glbegin (gl_points );
Glvertex2f (0.0f, 0.0f );
Glvertex2f (0.5f, 0.0f );
Glend ();
The two points will be painted separately. If you replace gl_points with gl_lines, the two points will be considered as the two endpoints of a straight line, and OpenGL will draw a straight line.
We can also specify more vertices and draw more complex images.
In addition to gl_points and gl_lines, gl_line_strip, gl_line_loop, gl_triangles, gl_triangle_strip, and gl_triangle_fan, glbegin supports the following methods:

Statement: this image is from www.opengl.org. It is a picture of the book OpenGL programming guide. Since the old version of this book (the first version, 1994) has been circulating on the internet, I hope I have not touched on copyright issues.

I am not going to make a big article on glbegin various methods. You can try to change the glbegin method and vertex position to generate interesting patterns.

Program code:
Void mydisplay (void)
{
Glclear (gl_color_buffer_bit );
Glbegin (/* enter the desired mode here */);
/* Use glvertex * series functions here */
/* Specify the desired vertex position */
Glend ();
Glflush ();
}
Change this code to what you like and use it to replace the mydisplay function in the first lesson. Compile the code and run it.
Two examples
Example 1: Draw a circle /**//*
Positive quadrilateral, positive Pentagon, positive hexagonal ,......, Until the positive N side shape, when n is greater, the closer the graph is to the circle.
When N is large to a certain extent, the human eyes cannot distinguish it from the real circle.
At this time, we have successfully drawn a circle"
(Note: There are many methods to draw circles, which are relatively simple but inefficient)
Modify the following const int N value and observe the output changes when n is 3, 4, 5, 8, 10, 15, 20, 30, 50, and other values.
Change gl_polygon to gl_line_loop, gl_points, and other methods to observe the output changes.
*/
# Include <math. h>
Const int n = 20;
Const glfloat r = 0.5f;
Const glfloat Pi = 3.1415926536f;
Void mydisplay (void)
...{
Int I;
Glclear (gl_color_buffer_bit );
Glbegin (gl_polygon );
For (I = 0; I <n; ++ I)
Glvertex2f (R * Cos (2 * PI/N * I), R * sin (2 * PI/N * I ));
Glend ();
Glflush ();
}

Example 2: Draw a star

 

/**//*
The relationship between the five vertices of a star is as follows:
A
E B

D C
First, calculate the distance from the center of a pentagram to the vertex Based on the cosine theorem column equation.
(Assume that the edges of the Pentagon corresponding to the Pentagon are. 0)
A = 1/(2-2 * Cos (72 * PI/180 ));
Then, according to the definition of sine and cosine, calculate the X coordinate Bx and Y coordinate by of B and the Y coordinate of C.
(Assume that the center of the star is at the coordinate origin)
BX = A * Cos (18 * PI/180 );
By = A * sin (18 * PI/180 );
Cy =-A * Cos (18 * PI/180 );
The coordinates of the five points can be expressed by the above four quantities and some constants.
*/
# Include <math. h>
Const glfloat Pi = 3.1415926536f;
Void mydisplay (void)
...{
Glfloat A = 1/(2-2 * Cos (72 x pi/180 ));
Glfloat BX = A * Cos (18 * PI/180 );
Glfloat by = A * sin (18 * PI/180 );
Glfloat Cy =-A * Cos (18 * PI/180 );
Glfloat
Pointa [2] =... {0, },
Pointb [2] =... {BX, },
Pointc [2] =... {0.5, Cy },
Pointd [2] =... {-0.5, Cy },
Pointe [2] =... {-bx, };

Glclear (gl_color_buffer_bit );
// In the order of A-> C-> E-> B-> D-> A, you can draw a five-star
Glbegin (gl_line_loop );
Glvertex2fv (pointa );
Glvertex2fv (pointc );
Glvertex2fv (Pointe );
Glvertex2fv (pointb );
Glvertex2fv (pointd );
Glend ();
Glflush ();
}

Example 3: Draw the sine function

 

/**//*
The default coordinate value of OpenGL can only be changed from-1 to 1 (this can be modified, but the method is left for future reference)
Therefore, we set a factor to reduce the proportions of all coordinate values,
In this way, more sine periods can be drawn.
Modify the value of factor to observe the change.
*/
# Include <math. h>
Const glfloat factor = 0.1f;
Void mydisplay (void)
...{
Glfloat X;
Glclear (gl_color_buffer_bit );
Glbegin (gl_lines );
Glvertex2f (-1.0f, 0.0f );
Glvertex2f (1.0f, 0.0f); // you can draw the X axis for the preceding two points.
Glvertex2f (0.0f,-1.0f );
Glvertex2f (0.0f, 1.0f); // you can draw the Y axis at the preceding two points.
Glend ();
Glbegin (gl_line_strip );
For (x =-1.0f/factor; x <1.0f/factor; x + = 0.01f)
...{
Glvertex2f (x * factor, sin (x) * factor );
}
Glend ();
Glflush ();
}

Summary
This lesson describes the concepts of vertices, straight lines, and polygon, and how to use OpenGL to describe vertices and vertices to describe ry.
You can use your imagination to draw a variety of geometric figures. Of course, you can also use gl_line_strip to connect a lot of points close to each other to form a attention image. If you are interested, you can also find some functions that are more beautiful in the image, do it yourself, and use OpenGL to draw it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.