Primitive assembly and Grating

Source: Internet
Author: User

1. What is a primitive?

The primitive is a geometric object that can be drawn using gldrawarrays and gldrawelements. A primitive is described by a series of vertices. Each vertex contains the position, color, normal, and texture coordinates.

The primitives include vertices, lines, and triangular rows.

Ii. Primitive types

1. Triangle primitive type

1) gl_triangles: triangle vertices are not reused. For example, vertex {v0, V1, V2, V3, V4, V5} describes two triangles. The triangle vertices are: (v0, v1, V2) and (V3, V4, V5 ).

2) gl_triangle_strip: triangle vertices are reused and draw a series of connected triangles. The last two vertices of a triangle are the first two vertices of the next triangle. For example, the vertex {v0, V1, V2, V3, V4} describes three triangles. The triangle vertices are: (v0, V1, V2), (V2, V1, V3) <Note that this order should be in the counterclockwise direction> and (V2, V3, V4 ).

3) gl_triangle_fan: The triangle vertices are reused and draw a series of connected triangles. The 1st and 3 vertices of the triangle are the 1st and 2 vertices of the triangle. For example, the vertex {v0, V1, V2, V3, V4} describes three triangles. The triangle vertices are: (v0, V1, V2), (v0, V2, V3) and (v0, V3, V4 ).

2. Line primitive type

1) gl_lines: vertices of the line are not reused. For example, the vertex {v0, V1, V2, V3, V4, V5} describes three lines. The line vertices are: (v0, V1), (V2, V3) and (V4, V5 ).

2) gl_line_strip: the vertex of the line is reused. The last vertex of the front line is the first vertex of the next line. For example, the vertex {v0, V1, V2, V3} describes three lines. The line vertices are: (v0, V1), (V1, V2), and (V2, V3 ).

3) gl_line_loop: the vertex of the line is reused. Similar to gl_line_strip, the last vertex of the last line is connected to the first vertex of the first line. For example, the vertex {v0, V1, V2, V3, V4} describes five lines. The line vertices are: (v0, V1), (V1, V2), (V2, v3), (V3, V4), and (V4, V0 ).

You can use the void gllinewidth (glfloat width) function to set the line width.

3. vertex primitive

Gl_points: draws a graph for each vertex.

Note: The Window coordinate (0, 0) is located in the lower left corner of the window. The coordinate of the vertex (0, 0) is located in the upper left corner of the window.

Gl_pointsize is an embedded variable in vertex shader. gl_pointcoord is an embedded variable in fragment shader. Its value range is [0.0, 1.0] and increases from left to right or from top to bottom.

3. graphic primitive

OpenGL ES 2.0 has the following two drawing primitives:

1) gldrawarrays

2) gldrawelements.

1. gldrawarrays

Void gldrawarrays (glenum mode, glint first, glsizei count)
• Mode: Specifies the primitive type to be drawn. The valid values are as follows:
Gl_points
Gl_lines
Gl_line_strip
Gl_line_loop
Gl_triangles
Gl_triangle_strip
Gl_triangle_fan

• First: Index of the starting vertex in enabled vertex Arrays

• Count: Number of vertices to be used for drawing

For example, gldrawarrays (gl_triangles, 0, 6) draws two triangles. The vertex array indexes of vertices are: (0, 1, 2) and (3, 4, 5 ).

2. gldrawelements

Void gldrawelements (glenum mode, glsizei count, glenum type, const glvoid * indices)
• Mode: Specifies the primitive type to be drawn. The valid values are as follows:
Gl_points
Gl_lines
Gl_line_strip
Gl_line_loop
Gl_triangles
Gl_triangle_strip
Gl_triangle_fan

• Count: Number of vertex indexes in * indices

• Type: indicates the Data Type of the element in * indices. The valid values are as follows:

Gl_unsigned_byte
Gl_unsigned_short
Gl_unsigned_int (optional, available only when the oes_element_index_uint extension is implemented)

• Indices: An array or pointer for storing vertex Indexes

3.Gldrawarrays &Respective use of gldrawelements

If the vertices in the enbaled vertex array are not shared by multiple primitives, gldrawarrays is more efficient because the vertex index array is saved; otherwise, gldrawelements is used for efficiency, because the same vertex has only one copy of data in the enabled vertex array, there is no copy of multiple data, saving the memory space.

For example ):

1) the code for using gldrawarrays is as follows:

