Base map entry under the OpenGL ES trip --OpenGL yuan

Let's look at the basic chart in the OpenGL yuan What type?

Entity type Enumeration values description
point GL_POINTS Each vertex point on the screen is a separate
line GL_LINES Each vertex defines a segment
Strap (not closed) GL_LINE_STRIP After a turn from each of the subsequent vertex to the first vertex lines drawn
Wire loop (closed) GL_LINE_LOOP After a subsequent vertex from each vertex of a first drawn sequentially into a line, while connecting the last vertex and the second vertex
triangle GL_TRIANGLES Each of the three vertices define a new triangle
Triangle strip GL_TRIANGLE_STRIP A common apex strip of a set of triangles
Triangle fan GL_TRIANGLE_FAN A dot in the center of the fan-shaped arrangement, a common set of adjacent vertices of the triangle

By this table, we already know the basic geometry in OpenGL yuan, as well as how they were formed, enumerated values ​​they represent. Here is more from the bottom of our more fundamental point of view to study primitive rendering in OpenGL.

Basic elements

1 o'clock

Point is the most basic elements, but also the most simple image. In OpenGL, referred to the apex point (Vertex), the form usually expressed by a (x, y, z) of the three-dimensional coordinate values. Each specific vertex on the screen are only a single point. In general, OpenGL point is painted in a single pixel, in fact, also points may be smaller, although it may be small enough, but not infinitely small, may depict a plurality of points within a pixel, depending on the point size setting, a default point size is the size of a pixel. Function of the size of the set point:

void glPointSize(GLfloat size);
复制代码

This is the size of the set point simplest and most commonly used method, wherein the size must be greater than 0.0f, the default value 1.0f, unit is "pixels." Specific achieve the following:

1.设置点大小
glPointSize(5.0f); //设置点的大小为5像素

这里需要注意一下,OpenGL是一个状态机,如果把点的大小更改成5.0f之后,
那么在OpenGL的场景下面,它所有的点都会更改成5.0f,
所以我们在设置当前点完成之后,把它恢复原始状态1.0f.

2.设置点的大小范围和点与点之间的间隔
GLfloat sizes[2] = {2.0f,4.0f};
GLfloat step = 1.0f;  //设置2个范围2~4,间隔为1的点

3.获取点的大小范围和最小步长 ---所调用的方法
glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
glGetFloatv(GL_POINT_GRAULARITY,&step);

4.通过使用程序点大小模式来设置点大小
glEnable(GL_PROGRAM_POINT_SIZE);

在这种模式下允许我们通过编程在顶点着色器和几何着色器中设置点的大小。
着色器内建变量:gl_PointSize; 并且可以直接在着色器源码写
 gl_PointSize = 5.0;
复制代码

2. Line

Further than the point is independent of the line segment, the line segment connecting together two vertices formed by a primitive. By default, the line width is one pixel, the only way is to change the line: glLineWidth line width setting

glLineWidth(4.5f);  //设置独立线段的宽度为4.5f(像素);
复制代码

FIG base element list by starting the article we already know that the connection between the three segments: (1) separate segments: between the primitive type parameter --GL_LINES (2) connected end to end, but the final segment is not closed: Line band (line) , end to end between the primitive type parameter --GL_LINE_STRIP (3) the final sealing closure segment: the line loop (pattern), primitive type parameters --GL_LINE_LOOP three connections corresponding legend shown below:

Is a continuous tape from a vertex to a next vertex line drawn, and a line segment to form a real connection, connected to the pattern, each vertex is connected to selected twice, once as the end point of the line segment, a as a starting point for the next segment. It is an extension of the wire loop of tape, the tape base line, additional lines with a line segment closed up.

3. Triangle

3.1 Draw a triangle

On the basis of the primitive, the most popular is probably the triangle, and at the same time, the triangle is the simplest polygon entity, it only has three sides. And it is now the triangle has only supported in OpenGL polygonal, each of three vertices define a new triangle, other polygons may be constituted by a triangle. (Rasterization hardware is the most popular triangles). The following are two triangles GL_TRIANGLES drawn:

From the apex of the triangular type, not all the triangles are equilateral triangles, if the connection order of the vertices is clockwise, then the triangle is cis, if the connection order of the vertices is counterclockwise, then that is an inverse triangle.

2. The triangular surround mode

According to the vertices specified order and direction combination as a surround. FIG Through the above we know that the two polygons on the map in a clockwise loop. We can change the order of the vertices, thereby changing the circumferential direction. We left vertex v4 triangular figure above and the order v5 swapped over, and then to draw from a vertex V3, then the triangle is counter-clockwise circumferential direction of.

