Reprint: The principle and realization of perspective projection

Source: Internet
Author: User

The principle and implementation of perspective projection

by goncely

Reprint: http://blog.csdn.net/wong_judy/article/details/6283019

abstract : Perspective projection is the basic concept of 3D rendering and the basis of 3D programming. Mastering the principle of perspective projection plays an important role in understanding other 3D rendering pipelines in depth. In this paper, the principle and algorithm of perspective projection are introduced in detail, including the standard model of perspective projection, general model and screen coordinate transformation, and a demonstration program is realized by VC.

1 overview

In computer three-dimensional image, projection can be regarded as a method to transform three-dimensional coordinate into two-dimensional coordinate, which is commonly used in orthogonal projection and perspective projection. Orthogonal projection is used in three-dimensional health model, perspective projection is similar to human visual system, it is used in two-dimensional plane to the three-dimensional world presentation.

Perspective projection (Perspective Projection) is a method of drawing or rendering on two-dimensional paper or canvas planes in order to achieve visual effects close to a real three-dimensional object, also known as perspective [1]. It has a series of perspective characteristics, such as vanishing sensation, distance sense, and the same size of body, which can reflect the spatial image of the body realistically. Perspective projections are often used for animations, visual simulations, and many other aspects that have a realistic reflection.

2 Principle of perspective projection

The basic perspective projection model is composed of the viewpoint E and the visual plane p two parts (requires that E is not on the plane P). The viewpoint can be regarded as the observer's position, but also the angle of observing the three-dimensional world. A visual plane is a two-dimensional plane that renders a three-dimensional object perspective. As shown in 1. For any point x in the world, construct a ray r,r with a starting point of E and X points and the intersection of the plane P XP is the perspective projection result of X point. Objects in the three-dimensional world can be thought of as a collection of points {Xi}, in order to construct a starting point of E, and through the X-ray Ri, the intersection of these rays and the apparent plane P is the perspective of the three-dimensional world at the current point of view, as shown in 2.

Figure 1 Basic model of perspective projection [2]

_

Figure 2 Perspective Imaging principle [6]

The basic perspective projection model has no limit on the position of the viewpoint E and the size of the apparent plane p, as long as the viewpoint is not on the viewing plane. P Infinity is only applicable to theoretical analysis, the actual situation always limits p to a certain size of the rectangular plane, the perspective results beyond the perspective of P will be cut. It is conceivable that the viewing plane is a transparent glass window, the observer in front of the glass window, the outside world that the observer sees through the glass window, is equivalent to the perspective projection of the outside world on the glass window (the total feeling is not very appropriate, but can not think of a better analogy).

When the size of P is limited, the visible interval of viewpoint e (or the view body) is degenerated into a one-sided vertebral body, shown in 3. The prismatic vertebral body is still an infinite region, wherein the viewpoint E is the apex of the pyramidal body, and the apparent plane P is the cross-section of the ridged vertebral body. In practice, the bevel, which is located in the middle of the two cross-sections, is the visible area (4), and objects completely outside the bevel will be removed, and objects at the bevel boundary will be cut. The bevel is also called the visual vertebral body, which is a projection model commonly used in computer graphics.

Fig. 3 visual interval of a finite view plane [3]

Fig. 4 Visual vertebral body model of perspective projection [3]

3 Standard Model for perspective projection

Set Viewpoint E at the origin, the view plane p is perpendicular to the z axis, and the four sides are parallel to the x-axis and the y-axis, 5, we refer to the model as the standard model of perspective projection, where the near section of the vertebral body from the point of view of the distance is N, far cross-section from the viewpoint of the distance is The transformation equation of the perspective projection Standard Model is deduced below.

Figure 5 Standard Model for perspective projection [4]

Any point located in the body of the viewport x (x, y, z) is projected as XP (XP, YP, ZP) in the perspective of the visual plane, from point X and XP to the vertical line of the z axis, and respectively in the x-z plane and y-z plane projection, Figure 6 is the projection result on the x-z plane.

Fig. 6 Similar triangles of perspective projection [6]

According to the triangular similarity principle, it is possible to:

xp/n = x/z, yp/n = y/z

The solution was:

XP = x*n/z, YP = y*n/z, ZP = N.

The equation for the transformation of perspective projection is very simple, isn't it? It is necessary to note that, since the pivot point is always in the view plane, so ZP constant equals n, the actual calculation can not consider the ZP. You can also consider perspective projections from the camera model. Think of viewpoint e as a virtual camera, and the view plane as a film, then Figure 5 is also a standard camera model.

PS: The above discussion is based on the rectangular view plane to consider, in fact, we can take the visual plane for any shape, such as a circle, when the visual body into a cone, of course, it seems that there is no circular display device. In addition, I have considered to take the view plane as concave or convex, when the projection results should be mirrors effect bar (purely imagined, not verified). You can also imagine placing the view plane on the other side of E, when the projected image is inverted, but is it closer to the human visual imaging model? In addition, you can consider two or more viewpoints of perspective projection, in short, give full play to your resemblance, may be able to get unexpected results.

4 General model of perspective projection

The x-axis of the world coordinate system points to the right of the screen, the y-axis points to the top of the screen, and the z-axis points outside the screen. When we discuss the standard model, we have assumed that the coordinates of e are the origin, in fact, the viewpoint E in addition to the positional attributes, there is a posture attribute, usually denoted by [L u d] (D3D is [R u d]), where L represents the left side of the viewpoint D indicates direction (Direction). In the standard model, there are l=[-1,0,0]t, U=[0,1,0]t, d=[0,0,-1]t.

