This course is about drawing simple geometries, before actually drawing, let's familiarize ourselves with some concepts.
point, Line, and Polygon
We know the concept of a bit, line, and polygon in math (specifically, geometry), but these concepts are different in computers.
Math point, only position, no size. However, in a computer, no matter how precision is calculated, it cannot always represent an infinitely small point. On the other hand, no matter how accurate a graphics output device (for example, a display) is, you cannot output an infinitely small point at all. In general, the point in OpenGL is drawn as a single pixel (the concept of a pixel, please search by itself), although it may be small enough, but 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 the implementation of OpenGL. Of course, excessive attention to detail is a dead-eye, we can not spend too much effort to study "How many points on the same pixel".
In the same way, the math line has no width, but OpenGL's line is wide. At the same time, OpenGL's line must be a finite length, not as infinite as the mathematical concept. It can be argued that OpenGL's "straight line" concept is close to the mathematically "line segment", which can be determined by two endpoints.
A polygon is a closed area formed by the end-to-end of multiple segments. OpenGL stipulates that a polygon must be a "convex polygon" (defined as: any of the two points in the polygon is determined by the segments within the polygon, which can also be deduced that the convex polygon cannot be hollow). Polygons can be determined by the endpoints of their edges, which can be referred to as vertices. (Note: If the polygons used are not convex polygons, then the effect of the last output is undefined--opengl for efficiency, easing the check, which can cause display errors.) To avoid this error, use triangles as much as possible, because triangles are convex polygons)
As you can imagine, the dots, lines, and polygons can be combined into various geometries. Even, you can think of an arc as a lot of short, straight segments that are short enough to be less than the width of one pixel. The arcs and circles can also be represented. By connecting small polygons in different planes, we can also form a "surface".
Ii. specifying vertices in OpenGL
As can be known from the above discussion, "point" is the basis of everything.
How do I specify a point?OpenGL provides a series of functions. They all start with Glvertex, followed by a number and one or two letters.。 For example:
glvertex2d
glvertex2f
glvertex3f
Glvertex3fv
Wait a minute.
Number indicates the number of parameters,2 means there are two parameters, 3 means three, 4 means four(I know a little wordy ~).
The letter represents the type of the parameter, s represents a 16-bit integer (this type is defined as Glshort in OpenGL).
I represents a 32-bit integer (this type is defined as glint and Glsizei in OpenGL).
F represents a 32-bit floating-point number (this type is defined as Glfloat and Glclampf in OpenGL),
D represents a 64-bit floating-point number (this type is defined as gldouble and GLCLAMPD in OpenGL).
V represents the way in which several parameters are passed using pointers, as shown in the following example.
These functions are identical except for the type and number of arguments. For example, the following five pieces of code are functionally equivalent:
(i) Glvertex2i (1, 3);
(ii) glvertex2f (1.0f, 3.0f);
(iii) glvertex3f (1.0f, 3.0f, 0.0f);
(d) glvertex4f (1.0f, 3.0f, 0.0f, 1.0f);
(v) glfloat vertexarr3[] = {1.0f, 3.0f, 0.0f};
GLVERTEX3FV (VERTEXARR3);
later we will use glvertex* to represent this series of functions.
Attention:Many of OpenGL's functions are in this form, with an identical prefix followed by a parameter description tag, which will be more experienced as you learn more deeply.
third, start drawing
Assuming that I have specified a number of vertices now, how does OpenGL know what I want to do with these vertices? Is it a picture of one or even a line? Or form a polygon? Or do something else?
To solve this problem, OpenGL requires:the command that specifies the vertex must be included after the Glbegin function, before the Glend function (otherwise the specified vertex is ignored). And by Glbegin to indicate how these points are used.
For example I write:
Glbegin (gl_points); glvertex2f (0.0f0.0f); glvertex2f (0.5f0.0f); Glend ();
The two points will be drawn separately. If you replace gl_points with Gl_lines, two points are considered to be two endpoints of the line, and OpenGL will draw a line.
We can also specify more vertices, and then draw more complex graphics.
On the other hand,Glbegin supports the way in addition to point gl_points and line gl_lines, there are planar bands Gl_line_strip,gl_line_loop, triangles Gl_triangles,gl_triangle_strip , flat fan Gl_triangle_fan, etc. , the approximate effect of each method is as follows:
Glbegin Reference Example:
void mydisplay (void ) {Glclear (gl_color_buffer_bit); Glbegin ( /* here, fill in the pattern you want */ ); /* Here use glvertex* series functions */ Specify the vertex position you want */ Glend (); Glflush ();}
Two examples
Example one, draw a circle
/*
Positive quadrilateral, positive Pentagon, positive hexagon, ... until n-side shape, when n is larger, the graph is closer to the circle, when n is a certain degree, the human eye will not be able to distinguish it from the real circle, when we have successfully drawn a "circle"
(Note: There are many ways to draw a circle, which is relatively simple, but less efficient)
Try to modify the value of the const int n below, observe the change of output when the different values such as n=3,4,5,8,10,15,20,30,50 will be changed Gl_polygon to Gl_line_loop, gl_points and other ways to observe the change of the output
#include <math.h>Const intn = -;ConstGlfloat R =0.5f;ConstGlfloat Pi =3.1415926536f;voidMydisplay (void){ inti; 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 two, draw a pentagram
/*
The five vertex distribution positions of the pentagram are as follows:
A
E B
D C
First, based on the cosine theorem column equation, the distance a from the center of the pentagram to the vertex is computed (assuming that the side length of the pentagram corresponds to the positive Pentagon is. 0)
A = 1/(2-2*cos (72*pi/180));
Then, based on the definition of sine and cosine, calculates the x-coordinate bx and y-coordinate by of B, and the y-coordinate of C
(assuming the center of the pentagram at the origin of the coordinates)
BX = a * cos (* pi/180);
by = a * sin (* pi/180);
cy =-A * cos (* pi/180);
The coordinates of the five points can be simply represented by the above four quantities and some constants.
#include <math.h>ConstGlfloat Pi =3.1415926536f;voidMydisplay (void) {Glfloat a=1/ (2-2*cos ( the*pi/ the)); Glfloat BX= A * cos ( -* pi/ the); Glfloat by= A * sin ( -* pi/ the); Glfloat Cy=-A * cos ( -* pi/ the); Glfloat pointa[2] = {0, a}, pointb[2] ={bx, by}, pointc[2] = {0.5, Cy}, pointd[2] = { -0.5, Cy}, pointe[2] = { -bx, by}; Glclear (Gl_color_buffer_bit); //according to the order of A->c->e->b->d->a, you can draw the pentagramGlbegin (Gl_line_loop); GLVERTEX2FV (Pointa); GLVERTEX2FV (POINTC); GLVERTEX2FV (PointE); GLVERTEX2FV (POINTB); GLVERTEX2FV (POINTD); Glend (); Glflush ();}
About the use of Glbegin
The objects in OpenGL are described using the polygon boundary model. We divide the surface of the object into many planes, record the shape, color, and texture of the surface, and OpenGL will then draw the polygons based on these parameters, and we can see the solid entities in the scene. The most basic parameter of an object model is its shape, which can be recorded with a surface. And the edge of the recording surface can describe the shape of the polygon. The method for describing edges is to record the endpoints of all segments on the edge. So the core of the shape of the object by the polygon boundary model is to record all the vertices of the object's surface. These vertices are arranged in the order in which they are located to describe a plane, and many planes represent the surface of an object, and it is obvious that more planes are more realistic in shape. The method for describing a vertex in OpenGL is to specify its coordinates and properties, which are: glvertex* (coordination);
The command defines a vertex with the coordinates specified by the parameter and the current color, texture coordinates, and normal properties. The method for describing a polygon in OpenGL is the Glbegin/glend command group: Glbegin (Shape); Glvertex (vertex 1); Glvertex (vertex 2); ...... Glend ();
OpenGL support points (gl_points), Segments (gl_lines), triangles (gl_triangles), quads (gl_quads), polygons (Gl_polygon). You can define multiple planes of the same type in a begin/end pair, such as in Glbegin (Gl_triangles), with Glend (), and 11 vertices between them, 3 triangles are formed, and the last two vertices are discarded. OpenGL also supports planar bands (STRIP) and flat fans (fan). Fan: in Glbegin (Gl_triangle_fan),/glend (), enter a number of vertices, the first vertex as a common vertex, the second to third defines a triangle as the bottom edge, and thereafter each vertex and the previous vertex as the bottom of the definition of a triangle, So 11 vertices define 9 triangles of a common vertex that will form a fan. Band: input vertex number is 123456789 ... ; The defined triangle is 132 243 354 465 567 ..., the adjacent triangles share an edge, forming a strip, and so on, the four-sided shape defined is 1243 3465 5687 .... In practical use, tools such as 3DS Max/softimage/maya can be used to create an object model in an intuitive manner and then convert the model into a series of vertices using a specialized decoding tool. There are three types of decoding tools: plug-ins, tool software, and libraries. There are many code libraries that can parse three-dimensional scenes in many formats, and if not, you can write them yourself. |
Example three, draw a graph of sine function
/*
Since opengl default coordinate values can only be from 1 to 1, (can be modified, but the method is left to speak later) so we set a factor factor, all the coordinate values, such as reducing the scale, so that we can draw more sine cycle test to modify the value of factor, observe the change situation
#include <math.h>ConstGlfloat factor =0.1f;voidMydisplay (void) {glfloat x; Glclear (Gl_color_buffer_bit); Glbegin (Gl_lines); GLVERTEX2F (-1.0f,0.0f); GLVERTEX2F (1.0f,0.0f);//above two points can be drawn X axisGLVERTEX2F (0.0f, -1.0f); GLVERTEX2F (0.0f,1.0f);//two points above can draw Y axisGlend (); 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 points, lines, and polygons, and how to use OpenGL to describe points and use points to describe the geometry.
Everyone can play their own imagination, draw a variety of geometric figures, of course, you can also use Gl_line_strip to connect a lot of locations close to each other, forming a function image. If you are interested, you can also find some beautiful image of the function, do it yourself, use OpenGL to draw it out.
OpenGL learning-------Drawing Simple geometries