3D Development 2 camera in Windows Phone

Source: Internet
Author: User

In the previous section, we learned about the coordinate system, the most basic concept in the 3D world, and used the vector3 type in xNa for representation. However, because of the introduction of depth information, to correctly project objects in a space, there is still a lack of an important item-camera.

 

Let's take a look at the experience of using a camera or camera! For the same target object, the imaging results are affected by the following factors: camera position, camera direction, and lens parameters. When the camera is in different positions, the angle of observation on the object affects the imaging results. When the camera's horizontal or vertical angle is different, the imaging is also at different horizontal or vertical angles. When lenses with different focal lengths are used, the imaging results are still different even if the camera positions and directions remain unchanged.

 

It can be seen that to obtain the correct imaging results, that is, to correctly draw and render objects in 3D space, it is very important to determine the camera. In other words, a definite rendering result can be obtained only when the camera and object are determined.

 

How can we express such complicated camera information in xNa? The score is expressed in two parts. One part is used to indicate the camera's position, orientation, and upward direction. It is equivalent to fixing the camera; the other part is used to indicate the camera's angle of view, the aspect ratio of imaging, and the projection parameters. It is equivalent to fixing the lens and photo and paper information. Next, let's take a look at the representation of these two parts in xNa.

 

In computer graphics, we know that the 3D world is actually a matrix. In the future, a large number of operations in the 3D world are actually matrix operations, so we should first introduce the matrix. Of course, it is not about mathematical concepts, but the matrix class in xNa.

 

XNa provides the matrix class used to represent a matrix. It is a 4x4 matrix that can be used for mathematical operations. It also provides some methods for creating a matrix. There are two common constructor methods:

Matrix matrix = matrix. identity;

 

The other is to create a special matrix. The matrix class provides a set of static methods starting with "CREATE" for creating the required matrix.

 

Return to the representation of the camera. The camera location information actually uses the createlookat () method of the matrix class to create a matrix of viewpoint. The prototype of the method is:

Public static matrix createlookat (vector3cameraposition, vector3 cameratarget, vector3 cameraupvector );

The three parameters correspond to the camera position, orientation, and upward direction respectively.

 

The lens information must be a little more complex. We need to first understand the projection. Take a look at the projection below (note: the picture comes from the network ). In fact, what we need to define is not the focal length and aperture of the real lens, but the information of this heartbeat. This heartbeat is a projection matrix, and objects other than this body cannot be imaged. For this intercept, we need to determine the parameters including the angle of view (Visual Range), usually a 45-degree angle, the aspect ratio, the near plane, and the far plane. With these four parameters, the lens information is defined.


 


 

Similarly, the matrix class provides the createperspectivefieldofview () method to create a projection matrix. The prototype of this method is:

Public static matrixcreateperspectivefieldofview (float fieldofview, float aspectratio, floatnearplanedistance, float farplanedistance );

The four parameters correspond to the four values of the intercept body.

 

So far, we have used two matrices to determine the camera information. These two matrices can also be called view matrices and projection matrices. Because cameras are used in 3D development, we define a camera class to simplify camera construction and facilitate use in future code. The code for this class is as follows:

 

  public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        public Matrix view{get;protected set;}
        public Matrix projection { get; protected set; }

        public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up, float fieldOfView, float aspectRatio, float nearPlaneDistance,float farPlaneDistance)
            : base(game)
        {
            view = Matrix.CreateLookAt(pos, target, up);
            projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView,aspectRatio,nearPlaneDistance,farPlaneDistance);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
    }

Now, the content of this section is here. With the camera, you can start development work later.

 

-- Welcome to reprint, please indicate the source of http://blog.csdn.net/caowenbin --

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.