VB6 programming: DirectX 2D graphics learning log 13 summary of lesson 8

VB6 programming: DirectX 2D graphics learning log 12 summarizes Lesson 8 as follows:
Tutorial download address : https://download.csdn.net/download/gosub60/13696651
First define the vertex type, set the vertex buffer, etc., in this regard Refer to the source code and analysis diary above for the content.

Private Vertex_List(3) As TLVERTEX '4个顶点将构成一个正方形。 此列表将用于
                                    'D3DPT_TRIANGLESTRIP
Private Vertex_List_2(5) As TLVERTEX '6个顶点也将通过使用2来形成一个正方形
                                      '三角形。 此列表将用于D3DPT_TRIANGLELIST
                                     
Private Vertex_List_3(5) As TLVERTEX '我们将使用它来创建一些自定义形状的多边形
                                      '“此列表将用于D3DPT_TRIANGLEFAN
                                     
Private Vertex_List_4(2) As TLVERTEX '3个顶点将形成一个三角形。

Then create the polygon:

 '创建多边形1。
    '---------------------------------------------------------------
    Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, D3DColorRGBA(255, 0, 0, 0), 0, 0, 0)
    Vertex_List(1) = Create_TLVertex(100, 0, 0, 1, D3DColorRGBA(0, 255, 0, 0), 0, 1, 0)
    Vertex_List(2) = Create_TLVertex(0, 100, 0, 1, D3DColorRGBA(0, 0, 255, 0), 0, 0, 1)
    Vertex_List(3) = Create_TLVertex(100, 100, 0, 1, D3DColorRGBA(255, 0, 255, 0), 0, 1, 1)
    '---------------------------------------------------------------
    
    '创建多边形2。
    '---------------------------------------------------------------
    Vertex_List_2(0) = Create_TLVertex(120, 0, 0, 1, D3DColorRGBA(255, 0, 0, 0), 0, 0, 0)
    Vertex_List_2(1) = Create_TLVertex(220, 0, 0, 1, D3DColorRGBA(0, 255, 0, 0), 0, 1, 0)
    Vertex_List_2(2) = Create_TLVertex(120, 100, 0, 1, D3DColorRGBA(0, 0, 255, 0), 0, 0, 1)
    Vertex_List_2(3) = Create_TLVertex(220, 0, 0, 1, D3DColorRGBA(0, 255, 0, 0), 0, 1, 0)
    Vertex_List_2(4) = Create_TLVertex(220, 100, 0, 1, D3DColorRGBA(255, 0, 255, 0), 0, 1, 1)
    Vertex_List_2(5) = Create_TLVertex(120, 100, 0, 1, D3DColorRGBA(0, 0, 255, 0), 0, 0, 1)
    '---------------------------------------------------------------

    '创建多边形3。
    '---------------------------------------------------------------
    Vertex_List_3(0) = Create_TLVertex(100, 155, 0, 1, D3DColorRGBA(255, 0, 0, 0), 0, 0, 0)
    Vertex_List_3(1) = Create_TLVertex(150, 105, 0, 1, D3DColorRGBA(0, 255, 0, 0), 0, 0, 0)
    Vertex_List_3(2) = Create_TLVertex(250, 105, 0, 1, D3DColorRGBA(0, 0, 255, 0), 0, 0, 0)
    Vertex_List_3(3) = Create_TLVertex(300, 155, 0, 1, D3DColorRGBA(255, 0, 255, 0), 0, 0, 0)
    Vertex_List_3(4) = Create_TLVertex(250, 205, 0, 1, D3DColorRGBA(255, 255, 0, 0), 0, 0, 0)
    Vertex_List_3(5) = Create_TLVertex(150, 205, 0, 1, D3DColorRGBA(0, 255, 255, 0), 0, 0, 0)
    '---------------------------------------------------------------
    
    '创建多边形4。
    '---------------------------------------------------------------
    Vertex_List_4(0) = Create_TLVertex(0, 150, 0, 1, D3DColorRGBA(255, 0, 0, 0), 0, 0, 0)
    Vertex_List_4(1) = Create_TLVertex(100, 175, 0, 1, D3DColorRGBA(0, 255, 0, 0), 0, 1, 1)
    Vertex_List_4(2) = Create_TLVertex(25, 200, 0, 1, D3DColorRGBA(0, 0, 255, 0), 0, 0, 1)
    '---------------------------------------------------------------

Then render the created polygon:

