3D perspective projection for iOS

Source: Internet
Author: User

Homeaboutguestbookcategoriestagslinkssubscribe

I. Overview

In iOS, the structure catransform3d is used to represent the 3D homogeneous coordinate transformation matrix. homogeneous coordinates are a coordinate representation method. The coordinates of an n-dimensional space must be represented by coordinate tuples of N + 1 elements.
In 2D transform, the application of homogeneous coordinates is related to the transformation of two-dimensional space. The last element of the homogeneous coordinates of a certain point is always set to 1. The use of homogeneous coordinates instead of simple mathematical coordinates is to facilitate the graph's affinine transformation, which can be achieved through the affinine transformation matrix. 3D affinine transformation can be implemented, such as translation ), rotation, scaling, and shear transformations. If homogeneous coordinates are not required, two operations may be involved in the transformation of coordinates, namely addition (translation) and multiplication (rotation, scaling ), after using the homogeneous coordinates and the matrix of homogeneous coordinate transformation, you only need matrix multiplication to complete everything. If you need to have an in-depth understanding of the above, you need to learn the relevant knowledge of a transformation, and calculate the multiplication of the matrix by yourself.

In iOS, calayer 3D is essentially not a real 3D (its viewpoint, that is, the position of the observation point or the so-called camera, cannot be changed), but only a 3D projection on a two-dimensional plane, the projection plane is the plane composed of the mobile phone screen or the XY axis (note that the left-hand coordinate system is in IOS). How can we determine the position of the viewpoint? It can be indirectly specified through m34 in catransform3d. m34 =-1/Z, where Z is the value of the observation point on the Z axis, while the position on the Z axis of the layer is specified through anchorpoint, the so-called anchorpoint (anchorpoint) is the point that remains unchanged in the transformation, that is, the origin of a layer in the transformation. XYZ three axes intersect at this point. In iOS, layer anchorpoint uses Unit
Coordinate space is described. Unit coordinate space uses the relative position in layer bounds instead of specifying actual coordinate points,

In m34 =-1/Z, when Z is positive, our human eyes observe the real world, that is, the projection plane shows the effect of near, far, and small, the closer Z is to the origin, the more obvious this effect is. The more far away from the origin, the less obvious it is. When Z is positive infinity, it will lose the effect of being close, far, and small, at this time, the projection line is perpendicular to the projection plane, that is, the viewpoint is infinite distance. In catransform3d, the default value of m34 is 0, that is, the viewpoint is infinite distance.

Another thing to note is that the transformation from homogeneous coordinates to mathematical coordinates is the universal homogeneous coordinates (A, B, C, H ), it is converted to mathematical coordinates (A/H, B/h, C/h ).

Ii. Algebra

Assume that a layer anchorpoint is the default value (0.5, 0.5). In the 3D space, a vertex (6, 0, 0), m34 =-1/1000. 0, then this point moves 10 units to the negative direction of the Z axis, then what is the coordinate of the Point seen on the projection plane?

Point A is expressed as (6, 0, 0, 1) Using homogeneous coordinates)

The quartzcore framework provides a function to calculate the required matrix,

    CATransform3D transform = CATransform3DIdentity;    transform.m34 = -1/1000.0;    transform = CATransform3DTranslate(transform, 0, 0, -10);

The calculated matrix is

 { 1,    0,    0,     0;     0,    1,    0,     0;     0,    0,    1,     -0.001;     0,    0,  -10,    1.01;      }   

In fact, the preceding transformation matrix is essentially a transformation matrix obtained by multiplying two matrices * The projection matrix transformation matrix is

{1,    0,    0,    0;    0,    1,    0,    0;    0,    0,    1,    0;    0,    0,   -10,  1;      }     

The projection matrix is

 {1,    0,    0,    0;    0,    1,    0,    0;    0,    0,    1, -0.001;    0,    0,    0,    1;   }     

The above two matrices are multiplied to obtain the final transformation matrix (if you forget the matrix multiplication, you can go to the linear algebra Review), so a matrix can complete transformation and projection.

Multiply the coordinate of point A to the final transformation matrix, and the coordinate points {6, 0,-10, 1.01} are converted to {6/1. 01, 0, 10/1. 01}, you can know that its projection point on the projection plane is {6/1. 01, 0, 0} is the point after the transformation. It is closer to the origin than before. The more you move toward the negative direction of the Z axis, the closer the projection plane is to the origin.

Iii. GEOMETRIC INTERPRETATION

The above example is interpreted and analyzed using ry. When we look down along the positive direction of the Y axis, we can see the following picture:

The dotted line is the projection line, and its intersection with the X axis is the projection point of point. From the theorem of a similar triangle, we can easily calculate the projection point,

1000/(1000 + 10) = x/6, then x = 6*1000/1010 = 6/1. 01

Related Article

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.