Research on the general model of perspective projection view E in any position, the generation algorithm of perspective graph under arbitrary attitude. The idea is simple to transform the general model into a standard model, and then use the perspective projection formula of the standard Model to calculate the perspective results. The following is a general model transformation to a standard model of the mathematical formula.

Set the point x in the general model, which corresponds to Y in the Standard model, then x and Y have the following relationship when the viewpoint is at E and the attitude is R:

X = E+ry

In turn, there are:

Y = R-1 (X-E)

Usually take R is orthogonal array, namely R-1 =rt, so there are

Y = RT (X-E)

The form of the homogeneous matrix is rewritten as follows:

Hview is the transformation matrix of perspective projection from general model to standard Model.

5 Converting to screen coordinates

For the standard model of perspective projection, the coordinate origin of the view plane is at the center of the viewport, the x-axis is horizontal to the right, and the y-axis is vertically upward, as shown in coordinates model 7 of the viewport. To display the results of a perspective projection on a computer screen, you need to transform the perspective into a coordinate transformation from the view plane coordinate system to the screen coordinate system.

Figure 7 Visual plane coordinate model

The computer screen's coordinate model 8 shows that its origin is on the upper corner of the screen and the y-axis is vertically downward. The width of the view plane is Wp, the height is Hp, the width of the screen is Ws, and the height is Hs.

Figure 8 Screen coordinate model [5]

The points in the visual plane coordinate system (XP, YP) correspond to points in the screen coordinate system (XS, YS), and their transformation relationships are as follows:

xs = a*xp + b;

YS = C*yp + D

As shown in Figure 7 and Figure 8, the (0, 0) points in the view plane correspond to the center point in the screen coordinate system (0.5*ws-0.5, 0.5*hs-0.5) (PS: Because 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, H s)); In addition, the viewport ( -0.5*WP, -0.5*HP) corresponds to the (0, 0) point of the screen. Substituting the above two values into the transformation equation can be obtained:

The equation of transformation is the plane coordinate system to the screen coordinate system.

6 Implementation of perspective projection

6.1 Loading 3D Models

The MODEL_3DS class implemented with Matt Fairfax supports loading of 3DS model files, and the implementation of this class is very simple and easy to use, with reference to [7]. Because the demo of this paper only needs the model loading function, the source code is truncated, the texture loading (not required) and rendering (we implement) code is removed, and the resource release code is added to the destructor.

6.2 View Transformations

In order to represent the general model of perspective projection, the Kcamera class is implemented, in addition to preserving the position and posture of the viewpoint, and saving the View transformation matrix M_kmview, the view matrix is constantly updated with the change of viewpoint position and attitude, and the update algorithm is described in the 4th section. For any point in the world coordinate System V (x, y, z), transform it to the standard model coordinate system of the perspective projection by V = m_kmview*v, see the Kcamera::transform function.

6.3 Perspective Transformations

The Kfrustum class is used to model the standard models of perspective projections, including the size of the viewport, and the z-axis coordinates of the near and far sections. Kfrustum transforms the result of the view transformation into perspective coordinates through the project function. The principle of the algorithm is shown in section 3rd, the code is implemented as follows:

void Kfrustum::P roject (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 Transform

The algorithm of screen transform 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

The rendering in the demo uses the software implementation, does not use any third-party graphics library, the main code in the Kcamera::render function, it receives two parameters: Model_3ds and Ksurface, the vertices in the model_3ds perspective projection, The results are then drawn into the ksurface. The function code is as follows:

BOOL Kcamera::render (model_3ds& m3ds, ksurface& ksurface)

{

Ksurface.fill (RGB (0,0,0)); Background is black

COLORREF Crpen = RGB (255,0,0); Draw a 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 a 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;

}

6.6 Demo and

Demo program using VC6 implementation, engineering source code can be downloaded in my download space. The project code contains a chess 3DS model file chess.3ds, which is shown in the demo's render result 9.

Figure 9 Demo Program rendering results

7 Summary

In this paper, the basic theory of perspective projection is introduced in detail, and the correlation algorithm of perspective projection is verified by program. However, this article is only about the most basic aspects of perspective projection, in order to make the article concise, there is a lot of knowledge is not discussed, including visual body normalization, viewport (view port), as well as deeper depth information, rasterization and interpolation correction, code implementation also did not consider the culling and cut, There will be an opportunity to continue to introduce the relevant content.

8 References

[1] http://zh.wikipedia.org/

[2] Donald Hearn, M. Pauline Baker, computer Graphics C Version (2nd Edition), Prentice Hall, 1996

[3] Eric Lengyel, Mathematics for 3D Game programming and Computer Graphics (2nd Edition), Charles River Media, Inc., 2004

[4] David H. eberly, 3D Game Engine design:a Practical approach to real-time computer Graphics, Morgan Kaufmann, 2000

[5] andrélamothe, Tricks of the 3D Game programming gurus:advanced 3D Graphics and Rasterization, Sams, 2003

[6] View transformation. CSC 830 Note 3. Course Note to Prof Seth Teller, MIT.

[7] http://www.torquepowered.com/community/resources/view/506

Reprint: The principle and realization of perspective projection

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.