I have rarely explained this principle easily and clearly. I have tried to explain the technical problem on my own and hope that my confused friends can understand the principle.
here, I assume that you have a good understanding of the geometric organization of quake3, so I will go directly to the topic to explain the principle of transformation. Some important concepts in quake3 Code may be introduced during the explanation.
first, we will explain two very important conditions:
1. The plane surface of quake3 is flat.
2. U and V of the illumination map are clamped at 0 ~ Between 1.
How does quake3 map the 3D space coordinates of any vertex on a plane surface to the unique U and V corresponding to the illumination map?
A: In the first step, quake3 calculates the AABB (axis alignment) surround box of the plane surface, the AABB box is defined by two vectors: vector_min, and vector_max, which respectively represent the minimum vertex and the maximum vertex of the AABB box. Then, based on the absolute values of X, Y, and Z in the normal of the plane surface, the plane is projected to the axis plane, and the projected area of the AABB box needs to be maximized. Suppose: If ABS (x) (returns the absolute value of X, when the rendering plane is projected onto the YZ plane to obtain the largest projected area, it is clear that the projected area is a rectangle, and the two sides of the rectangle are: ABS [(vector_max-vector_min ). y] and ABS [(vector_max-vector_min ). z]. Now we have completed the first step: Project The AABB surround box of the rendering plane to a reasonable axis plane, points in all 3D spaces on the paintplane are projected into a rectangle in the axis plane, for example, a (10, 8, 7)-> A' (0, 8, 7) Is there hope? Our texture is also rectangular.
Step 2: We have some small work to do. Now, after the axis plane is mapped, the coordinates are in the 2D plane (where the value of one dimension is equal to 0 ), but obviously it does not meet the second important condition mentioned at the beginning: the U and V of the illumination map are clamped at 0 ~ To achieve this, we also need to translate and zoom the ing rectangle of the AABB box to make the ing value positive, the scaling is to make it stuck at 0 ~ Between 1. For example, to obtain the coordinate vector_prj_min after vector_min projection, the expression of the translation operation is:
Vector_prj_trans_a =Vector_prj_a-vector_prj_min;
// Vector_prj_x and vector_prj_min are the coordinates after a vertex A and vector_min of the plane surface are projected to the axis plane.
Then scale the translated value:
U =Vector_prj_trans_a. Y/ABS [(vector_max-vector_min). Y];// Assume that u corresponds to the Y value
V =Vector_prj_trans_a. Z/ABS [(vector_max-vector_min). Z];
The ing is completed! We mapped a unique 0 ~ UV coordinate value between 1.
How can we map a unique UV coordinate to a 3D coordinate?
A: Which of the following examples can be used next? To perform inverse operations, we must know the following values:
1. The scaling coefficients S1 and S2 of the two sides of the rectangle correspond to ABS [(vector_max-vector_min). Y] and ABS [(vector_max-vector_min). Z in the example respectively.
2. The projection coordinate of vector_min, that is, vector_prj_min.
3. The distance from the plane surface to the origin Dist.
4. The normal of the plane surface.
These four values need to be saved and used here when "ing the 3D space coordinates of any vertex on a plane surface to the unique U and V corresponding to the illumination map.
Now let's take a look at how to use these four values for UV-> 3D coordinate ing.
1. Inverse scaling operation: vector_tmp.y = u * S1, vector_tmp.z = V * S2.
2. Reverse Translation: vector_tmp.y + = vector_prj_min.y, vector_tmp.z + = vector_prj_min.z;
3. Inverse Projection operation: obtain the corresponding x value, that is, the distance from the point (0, vector_tmp.y, vector_tmp.z) along the projection direction to the plane DX:
D = dot [(0, vector_tmp.y, vector_tmp.z), normal]-Dist;// The shortest distance from the point to the plane
DX = D/normal. X;
The quake3 ing principle is the same, but the stored values are somewhat different. quake3 stores the scaling vectors on two flat surfaces, and vector_min projects the values along the Projection Direction on the flat surface, for more information, seeSource code.