Visual C # DirectX Development Series two understanding cameras

Source: Internet
Author: User

With some online text and picture descriptions, you'll get a clearer view of the cameras in DirectX. (Part of the content stems from the Xiaozeiun teacher's book, the ultimate goal is to give you a clearer understanding of DirectX Development in C #)

Get to know three concepts before using a camera: World space, Camera space (Cameraspace), and model space. World space can be considered an objective world space, and all objects are in this world space. Camera space is used to display the display area, similar to the human eye. Model space is the spatial coordinate system of the model itself, such as the space it has when modeling before importing a model.

Here are three concepts: View transformation, world transformation, and projectiontransformation. View transformation is a point where the observer is in world space, also known as a camera transform, to convert vertices into camera space. World transformation is used to convert coordinates from model space to world coordinates. The Projection transformation (projection transform) can be thought of as controlling the camera, a bit like setting the camera lens, which is the most complex of these three transformations. Where the definition of the view transform and the projection transformation is required by the analog camera, if the world matrix is not specified, it is a four-order unit matrix by default. Coordinates in three-dimensional space, after the world transformation, view transformation (camera transformation), projection transformation and screen conversion, to get the coordinates on the two-dimensional screen, the process as shown:


First we define the view transformation: You can use MATRIX.LOOKATLH or MATRIX.LOOKATRH to create a view matrix that represents the camera position and camera target location, where MATRIX.LOOKATLH is used to create a view matrix for the left hand rule. MATRIX.LOOKATRH the view matrix used to create the right-hand rule. Create the View matrix code as follows:

Vector3 eye = new Vector3 (0, 0,-30); Vector3 at = new Vector3 (0, 0, 0); Vector3 up = new Vector3 (0, 1, 0); Matrix Viewmatrix = Matrix.lookatlh (eye, at, up);
Where the vector eye represents the camera position, the vector at represents the camera's target position, and the vector up represents the upward direction, which is generally (0,1,0).


Next, you define the projection transformation: You can use MATRIX.PERSPECTIVEFOVLH or matrix.perspectivefovrh to create a projection transformation based on the field of view (FOV), where PERSPECTIVEFOVLH is used to create a projection matrix of left-handed laws. , the Perspectivefovrh is used to create a projection matrix of the right hand rule. The following is an example of Perspectivefovrh, which defines the Perspectivefovrh () function as follows:
public static Matrix Perspectivefovlh

(

Float Fieldofviewy,
Float Aspectratio,
Float Znearplane,
Float Zfarplane
);
Where the parameter fieldofviewy represents the arc of the field of view in the y direction, the parameter aspectratio represents the plane aspect ratio,
The parameter znearplane represents the distance near the plane, and the parameter Zfarplane represents the distance from the far plane. As shown in the following:


Build the projection matrix code as follows:

Matrix projection = Matrix.perspectivefovlh ((float) math.pi/4,this. Width/this. Height, 1.0f, 50.0f);

This is followed by setting the drawing device projection and view matrix, drawing triangles, and so on. The full code is as follows:

Using system;using system.componentmodel;using system.data;using system.drawing;using System.Windows.Forms;using  Microsoft.directx;using microsoft.directx.direct3d;namespace Camera {public partial class Camera:form {Device device = null;//defines the drawing device public Camera () {this. ClientSize = new Size (800, 800);//Specify form size this.                Text = "camera";//Specify form caption} public bool Initializedirect3d () {try {                PresentParameters presentparams = new PresentParameters (); Presentparams.windowed = true; Specifies that Presentparams.swapeffect = Swapeffect.discard be displayed as Windows Forms;  After the current screen is drawn it will automatically remove device = new device from memory (0, Devicetype.hardware, this, createflags.softwarevertexprocessing, Presentparams);            Instantiate the device object return true;                } catch (Directxexception e) {MessageBox.Show (e.tostring (), "Error");//Handling Exceptions Return false;                }} public void Render () {if (device = = NULL)///If device is empty, do not render {            Return            } Vector3 eye = new Vector3 (30, 0,-30);            Vector3 at = new Vector3 (0, 0, 0);            Vector3 up = new Vector3 (0, 1, 0);            Matrix Viewmatrix = Matrix.lookatlh (eye, at, up); Matrix projection = Matrix.perspectivefovlh ((float) Math.pi/4, this. Width/this.            Height, 1.0f, 50.0f); Device.            Transform.projection = Projection; Device.            Transform.view = Viewmatrix; Device.            Renderstate.fillmode = Fillmode.wireframe; Device.            Renderstate.lighting = false; Device.  Clear (Clearflags.target, Color.Black, 1.0f, 0); Clears the Windows interface to black device.            BeginScene (); Add render drawing code here customvertex.positioncolored[] vertices = new customvertex.positioncolored[3];//define vertex ve Rtices[0].            Position = new Vector3 (0f, 0f, 0f); VeRtices[0].            Color = Color.Red.ToArgb (); VERTICES[1].            Position = new Vector3 (5f, 10f, 0f); VERTICES[1].            Color = Color.Green.ToArgb (); VERTICES[2].            Position = new Vector3 (10f, 0f, 0f); VERTICES[2].            Color = Color.Yellow.ToArgb (); Device.            VertexFormat = CustomVertex.PositionColored.Format; Device.                        Drawuserprimitives (primitivetype.trianglelist, 1, vertices); Device.            EndScene (); Device.        Present (); } static void Main () {Camera camera = new Camera ();//Create Form object if (camera.initializedir Ect3d () = = false)//check if Direct3D starts {MessageBox.Show ("Cannot Start direct3d! "," Error!                ");            Return } camera.show (); If everything is initialized successfully, the form while (camera.created)//Set a loop is used to update the rendering status in real time {camera.render ();//Hold D Evice renders until the program ends Application.doevents (); Handle input events such as keyboard and Mouse}}}}

Final effect



This article source: http://download.csdn.net/detail/yangyisen0713/8385885

Visual C # DirectX Development Series two understanding cameras

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.