#define VERTEX_POS_INDX 0#define NUM_FACES 6GLfloat vertices[] = { … }; // (x, y, z) per vertexglEnableVertexAttribArray(VERTEX_POS_INDX);glVertexAttribPointer(VERTEX_POS_INDX, 3, GL_FLOAT, GL_FALSE,0, vertices);for (i=0; i<NUM_FACES; i++){    glDrawArrays(GL_TRIANGLE_FAN, first, 4);    first += 4;}//or glDrawArrays(GL_TRIANGLES, 0, 36);

Note:A total of 8 vertices, but the data of 24 or 36 vertices is saved.

2) the code for using gldrawelements is as follows:

#define VERTEX_POS_INDX 0GLfloat vertices[] = { … };// (x, y, z) per vertexGLubyte indices[36] = { 0, 1, 2, 0, 2, 3,                        0, 3, 4, 0, 4, 5,                        0, 5, 6, 0, 6, 1,                        7, 6, 1, 7, 1, 2,                        7, 4, 5, 7, 5, 6,                        7, 2, 3, 7, 3, 4 };glEnableVertexAttribArray(VERTEX_POS_INDX);glVertexAttribPointer(VERTEX_POS_INDX, 3, GL_FLOAT, GL_FALSE,0, vertices);glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLubyte),GL_UNSIGNED_BYTE, indices);

4. Summary

As long as vertices are reused, gldrawelements provides better performance than gldrawarrays, because not only does the CPU and GPU provide less memory bandwidth for data transmission, but also consumes less memory in the GPU.

4. Primitive assembly)

Shows the Assembly stage of OpenGL ES 2.0 primitive:

1. Coordinate System

2. Clip volume)

The cropping body consists of six cropping surfaces: near, far, left, right, top, and bottom. As shown in:

In the clipping phase, each primitive is cropped by the cropping body. Perform the following operations on each primitive:

1) clipping triangles

• If all are cropping the body, do nothing

• If all are not in the cropping body, discard

• If the part is in the cropping body, a new vertex is generated and a triangle fan is formed

2) clipping lines

• If all are cropping the body, do nothing

• If all are not in the cropping body, discard

• If the part is in the cropping body, a new vertex is generated and a new line is formed.

3) clipping point sprites

• If all are cropping the body, do nothing

• If all are not in the cropping body, discard

3. Perspective Division (perspectivedivision) 

The goal is to convert the cropping coordinates to the normalized device coordinates. The value range is [-]. The implementation method is to divide each component of the coordinate by WC. The crop coordinates (XC, YC, ZC, WC) are divided by perspective Division (XC/WC), (YC/WC), and (ZC/WC) are changed to the normalized device coordinates (XD, YD, ZD ).

4. viewport Transformation)

It converts the normalized device coordinates (XD, YD, ZD) to window coordinates or screen coordinates (XW, YW, ZW ).

1) You can use the following API to set the CTR:

Void glviewport (glint X, glint y, glsizei W, glsizei H)
• X, Y: Specifies the screen coordinates (in pixels) in the lower left corner of the view)

• W, H: Specify the width and height of the view (in pixels)

2) use the following API to set the preview depth:

Void gldepthrange (glclampf N, glclampf F)
• N, F: specify the required depth range. Default Value: N = 0.0, F = 1.0, and the value range is [0.0, 1.0].

3) the method for converting the view is as follows:

Ox = (x + W)/2, oy = (Y + H)/2, N and F indicate the required depth range.


5. Rasterization)

Raster each primitive (such as vertex, line, and triangle), and then generate a suitable fragment for this primitive. A fragment represents a pixel position (x, y) in the screen space, and the fragment data is processed by the fragment shader to generate a fragment color.

6. Selection)

Before a triangle is raked, I need to determine which faces the observer and which faces the observer. The culling operation discards the Back-to-Observer faces to raster these invisible triangles, saving GPU time and improving GPU performance. The related functions are as follows:

1) void glfrontface (glenum DIR)
• Dir: indicates the direction of the triangle facing the observer (gl_cw <clockwise> or gl_ccw <counterclockwise>). The default value is gl_ccw.

CW: clockwise, CCW: counter-clockwise

2) void glcullface (glenum Mode)

• Mode: indicates which side of the triangle is selected (gl_front, gl_back, gl_front_and_back). The default value is gl_back.

3) void glable (glenum cap)
Void gldisable (glenum cap)
• Cap: Set it to gl_cull_face. In the initial stage, the culling is disabled.

 

 

 

 

 

 

 

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.