'渲染代码在这里
            
                Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
                
                Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLELIST, 2, Vertex_List_2(0), Len(Vertex_List_2(0))
            
                Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 4, Vertex_List_3(0), Len(Vertex_List_3(0))
             
                Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLELIST, 1, Vertex_List_4(0), Len(Vertex_List_4(0))

About: Elements of
a triangle A triangle is defined by three points, "points" are also called "vertices". The three vertices with unique positions define a unique triangle. In order for the GPU to render the triangle, we must tell it the position of the three vertices of the triangle. For the 2D example, suppose we want to render a triangle, such as the triangle in Figure 1. We pass the three vertices along with the positions (0,0) (0,1) and (1,0) to the GPU, and then the GPU has enough information to render the triangle we want.
Triangle point map
The following is the result of running the program, you can see the difference in the graphic scheme constructed by triangles.
Example: Graphics created by triangles
In order to render a single triangle, the application needs to send three vertices to the GPU. Therefore, there are three vertices in the vertex buffer. What if we want to render two triangles? One way is to send 6 vertices to the GPU. The first three vertices define the first triangle, and the last three vertices define the second triangle. This topology is called a triangle list. Triangular lists have the advantage of being easy to understand, but in some cases they are very inefficient. This happens when triangles that are continuously rendered share vertices. For example, the left side of Figure 3a shows a square composed of two triangles: ABC and CBD.
Figure 3a contains a square composed of two triangles; Figure 3b contains a pentagon composed of three triangles.

(By convention, triangles are usually defined by listing their vertices in clockwise order.)

If we use the triangle list to send these two triangles to the GPU, our vertex buffer will look like this:

A B C C B D

Note that B and C appear twice in the vertex buffer because they are shared by the two triangles.
If we can tell the GPU to render the second triangle, we can make the vertex buffer smaller, instead of getting all three vertices from the vertex buffer, use the 2 vertices from the previous triangle and get them from the vertex buffer Only get 1 vertex. It turns out that this is supported by Direct3D, and the topology is called Triangle Strip. When rendering the triangle strip, the first triangle is defined by the first three vertices in the vertex buffer. The next triangle is defined by the last two vertices of the previous triangle plus the next vertex in the vertex buffer. Taking the square in Figure 3a as an example, using triangle strips, the vertex buffer looks like:

A B C D

The first three vertices ABC define the first triangle. The second triangle is defined by B and C, that is, the last two vertices of the first triangle plus D. Therefore, by using the original topology of triangles, the vertex buffer size is changed from 6 vertices to 4 vertices. Similarly, for three triangles, such as the triangle in Figure 3b, using the triangle list will require a vertex buffer, for example:

A B C C B D C D E

Using triangle strips, the size of the vertex buffer is significantly reduced:

A B C D E

You may have noticed that in the triangle strip example, the second triangle is defined as BCD. These three vertices do not form a clockwise order. This is a natural phenomenon using triangular strips. To overcome this problem, the GPU will automatically swap the order of the two vertices from the previous triangle. It only performs this operation on the second triangle, the fourth triangle, the sixth triangle, the eighth triangle, etc. This ensures that each triangle is defined by the vertices in the correct winding order (clockwise in this case).

So why do 2D or 3D games need triangles to render? Because:
Triangle Mesh, game developers will use triangular mesh to model. A triangle is a piecewise linear approximation of a surface. If multiple connected line segments are used to approximate a function or curve piecewise.
The reason why triangles are chosen for real-time rendering is that triangles have the following advantages:
triangles are the simplest polygons and cannot be a surface with less than 3 vertices;
triangles must be flat, and polygons with 4 or more vertices are not. It must be flat, three points determine a plane, and the extra points may be above or below this surface; the
triangle is still a triangle after various transformations, which is also true for affine transformation and perspective transformation. In the worst case, when viewed from the sides of the triangle, the triangle will degenerate into a line segment. Observed from other angles, it can still be triangular;
almost all commercial graphics acceleration hardware is designed for triangular rasterization.

In a 3D model, usually the more faces (that is, the number of triangles), the finer the model, and vice versa (there are high and low modes according to the number of faces)-(as shown in the figure below) )
Image source: http://www.atangeo.com/examples/horse
So from a visual and artistic point of view, the more faces are of course the better, but from a program and performance point of view, the fewer faces, the better. Less means less calculations and faster rendering speeds. The power is reduced a lot, so it needs to be balanced.

Guess you like

Origin blog.csdn.net/gosub60/article/details/111178434