Processing vertices -- Remove redundant vertices using Indexes

Source: Internet
Author: User
Problem

The triangle you want to draw shares many vertices, as shown in Figure 5-7.

Figure 5-7 structures that benefit from using Indexes

The eight triangles shown in 5-7 require 8x3 = 24 vertices when trianglelist is used. From this figure, we can see that there are actually only 9 independent vertices, therefore, the remaining 15 vertices will waste the memory, bandwidth, and processing capacity of the video card.

Solution

The good solution is to store the nine independent vertices in the array and pass the array to the video card. Create a set of 24 numbers as references to these 9 vertices. Figure 5-8 shows a large set of 24 vertices on the left and a small set of 9 vertices and 24 indexes on the right. Note that each index corresponds to one vertex.

Figure 5-8 8 8 triangles (left) drawn from 24 vertices or 24 indexes (right) pointing to 9 vertices)

This can bring great benefits. Because the index is only a number, the index is very small for vertices that contain a vector3, a color, a texture coordinate, and other information. This can save memory and bandwidth. In addition, the vertex shader on the video card only needs to process 9 vertices instead of 24. The advantage of this is obvious when dealing with complex structures composed of thousands of triangles.

Each index points to one of nine vertices, so the triangle is actually drawn based on the index. When using trianglelist, you need 24 indexes to draw 8 triangles.

Working Principle

First, you need to define an array of independent vertices. This array corresponds to the structure in Figure 5-7:

Private void initvertices () {vertices = new vertexpositioncolor [9]; vertices [0] = new vertexpositioncolor (New vector3 (0, 0, 0), color. red); vertices [1] = new vertexpositioncolor (New vector3 (1, 0, 0), color. green); vertices [2] = new vertexpositioncolor (New vector3 (2, 0, 1), color. blue); vertices [3] = new vertexpositioncolor (New vector3 (0, 1,-1), color. orange); vertices [4] = new vertexpositioncolor (New vector3 (1, 1, 0), color. olive); vertices [5] = new vertexpositioncolor (New vector3 (2, 1, 0), color. magenta); vertices [6] = new vertexpositioncolor (New vector3 (0, 2, 0), color. yellow); vertices [7] = new vertexpositioncolor (New vector3 (1, 2, 1), color. tomato); vertices [8] = new vertexpositioncolor (New vector3 (2, 2,-1), color. plum); myvertexdeclaration = new vertexdeclaration (device, vertexpositioncolor. vertexelements );}

Now you need to define which vertex to use to create a triangle. Each triangle requires three indexes.

Next, you will create an index set to correspond to these vertices. First, add an array to save these indexes:

Private int [] indices;

The following method adds the index to the newly defined array:

 
Private void initindices () {indices = new int [24]; indices [0] = 0; indices [1] = 3; indices [2] = 1; indices [3] = 1; indices [4] = 3; indices [5] = 4; indices [6] = 1; indices [7] = 4; indices [8] = 5; indices [9] = 1; indices [10] = 5; indices [11] = 2; indices [12] = 3; indices [13] = 6; indices [14] = 7; indices [15] = 3; indices [16] = 7; indices [17] = 4; indices [18] = 4; indices [19] = 7; indices [20] = 5; indices [21] = 5; indices [22] = 7; indices [23] = 8 ;}

I willCodeA code block consists of three indexes, each of which corresponds to a triangle. This set also corresponds to the gray number in Figure 5-7. For example, the triangle in the upper left corner is defined by index 12 to 14, corresponding to vertex 3, 6, and 7.

Note:Triangle vertices are defined in clockwise direction. You need to know why to see tutorial 5-6. Don't forget to call this method, for example, in the initialize method: initindices ();

After defining the vertex and index, you can send the data to the video card to draw a triangle:

Basiceffect. world = matrix. createscale (2.0f); basiceffect. view = fpscam. viewmatrix; basiceffect. projection = fpscam. projectionmatrix; basiceffect. vertexcolorenabled = true; basiceffect. begin (); foreach (effectpass pass in basiceffect. currenttechnique. passes) {pass. begin (); device. vertexdeclaration = myvertexdeclaration; device. drawuserindexedprimitives <vertexpositioncolor> (primitivetype. trianglelist, vertices, 0, 9, indices, 0, 8); pass. end ();} basiceffect. end ();

Except for two lines of code, the code snippet above is the same as that in the 5-1 tutorial. The first line of code scales the world matrix to 2 times, indicating that all vertices are multiplied by 2. In this way, the grid will be extended from 0 to 4. For more information about the World matrix, see tutorial 4-2.

The second line of code uses the drawuserindexedprimitives method to indicate that you use an index array to draw triangles. You need to declare the vertex array, the number of vertices to be drawn, and the vertex from which to draw. Next, you need to specify the array containing the index and from which index to draw. The last parameter specifies the number of elements to be drawn. In this example, the element is a triangle.

Note:This method supports all primitive types discussed in tutorial 5-1. For example, you can use indexes to draw trianglestrip or linelist, but using indexes to draw points is useless.

When to use indexes?

Using indexes does not necessarily optimize performance. Therefore, you should not use indexes to draw triangles before using indexes.

In some cases, using indexes may degrade the performance. For example, five triangles do not share one vertex. If you do not use indexes, you need to use 15 vertices. If you use indexes, you still need to define 15 vertices because these vertices are independent! In addition, 15 indexes need to be defined. Each index corresponds to a vertex. In this case, the data transferred to the video card increases!

As a rule, you can divide (number of independent vertices) by (number of triangles ). If no vertex is shared, the value is 3. If the vertex is shared, the value is smaller than 3. The smaller the value, the more performance the index can improve. For example, in Figure 5-7, the value is 9/8 = 1.125, which means that using indexes can greatly improve performance.

Code

I have included all the code above, so I will not repeat it here.

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.