3D game from scratch-Sixth: Camera

Source: Internet
Author: User

: Golden point time and space ::

Sixth: Camera

I have always dreamed of having a digital camera (DV), but the money in my pocket is not allowed. But today we can finally achieve this dream, because this time we will talk about how to implement the camera function in d3d (wangmei Zhike or pai_^ ).
First, let's look at the principles of the camera. (A picture is better than a thousand words)

A video camera has the following attributes:
1. Observation Point: Position of the camera
2. Target point: the camera faces a certain place
3. Observed range: the angle of the projection range of the conical shape, in radians.
4. The nearest and longest visible distance: The observed range between the two distances is the visible area.
5. aspect ratio: the width and height ratio of the display, which is generally 1.

After understanding the principles of the camera, let's look at how d3d sets it. There are two matrices used to set up the camera, one is the observation matrix and the other is the projection matrix,ProgramEverything.
First, set the observation matrix of the camera:

M_veyept = d3dxvector3 (0.0f, 3.0f,-5.0f); // viewpoint Coordinate
M_vlookatpt = d3dxvector3 (0.0f, 0.0f, 0.0f); // coordinate of the target point
M_vupvec = d3dxvector3 (0.0f, 1.0f, 0.0f); // The upward direction of the current world, usually (0, 1, 0)

// Create an observation matrix based on the left-hand Coordinate System Based on the three values given above
D3dxmatrixlookatlh (& m_matview, & m_veyept, & m_vlookatpt, & m_vupvec );

// Set this matrix to the current observation matrix of d3d
Pd3ddevice-> settransform (d3dts_view, & m_matview );

Set the projection matrix of the camera:

M_ffov = d3dx_pi/4; // angle of the observed range, which is 90 degrees
M_faspect = 1.0f; // the aspect ratio is 1.
M_fnearplane = 1.0f; // The nearest visible distance.
M_ffarplane = 00000000f; // The longest visible distance.

// Create a projection matrix based on the left-hand Coordinate System
D3dxmatrixperspectivefovlh (& m_matproj, ffov, faspect, fnearplane, ffarplane );

// Set this matrix to the current projection matrix of d3d
Pd3ddevice-> settransform (d3dts_projection, & m_matproj );

It's very easy, isn't it? But to really understand this, we must first understand the matrix. The most used mathematical knowledge for 3D games is probably a matrix. Therefore, we recommend that you do not worry about writing programs, find out the textbook of linear algebra and review it.
If you don't believe it, please refer to the rotating and moving parts of the camera included in this example. As long as you can fully understand it, I won't say much about it, this is because it is completely dealing with matrix operations.
By the way, I used a ccamera class to encapsulate the example program, which is much easier to use than setting a matrix, try to press the direction key during running. If this is the first time, you will have a sense of accomplishment.
If you do not understand this, you are welcome to discuss on the message board of "golden point time and space.
This evening, there was a power outage, and it was about 12 o'clock. It was about 1 o'clock in the morning. I have to go to work tomorrow.

Running result:

[Download the complete source code of this back-to-source program]

Home: http://www.gpgame.com
Mail: softboy@sina.com
Soft at a.m.,-8-5


Return

Golden point time and space 2000/8/5

Www.gpgame.net

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.