Processing vertices -- enable backface culling: what it is and what it can do

Source: Internet
Author: User
Problem

In the previous tutorial, you can often see the following in the draw method:Code:

 
Device. renderstate. cullmode = cullmode. None;

If you remove this line of code and move the camera behind the triangle, they will disappear! When you define triangle vertices without understanding backface culling, 50% of triangles may not be displayed.

Solution

The way you define triangle vertices tells xNa which side you want to bring to the camera.

When drawing a solid object, people can clearly tell which side of the triangle is inside or outside the object. To draw a required triangle, you can ask xNa to only draw the plane facing the camera. Other triangles are hidden in front of the object!

But it is not that simple for computers. For each triangle you define, You need to specify whether the triangle is outside or inside. This can be done by defining the vertex of the triangle in the clockwise or clockwise direction.

Working Principle

When defining a vertex, you need to consider the camera position. To draw a triangle, the order of vertices must increase clockwise in the camera's opinion.

You can imagine a ray pointing from the camera to the center of the triangle, as shown in 5-9. If the order of vertices (from the camera !) The triangle rotates clockwise around the center (left). xxna considers the triangle to be camera oriented so that the triangle will be drawn.

Figure 5-9 the camera located in front of the triangle (left) and rear (right)

If you place the camera on the other side of the same triangle (as shown in the figure 5-9 on the right), the order of the vertices (also viewed from the camera) is counter-clockwise, xNa will think that this triangle does not face the camera and does not draw it.

Figure 5-10 shows the same situation, but this time from the camera's perspective, it is clear that in the right picture, the camera sees a counter-clockwise order.

The triangle defined in clockwise direction in Figure 5-10 is drawn (on the left), and the triangle defined in clockwise direction is removed.

Why does it need to be removed from the back?

There is only one triangle to illustrate the principle, but it cannot explain the benefits of using backend removal. As mentioned earlier in this tutorial, it is useful to remove the back of a solid object, such as a model or terrain.

Let's discussCube. When you rotate a cube, you can only see three faces at any time. This means that the rendering of the other three faces the processing capabilities of the video card is a waste, there is no need to draw a triangle toward the camera.

Imagine holding a cube and seeing only the left side of a face 5-11. Then, let's take a look at the right image. Each triangle on the front and back shows a bold display.

Numbers represent the order of vertices. Six faces composed of 12 triangles, 36 vertices need to be defined. The number in Figure 5-11 indicates which vertex defines two triangles.

Note:If you use an index, the cube only requires 8 independent vertices, but still requires 36 indexes to draw 12 triangles from 8 vertices. The number in Figure 5-11 indicates which index defines two triangles.

Figure 5-11 cubes: two triangles on the front and back Surfaces

On the left of Figure 5-12, a ray from the camera is displayed and intersecting two triangles. dots represent the intersection of rays and the triangle above. When you move forward along the order (from 0 to 1 to 2) of the vertex of the triangle above, you will find clockwise rotation around the dot, and xNa will draw the triangle.

Now let's take a look at the intersection of the ray and the triangle behind it. When you move forward along the order of vertex increase (from 6 to 7 to 8), it will rotate around the counter-clockwise, at this time, xNa will remove the triangle, which is well handled because it is located behind the cube and hidden from the front surface.

On the Right of Figure 5-12, the same cube is displayed, but it is rotated 180 degrees, so that the front and back surfaces are swapped and the camera is still on the same side of the page. Now, if you follow the vertex 6 to 7 to 8, it will rotate clockwise around the line of sight. This time, the video card will draw this triangle and remove another one! This is what we want.

In this way, the video card knows which triangles should be removed, and the triangles that do not face the camera can be greatly improved.ProgramPerformance, which is also activated by default.

Figure 5-12 intersection of rays emitted from the eyes and two triangles

TIPS:In tutorial 5-7, there is an example of a cylinder. Make sure that the removal is enabled. Try to move the camera over the cylinder and check the result. (Note: you cannot see the upper surface of the cylinder )!

How to disable Removal

Although removing an object that consists of a solid surface can bring great benefits, you sometimes need to disable it. For example, if you want to create a rectangular wall composed of only two triangles, it looks good from the outside, but when you enter the building, the wall will be removed, as a result, you can see through this wall!

To solve this problem, you can define two extra triangles on the opposite side of the wall, so that no matter from which side you observe the wall, there are always two triangles drawn and the other two are removed. The simpler method is to use the following code to remove and close the program:

 
Device. renderstate. cullmode = cullmode. None;

Don't forget to remove it after creating the wall.

TIPS:When designing, especially debugging programs, you can remove and close them to eliminate the possibility that objects are not drawn.

Code

The sample code defines a cube along the clockwise and counterclockwise direction. When you run the counterclockwise version, you will see that xNa only draws those triangles that do not need to be drawn.

When running the clockwise version, try to move the camera inside the cube.

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.