OpenGL notes vertex

Source: Internet
Author: User

This course is about drawing simple ry. Before actually drawing, let's get familiar with some concepts.
Vector: http://zh.wikipedia.org/wiki/%E7%9F%A2%E9%87%8F

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:


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 (/* fill in the desired mode here */); /* use glVertex * series functions * // * 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 edge shape, when n is greater, the closer the graph is to the circle. When n is greater to a certain extent, the human eye cannot distinguish it from the real circle. At this time, we have successfully drawn a "circle" (note: There are many methods to draw a circle. Here we use a relatively simple method, but less efficient) try to modify the following const int n value, observe when n is 3, 4, 5, 8, 10, 15, 20, 30, change GL_POLYGON to GL_LINE_LOOP, GL_POINTS, and other methods for output changes such as 50. */# 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); // 2 * Pi radians = 360 angle glEnd (); glFlush ();}


Example 2: draw a star

/* Set the distribution relationship between the five vertices of a pentagram as follows: AE B D C first, according to the cosine theorem column equation, calculate the distance from the center of a pentagram to the vertex. (assume that the edges of the pentagram correspond to the edges of the Pentagon. 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 (assuming that the center of the pentagram is at the coordinate origin) bx = a * cos (18 * Pi/180 ); by = a * sin (18 * Pi/180); cy =-a * cos (18 x 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 * Pi/180 )); GLfloat bx = a * cos (18 * Pi/180); GLfloat by = a * sin (18 x Pi/180 ); GLfloat cy =-a * cos (18 * Pi/180); GLfloat PointA [2] = {0, a}, PointB [2] = {bx, }, pointC [2] = {0.5, cy}, PointD [2] = {-0.5, cy}, PointE [2] = {-bx, by}; glClear (GL_COLOR_BUFFER_BIT ); // in the order of A-> C-> E-> B-> D-> A, you can draw glBegin (GL_LINE_LOOP) and glVertex2fv (PointA) from A pentagram ); glVertex2fv (PointC); glVertex2fv (PointE); glVertex2fv (PointB); glVertex2fv (PointD); glEnd (); glFlush ();}


Example 3: draw the sine function

/* The default coordinate values 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, we can plot more sine periods and try to modify the value of factor to observe the variation. */# include <math. h> const GLfloat factor = 0.1f; void myDisplay (void) {GLfloat x; glClear (rows); glBegin (GL_LINES); glVertex2f (-1.0f, 0.0f); glVertex2f (1.0f, 0.0f); // you can draw the x axis glVertex2f (0.0f,-1.0f); glVertex2f (0.0f, 1.0f); // you can draw the y axis glEnd () for the above two points (); 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.

=================================== Second lesson completion ====================== =====
================================= To be continued ======================== =====

 

OpenGL notes vertex

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.