Projection mode-Unity3d game development Training

Source: Internet
Author: User

Projection Mode -Unity3d Game Development Training

Deng Jiahai

2018-02-12 20:33:13

Summary

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.

In the Unity3d, the projection mode determines the scene that we see, the projection method is generally divided into perspective projection and orthogonal projection two, perspective projection equivalent to what our eyes see. The farther the smaller, the nearer the greater. There will also be a reflection. In physics, the keyhole imaging is the perspective projection. The orthogonal projection is the projection of the parallel light source, and the object does not change with distance. In computer three-dimensional image, projection can be regarded as a method of turning three-dimensional coordinates into two-dimensional coordinates.

Camera settings

Perspective projection: Similar to the human visual system, it is used in three-dimensional plane to render the three-dimensional world. The model is composed by the viewpoint E and the view plane p two parts (requires e not on the plane p), the viewpoint is the observer position, namely the three-dimensional world angle, the view plane is renders the three-dimensional object's two-dimensional plan, for any point x, constructs one from E to the X ray R,r and the plane P's intersection x, P is the perspective projection result of the X point. :

Figure 1-1

Figure 1-2

Orthographic projection: For the presentation of a two-dimensional world.

Figure 1-3

No matter how far or near, the size is the same

3D is the default perspective projection (Perspective), and 2D is the orthogonal projection (orthographic) by default.

Implementation of perspective projection

6.1 Gta5-In 3D Model

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:

1 voidKfrustum::P roject (kvector3&v)2 3 {4 5     //XP = x*n/z, YP = y*n/z, ZP = N.6 7     floatFfactor = Getnear ()/v.z;8 9v.x *=Ffactor;Ten  OneV.y *=Ffactor; A  -V.z =getnear (); -  the}

6.4 Screen Transform

The algorithm of screen transform is implemented by macro, the code is as follows:

1 #define Toscreen (V, Ws, Hs)/ 2  3 {/ 4  5     float x = (v.x/getwidth () +0.5f ) * (ws-1);/ 6  7     float y = (v.y/getheight () +0.5f) * (hs-  1);/ 8  9     v.x = kmath::round (x);/ten     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:

1 BOOLKcamera::render (model_3ds& m3ds, ksurface&ksurface)2 3 {4 5Ksurface.fill (RGB (0,0,0));//background is black6 7COLORREF Crpen = RGB (255,0,0);//draw a model in red8 9    Ten  OneKMatrix4 m =M_kmview; A  -     intWs =ksurface.getwidth (); -  the     intHs =ksurface.getheight (); -  -   -  +      for(intI=0; i<m3ds.numobjects; i++) -  +     { A  atmodel_3ds::object& obj =M3ds.objects[i]; -  -   -  -          for(intn=0; n<obj.numfaces; n+=3) -  in         { -  to             intindex = obj. faces[n]*3; +  -KVector4 v0 (obj. Vertexes[index], obj. vertexes[index+1], obj. vertexes[index+2]); the  *index = obj. faces[n+1]*3; $ Panax NotoginsengKVector4 v1 (obj. Vertexes[index], obj. vertexes[index+1], obj. vertexes[index+2]); -  theindex = obj. faces[n+2]*3; +  AKVector4 v2 (obj. Vertexes[index], obj. vertexes[index+1], obj. vertexes[index+2]); the  +   -  $ Transform (V0, Ws, Hs); $  - Transform (v1, Ws, Hs); -  the Transform (v2, Ws, Hs); - Wuyi   the  -             //draw a network cable Wu  - Ksurface.moveto (v0.x, v0.y); About  $ Ksurface.lineto (v1.x, V1.y, crpen); -  - Ksurface.lineto (v2.x, V2.y, crpen); -  A Ksurface.lineto (v0.x, V0.y, crpen); +  the         } -  $     } the  the   the  the     return true; -  in}

Projection mode-Unity3d game development Training

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.