Detailed explanation of OpenGL drawing method

Briefly

OpenGL drawing method

OpenGL can support many different primitive types, the most basic being points, lines, or triangles. Lines and triangles can be combined into strips, loops or sector triangles. Points, lines, or triangles are also basic primitive types supported by most graphics hardware devices.

point drawing

A point can be represented by a single vertex. A point does not actually have an area. In OpenGL, it is simulated by a rectangular area on the screen. When rendering a point source, OpenGL will determine the position of the point through the rasterization rule class. Draw a quadrilateral area at the center of the point. The side length of the quadrilateral area is equal to the size of the point. It is a fixed state and can be set by calling the function glPointSIze().

voidglPointSIze(GLfloat size);

Set a fixed pixel size. If GL_PROGRAM_POINT_SIZE is not enabled, it is used to set the size of the point by default

When OpenGL renders points, the fragment shader is executed for each point's fragment, which is essentially a square area on the screen, and each pixel can be shaded with a different color. OPenGL's fragment shader provides a special built-in variable called gl_PointCoord, which contains the coordinate information of the current fragment in the point area. It can only work in the fragment shader, and its value is only valid for point rendering game


Lines, strips and loops

A line in OpenGL represents a line segment, a line can be represented by two vertices, a polyline can be represented by multiple line segment links, a polyline closed at the beginning and end is called a loop line, and the width of the line can be set by glLineWidth()

voidglLineWidth(GLfloat width);

width indicates the width of the line, the default value is 1.0, it must be a value greater than 0.0, otherwise an error will be reported


Triangles, strips and sectors

When the triangle is drawn, each triangle is independent of each other. If a rectangle is drawn in the triangle, two triangles need to be drawn, providing 6 vertices.

Strip drawing requires 4 vertices, the first three vertices form the first triangle, and subsequent vertices will form a new triangle with the last two vertices of the previous triangle.

When the fan is drawn, the first vertex will exist as a shared point, which is a component of each subsequent triangle, and every two subsequent vertices will form a new triangle with this shared point.


Correspondence between drawing method and OpenGL enumeration

Element Type

OpenGL enumerator

point

GL_POINTS

String

GL_LINES

strip line

GL_LINE_STRIP

loop line

GL_LINE_LOOP

independent triangle

GL_TRIANGLES

triangle strip

GL_TRIANGLE_STRIP

triangular sector

GL_TRIANGLE_FAN

 

Example of drawing method

First build a vertex array

float points[] = {

-0.6f, 0.2f, 1.0f, 0.0f, 1.0f,

-0.6f, -0.2f, 1.0f, 1.0f, 1.0f,

-0.2f, 0.2f, 0.0f, 0.0f, 1.0f,

-0.2f, -0.2f, 1.0f, 0.0f, 0.0f,

0.2f, 0.2f, 0.0f, 0.0f, 1.0f,

0.2f, -0.2f, 0.0f, 1.0f, 0.0f,

};

Distribution position and order in space

 

point drawing

set point size

glPointSize(5);

glDrawArrays(GL_POINTS, 0, 6);

Effect

Line segment drawing

Set the width of the line segment

glLineWidth(10);

glDrawArrays(GL_LINES, 0, 6);

Effect 

polyline drawing

glDrawArrays(GL_LINES, 0, 6);

Effect

 

Loop Line Drawing

glDrawArrays(GL_LINE_LOOP, 0, 6);

Effect

 

Independent triangle drawing

A total of six vertices, equivalent to drawing two triangles

glDrawArrays(GL_TRIANGLES, 0, 6);

Effect 

 

Triangle strip drawing

Six triangles are drawn to form a rectangle

 

draw

glDrawArrays(GL_TRIANGLE_STRIP0, 6);

Effect

 

 

Triangle fan drawing

Take the first point as the shared point, and form a triangle with the subsequent points.

rebuild vertex array

float points[] = {

0.0f, 0.0f, 1.0f, 0.0f, 0.0f,

0.4f, 0.0f, 0.0f, 1.0f, 0.0f,

0.346f, 0.2f, 0.0f, 0.0f, 1.0f,

0.2f, 0.346f, 0.0f, 0.0f, 1.0f,

0.0f, 0.4f, 1.0f, 0.0f, 1.0f,

-0.2f, 0.346f, 1.0f, 1.0f, 1.0f,

};

在空间中的分布位置及顺序

 

绘制

glDrawArrays(GL_TRIANGLE_FAN, 0, 6);

效果

 

 

多边形渲染模式

可以将多边形渲染为点集,轮廓线或填充。

可以调用glPolygonMode()函数进行设置

voidglPolygonMode(GLenum face, GLenum mode);

参数:face必须设为GL_FRONT_AND_BACK

mode 可以设置为GL_POINT,GL_LINE,GL_FILL,分别对应点集,轮廓线,填充

以上面的扇形为例

点集效果

glPolygonMode(GL_FRONT_AND_BACK ,GL_POINT);

 

轮廓线效果

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

 

填充效果

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

 

 

参见:《OpenGL编程指南》第八版第3章

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851977&siteId=291194637