Original:D3DXMATRIXPERSPECTIVEFOVLH and D3DXMatrixOrthoLH projection functions in 3d/2d
The z-value in 3D affects the conversion between the screen coordinate system and the world coordinate system, and the Z-value in 2D does not have an effect (only the screen aspect ratio is affected, and the z-value only affects depth culling). So if a 2D camera is used in the u3d, the conversion between screen coordinates and world coordinates needs to be done with the specified 2D camera, and if the main 3D camera is used, the UI conversion will result in an abnormal calculation.
First, D3dxmatrixperspectivefovlh function
Function: Builds a left-handed perspective projection matrix based on a field of view. Gets the perspective projection matrix for the specified parameter for the 3D projection transformation.
Use:
D3dxmatrix proj;
D3DXMATRIXPERSPECTIVEFOVLH (
&proj,
D3DX_PI * 0.5f,//90-degree
(float) Width/(float) Height,
1.0f,
1000.0f);
Device->settransform (D3dts_projection, &proj); Set the transformation matrix and state, do not really start the transformation, submitted to the graphics hardware
1. LENS shift: Change the camerapos to calculate the observation position
2. Zoom in and out of the object:
1). Set the position of the observer, Camerapos is used to calculate the viewing position (it is possible to ignore Z-values).
2) Fovy The angle between the z-axis and the zy diagonal becomes smaller, the object will also become larger (aspect can not be arbitrarily transformed otherwise unequal proportions).
3. Pull the lens high pull Low:
1) Camerapos to pull high, y change on it.
Prototype:
d3dxmatrix* D3DXMATRIXPERSPECTIVEFOVLH (
_inout_ D3dxmatrix *pout,
_in_ FLOAT Fovy,
_in_ FLOAT aspect,
_in_ FLOAT Zn,
_in_ FLOAT ZF
);
POut: A Perspective transformation matrix that transforms an object within the frustum into a square body ( -1,-1,-1)--(1,1,1).
Fovy: the upper and lower angle of the cone (6-face), the z-axis split the angle.
When the Fovy become small, because also in the screen 90 degrees, so the height of the direction is enlarged, and vice versa Fovy become larger, the height of the direction smaller.
aspect:aspect = W/h,fovy = 90 degrees, by the projection matrix of the calculation process, projection Yscale = cot (FOVY/2); XScale = Yscale/aspect.
When the screen w:h = 100:20, when aspect = 5:1, then the [5,1] square cross-sectional world within the cone will be transformed into a plane of (),
XScale is compressed by 1/5, and when the perspective projection is mapped to screen coordinates [the 5,1],xscale direction needs to be magnified 5 times times according to the screen, so that the world inside the cone is scaled to the screen.
When aspect = 1 becomes smaller, then Yscale = 1, XScale = 1, the xy-square cross-section of the cone is placed in [the] world; The map to screen for [5,1]xscale maps to the screen is magnified 5 times times, Yscale unchanged.
When aspect = 10 is larger, then Yscale = 1,xscale = 1/10, the xy-square cross-section of the cone [10,1] world-wide->[5,1],xscale is reduced twice times and yscale unchanged.
ZN,ZF:z near clipping surface, Z far clipping surface, Z view cone depth depth change, map to the screen, is also the Zn depth becomes big, then the screen object becomes smaller, the Zn depth becomes smaller, then the object on the screen will become bigger.
Transformation matrices:
XScale 0 0 yscale 0 0 zf/(zf-zn) 0 -zn*zf/(ZF-ZN) 0where:yscale = Cot (FOVY/2) XScale = Yscale/aspect ratio
Yscale is based on the angle of view cone, xscale by the screen size to set equal to XScale = Yscale/aspect = Yscale * h/w, that is, hope: Xscale/yscale = h/w. That is, when yscale = 1 o'clock, the screen W /h = 4/3. Then the XScale scale is 3/4, that is, cot (FOVX/2) is smaller, fovx the larger the point of projection, when the XScale is smaller, divided by w = z, then [ -1,1] device coordinate system X range can be put more points, when the conversion to the screen coordinate system, The X-direction points are magnified in the Y-direction by 4/3, so the X-direction and Y-direction scaling is 1:1, i.e. the scaling of equal ratios does not cause problems, the main scaling comes from the angle of the Fovy frustum, and the camera position.
That is to say, XScale = Yscale/aspect ratio is a wise formula that guarantees equal-to-scale, and is separated from true scaling.
The z-axis zf/(ZF-ZN) is the constant portion of the 1/z interpolation, and-zn*zf/(ZF-ZN) is the coefficient portion of the 1/z interpolation.
M31 = 1 is the most secretive of the tricks.
Second, D3DXMatrixOrthoLH function
Function: Create a left-handed orthogonal projection matrix for the 2D transformation of the view coordinates to the projected coordinate system.
Use:
D3dxmatrix Mortho
D3DXMatrixOrthoLH (&mortho, Window_width, Window_height, 0.1f, 1000.0f);
G_pd3ddevice->settransform (D3dts_projection,&mortho); Set the transformation matrix and state, do not really start the transformation, submitted to the graphics hardware to implement the transformation
1. LENS shift: Change the camerapos to calculate the observation position
2. Zoom in on the object:
1). Z Depth value is discarded, can be enlarged by enlarging the screen projection, then need to enlarge the value of the orthogonal projection, because the orthogonal cast movie Queen 2/w, 2/h, so you can reduce the orthogonal projection of the incoming W, h to achieve amplification, amplification of the incoming W, H can achieve simulation away.
3. Pull the lens high pull Low:
1) The Camerapos,y value is enlarged to pull high, and by enlarging the incoming W, H can realize the distance reduction of the simulation.
Prototype:
d3dxmatrix* D3DXMatrixOrthoLH (
_inout_ D3dxmatrix *pout,//changing matrix of output
_in_ FLOAT W,//width of screen
_in_ FLOAT H,//height of screen
_in_ FLOAT Zn,//z Depth Cache min
_in_ FLOAT ZF//z depth Cache max
);
The resulting transformation matrix is:
2/W 0 0 0
0 2/h 0 0
0 0 1/(ZF-ZN) 0
0 0 zn/(ZN-ZF) 1
The orthogonal transformation transforms the point inside the camera coordinate space into the device normalized coordinate space.
2/w,2/h in the camera space X, y orthogonal projection, a certain scale, when the larger the w,h, the larger the size of the device normalized coordinate system [ -1,1] to accommodate the more pixels, the transformation into the screen coordinate system to feel away from the narrowing. Z is linearly interpolated in Z-space, so that Z-values are in [0,1] space, with Zn at 0,zf time of 1 (depth cache for occlusion culling depth testing, and stencil testing).
Reference:
Http://msdn.microsoft.com/en-us/library/windows/desktop/bb205350%28v=vs.85%29.aspx
Http://msdn.microsoft.com/en-us/library/windows/desktop/bb204940%28v=vs.85%29.aspx
A detailed description of the D3DXMATRIXPERSPECTIVEFOVLH and D3DXMatrixOrthoLH projection functions in the "reprint" 3d/2d