By default, OpenGL default counterclockwise loop direction is positive, i.e., counterclockwise loop is a front triangle. FIG upper left triangle shows the front and back right is displayed. ( Inverse is positive, that is, trans-cis ) directions around the polygon is a very useful property, may be used to eliminate unnecessary surface.

So why are there front and back of this argument? For chestnut: If you have a particularly thin square paper notes, we may think that this is a square, then this piece of paper will still exist on both sides, front and back side of the paper. If we use the same size of six square sticky note paper surrounded by a cube, so either side of the cube is a square, and the square is a front and back, assuming you can see the outward part is positive, then North Korea can not see the inside of that part of that is the opposite.

In OpenGL is also necessary to distinguish between positive and negative, we can also function glFrontFace (GL_CW), according to tells OpenGL vertex disposed clockwise order around the polygon is the opposite, by the function glFrontFace (GL_CCW), according to tells OpenGL vertex arranged sequentially counterclockwise around the polygon is positive.

The reason for this is set, by default, OpenGL draw when both sides will draw three-dimensional objects, and in fact when we look at an object, we can not see a lot of faces, such as polygon objects in the direction, but also there are some cases between objects obscured led some polygon is not visible, so in order to improve performance, we need to eliminate these unnecessary rendering.

glEnable(GL_CULL_FACE); //来启动剔除功能
glCullFace(); //参数可以是GL_FRONT、GL_BACK、GL_FRONT_AND_BACK,
复制代码

3. triangle strip

We can draw a plurality of triangles joined to form a plurality of polygons or surface. GL_TRIANGLE_STRIP using primitives may be connected to a string of polygon draw, save a lot of time.

Can be seen from the figure, the drawing order sided polygon vertex order not entirely consistent with our specified. But in the order specified in the counterclockwise direction OpenGL rendering.

Instead of using the triangle strip are designated for each vertex of the triangle advantages: 1. The first three vertices of a triangle specified later, and then only be able to specify a vertex of the second triangle shown. Thus when drawing a large number of triangles, rendering this method can save code space and a data storage 2. The improved operational performance and bandwidth savings. Fewer vertices from the graphics memory transfer time is faster and less involved in transformation of the vertex.

4. triangle fan

For many surface or shape, we will need to draw a triangle connected ones, then we can use the primitive drawing GL_TRIANGLE_FAN a set of triangles connected around a central point, i.e., a triangle fan.

5. Draw mode

From the above, we know, from a three dimensional perspective, each polygon having two faces (front and back). Each face may be provided in different ways to draw: common polygon rendering modes are: filled (GL_FILL), the contour of formula (GL_LINE), the apex of formula (GL_POINT) and filled hole pattern (the last special), in the default OpenGL rendering is: filled . We use glPolygonMode () function to specify the mode:

void glPolygonMode(GLenum face,GLenum mode);
//该函数要求说明是对多边形哪一个面是定face设置模式
在设置绘制模式时可以为两个面分别设置不同的方式:
glPolygonMode(GL_FRONT, GL_FILL);            // 设置正面为填充方式
glPolygonMode(GL_BACK, GL_LINE);             // 设置反面为边缘绘制方式
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); // 设置两面均为顶点绘制方式
复制代码

4. A utility class (container) GLBatch

GLTools library contains a simple container class called GLBatch, this tool can be used as the most common class of seven basic elements of a simple batch of containers, and it knows that when using any of the supported storage GL_ShaderManager how primitive shaders rendering.

void GLBatch::Begain(GLeunm primitive,GLuint nVerts,GLuint nTexttureUnints = 0);
参数1:图元
参数2:顶点数
参数3:一组或者两组纹理坐标(可选)

//复制顶点数据(一个由3分量x,y,z顶点组成的数组)
void GLBatch::CopyVerterxData3f(GLfloat *vVerts)

//复制表面法线数据
void GLBatch::CopyNormalDataf(GLfloat *vNorms);

//复制颜色数据void GLBatch::CopyColorData4f(GLfloat *vColors);

//复制纹理坐标数据
void GLBatch::CopyTexCoordData2f(GLFloat *vTextCoords,GLuint u iTextureLayer);

//结束数据绘制
void GLBatch::End(void);
复制代码

Reproduced in: https: //juejin.im/post/5d0adf8ae51d4510bf1d668f

Guess you like

Origin blog.csdn.net/weixin_34013044/article/details/93182329