Principle and Implementation of Perspective Projection

Source: Internet
Author: User
Principle and Implementation of Perspective Projection

AbstractPerspective Projection is the basic concept of 3D rendering and also the basis of 3D programming. Understanding the principle of Perspective Projection plays an important role in understanding other 3D rendering pipelines. This article describes in detail the principle and Algorithm Implementation of perspective projection, including the standard model, general model, and screen Coordinate Transformation of perspective projection, and implements a demo program through VC.

1 Overview

In computer 3D images, projection can be seen as a method to convert three-dimensional coordinates into two-dimensional coordinates. Commonly Used projects include forward projection and Perspective Projection. Orthogonal projection is mostly used for 3D models, while perspective projection is similar to human visual systems. It is mostly used to present the 3D world in a two-dimensional plane.

Perspective Projection is a method of drawing or rendering on a two-dimensional paper or canvas plane to obtain the visual effect of a realistic 3D object. It is also called perspective [1]. It has a series of perspective features, such as a sense of disappearance, a sense of distance, and regular changes in shapes of the same size, which can vividly reflect the Space image of a shape. Perspective Projection is usually used for animation, visual simulation, and many other aspects with authenticity reflection.

2. Principle of Perspective Projection

The basic perspective projection model consists of the viewpoint e and the video plane P (requires that E is not on the plane P ). The viewpoint can be regarded as the position of the observer and the angle of observing the 3D world. The visual plane is the two-dimensional plane used to render the perspective of a three-dimensional object. 1. For any point in the world, X constructs a Ray R with the starting point E and passing through the X point. The cross point XP between the R and the plane P is the Perspective Projection result of the X point. Objects in the 3D world can be viewed as composed of point sets {Xi}. In this way, the starting point is E, and the X-ray RI of point Xi is constructed in sequence, the intersection of these rays and P is the perspective of the 3D world at the current point of view, 2 shows.

Figure 1 Basic Perspective Projection Model

Figure 2 principle of perspective Imaging

The basic perspective projection model has no restrictions on the position of viewpoint e and the size of the video plane P, as long as the viewpoint is not on the video plane. The infinite P value is only applicable to theoretical analysis. In actual situations, the limit P is always a rectangular plane of a certain size. The perspective result outside of P will be dropped. It can be imagined that the visual plane is a transparent glass window, and the viewpoint is the observer before the glass window. The external world seen by the observer through the glass window is equivalent to the perspective projection of the external world on the glass window (it is not quite appropriate in general, but I can't think of a better analogy ).

When the size of P is limited, the visible range (or visual body) of viewpoint e degrades to an alignment cube, as shown in 3. The rib is still an infinite area, where viewpoint e is the vertex of the rib, and the visual plane P is the cross section of the rib. In practical application, the data center is usually located in the visible area (4). Objects completely located outside the prism will be removed, and objects located at the prism border will be dropped. This pyramid is also known as the video body. It is a projection model often used in computer graphics.

Figure 3 limited visual area

Figure 4 View body model of fluoroscopy projection

3. Standard pivoting Model

Point of View e is located at the origin, and P is perpendicular to the Z axis, and the four sides are parallel to the X axis and Y axis, respectively. As shown in figure 5, this model is called the standard model of perspective projection, in this solution, the distance between the near-cross-section and the viewpoint is N, and the distance between the far-cross section and the viewpoint is F. Generally, the near-cross-section is regarded as the visual plane. The transformation equation of the Perspective Projection standard model is derived below.

Figure 5 standard pivoting Model

Set any point of x (x, y, z) in the video body to XP (XP, YP, ZP ), from point X and XP do the vertical line of the Z axis, and projection on the X-Z plane and the Y-Z plane respectively, figure 6 is the projection result on the X-Z plane.

Figure 6 similar triangle in Perspective Projection

Based on the triangle similarity principle, you can obtain:

 

XP/N = x/Z, YP/N = y/z

 

The above formula is as follows:

 

XP = x * n/Z, Yp = y * n/Z, ZP = n.

 

The above formula is the transformation formula of perspective projection, which is very simple, isn't it? Note that ZP is always equal to N because the perspective point is always on the video plane. ZP is not considered in actual calculation. You can also consider perspective projection from the camera model. Imagine viewpoint e as a virtual camera and the video plane as a film, then figure 5 is also a standard camera model.

PS: The above discussion is based on the rectangular optical plane. In fact, we can take the optical plane as any shape, such as a circle. At this time, the visual body becomes a cone, of course, there seems to be no circular display device. In addition, I have also considered taking the video plane as a concave or convex plane. The projection result at this time should be a mirror effect (it is purely imaginary, not verified ). We can also imagine placing the visual plane on the other side of E. At this time, the projection image is inverted, but not closer to the human visual imaging model? In addition, you can also consider two or more perspective pivoting projections. In short, you can make full use of your similarity and perhaps get unexpected results.

4. General pivoting Model

Point the X axis of the world coordinate system to the right of the screen, the Y axis to the top of the screen, and the Z axis to the outside of the screen (the right coordinate system ). When we discussed the standard model, we assumed that the coordinate of E was the origin. In fact, in addition to the position attribute, the viewpoint e also had the posture attribute, generally, [l u d] is used ([r u d] is used in d3d), where l represents the left direction of the viewpoint (left), and u represents the top (up ), d indicates the direction ). In the standard model, L = [-, 0] T, U = [, 0] T, D = [,-1] T.

The general model of Perspective Projection studies the generation algorithm of perspective e at any position and attitude. The idea is very simple. First, the general model is transformed into a standard model, and then the perspective projection formula of the standard model can be used to calculate the perspective results. The following describes the mathematical formula for transforming a general model into a standard model.

