Implement the 3D image engine from scratch: (14) three traps on the back

Source: Internet
Author: User

1. Why do I need to hide it on the back?

Through the previous demo, we can know that if there are more polygon in the rendering process, the more content to be processed will consume the processing capability of the computer. For an object, we generally only see its face. It may not be a face, but there must be many faces that are completely opposite to us. We should skip them during rendering. This will reduce the triangle rendering volume by about half. The principle is actually very simple, that is, the relationship between the vector and the plane. In my article on parameterized straight lines and the plane, I specifically deduced the entire ins and outs.

I like to analyze problems in 2D top view, such:

 

 

 

2. Note 1: the backend test shows the relationship between the line of sight and the face orientation, which is not directly related to the camera orientation.

When I first got in touch with the back, I thought it was to take the camera's orientation vector and the surface's normal vector to calculate the angle. Although it can also eliminate some faces, it was found that it was wrong, this is because:

 

The red and green faces have the same normal vectors, but one is the front and the other is the back. Because the orientation of the camera is fixed, in this case, the angle between the camera orientation and the surface method vector is incorrect.

 

In the figure, we can find that if we connect any point on the face to the camera position, this situation can be distinguished. Because the angle between the line of sight vector and the normal vector is different, one is acute angle and the other is blunt angle. (At first glance, they are all acute angles. You can see the difference by moving their starting points to the origin)

 

It can also be noted that no matter which point on the face is used as the link, it is correct.

 

 

 

3. Note 2: The result of the Plane Method vector is closely related to the "Storage order of vertices". The storage order of each vertex must be consistent.

How can I make a wrong call for a period of time when I decode the back? I think there is definitely no problem. How can it be wrong?

So I calculated it for half a day on the paper and found a fatal mistake-the storage order of each vertex in my triangle was arbitrary, resulting in inconsistency of the normal vector.

See:

If you want to obtain a surface normal vector, the simplest method is to get two vectors and then perform cross multiplication. For example, for the triangle OAC above, you can use Vertex a minus vertex o to obtain the OA vector. Point C minus point O to get the OC vector, and then use OA × oC to get the normal vector, which implies an important mathematical property, that is, the cross multiplication does not meet the exchange law.

That is, OA × Oc! = OC × OA.

 

Because when we get three vertices, we don't know how to sort these three vertices, so if a certain surface 1 is stored counterclockwise, such as: O-A-C, another area is clockwise to store, such as O-C-A, so in the Cross multiplication, the results will be completely different.

 

Therefore, you must specify the sequence for each vertex storage. For example, perform the following steps:

1) from the outside of the object, we are looking at this plane.

2) store these points in a counter-clockwise manner (it doesn't matter if the order is normal or reverse, but all aspects should be unified)

 

Example 1:

For the surface OAc, we first look at the surface outside the object, rather than inside the object. Then the point is stored counterclockwise, and the storage order is: O-A-C

 

Example 2:

For surface ABC, we first look at the surface outside the object. Then the counter-clockwise to store these points, the storage order is: A-B-C, not A-C-B

 

Because of this principle, if you export the objects in 3DS MAX, you can find that the order of storing the vertices in the objects stored in 3DS satisfies the above consistency conventions.

 

 

 

4. Note 3: The subtle relationships among vertex storage sequence, vector cross-multiplication sequence, and line of sight Vector

If you want to write a correct back-end test function, you must understand the relationship between the three factors, because there is an incorrect one, and your results are the opposite. (If there are two mistakes, they will be correct and interesting, but the calculated normal vectors may be incorrect, which will affect the subsequent illumination calculation, so we still need to figure this out ).

 

Here is a correct example for some experience:

1) vertex storage sequence Convention: from the outside of the object, it is stored counterclockwise.

2) when preparing for cross-multiplication, you can get the three vertices (P0, P1, P2) in the sorted order, and use U = p1-P0, V = P2-P0, that is, switching from u to V is also a counter-clockwise rotation. Because P0-> P1-> P2 is counter-clockwise.

3) Evaluate U × V and obtain the surface method vector N. Because the uniformity of point storage is the same as that of U-> V, the surface method vector is correct, is oriented outside the object.

4) The line of sight vector view uses the camera coordinate to subtract any point on the face. In this case, the vector points from the triangle to the camera.

5) Calculate the dot product of N and view. If the dot product is greater than 0, it is an acute angle, positive. If the dot product is smaller than 0, it is a blunt angle, and it is the back. This derivation is introduced in the article in the vector function library.

 

 

 

5. Summary

So far, our hidden function on the back is correct. In the book "3D master", although his code is correct, the translated explanations are sometimes confusing. You can check the original version later ~ If I did not deduce it again this time, I would not understand the xuanjicang mentioned in the book. In addition, this blog writing has deepened my understanding. while writing this article, I also found a new understanding and the logic errors hidden in the code.

 

 

 

6. Download Code

The Code modified the previous several errors.

1) all vertices are stored in a unified reverse order.

2) determine with sight rather than camera orientation

 

Download complete project source code:> click to enter the download page <

 

The effect is similar to that of the previous one.

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.