First formula: Basic spatial Coordinate Transformation
The computer screen is two-dimensional, and how does it feel three-dimensional. Then we can only change the size of an object on the screen to give it a sense of distance. This formula is very simple. Based on the human's visual feelings, the farther away from oneself, the smaller the size. The expression is as follows:
Formula: X1 = (d/Z) * X
D is the distance from the human eye to the screen, z is the distance from the human eye, X is the relative position of a point on the plane, and X1 is the relative position of the point on the screen projection. The black box in the figure is the computer screen. When a plane is set to 0 from the screen, what is displayed on the screen is its biggest appearance.
Take a look at this formula: d is fixed, and X also has a maximum value, so as long as the plane is farther away from the screen, x will be smaller. Meets people's visual feelings.
Second formula: texture coordinate transformation
Because a figure is projected to the screen, it will only be smaller than the original, so you can find the corresponding points based on the position on the screen.
Assume that the source ImageX, Y, 0Projected to the screenU, V, 0
SoU, V, 0Any point in the spatial Coordinate SystemWCorrespondsX, Y, 0Coordinate System in?
You can findWInX, Y, 0Corresponding VectorP:
P = (Pz/d) W
We want the coordinates of P (x, y). After deformation, there are:
A =V X O
B = 0 X U
C = U X V
X = A. W/C. W
Y = B. W/C. W
Note:"X"Is the intersection, and"."Is the dot multiplication.
Below is the code implementation
Public class Method1 extends ScanRenderer {// A scanned line on the screen
Public void render (int offset, int left, int right) {// the start point of the pixel, the start point of the scan, and the end point of the scan.
For (int x = left; x <= right; x ++) {// each row is scanned from left to right.
Int tx = (int) (a. getDotProduct (viewPos) // getDotProduct is a dot multiplication // viewPos is a point on the screen. We transform it to find the corresponding pixel in the graph.
C. getDotProduct (viewPos ));
Int ty = (int) (B. getDotProduct (viewPos )/
C. getDotProduct (viewPos ));
DoubleBufferData [offset ++] = // locate the pixel from the image and place it in an array.
CurrentTexture. getColor (tx, ty );
ViewPos. x ++; // the point on the next Screen
}
}
}
The following is the optimized code:
Deformation Based on this formula:
U = A. V = Ax. Vx + Ay. Vy
In the same row of V, the horizontal coordinate of V is Vx + 1.
At this time, U' = A. V' = Ax. Vx + Ay. Vy + Ax
= U + ax
//////////////////////////////////////// //////////////////////////
Public class method2 extends scanrenderer {
Public void render (INT offset, int left, int right ){
Float u = A. getdotproduct (viewpos );
Float v = B. getdotproduct (viewpos );
Float z = C. getdotproduct (viewpos );
Float du = A. X;
Float DV = B. X;
Float DZ = C. X;
For (int x = left; x <= right; x ++ ){
DoubleBufferData [offset ++] =
CurrentTexture. getColor (
(Int) (u/z), (int) (v/z ));
U + = du;
V + = dv;
Z + = dz;
}
}
}