Set the point X in the general model, which corresponds to the point Y in the standard model. When the viewpoint is in E and the attitude is R, the relationship between x and y is as follows:

 

X = e + Ry

 

In turn:

 

Y = R-1 (X-E)

 

Usually take R as orthogonal array, that is, R-1 = RT, so there are

 

Y = RT (X-E)

 

The preceding formula is rewritten into a homogeneous matrix (homogeneous matrix) in the following forms:

Hview is the transformation matrix from the general model to the standard model.

5. Convert to screen coordinates

For the standard model of perspective projection, as shown in coordinate model 7 of the plane, its coordinate origin is located in the center of the plane, the X axis is horizontal to the right, and the Y axis is vertical to the right. To display the results of perspective projection on a computer screen, you need to perform coordinate transformation on the perspective and convert it from the visual plane coordinate system to the screen coordinate system.

Figure 7 visual plane coordinate Model

The coordinate Model 8 of the computer screen shows that its origin is located in the sitting area of the screen, and the Y axis is vertical and downward. Set the width of the video plane to WP and the height to HP. The screen width to WS and the height to HS.

Figure 8 screen coordinate Model

Point (XP, YP) in the visual plane coordinate system corresponds to point (XS, ys) in the screen coordinate system. Their transformation relationships are as follows:

Xs = A * XP + B;

YS = C * YP + d

Figure 7 and Figure 8 show that (0, 0) points in the plane correspond to the center point in the screen coordinate system (0.5 * Ws-0.5, 0.5 * Hs-0.5) (PS: Since the screen coordinate system is a discrete coordinate system, the coordinates of all the lower right points of the screen are (Ws-1, Hs-1), instead of (WS, HS); in addition, the video plane (-0.5 * WP,-0.5 * HP) corresponding to the (0, 0) Point of the screen. The above two values can be substituted into the transformation equation:

The above formula is the transformation equation from the visual plane coordinate system to the screen coordinate system.

6. Implementation of Perspective Projection

6.1 load 3D Models

The model_3ds class implemented by Matt Fairfax supports loading 3DS model files. The implementation of this class is very simple and easy to use. For details, refer to [7]. Because the demo in this article only requires the model loading function, the source code is deleted, removing the texture loading (not required currently) and rendering (implemented by ourselves) code, added the resource release code to the destructor.

6.2 view Transformation

To represent the general model of perspective projection, kcamera class is implemented. In addition to the position and posture of the viewpoint, the view transformation matrix m_kmview is saved. With the change of the viewpoint position and posture, view matrices are constantly updated. For more information about updating algorithms, see section 4th. For any point V (x, y, z) in the world coordinate system, use v = m_kmview * V to convert it to the standard model Coordinate System of Perspective Projection. For details, see kcamera: transform function.

6.3 perspective transformation

Kfrustum class is used to model the standard model of Perspective Projection. Its members include the size of the video plane and the Z axis coordinates of the near and far cross sections. Kfrustum uses the project function to convert the view transformation result to the perspective coordinate. For the algorithm principles, see section 3rd. The code implementation is as follows:

 

Void kfrustum: Project (kvector3 & V)

{

// XP = x * n/Z, Yp = y * n/Z, ZP = n.

Float ffactor = getnear ()/V. Z;

V. x * = ffactor;

V. y * = ffactor;

V. z = getnear ();

}

 

6.4 screen Conversion

The screen conversion algorithm is implemented by Macro. The Code is as follows:

 

# Define toscreen (v, WS, HS )/

{/

Float x = (V. X/getwidth () + 0.5f) * (Ws-1 );/

Float y = (V. Y/getheight () + 0.5f) * (Hs-1 );/

V. x = kmath: round (x );/

V. Y = kmath: round (y );/

}

 

6.5 Rendering

Rendering in the demo is implemented using software without any third-party graphics library. The main code is in the kcamera: render function and it receives the parameters model_3ds and ksurface twice, perform perspective projection on the vertices in model_3ds, and then draw the results to the ksurface. The function code is as follows:

 

Bool kcamera: render (model_3ds & m3ds, ksurface & ksurface)

{

Ksurface. Fill (RGB (0, 0); // The background is black.

Colorref crpen = RGB (, 0); // draw the model in red

Kmatrix4 M = m_kmview;

Int Ws = ksurface. getwidth ();

Int HS = ksurface. getheight ();

 

For (INT I = 0; I <m3ds. numobjects; I ++)

{

Model_3ds: Object & OBJ = m3ds. objects [I];

 

For (INT n = 0; n <obj. numfaces; n + = 3)

{

Int Index = obj. Faces [N] * 3;

Kvector4 V0 (obj. vertexes [Index], obj. vertexes [index + 1], obj. vertexes [index + 2]);

Index = obj. Faces [n + 1] * 3;

Kvector4 V1 (obj. vertexes [Index], obj. vertexes [index + 1], obj. vertexes [index + 2]);

Index = obj. Faces [n + 2] * 3;

Kvector4 V2 (obj. vertexes [Index], obj. vertexes [index + 1], obj. vertexes [index + 2]);

 

Transform (v0, WS, HS );

Transform (V1, WS, HS );

Transform (V2, WS, HS );

 

// Draw the network cable

Ksurface. moveTo (v0.x, v0.y );

Ksurface. lineto (v1.x, v1.y, crpen );

Ksurface. lineto (v2.x, v2.y, crpen );

Ksurface. lineto (v0.x, v0.y, crpen );

}

}

 

Return true;

}

 

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.